registry.hh

Go to the documentation of this file.
00001 #ifndef TYPELIB_REGISTRY_H
00002 #define TYPELIB_REGISTRY_H
00003 
00004 #include "typemodel.hh"
00005 #include <boost/shared_ptr.hpp>
00006 #include <stdexcept>
00007 
00008 namespace Typelib
00009 {
00010     class RegistryIterator;
00011     
00013     class RegistryException : public std::runtime_error 
00014     { 
00015     public:
00016         RegistryException(std::string const& what) : std::runtime_error(what) {}
00017     };
00018 
00020     class Undefined : public RegistryException
00021     {
00022         std::string m_name;
00023 
00024     public:
00025         Undefined(const std::string& name)
00026             : RegistryException("undefined type '" + name + "'")
00027             , m_name(name) {}
00028         ~Undefined() throw() {}
00029 
00030         std::string getName() const { return m_name; }
00031     };
00032 
00035     class AlreadyDefined : public RegistryException
00036     {
00037         std::string m_name;
00038 
00039     public:
00040         AlreadyDefined(std::string const& name)
00041             : RegistryException("type " + name + " already defined in registry")
00042             , m_name(name) {}
00043         ~AlreadyDefined() throw() {}
00044 
00045         std::string getName() const { return m_name; }
00046     };
00047 
00049     class BadName : public RegistryException
00050     {
00051         const std::string m_name;
00052 
00053     public:
00054         BadName(const std::string& name)
00055             : RegistryException(name + " is not a valid type name")
00056             , m_name(name) {}
00057         ~BadName() throw() {}
00058     };
00059 
00063     class DefinitionMismatch : public RegistryException
00064     {
00065         const std::string m_name;
00066 
00067     public:
00068         DefinitionMismatch(const std::string& name)
00069             : RegistryException(name + " already defines a type in the registry, but with a different definition") {}
00070         ~DefinitionMismatch() throw() {}
00071     };
00072 
00073 
00086     class Registry
00087     {
00088         friend class RegistryIterator;
00089         
00090     private:
00091         struct RegistryType
00092         {
00093             Type*       type;
00094             bool        persistent;
00095             std::string source_id;
00096         };
00097         typedef std::map
00098             < const std::string
00099             , RegistryType
00100             , bool (*) (const std::string&, const std::string&)
00101             >     TypeMap;
00102 
00103         typedef std::map<const std::string, RegistryType>  NameMap;
00104 
00105         TypeMap                 m_global;
00106         NameMap                 m_current;
00107         std::string             m_namespace;
00108 
00109         void addStandardTypes();
00110 
00111         static char const* const s_stdsource;
00112         static bool isPersistent(std::string const& name, Type const& type, std::string const& source_id);
00113         void add(std::string const& name, Type* new_type, std::string const& source_id);
00114 
00115         /* Internal version to get a non-const Type object */
00116         Type* get_(const std::string& name);
00117 
00118     public:
00119         typedef RegistryIterator Iterator;
00120 
00121         Registry();
00122         ~Registry();
00123 
00129         void importNamespace(const std::string& name, bool erase_existing = false);
00130 
00134         bool setDefaultNamespace(const std::string& name);
00138         std::string getDefaultNamespace() const;
00139        
00148         bool        has(const std::string& name, bool build = true) const;
00149 
00153         std::string source(Type const* type) const;
00154 
00156         Type const* build(const std::string& name);
00157 
00162         Type const* get(const std::string& name) const;  
00163 
00168         void        add(Type* type, std::string const& source_id = "");
00169 
00178         void        alias(std::string const& base, std::string const& alias, std::string const& source_id = "");
00179 
00182         size_t      size() const;
00184         void        clear();
00185 
00189         std::string getFullName (const std::string& name) const;
00190 
00192         RegistryIterator begin() const;
00194         RegistryIterator end() const;
00195         
00197         static Type const& null();
00198 
00200         void merge(Registry const& registry);
00201 
00204         Registry* minimal(Registry const& auto_types) const;
00205 
00213         bool isSame(Registry const& other) const;
00214         
00215     public:
00216         enum DumpMode
00217         {
00218             NameOnly = 0, 
00219             AllType  = 1, 
00220             WithSourceId = 2,
00221             RecursiveTypeDump = 4
00222         };
00223         void dump(std::ostream& stream, int dumpmode = AllType, const std::string& source_filter = "*") const;
00224     };
00225 }
00226 
00227 #endif
00228 

Generated on Thu Jan 22 16:37:20 2009 for typeLib by doxygen 1.5.6
SourceForge.net Project Page