mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-19 13:59:29 +02:00
+ Initial implementation
This commit is contained in:
parent
50182fcd57
commit
a821c38ce6
23
docs/objectex/ex10.pp
Normal file
23
docs/objectex/ex10.pp
Normal file
@ -0,0 +1,23 @@
|
||||
Program ex10;
|
||||
|
||||
{
|
||||
Program to demonstrate the TStream.StrRead TStream.StrWrite functions
|
||||
}
|
||||
|
||||
Uses objects;
|
||||
|
||||
Var P : PChar;
|
||||
S : PStream;
|
||||
|
||||
begin
|
||||
P:='Constant Pchar string';
|
||||
Writeln ('Writing to stream : "',P,'"');
|
||||
S:=New(PMemoryStream,Init(100,10));
|
||||
S^.StrWrite(P);
|
||||
S^.Seek(0);
|
||||
P:=Nil;
|
||||
P:=S^.StrRead;
|
||||
DisPose (S,Done);
|
||||
Writeln ('Read from stream : "',P,'"');
|
||||
Freemem(P,Strlen(P)+1);
|
||||
end.
|
17
docs/objectex/ex11.pp
Normal file
17
docs/objectex/ex11.pp
Normal file
@ -0,0 +1,17 @@
|
||||
Program ex11;
|
||||
|
||||
{ Program to demonstrate the TStream.GetPos function }
|
||||
|
||||
Uses objects;
|
||||
|
||||
Var L : String;
|
||||
S : PStream;
|
||||
|
||||
begin
|
||||
L:='Some kind of string';
|
||||
S:=New(PMemoryStream,Init(100,10));
|
||||
Writeln ('Stream position before write: ',S^.GetPos);
|
||||
S^.WriteStr(@L);
|
||||
Writeln ('Stream position after write : ',S^.GetPos);
|
||||
Dispose(S,Done);
|
||||
end.
|
17
docs/objectex/ex12.pp
Normal file
17
docs/objectex/ex12.pp
Normal file
@ -0,0 +1,17 @@
|
||||
Program ex12;
|
||||
|
||||
{ Program to demonstrate the TStream.GetSize function }
|
||||
|
||||
Uses objects;
|
||||
|
||||
Var L : String;
|
||||
S : PStream;
|
||||
|
||||
begin
|
||||
L:='Some kind of string';
|
||||
S:=New(PMemoryStream,Init(100,10));
|
||||
Writeln ('Stream size before write: ',S^.GetSize);
|
||||
S^.WriteStr(@L);
|
||||
Writeln ('Stream size after write : ',S^.GetSize);
|
||||
Dispose(S,Done);
|
||||
end.
|
24
docs/objectex/ex13.pp
Normal file
24
docs/objectex/ex13.pp
Normal file
@ -0,0 +1,24 @@
|
||||
Program ex13;
|
||||
|
||||
{
|
||||
Program to demonstrate the TStream.ReadStr TStream.WriteStr functions
|
||||
}
|
||||
|
||||
Uses objects;
|
||||
|
||||
Var P : PString;
|
||||
L : String;
|
||||
S : PStream;
|
||||
|
||||
begin
|
||||
L:='Constant string line';
|
||||
Writeln ('Writing to stream : "',L,'"');
|
||||
S:=New(PMemoryStream,Init(100,10));
|
||||
S^.WriteStr(@L);
|
||||
S^.Seek(0);
|
||||
P:=S^.ReadStr;
|
||||
L:=P^;
|
||||
DisposeStr(P);
|
||||
DisPose (S,Done);
|
||||
Writeln ('Read from stream : "',L,'"');
|
||||
end.
|
25
docs/objectex/ex14.pp
Normal file
25
docs/objectex/ex14.pp
Normal file
@ -0,0 +1,25 @@
|
||||
Program ex14;
|
||||
|
||||
{ Program to demonstrate the TStream.Close method }
|
||||
|
||||
Uses Objects;
|
||||
|
||||
Var L : String;
|
||||
P : PString;
|
||||
S : PDosStream; { Only one with Close implemented. }
|
||||
|
||||
begin
|
||||
L:='Some constant string';
|
||||
S:=New(PDosStream,Init('test.dat',stcreate));
|
||||
Writeln ('Writing "',L,'" to stream with handle ',S^.Handle);
|
||||
S^.WriteStr(@L);
|
||||
S^.Close;
|
||||
Writeln ('Closed stream. File handle is ',S^.Handle);
|
||||
S^.Open (stOpenRead);
|
||||
P:=S^.ReadStr;
|
||||
L:=P^;
|
||||
DisposeStr(P);
|
||||
Writeln ('Read "',L,'" from stream with handle ',S^.Handle);
|
||||
S^.Close;
|
||||
Dispose (S,Done);
|
||||
end.
|
31
docs/objectex/ex15.pp
Normal file
31
docs/objectex/ex15.pp
Normal file
@ -0,0 +1,31 @@
|
||||
Program ex15;
|
||||
|
||||
{ Program to demonstrate the TStream.Flush method }
|
||||
|
||||
Uses Objects;
|
||||
|
||||
Var L : String;
|
||||
P : PString;
|
||||
S : PBufStream; { Only one with Flush implemented. }
|
||||
|
||||
begin
|
||||
L:='Some constant string';
|
||||
{ Buffer size of 100 }
|
||||
S:=New(PBufStream,Init('test.dat',stcreate,100));
|
||||
Writeln ('Writing "',L,'" to stream with handle ',S^.Handle);
|
||||
S^.WriteStr(@L);
|
||||
{ At this moment, there is no data on disk yet. }
|
||||
S^.Flush;
|
||||
{ Now there is. }
|
||||
S^.WriteStr(@L);
|
||||
{ Close calls flush first }
|
||||
S^.Close;
|
||||
Writeln ('Closed stream. File handle is ',S^.Handle);
|
||||
S^.Open (stOpenRead);
|
||||
P:=S^.ReadStr;
|
||||
L:=P^;
|
||||
DisposeStr(P);
|
||||
Writeln ('Read "',L,'" from stream with handle ',S^.Handle);
|
||||
S^.Close;
|
||||
Dispose (S,Done);
|
||||
end.
|
30
docs/objectex/ex16.pp
Normal file
30
docs/objectex/ex16.pp
Normal file
@ -0,0 +1,30 @@
|
||||
Program ex16;
|
||||
|
||||
{ Program to demonstrate the TStream.Truncate method }
|
||||
|
||||
Uses Objects;
|
||||
|
||||
Var L : String;
|
||||
P : PString;
|
||||
S : PDosStream; { Only one with Truncate implemented. }
|
||||
|
||||
begin
|
||||
L:='Some constant string';
|
||||
{ Buffer size of 100 }
|
||||
S:=New(PDosStream,Init('test.dat',stcreate));
|
||||
Writeln ('Writing "',L,'" to stream with handle ',S^.Handle);
|
||||
S^.WriteStr(@L);
|
||||
S^.WriteStr(@L);
|
||||
{ Close calls flush first }
|
||||
S^.Close;
|
||||
S^.Open (stOpen);
|
||||
Writeln ('Size of stream is : ',S^.GetSize);
|
||||
P:=S^.ReadStr;
|
||||
L:=P^;
|
||||
DisposeStr(P);
|
||||
Writeln ('Read "',L,'" from stream with handle ',S^.Handle);
|
||||
S^.Truncate;
|
||||
Writeln ('Truncated stream. Size is : ',S^.GetSize);
|
||||
S^.Close;
|
||||
Dispose (S,Done);
|
||||
end.
|
34
docs/objectex/ex17.pp
Normal file
34
docs/objectex/ex17.pp
Normal file
@ -0,0 +1,34 @@
|
||||
Program ex17;
|
||||
|
||||
{ Program to demonstrate the TStream.Seek method }
|
||||
|
||||
Uses Objects;
|
||||
|
||||
Var L : String;
|
||||
Marker : Word;
|
||||
P : PString;
|
||||
S : PDosStream;
|
||||
|
||||
begin
|
||||
L:='Some constant string';
|
||||
{ Buffer size of 100 }
|
||||
S:=New(PDosStream,Init('test.dat',stcreate));
|
||||
Writeln ('Writing "',L,'" to stream.');
|
||||
S^.WriteStr(@L);
|
||||
Marker:=S^.GetPos;
|
||||
Writeln ('Set marker at ',Marker);
|
||||
L:='Some other constant String';
|
||||
Writeln ('Writing "',L,'" to stream.');
|
||||
S^.WriteStr(@L);
|
||||
S^.Close;
|
||||
S^.Open (stOpenRead);
|
||||
Writeln ('Size of stream is : ',S^.GetSize);
|
||||
Writeln ('Seeking to marker');
|
||||
S^.Seek(Marker);
|
||||
P:=S^.ReadStr;
|
||||
L:=P^;
|
||||
DisposeStr(P);
|
||||
Writeln ('Read "',L,'" from stream.');
|
||||
S^.Close;
|
||||
Dispose (S,Done);
|
||||
end.
|
25
docs/objectex/ex18.pp
Normal file
25
docs/objectex/ex18.pp
Normal file
@ -0,0 +1,25 @@
|
||||
program ex18;
|
||||
|
||||
{ Program to demonstrate the TStream.Read method }
|
||||
|
||||
Uses Objects;
|
||||
|
||||
Var Buf1,Buf2 : Array[1..1000] of Byte;
|
||||
I : longint;
|
||||
S : PMemorySTream;
|
||||
|
||||
begin
|
||||
For I:=1 to 1000 do
|
||||
Buf1[I]:=Random(1000);
|
||||
Buf2:=Buf1;
|
||||
S:=New(PMemoryStream,Init(100,10));
|
||||
S^.Write(Buf1,SizeOf(Buf1));
|
||||
S^.Seek(0);
|
||||
For I:=1 to 1000 do
|
||||
Buf1[I]:=0;
|
||||
S^.Read(Buf1,SizeOf(Buf1));
|
||||
For I:=1 to 1000 do
|
||||
If Buf1[I]<>buf2[i] then
|
||||
Writeln ('Buffer differs at position ',I);
|
||||
Dispose(S,Done);
|
||||
end.
|
27
docs/objectex/ex19.pp
Normal file
27
docs/objectex/ex19.pp
Normal file
@ -0,0 +1,27 @@
|
||||
Program ex19;
|
||||
|
||||
{ Program to demonstrate the TStream.CopyFrom function }
|
||||
|
||||
Uses objects;
|
||||
|
||||
Var P : PString;
|
||||
L : String;
|
||||
S1,S2 : PStream;
|
||||
|
||||
begin
|
||||
L:='Constant string line';
|
||||
Writeln ('Writing to stream 1 : "',L,'"');
|
||||
S1:=New(PMemoryStream,Init(100,10));
|
||||
S2:=New(PMemoryStream,Init(100,10));
|
||||
S1^.WriteStr(@L);
|
||||
S1^.Seek(0);
|
||||
Writeln ('Copying contents of stream 1 to stream 2');
|
||||
S2^.Copyfrom(S1^,S1^.GetSize);
|
||||
S2^.Seek(0);
|
||||
P:=S2^.ReadStr;
|
||||
L:=P^;
|
||||
DisposeStr(P);
|
||||
Dispose (S1,Done);
|
||||
Dispose (S2,Done);
|
||||
Writeln ('Read from stream 2 : "',L,'"');
|
||||
end.
|
27
docs/objectex/ex20.pp
Normal file
27
docs/objectex/ex20.pp
Normal file
@ -0,0 +1,27 @@
|
||||
Program ex20;
|
||||
|
||||
{ Program to demonstrate the TMemoryStream.Truncate method }
|
||||
|
||||
Uses Objects;
|
||||
|
||||
Var L : String;
|
||||
P : PString;
|
||||
S : PMemoryStream;
|
||||
I,InitMem : Longint;
|
||||
|
||||
begin
|
||||
initMem:=Memavail;
|
||||
L:='Some constant string';
|
||||
{ Buffer size of 100 }
|
||||
S:=New(PMemoryStream,Init(1000,100));
|
||||
Writeln ('Free memory : ',Memavail);
|
||||
Writeln ('Writing 100 times "',L,'" to stream.');
|
||||
For I:=1 to 100 do
|
||||
S^.WriteStr(@L);
|
||||
Writeln ('Finished. Free memory : ',Memavail);
|
||||
S^.Seek(100);
|
||||
S^.Truncate;
|
||||
Writeln ('Truncated at byte 100. Free memory : ',Memavail);
|
||||
Dispose (S,Done);
|
||||
Writeln ('Finished. Lost ',InitMem-Memavail,' Bytes.');
|
||||
end.
|
29
docs/objectex/ex21.pp
Normal file
29
docs/objectex/ex21.pp
Normal file
@ -0,0 +1,29 @@
|
||||
Program ex21;
|
||||
|
||||
{ Program to demonstrate the TCollection.Foreach method }
|
||||
|
||||
Uses Objects,MyObject; { For TMyObject definition and registration }
|
||||
|
||||
Var C : PCollection;
|
||||
M : PMyObject;
|
||||
I : Longint;
|
||||
|
||||
Procedure PrintField (Dummy: Pointer;P : PMyObject);
|
||||
|
||||
begin
|
||||
Writeln ('Field : ',P^.GetField);
|
||||
end;
|
||||
|
||||
begin
|
||||
C:=New(PCollection,Init(100,10));
|
||||
For I:=1 to 100 do
|
||||
begin
|
||||
M:=New(PMyObject,Init);
|
||||
M^.SetField(100-I);
|
||||
C^.Insert(M);
|
||||
end;
|
||||
Writeln ('Inserted ',C^.Count,' objects');
|
||||
C^.ForEach(@PrintField);
|
||||
C^.FreeAll;
|
||||
Dispose(C,Done);
|
||||
end.
|
30
docs/objectex/ex22.pp
Normal file
30
docs/objectex/ex22.pp
Normal file
@ -0,0 +1,30 @@
|
||||
Program ex22;
|
||||
|
||||
{ Program to demonstrate the TCollection.Load method }
|
||||
|
||||
Uses Objects,MyObject; { For TMyObject definition and registration }
|
||||
|
||||
Var C : PCollection;
|
||||
M : PMyObject;
|
||||
I : Longint;
|
||||
S : PMemoryStream;
|
||||
|
||||
begin
|
||||
C:=New(PCollection,Init(100,10));
|
||||
For I:=1 to 100 do
|
||||
begin
|
||||
M:=New(PMyObject,Init);
|
||||
M^.SetField(100-I);
|
||||
C^.Insert(M);
|
||||
end;
|
||||
Writeln ('Inserted ',C^.Count,' objects');
|
||||
S:=New(PMemorySTream,Init(1000,10));
|
||||
C^.Store(S^);
|
||||
C^.FreeAll;
|
||||
Dispose(C,Done);
|
||||
S^.Seek(0);
|
||||
C^.Load(S^);
|
||||
Writeln ('Read ',C^.Count,' objects from stream.');
|
||||
Dispose(S,Done);
|
||||
Dispose(C,Done);
|
||||
end.
|
26
docs/objectex/ex23.pp
Normal file
26
docs/objectex/ex23.pp
Normal file
@ -0,0 +1,26 @@
|
||||
Program ex21;
|
||||
|
||||
{ Program to demonstrate the TCollection.At method }
|
||||
|
||||
Uses Objects,MyObject; { For TMyObject definition and registration }
|
||||
|
||||
Var C : PCollection;
|
||||
M : PMyObject;
|
||||
I : Longint;
|
||||
|
||||
begin
|
||||
C:=New(PCollection,Init(100,10));
|
||||
For I:=1 to 100 do
|
||||
begin
|
||||
M:=New(PMyObject,Init);
|
||||
M^.SetField(100-I);
|
||||
C^.Insert(M);
|
||||
end;
|
||||
For I:=0 to C^.Count-1 do
|
||||
begin
|
||||
M:=C^.At(I);
|
||||
Writeln ('Object ',i,' has field : ',M^.GetField);
|
||||
end;
|
||||
C^.FreeAll;
|
||||
Dispose(C,Done);
|
||||
end.
|
33
docs/objectex/ex24.pp
Normal file
33
docs/objectex/ex24.pp
Normal file
@ -0,0 +1,33 @@
|
||||
Program ex21;
|
||||
|
||||
{ Program to demonstrate the TCollection.At method }
|
||||
|
||||
Uses Objects,MyObject; { For TMyObject definition and registration }
|
||||
|
||||
Var C : PCollection;
|
||||
M,Keep : PMyObject;
|
||||
I : Longint;
|
||||
|
||||
begin
|
||||
Randomize;
|
||||
C:=New(PCollection,Init(100,10));
|
||||
Keep:=Nil;
|
||||
For I:=1 to 100 do
|
||||
begin
|
||||
M:=New(PMyObject,Init);
|
||||
M^.SetField(I-1);
|
||||
If Random<0.1 then
|
||||
Keep:=M;
|
||||
C^.Insert(M);
|
||||
end;
|
||||
If Keep=Nil then
|
||||
begin
|
||||
Writeln ('Please run again. No object selected');
|
||||
Halt(1);
|
||||
end;
|
||||
Writeln ('Selected object has field : ',Keep^.GetField);
|
||||
Write ('Selected object has index : ',C^.IndexOf(Keep));
|
||||
Writeln (' should match it''s field.');
|
||||
C^.FreeAll;
|
||||
Dispose(C,Done);
|
||||
end.
|
33
docs/objectex/ex25.pp
Normal file
33
docs/objectex/ex25.pp
Normal file
@ -0,0 +1,33 @@
|
||||
Program ex21;
|
||||
|
||||
{ Program to demonstrate the TCollection.Foreach method }
|
||||
|
||||
Uses Objects,MyObject; { For TMyObject definition and registration }
|
||||
|
||||
Var C : PCollection;
|
||||
M : PMyObject;
|
||||
I : Longint;
|
||||
|
||||
Function CheckField (Dummy: Pointer;P : PMyObject) : Longint;
|
||||
|
||||
begin
|
||||
If P^.GetField<56 then
|
||||
Checkfield:=1
|
||||
else
|
||||
CheckField:=0;
|
||||
end;
|
||||
|
||||
begin
|
||||
C:=New(PCollection,Init(100,10));
|
||||
For I:=1 to 100 do
|
||||
begin
|
||||
M:=New(PMyObject,Init);
|
||||
M^.SetField(I);
|
||||
C^.Insert(M);
|
||||
end;
|
||||
Writeln ('Inserted ',C^.Count,' objects');
|
||||
Writeln ('Last one for which Field<56 has index (should be 54) : ',
|
||||
C^.IndexOf(C^.LastThat(@CheckField)));
|
||||
C^.FreeAll;
|
||||
Dispose(C,Done);
|
||||
end.
|
33
docs/objectex/ex26.pp
Normal file
33
docs/objectex/ex26.pp
Normal file
@ -0,0 +1,33 @@
|
||||
Program ex21;
|
||||
|
||||
{ Program to demonstrate the TCollection.FirstThat method }
|
||||
|
||||
Uses Objects,MyObject; { For TMyObject definition and registration }
|
||||
|
||||
Var C : PCollection;
|
||||
M : PMyObject;
|
||||
I : Longint;
|
||||
|
||||
Function CheckField (Dummy: Pointer;P : PMyObject) : Longint;
|
||||
|
||||
begin
|
||||
If P^.GetField>56 then
|
||||
Checkfield:=1
|
||||
else
|
||||
CheckField:=0;
|
||||
end;
|
||||
|
||||
begin
|
||||
C:=New(PCollection,Init(100,10));
|
||||
For I:=1 to 100 do
|
||||
begin
|
||||
M:=New(PMyObject,Init);
|
||||
M^.SetField(I);
|
||||
C^.Insert(M);
|
||||
end;
|
||||
Writeln ('Inserted ',C^.Count,' objects');
|
||||
Writeln ('first one for which Field>56 has index (should be 56) : ',
|
||||
C^.IndexOf(C^.FirstThat(@CheckField)));
|
||||
C^.FreeAll;
|
||||
Dispose(C,Done);
|
||||
end.
|
16
docs/objectex/ex8.pp
Normal file
16
docs/objectex/ex8.pp
Normal file
@ -0,0 +1,16 @@
|
||||
program ex8;
|
||||
|
||||
{ Program to demonstrate the TObject.Done call }
|
||||
|
||||
Uses Objects;
|
||||
|
||||
Var O : PObject;
|
||||
|
||||
begin
|
||||
Writeln ('Memavail : ',Memavail);
|
||||
// Allocate memory for object.
|
||||
O:=New(PObject,Init);
|
||||
Writeln ('Memavail : ',Memavail);
|
||||
O^.Done;
|
||||
Writeln ('Memavail : ',Memavail);
|
||||
end.
|
24
docs/objectex/ex9.pp
Normal file
24
docs/objectex/ex9.pp
Normal file
@ -0,0 +1,24 @@
|
||||
Program ex9;
|
||||
|
||||
{ Program to demonstrate TStream.Get and TStream.Put }
|
||||
|
||||
Uses Objects,MyObject; { Definition and registration of TMyObject}
|
||||
|
||||
Var Obj : PMyObject;
|
||||
S : PStream;
|
||||
|
||||
begin
|
||||
Obj:=New(PMyObject,Init);
|
||||
Obj^.SetField($1111) ;
|
||||
Writeln ('Field value : ',Obj^.GetField);
|
||||
{ Since Stream is an abstract type, we instantiate a TMemoryStream }
|
||||
S:=New(PMemoryStream,Init(100,10));
|
||||
S^.Put(Obj);
|
||||
Writeln ('Disposing object');
|
||||
S^.Seek(0);
|
||||
Dispose(Obj,Done);
|
||||
Writeln ('Reading object');
|
||||
Obj:=PMyObject(S^.Get);
|
||||
Writeln ('Field Value : ',Obj^.GetField);
|
||||
Dispose(Obj,Done);
|
||||
end.
|
68
docs/objectex/myobject.pp
Normal file
68
docs/objectex/myobject.pp
Normal file
@ -0,0 +1,68 @@
|
||||
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;
|
||||
|
||||
Function 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.
|
Loading…
Reference in New Issue
Block a user