[next] [prev] [prev-tail] [tail] [up]

### 17.3 Procedures and Functions

#### NewStr

Declaration:
  Function NewStr (Const S: String): PString;
Description:
  NewStr makes a copy of the string S on the heap, and returns a pointer to this copy.

  The allocated memory is not based on the declared size of the string passed to NewStr, but is baed on the actual length of the string.

Errors:
  If not enough memory is available, an ’out of memory’ error will occur.
See also:
  DisposeStr (548)

Listing: objectex/ex40.pp

Program ex40;
{ Program to demonstrate the NewStr function }
Uses Objects;
Var S : String;
P : PString;
begin
S:='Some really cute string';
Writeln ('Memavail : ',Memavail);
P:=NewStr(S);
If P^<>S then
Writeln ('Oh-oh... Something is wrong !!');
Writeln ('Allocated string. Memavail : ',Memavail);
DisposeStr(P);
Writeln ('Deallocated string. Memavail : ',Memavail);
end.

#### DisposeStr

Declaration:
  Procedure DisposeStr (P: PString);
Description:
  DisposeStr removes a dynamically allocated string from the heap.
Errors:
  None.
See also:
  NewStr (548)

For an example, see NewStr (548).

#### Abstract

Declaration:
  Procedure Abstract;
Description:
  When implementing abstract methods, do not declare them as abstract. Instead, define them simply as virtual. In the implementation of such abstract methods, call the Abstract procedure. This allows explicit control of what happens when an abstract method is called.

  The current implementation of Abstract terminates the program with a run-time error 211.

Errors:
  None.
See also:
  Most abstract types.

#### RegisterObjects

Declaration:
  Procedure RegisterObjects;
Description:
  RegisterObjects registers the following objects for streaming:
  1. TCollection, see section 17.10, page 592.
  2. TStringCollection, see section 17.12, page 619.
  3. TStrCollection, see section 17.13, page 623.
Errors:
  None.
See also:
  RegisterType (549)

#### RegisterType

Declaration:
  Procedure RegisterType (Var S: TStreamRec);
Description:
  RegisterType registers a new type for streaming. An object cannot be streamed unless it has been registered first. The stream record S needs to have the following fields set:
  ObjType: Sw__Word
    This should be a unique identifier. Each possible type should have it’s own identifier.
  VmtLink: pointer
    This should contain a pointer to the VMT (Virtual Method Table) of the object you try to register. You can get it with the following expression:

                  VmtLink: Ofs(TypeOf(MyType)^);

  Load : Pointer
    is a pointer to a method that initializes an instance of that object, and reads the initial values from a stream. This method should accept as it’s sole argument a PStream type variable.
  Store: Pointer
    is a pointer to a method that stores an instance of the object to a stream. This method should accept as it’s sole argument a PStream type variable.
Errors:
  In case of error (if a object with the same ObjType) is already registered), run-time error 212 occurs.

Listing: objectex/myobject.pp

Unit MyObject;
Interface
Uses Objects;
Type
PMyObject = ^TMyObject;
TMyObject = Object(TObject)
Field : Longint;
Constructor Init;
Constructor Load (Var Stream : TStream);
Destructor Done;
Procedure Store (Var Stream : TStream);
Function GetField : Longint;
Procedure SetField (Value : Longint);
end;
Implementation
Constructor TMyobject.Init;
begin
Inherited Init;
Field:=-1;
end;
Constructor TMyobject.Load (Var Stream : TStream);
begin
Stream.Read(Field,Sizeof(Field));
end;
Destructor TMyObject.Done;
begin
end;
Function TMyObject.GetField : Longint;
begin
GetField:=Field;
end;
Procedure TMyObject.SetField (Value : Longint);
begin
Field:=Value;
end;
Procedure TMyObject.Store (Var Stream : TStream);
begin
Stream.Write(Field,SizeOf(Field));
end;
Const MyObjectRec : TStreamRec = (
Objtype : 666;
vmtlink : Ofs(TypeOf(TMyObject)^);
Load : @TMyObject.Load;
Store : @TMyObject.Store;
);
begin
RegisterObjects;
RegisterType (MyObjectRec);
end.

#### LongMul

Declaration:
  Function LongMul (X, Y: Integer): LongInt;
Description:
  LongMul multiplies X with Y. The result is of type Longint. This avoids possible overflow errors you would normally get when multiplying X and Y that are too big.
Errors:
  None.
See also:
  LongDiv (553)

#### LongDiv

Declaration:
  Function LongDiv (X: Longint; Y: Integer): Integer;
Description:
  LongDiv divides X by Y. The result is of type Integer instead of type Longint, as you would get normally.
Errors:
  If Y is zero, a run-time error will be generated.
See also:
  LongMul (552)

[next] [prev] [prev-tail] [front] [up]