// UniqueIdGenerator.h #ifndef UniqueIdGenerator_H #define UniqueIdGenerator_H // Interface for a unique ID (class UniqueId) generator. // // A unique ID includes a string context, a 32-bit collection // index and a 32-bit entry index. // // The collection index 0 is reserved for transient ID's, i.e. // there is no guarantee of uniqueness ouside of a process. #include #include "dataset_util/PThreadMutex.h" #include "dataset_id/UniqueId.h" class UniqueIdGenerator { public: // typdefs typedef UniqueId::Index Index; typedef UniqueIdGenerator* (*Create) (std::string context, std::string conn); public: // Static functions. // Find and opens a collection for a given context. // Uses the current default generator for the given context. // The generator is not deleted here. // Returns 0 for error, e.g. if no creator has been registered. static UniqueIdGenerator* find_generator(std::string context); // Register a creator by name. // Returns 0 for success. // Error if a different function has already been registered // with this name. static int register_creator(std::string name, Create pfun); // Set the default generation mechanism. The connection string // is of the form NAME:parameters. The creator registered with // name NAME is used with the given connection string. // For now, this may only be set once. static int set_generator(std::string conn); // Return the default generator connection string. static std::string connection(); private: // functions // Hide the copy constructor. UniqueIdGenerator(const UniqueIdGenerator& rhs); // Hide assignment. UniqueIdGenerator& operator=(const UniqueIdGenerator& rhs); public: // functions // Default constructor. UniqueIdGenerator(); // Destructor. // Closes the collection if it is open. virtual ~UniqueIdGenerator(); // Return the next ID. virtual UniqueId next() =0; // Close the generator. // May be ignored or used as a hist to record state. // Returns 0 for success. virtual int close() =0; public: // const functions // Is this a valid generator? virtual bool is_valid() const =0; // Is this a global generator, i.e. one for which ID's can be expected // to be unique even if they are generated in different processes. virtual bool is_global() const =0; // Output stream. virtual std::ostream& ostr(std::ostream& str) const =0; }; // Output stream. std::ostream& operator<<(std::ostream& lhs, const UniqueIdGenerator& rhs); #endif