// ObjData.hpp #ifndef ObjData_H #define ObjData_H // Enable lists. #define ObjData_supports_lists 1 //********************************************************************** // This class holds the named data for an object. // Persistent objects provide a method to bundle their data in an // object of this type and provide a function to create themselves // from such an object. // // The tyes of data supported are: // INT - int // DOUBLE - double // BOOL - bool // STRING - string // BARE_PTR - const ObjType* // SHARE_PTR - Ptr p which can be assigned with // Ptr::assign_with_dynamic_cast(p) // // It also hold containers of any of these object types. Supported // containers include vector, list and set. // // Thes containers are defined only if the flag ObjData_supports_lists // is defined. //********************************************************************** #include "objstream/ObjException.hpp" #include "objstream/ObjType.hpp" #include #include #include #include #include #include #include #ifdef NT_MSVCPP #include "objstream/ObjData_inlined.hpp" #else class ObjData { public: // enums // Enum listing the known types of data. // PTR is a pointer which shares enum Type { INT, DOUBLE, BOOL, STRING, BARE_PTR, SHARE_PTR #ifdef ObjData_supports_lists , INT_LIST, DOUBLE_LIST, BOOL_LIST, STRING_LIST, BARE_PTR_LIST, SHARE_PTR_LIST #endif }; public: // typedefs // Type name. typedef std::string TypeName; // Datum name. typedef std::string Name; // ordered list of Datum names. typedef std::vector NameList; // list of Datum names. typedef std::set NameSet; // Comparator for names. typedef std::less CmpName; // Map of types indexed by name. typedef std::map TypeMap; // Bare pointer. typedef const ObjType* BarePtr; // Shared management pointer. typedef ObjPtr SharePtr; #ifdef ObjData_supports_lists // Data Lists. typedef std::vector IntList; typedef std::vector DoubleList; #ifdef __KCC typedef std::list BoolList; #else typedef std::vector BoolList; #endif typedef std::vector StringList; typedef std::vector BarePtrList; typedef std::vector SharePtrList; #endif // Data maps. typedef std::map IntMap; typedef std::map DoubleMap; typedef std::map BoolMap; typedef std::map StringMap; typedef std::map BarePtrMap; typedef std::map SharePtrMap; #ifdef ObjData_supports_lists typedef std::map IntListMap; typedef std::map DoubleListMap; typedef std::map BoolListMap; typedef std::map StringListMap; typedef std::map BarePtrListMap; typedef std::map SharePtrListMap; #endif private: // data TypeName _type; NameList _ordered_names; NameSet _names; TypeMap _types; IntMap _ints; DoubleMap _doubles; BoolMap _bools; StringMap _strings; BarePtrMap _bare_ptrs; SharePtrMap _share_ptrs; #ifdef ObjData_supports_lists IntListMap _int_lists; DoubleListMap _double_lists; BoolListMap _bool_lists; StringListMap _string_lists; BarePtrListMap _bare_ptr_lists; SharePtrListMap _share_ptr_lists; #endif public: // Constructor. // Constructor. // User may specify the type name. ObjData(TypeName name =""); public: // Methods to add data. // Add an int. void add_int(Name name, int val); // Add a double. void add_double(Name name, double val); // Add a bool. void add_bool(Name name, bool val); // Add a string. void add_string(Name name, std::string val); // Add a bare pointer. void add_bare_pointer(Name name, BarePtr val); // Add a share pointer. template void add_share_pointer(Name name, const Ptr& ptr); #ifdef ObjData_supports_lists public: // Methods to add data lists. // Add ints. template void add_int_list(Name name, C&); // Add doubles. template void add_double_list(Name name, C&); // Add bools. template void add_bool_list(Name name, C&); // Add strings. template void add_string_list(Name name, C&); // Add bare pointers. template void add_bare_ptr_list(Name name, C&); // Add share pointers. template void add_share_ptr_list(Name name, C&); #endif public: // Methods to retrieve data. // Return if data name is used. bool has(Name name) const; // Return the type name. TypeName get_object_type() const { return _type; } // Retrieve the list of names. const NameList& get_names() const { return _ordered_names; } // Retrieve the type for given name. Type get_type(Name name) const; // Retrieve an int. int get_int(Name name) const; // Retrieve a double. double get_double(Name name) const; // Retrieve a bool. bool get_bool(Name name) const; // Retrieve a string. std::string get_string(Name name) const; // Retrieve a bare pointer. BarePtr get_bare_pointer(Name name) const; // Retrieve a typed bare pointer. template void get_bare_pointer(Name name, const T*& ptr) const; // Retrieve a share pointer. SharePtr get_share_pointer(Name name) const; // Retrieve a typed share pointer. template void get_share_pointer(Name name, Ptr& ptr) const; #ifdef ObjData_supports_lists public: // Methods to retrieve data lists. // Retrieve ints. template void get_int_list(Name name, C&) const; // Retrieve doubles. template void get_double_list(Name name, C&) const; // Retrieve bools. template void get_bool_list(Name name, C&) const; // Reteve strings. template void get_string_list(Name name, C&) const; // Retrieve bare pointers. template void get_bare_ptr_list(Name name, C&) const; // RetRieve share pointers. template void get_share_ptr_list(Name name, C&) const; #endif }; #endif // end NT_MSVCPP //********************************************************************** // Exceptions used by this class. // Base for exceptions from this class. class ObjDataException : public ObjException { }; // Duplicate datum name. class DuplicateDatumName : public ObjDataException { }; // Unknown datum name. class UnknownDatumName : public ObjDataException { }; // Type mismatch. class TypeNameMismatch: public ObjDataException { public: TypeNameMismatch(std::string type, std::string name); }; // Unknown type. class UnknownType : public ObjDataException { }; // Invalid cast. class InvalidCast : public ObjDataException { }; //********************************************************************** // Output stream. std::ostream& operator<<(std::ostream& stream, const ObjData& rhs); //********************************************************************** #ifndef NT_MSVCPP #include "objstream/ObjData.tpp" #endif #endif