/* ---- This file is part of SECONDO. Copyright (C) 2004, University in Hagen, Department of Computer Science, Database Systems for New Applications. SECONDO is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. SECONDO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with SECONDO; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ---- April 2006, M. Spiekermann. The file Algebra.h need to be divided into Operators.h. TypeConstructors.h, AlgebraClassDef.h and AlgebraInit.h */ #ifndef SEC_TYPECONSTRUCTOR_H #define SEC_TYPECONSTRUCTOR_H #include #include #include #include "NestedList.h" #include "AlgebraTypes.h" #include "ConstructorTemplates.h" #include "NList.h" #include "Attribute.h" // a forward declaration class AlgebraManager; /* 1 Class ~TypeConstructor~ An instance of class ~TypeConstructor~ consists of * a name * a function (``outFunc'') converting a value's Address-representation to the corresponding nested list representation * a function (``inFunc'') converting a value's nested list representation to the corresponding Address-represantation * a function (``createFunc'') allocating main memory for values which can't be represented by a single Address (may be valid for all types in the future) * a function (``deleteFunc'') releasing the memory previously allocated by createFunc. * a function (``openFunc''). * a function (``saveFunc''). * a function (``closeFunc''). * a function (``cloneFunc''). All properties of an instance of class TypeConstructor are set within the constructor. */ struct ConstructorInfo { std::string name; std::string signature; std::string typeExample; std::string listRep; std::string valueExample; std::string remarks; ConstructorInfo() : name(""), signature(""), typeExample(""), listRep(""), valueExample(""), remarks("") {} ConstructorInfo( const std::string& _name, const std::string& _signature, const std::string& _typeExample, const std::string& _listRep, const std::string& _valueExample, const std::string& _remarks ) { name = _name; signature = _signature; typeExample = _typeExample; listRep = _listRep; valueExample = _valueExample; remarks = _remarks; } ConstructorInfo( const NList& typeInfo) : name(""), signature(""), typeExample(""), listRep(""), valueExample(""), remarks("") { // note there was only made some convention // about the structure of a property list. Hence // we will be careful here. int len = typeInfo.length(); if (len >= 1) signature = typeInfo.elem(1).str(); if (len >= 2) typeExample = typeInfo.elem(2).str(); if (len >= 3) listRep = typeInfo.elem(3).str(); if (len >= 4) valueExample = typeInfo.elem(4).str(); if (len >= 5) remarks = typeInfo.elem(5).str(); } ListExpr list() const { ListExpr headList = nl->FiveElemList( nl->StringAtom("Signature"), nl->StringAtom("Example Type List"), nl->StringAtom("List Rep"), nl->StringAtom("Example List"), nl->StringAtom("Remarks") ); ListExpr specList = nl->FiveElemList( nl->TextAtom(signature), nl->TextAtom(typeExample), nl->TextAtom(listRep), nl->TextAtom(valueExample), nl->TextAtom(remarks) ); return nl->TwoElemList(headList, specList ); } ~ConstructorInfo() { name.clear(); signature.clear(); typeExample.clear(); listRep.clear(); valueExample.clear(); remarks.clear(); } }; class TypeConstructor { public: TypeConstructor( const std::string& nm, TypeProperty prop, OutObject out, InObject in, OutObject saveToList, InObject restoreFromList, ObjectCreation create, ObjectDeletion del, ObjectOpen open, ObjectSave save, ObjectClose close, ObjectClone clone, ObjectCast ca, ObjectSizeof sizeOf, TypeCheckFunction tcf ); template TypeConstructor( const ConstructorInfo ci, ConstructorFunctions cf ) { conInfo = ci; name = ci.name; propFunc = 0; outFunc = cf.out; inFunc = cf.in; saveToListFunc = cf.saveToList; restoreFromListFunc = cf.restoreFromList; createFunc = cf.create; deleteFunc = cf.deletion; openFunc = cf.open; saveFunc = cf.save; closeFunc = cf.close; cloneFunc = cf.clone; castFunc = cf.cast; sizeofFunc = cf.sizeOf; typeCheckFunc = cf.kindCheck; } /* Constructs a type constructor. */ virtual ~TypeConstructor(); /* Destroys an instance of a type constructor. */ void AssociateKind( const std::string& kindName ); /* Associates the kind ~kindName~ with this type constructor. */ ListExpr Property(); static ListExpr Property(const ConstructorInfo& ci); /* Returns the properties of the type constructor or converts a ConstructorInfo into a nested list representation. */ ListExpr Out( ListExpr type, Word value ); Word In( const ListExpr typeInfo, const ListExpr value, const int errorPos, ListExpr& errorInfo, bool& correct ); ListExpr SaveToList( ListExpr type, Word value ); Word RestoreFromList( const ListExpr typeInfo, const ListExpr value, const int errorPos, ListExpr& errorInfo, bool& correct ); Word Create( const ListExpr typeInfo ); Word Create( const NList& typeInfo ); void Delete( const ListExpr typeInfo, Word& w ); bool Open( SmiRecord& valueRecord, size_t& offset, const ListExpr typeInfo, Word& value ); bool Save( SmiRecord& valueRecord, size_t& offset, const ListExpr typeInfo, Word& value ); void Close( const ListExpr typeInfo, Word& w ); Word Clone( const ListExpr typeInfo, const Word& w ); int SizeOf(); inline int SerializedFixSize() { return serializedFixSize; } std::string& Name() { return name; } int NumOfFLOBs() { return numOfFlobs; } const ConstructorInfo& Info() { return conInfo; } /* Are methods to manipulate objects according to the type constructor. */ bool DefaultOpen( SmiRecord& valueRecord, size_t& offset, const ListExpr typeInfo, Word& value ); bool DefaultSave( SmiRecord& valueRecord, size_t& offset, const ListExpr typeInfo, Word& value ); /* Default methods for ~Open~ and ~Save~ functions if these are not provided. These methods use the ~RestoreFromList~ and ~SaveToList~ if provided, and ~In~ and ~Out~ otherwise. */ static Word DummyCreate( const ListExpr typeInfo ); static void DummyDelete( const ListExpr typeInfo, Word& w ); static void DummyClose( const ListExpr typeInfo, Word& w ); static Word DummyClone( const ListExpr typeInfo, const Word& w ); static int DummySizeOf(); inline bool TypeCheck( ListExpr type, ListExpr& errorInfo ) { if (typeCheckFunc) { return (*typeCheckFunc)( type, errorInfo); } else { return SimpleCheck( type, errorInfo ); } } const std::vector& GetKinds() { return kinds; } bool MemberOf(const std::string& k); void initKindDataProperties(); inline Attribute::StorageType GetStorageType() { return storageType; } std::string Storage2Str(); /* Dummy methods used as placeholders for type constructor functions. */ private: inline bool SimpleCheck( ListExpr type, ListExpr __attribute__((unused))& errorInfo ) { return (nl->IsEqual( type, name )); } ConstructorInfo conInfo; std::string name; // Name of type constr. TypeProperty propFunc; OutObject outFunc; InObject inFunc; OutObject saveToListFunc; InObject restoreFromListFunc; ObjectCreation createFunc; ObjectDeletion deleteFunc; ObjectOpen openFunc; ObjectSave saveFunc; ObjectClose closeFunc; ObjectClone cloneFunc; ObjectCast castFunc; ObjectSizeof sizeofFunc; TypeCheckFunction typeCheckFunc; std::vector kinds; // Kinds of type constr. int numOfFlobs; Attribute::StorageType storageType; int serializedFixSize; friend class AlgebraManager; }; #endif