[fpc-pascal] Looking for JavaScript component on FPC
Andrew Brunner
atbrunner at aurawin.com
Thu Apr 2 16:01:28 CEST 2015
On 4/2/2015 2:24 AM, Michael Van Canneyt wrote:
> And already utterly unreadable for me...
> IMHO goes to show that this is a very personal matter :-)
Units that include hierarchy via class types are a supplement for
namespaces in languages such as c#. Since Namespaces are not *yet*
available in FPC we have to use classes.
The dotted notation in libraries that use the organized unit makes for
easy code readability as well as having global variables available
within the namespace. So things like naming conventions become standard.
I have attached a 3.4kb example unit that is the back-end db module.
CMS.XML "namespace" holds all my XML related types,vars,consts,etc.
Units that use this module declare things like
var PP:dbmCMS.CMS.TPagePoint;
I realize the similarities in having a unit results in the same code as
above however, the real benefit comes when the database modules become
more complex.
The single most important feature I absolutely love is having *.TItem
and *.TItems where * is a hierarchy of nested classes (purposely
namespace). My most complex database module dbmSocial. There I have
Files.TItem/TItems and Folder.TItem/TItems.
I hope the attached unit brings some clarity to my motivation and need
for having organized units with namespaces (currently as nested classes).
-------------- next part --------------
unit dbmCMS;
{$mode objfpc}{$H+}
{
unit dbmCMS.pas
CMS Storage Database Module
DBMS facilities to handle Content Management System
Copyright Aurawin LLC 2014
Written by: Andrew Thomas Brunner
This code is protected under the Aurawin Release License
http://www.aurawin.com/aprl.html
}
interface
uses
Classes, SysUtils, hDatabase, uXML, uStringArray, uQWordArray, uByteArray, XMLRead, DOM,
uStreams, uDBModule, uDatabase, dbmContentTypes, hHTTP, uVarString;
type
CMS = class
type
XML=class
type
Stanzas=class
const
PagePoint = 'pp';
end;
Fields=class
const
FileID = 'fild';
FolderID = 'fold';
URI = 'uri';
Space = 'spc';
Data = 'data';
end;
end;
PPagePoint=^TPagePoint;
TPagePoint=record
FileID : QWord;
FolderID : QWord;
URI : TVarString;
Space : TVarString;
Data : TVarString;
end;
class function toXML(var Item:TPagePoint; Output:TMemoryStream; Header:boolean):boolean;
class function fromXML(xDoc:TXMLDocument; var Item:TPagePoint):boolean; overload;
class procedure Empty(Var Item:TPagePoint); overload;
class procedure Init(Var Item:TPagePoint); overload;
class procedure Done(Var Item:TPagePoint); overload;
end;
implementation
uses uStorage,uTimer,uImage,DB, sqldb,uBase64;
class function CMS.fromXML(xDoc:TXMLDocument; var Item:TPagePoint):boolean;
var
xItem:TDOMNode;
begin
Result:=False;
Empty(Item);
xItem:=uXML.XML.getNode(xDoc,XML.Stanzas.PagePoint);
if (xItem<>nil) then begin
with uXML.XML do begin
Item.FileID:=toQword(xItem,XML.Fields.FileID);
Item.FolderID:=toQword(xItem,XML.Fields.FolderID);
Item.Space:=toString(xItem,XML.Fields.Space);
Item.URI:=toString(xItem,XML.Fields.URI);
Item.Data:=toString(xItem,XML.Fields.Data);
Result:=True;
end;
end;
end;
class function CMS.toXML(var Item:TPagePoint; Output:TMemoryStream; Header:boolean):boolean;
begin
Result:=False;
if Header then
uXML.XML.Stamp(Main.Header.Encoding,Output);
Output.Position:=Output.Size;
uStreams.Write('<',1,Output);
uStreams.Write(XML.Stanzas.PagePoint,Output);
uStreams.Write('>',1,Output);
with uXML.XML do begin
uStreams.Write(Print(XML.Fields.FileID,Item.FileID),Output);
uStreams.Write(Print(XML.Fields.FolderID,Item.FolderID),Output);
uStreams.Write(Print(XML.Fields.Space,Item.Space,CDATA_OFF),Output);
uStreams.Write(Print(XML.Fields.URI,Item.URI,CDATA_ON),Output);
uStreams.Write(Print(XML.Fields.Data,Item.Data,CDATA_OFF),Output);
end;
uStreams.Write('</',2,Output);
uStreams.Write(XML.Stanzas.PagePoint,Output);
uStreams.Write('>',1,Output);
Result:=True;
end;
class procedure CMS.Empty(Var Item:TPagePoint);
begin
Item.FileID:=0;
Item.FolderID:=0;
SetLength(Item.Space,0);
SetLength(Item.URI,0);
SetLength(Item.Data,0);
end;
class procedure CMS.Init(Var Item:TPagePoint);
begin
Item.FileID:=0;
Item.FolderID:=0;
SetLength(Item.Space,0);
SetLength(Item.URI,0);
SetLength(Item.Data,0);
end;
class procedure CMS.Done(Var Item:TPagePoint);
begin
Finalize(Item.Space);
Finalize(Item.URI);
Finalize(Item.Data);
Finalize(Item);
end;
end.
More information about the fpc-pascal
mailing list