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 static bool isPersistent(std::string const& name, Type const& type, std::string const& source_id);
00110 void add(std::string const& name, Type* new_type, bool persistent, std::string const& source_id);
00111
00114 std::set<Type*> reverseDepends(Type const& type);
00115
00119 void updateCurrentNameMap();
00120
00121 public:
00122 typedef RegistryIterator Iterator;
00123
00124 Registry();
00125 ~Registry();
00126
00132 void importNamespace(const std::string& name, bool erase_existing = false);
00133
00137 bool setDefaultNamespace(const std::string& name);
00141 std::string getDefaultNamespace() const;
00142
00151 bool has(const std::string& name, bool build = true) const;
00152
00156 std::string source(Type const* type) const;
00157
00159 Type const* build(const std::string& name, std::size_t size = 0);
00160
00165 Type const* get(const std::string& name) const;
00166
00172 RegistryIterator find(std::string const& name) const;
00173
00174
00175
00176
00177 Type* get_(const std::string& name);
00178
00181 std::set<Type const*> reverseDepends(Type const& type) const;
00182
00189 std::set<Type *> remove(Type const& type);
00190
00191
00192
00193
00194 Type& get_(Type const& type);
00195
00198 void add(Type* type, std::string const& source_id = "");
00199
00204 void add(Type* type, bool persistent, std::string const& source_id);
00205
00214 void alias(std::string const& base, std::string const& alias, bool persistent, std::string const& source_id = "");
00215
00218 void alias(std::string const& base, std::string const& alias, std::string const& source_id = "");
00219
00222 size_t size() const;
00224 void clear();
00225
00229 std::string getFullName (const std::string& name) const;
00230
00232 RegistryIterator begin() const;
00234 RegistryIterator end() const;
00239 RegistryIterator begin(std::string const& prefix) const;
00255 RegistryIterator end(std::string const& prefix) const;
00256
00258 static Type const& null();
00259
00261 void merge(Registry const& registry);
00262
00265 void mergeMetaData(Registry const& registry);
00266
00270 void resize(std::map<std::string, size_t> const& new_sizes);
00271
00274 Registry* minimal(std::string const& name, bool with_aliases = true) const;
00275
00278 Registry* minimal(Registry const& auto_types) const;
00279
00287 bool isSame(Registry const& other) const;
00288
00290 bool isIncluded(Type const& type) const;
00291
00293 std::set<std::string> getAliasesOf(Type const& type) const;
00294
00296 void setSourceID(Type const& type, std::string const& source_id);
00297
00300 void copySourceIDs(Registry const& registry);
00301
00304 void clearAliases();
00305
00306 public:
00307 enum DumpMode
00308 {
00309 NameOnly = 0,
00310 AllType = 1,
00311 WithSourceId = 2,
00312 RecursiveTypeDump = 4
00313 };
00314 void dump(std::ostream& stream, int dumpmode = AllType, const std::string& source_filter = "*") const;
00315 };
00316 }
00317
00318 #endif
00319