// ObjTable.hpp #ifndef ObjTable_H #define ObjTable_H // Maintains a table of object pointers indexed by name. // This class is a singleton, i.e. at most one instance exists. // Its public member functions are all static. // The single instance of the table is created if needed when // any of these members are invoked. // // Objects held in the table must be derived from ObjectType // which implies that they provide a method to write their data // into a data object (object of type ObjData). Normally they // will also provide a free function which can be used to create // an object of the type from a data object. They should also // register this function with this class. // // The table has shared management of all the objects it contains. // Any contained objects without other references are deleted when // the table is deleted. // // There is a shared management pointer to the single instance of the // table. It is deleted when the static symbols for this class are // deleted. This guarantees that the object table and its contents are // eventually deleted. Any function or object which wishes to extend // the lifetime of the table beyond this time can obtain a pointer // sharing this management. // // Mutable and immutable objects: // // Object instances can be either mutable or immutable. Immutable objects // can not be deleted or modified. By default, objects are immutable. // Object instances be declared mutable only before thery are created. #include #include #include #include "objstream/ObjType.hpp" #include "objstream/ObjName.hpp" #include "ptr/Ptr.h" #include "ptr/SharedDeletePolicy.h" typedef void* (*ObjCreator_void)(const ObjData&); class ObjData; class ObjTable { public: // enums // error return status. enum Status { OK=0, UNKNOWN_NAME, UNKNOWN_OBJECT, DUPLICATE_NAME, DUPLICATE_OBJECT, UNKNOWN_TYPE, DUPLICATE_TYPE, DUPLICATE_CREATOR, TYPE_MISMATCH, CREATION_FAILURE, IMMUTABLE_OBJECT }; public: // typedefs // Type name. typedef ObjType::TypeName TypeName; private: // typedefs // Shared management pointer for ObjTable. typedef Ptr ObjTablePtr; // Map of objects indexed by name. typedef std::map OMap; // Map of names indexed by object. typedef std::map NMap; // List of objects, by name. typedef std::set OList; // For NT the following must be public. #ifdef NT_MSVCPP public: #endif // Map of creators indexed by type name. typedef std::map CMap; // Map of type names indexed by creator. typedef std::map TMap; private: // static data // Shared managing pointer to the single instance of this class. static ObjTablePtr _ptable; private: // member data // Map of creators indexed by type name. CMap _cmap; // Map of type names indexed by creator. TMap _tmap; // Map of object pointers indexed by object name. OMap _omap; // Map of object names indexed by object address. NMap _nmap; // List of mutable objects. OList _mutable_objects; private: // non-static methods // For NT the following must be public. #ifdef NT_MSVCPP public: #endif // Return the creator map. static CMap& get_cmap() { return get_managing_pointer()->_cmap; } // Return the type map. static TMap& get_tmap() { return get_managing_pointer()->_tmap; } // Return the object map. static OMap& get_omap() { return get_managing_pointer()->_omap; } // Return the name map. static NMap& get_nmap() { return get_managing_pointer()->_nmap; } // Return list of mutable objects. static OList& get_mutable_objects() { return get_managing_pointer()->_mutable_objects;} public: // static methods // Get a managing pointer for the table. // Any object holding such a pointer is guaranteed that the single // instance of ObjTable has not been deleted. // The table is created if needed. static ObjTablePtr& get_managing_pointer(); public: // static methods for types #ifndef NT_MSVCPP // Register a type, i.e. assign a creator for a type name. // The class must inherit from ObjType and provide unique // implementations of the static members get_type_name() and // get_creator(). template static Status register_type(); #endif // Return the creator associated with a type name. static ObjCreator get_creator(TypeName type); // Return the type name associated with creator. static TypeName get_type_name(ObjCreator cr); public: // static methods for objects // Register a type, i,e, assign a creator for a type name. // Add an object from memory. static Status add_object(ObjName name, const ObjPtr& ptr); // Delete an object (must be mutable). static Status delete_object(ObjName name); // Create and add an object from object data. static Status new_object(ObjName name, const ObjData& data); // Return the number of objects. static int get_object_count(); // Check that an object is stored. static bool has_object_pointer(const void* ptr); // Check that an object name is defined. static bool has_object_name(ObjName name); // Fetch an object name. static ObjName get_object_name(const void* ptr); // Declare an object to be mutable. static Status declare_mutable(ObjName name); // Determine whether an object is mutable. static bool is_mutable(ObjName); #ifndef NT_MSVCPP // Fetch an object. // Return nonzero for failure. template static Status get_object(ObjName name, P& ptr); // Fetch an object. // Return nonzero for failure. template static Status get_object(ObjName name, const T*& ptr); #endif #ifdef NT_MSVCPP #include "ObjTable_inlined.hpp" #else }; #endif #ifndef NT_MSVCPP #include "ObjTable.tpp" #endif #endif