20 #ifndef INCLUDE_SF_REGISTRY_HPP 21 #define INCLUDE_SF_REGISTRY_HPP 29 #include <RCF/Export.hpp> 30 #include <RCF/ThreadLibrary.hpp> 32 #include <RCF/Tools.hpp> 36 typedef RCF::ReadWriteMutex ReadWriteMutex;
37 typedef RCF::ReadLock ReadLock;
38 typedef RCF::WriteLock WriteLock;
40 class I_SerializerPolymorphic;
41 class I_SerializerAny;
43 class RCF_EXPORT Registry : Noncopyable
48 typedef std::string Rtti;
49 typedef std::map<std::string, Rtti> TypenameToRtti;
50 typedef std::map<Rtti, std::string> RttiToTypename;
53 std::pair<Rtti, Rtti>,
54 std::shared_ptr<I_SerializerPolymorphic> >
55 RttiToSerializerPolymorphic;
59 std::shared_ptr<I_SerializerAny> >
62 TypenameToRtti mTypenameToRtti;
63 RttiToTypename mRttiToTypename;
64 RttiToSerializerPolymorphic mRttiToSerializerPolymorphic;
65 RttiToSerializerAny mRttiToSerializerAny;
66 ReadWriteMutex mReadWriteMutex;
70 friend void initRegistrySingleton();
72 static Registry &getSingleton();
74 static Registry *getSingletonPtr();
76 template<
typename Type>
77 void registerAny(Type *);
79 template<
typename Type>
80 void registerType(Type *,
const std::string &typeName);
82 template<
typename Base,
typename Derived>
83 void registerBaseAndDerived(Base *, Derived *);
85 template<
typename Base>
86 I_SerializerPolymorphic &getSerializerPolymorphic(
88 const std::string &derivedTypeName);
91 std::string getTypeName()
93 return getTypeName( (T *) 0);
96 template<
typename Type>
99 registerAny( (Type *) 0);
102 template<
typename Type>
103 void registerType(
const std::string &typeName)
105 registerType( (Type *) 0);
108 template<
typename Base,
typename Derived>
109 void registerBaseAndDerived()
111 registerBaseAndDerived( (Base *) 0, (Derived *) 0);
114 template<
typename Base>
115 I_SerializerPolymorphic &getSerializerPolymorphic(
116 const std::string &derivedTypeName)
118 return getSerializerPolymorphic( (Base *) 0, derivedTypeName);
121 I_SerializerAny * getAnySerializer(
const std::string &which);
123 bool isTypeRegistered(
const std::string &typeName);
125 bool isTypeRegistered(
const std::type_info &ti);
128 std::string getTypeName(T *)
130 return getTypeName(
typeid(T));
133 std::string getTypeName(
const std::type_info &ti);
139 template<
typename Type>
140 inline void registerAny(Type *)
142 Registry::getSingleton().registerAny( (Type *) 0);
145 template<
typename Type>
146 inline void registerType(Type *,
const std::string &typeName)
148 Registry::getSingleton().registerType( (Type *) 0, typeName);
151 template<
typename Base,
typename Derived>
152 inline void registerBaseAndDerived( Base *, Derived *)
154 Registry::getSingleton().registerBaseAndDerived(
161 #include <SF/SerializePolymorphic.hpp> 171 void registerType(
const std::string &typeName)
173 Registry::getSingleton().registerType( (T *) 0, typeName);
179 template<
typename Base,
typename Derived>
180 void registerBaseAndDerived()
182 Registry::getSingleton().registerBaseAndDerived(
190 template<
typename Type>
191 void Registry::registerAny(Type *)
193 WriteLock lock(mReadWriteMutex);
194 RCF_UNUSED_VARIABLE(lock);
195 Rtti typeRtti =
typeid(Type).name();
197 if ( mRttiToTypename.find(typeRtti) == mRttiToTypename.end() )
203 mRttiToSerializerAny[typeRtti].reset(
new SerializerAny<Type>());
206 template<
typename Type>
207 void Registry::registerType(Type *,
const std::string &typeName)
209 WriteLock lock(mReadWriteMutex);
210 RCF_UNUSED_VARIABLE(lock);
211 Rtti typeRtti =
typeid(Type).name();
212 mRttiToTypename[typeRtti] = typeName;
213 mTypenameToRtti[typeName] = typeRtti;
224 template<
typename Base,
typename Derived>
225 void Registry::registerBaseAndDerived(Base *, Derived *)
227 WriteLock lock(mReadWriteMutex);
228 RCF_UNUSED_VARIABLE(lock);
229 Rtti baseRtti =
typeid(Base).name();
230 Rtti derivedRtti =
typeid(Derived).name();
231 std::pair<Rtti, Rtti> baseDerivedRtti(baseRtti, derivedRtti);
233 mRttiToSerializerPolymorphic[baseDerivedRtti].reset(
234 new SerializerPolymorphic<Base,Derived>);
237 template<
typename Base>
238 I_SerializerPolymorphic &Registry::getSerializerPolymorphic(
240 const std::string &derivedTypeName)
242 ReadLock lock(mReadWriteMutex);
243 RCF_UNUSED_VARIABLE(lock);
244 Rtti baseRtti =
typeid(Base).name();
245 Rtti derivedRtti = mTypenameToRtti[derivedTypeName];
246 std::pair<Rtti, Rtti> baseDerivedRtti(baseRtti, derivedRtti);
248 mRttiToSerializerPolymorphic.find(baseDerivedRtti)
249 == mRttiToSerializerPolymorphic.end())
257 return *mRttiToSerializerPolymorphic[ baseDerivedRtti ];
262 #endif // ! INCLUDE_SF_REGISTRY_HPP
Base class for all RCF exceptions.
Definition: Exception.hpp:67
Definition: ByteBuffer.hpp:188