Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Codestelle erklären (Tapi / shl)? (https://www.delphipraxis.net/171802-codestelle-erklaeren-tapi-shl.html)

Jumpy 26. Nov 2012 10:26

Delphi-Version: 5

Codestelle erklären (Tapi / shl)?
 
Hallo,
kann mir jemand die folgende Funktion einmal erklären? Es ist eine Funktion um den Zustand eines mit der Tapi überwachten Calls auszulesen, aber ich versteh nicht wie das funktioniert (die for-schleife):

Delphi-Quellcode:
function TMyCall.get_CallState: string;
var i: integer;
begin
 try
  if priv_LineCallStatus.MyCallStatus.dwCallState = 0 then begin
   Result := '*** false ***';
  end else begin
   Result := '';
   for i := low(TAPI_LineCallState) to High(TAPI_LineCallState) do
       if (priv_LineCallStatus.MyCallStatus.dwCallState and (1 shl i)) > 0
          then Result := Result + TAPI_LineCallState[i] + ', ';
   if Length(Result) > 0
      then Result := Copy(Result, 1, Length(Result) - 2);
  end;
 except
  MyTapi.ShowWarning('Error: TMyLine.get_CallState', 'uMyTapiObj');
 end;
end;
TAPI_LineCallState ist ein String Array:
Delphi-Quellcode:
  TAPI_LineCallState: array [0..15] of string =
                    ( 'LINECALLSTATE_IDLE',
                      'LINECALLSTATE_OFFERING',
                      'LINECALLSTATE_ACCEPTED',
                      'LINECALLSTATE_DIALTONE',
                      'LINECALLSTATE_DIALING',
                      //usw.
dwCallState kommt aus der Tapi:
Delphi-Quellcode:
  PLineCallStatus = ^TLineCallStatus;
  linecallstatus_tag = packed record
    dwTotalSize,
    dwNeededSize,
    dwUsedSize,
    dwCallState,
    dwCallStateMode,
    dwCallPrivilege,
    dwCallFeatures,
    dwDevSpecificSize,
    dwDevSpecificOffset: DWORD;
{$IFDEF TAPI20}
    dwCallFeatures2: DWORD;                             // TAPI v2.0
{$IFDEF WIN32}
    tStateEntryTime: TSystemTime;                       // TAPI v2.0
{$ELSE}
    tStateEntryTime: array[0..7] of WORD;               // TAPI v2.0
{$ENDIF}
{$ENDIF}
  end;
  {$EXTERNALSYM linecallstatus_tag}
  TLineCallStatus = linecallstatus_tag;
  LINECALLSTATUS = linecallstatus_tag;
  {$EXTERNALSYM LINECALLSTATUS}

Union 26. Nov 2012 10:31

AW: Codestelle erklären (Tapi / shl)?
 
mit dem dwCallState and (1 shl i) wird geprüft ob das entsprechende Bit gesetzt ist im Anruferstatus. Shl (shift left) verschiebt alle Bits in einem Wert um die angegebene Anzahl nach links, a shl b antspricht also a := a*2^b.

DeddyH 26. Nov 2012 10:31

AW: Codestelle erklären (Tapi / shl)?
 
Das ist einfach ein Vergleich mit einer Bitmaske, siehe auch http://www.delphipraxis.net/95180-ef...bitmasken.html

p80286 26. Nov 2012 10:37

AW: Codestelle erklären (Tapi / shl)?
 
Delphi-Quellcode:
for i := low(TAPI_LineCallState) to High(TAPI_LineCallState) do            {all poss. states}
       if (priv_LineCallStatus.MyCallStatus.dwCallState and (1 shl i)) > 0  {is Bit set? }
          then Result := Result + TAPI_LineCallState[i] + ', ';            {add the statustext }
Noch Fragen?
Gruß
K-H

ist der rote Kasten im Urlaub?

Jumpy 26. Nov 2012 11:24

AW: Codestelle erklären (Tapi / shl)?
 
Habs glaub ich verstanden:
(1 shl i) hat nur an iter Stelle eine 1 sonst überall 0.
Das and ist hier nicht eine logische Operation sondern die bitweise Addition.
Somit kommt da nur was >0 raus, wenn auch dwCallstate in binärer Darstellung an iter Position eine 1 hat.

Danke und bitte ein Bit für alle
:cheers:


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:48 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz