NCBI C++ ToolKit
classinfob.cpp

Search Toolkit Book for infob_8cpp_source

Go to the documentation of this file.
1 /* $Id: classinfob.cpp 84832 2018-12-17 14:45:09Z gouriano $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Eugene Vasilchenko
27 *
28 * File Description:
29 * !!! PUT YOUR DESCRIPTION HERE !!!
30 */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbistd.hpp>
34 #include <corelib/ncbithr.hpp>
36 #include <serial/objectinfo.hpp>
37 #include <serial/objhook.hpp>
38 #include <serial/serialimpl.hpp>
39 
41 
42 DEFINE_STATIC_MUTEX(s_ClassInfoMutex);
43 
45  size_t size, const char* name,
46  const void* /*nonCObject*/,
47  TTypeCreate createFunc,
48  const type_info& ti)
49  : CParent(typeFamily, size, name)
50 {
53  SetCreateFunction(createFunc);
54 }
55 
57  size_t size, const char* name,
58  const CObject* /*cObject*/,
59  TTypeCreate createFunc,
60  const type_info& ti)
61  : CParent(typeFamily, size, name)
62 {
63  m_IsCObject = true;
66  SetCreateFunction(createFunc);
67 }
68 
70  size_t size, const string& name,
71  const void* /*nonCObject*/,
72  TTypeCreate createFunc,
73  const type_info& ti)
74  : CParent(typeFamily, size, name)
75 {
78  SetCreateFunction(createFunc);
79 }
80 
82  size_t size, const string& name,
83  const CObject* /*cObject*/,
84  TTypeCreate createFunc,
85  const type_info& ti)
86  : CParent(typeFamily, size, name)
87 {
88  m_IsCObject = true;
91  SetCreateFunction(createFunc);
92 }
93 
95 {
96  Deregister();
97 }
98 
100  const void* memberPtr,
101  const CTypeRef& memberType)
102 {
103  CMemberInfo* memberInfo = new CMemberInfo(this, memberId,
104  TPointerOffsetType(memberPtr),
105  memberType);
106  GetItems().AddItem(memberInfo);
107  return memberInfo;
108 }
109 
111  const void* memberPtr,
112  const CTypeRef& memberType)
113 {
114  CMemberInfo* memberInfo = new CMemberInfo(this, memberId,
115  TPointerOffsetType(memberPtr),
116  memberType);
117  GetItems().AddItem(memberInfo);
118  return memberInfo;
119 }
120 
122 {
124 }
125 
127 {
128  CParent::DataSpec(spec);
129  GetItems().DataSpec(spec);
130 }
131 
133 {
134  m_Id = &id;
135  Register();
136 }
137 
142 
143 inline
145 {
146  TClasses* classes = sm_Classes;
147  if ( !classes ) {
148  CMutexGuard GUARD(s_ClassInfoMutex);
149  classes = sm_Classes;
150  if ( !classes ) {
151  classes = sm_Classes = new TClasses;
152  }
153  }
154  return *classes;
155 }
156 
157 inline
159 {
160  TClassesById* classes = sm_ClassesById;
161  if ( !classes ) {
162  CMutexGuard GUARD(s_ClassInfoMutex);
163  classes = sm_ClassesById;
164  if ( !classes ) {
165  const TClasses& cc = Classes();
166  unique_ptr<TClassesById> keep(classes = new TClassesById);
167  ITERATE ( TClasses, i , cc ) {
168  const CClassTypeInfoBase* info = *i;
169  if ( info->GetId() != typeid(bool) ) {
170  if ( !classes->insert(
171  TClassesById::value_type(&info->GetId(),
172  info)).second ) {
173  NCBI_THROW(CSerialException,eInvalidData,
174  string("duplicate class id: ")+
175  info->GetId().name());
176  }
177  }
178  }
179  sm_ClassesById = keep.release();
180  }
181  }
182  return *classes;
183 }
184 
185 inline
187 {
188  TClassesByName* classes = sm_ClassesByName;
189  if ( !classes ) {
190  CMutexGuard GUARD(s_ClassInfoMutex);
191  classes = sm_ClassesByName;
192  if ( !classes ) {
193  unique_ptr<TClassesByName> keep(classes = new TClassesByName);
194  const TClasses& cc = Classes();
195  ITERATE ( TClasses, i, cc ) {
196  const CClassTypeInfoBase* info = *i;
197  if ( !info->GetName().empty() ) {
198  classes->insert
199  (TClassesByName::value_type(info->GetName(), info));
200  }
201  }
202  sm_ClassesByName = keep.release();
203  }
204  }
205  return *classes;
206 }
207 
209 {
210  CMutexGuard GUARD(s_ClassInfoMutex);
211  delete sm_ClassesById;
212  sm_ClassesById = 0;
213  delete sm_ClassesByName;
214  sm_ClassesByName = 0;
215  Classes().insert(this);
216 }
217 
219 {
220  CMutexGuard GUARD(s_ClassInfoMutex);
221  delete sm_ClassesById;
222  sm_ClassesById = 0;
223  delete sm_ClassesByName;
224  sm_ClassesByName = 0;
225  Classes().erase(this);
226  if (Classes().size() == 0) {
227  delete sm_Classes;
228  sm_Classes = 0;
229  }
230 }
231 
233 {
235  TClassesById::iterator i = types.find(&id);
236  if ( i == types.end() ) {
237  string msg("class not found: ");
238  msg += id.name();
239  NCBI_THROW(CSerialException,eInvalidData, msg);
240  }
241  return i->second;
242 }
243 
245 {
246  TClassesByName& classes = ClassesByName();
247  pair<TClassesByName::iterator, TClassesByName::iterator> i =
248  classes.equal_range(name);
249  if ( i.first == i.second ) {
250  NCBI_THROW_FMT(CSerialException, eInvalidData,
251  "class not found: "<<name);
252  }
253  if ( --i.second != i.first ) {
254  // multiple types with the same name
255  const CClassTypeInfoBase* t1 = i.first->second;
256  const CClassTypeInfoBase* t2 = i.second->second;
258  (CSerialException, eInvalidData,
259  "ambiguous class name: "<<t1->GetName()<<
260  " ("<<t1->GetModuleName()<<"&"<<t2->GetModuleName()<<")");
261  }
262  return i.first->second;
263 }
264 
267 {
268  modules.clear();
269  CMutexGuard GUARD(s_ClassInfoMutex);
270  if (sm_Modules) {
271  modules.insert(sm_Modules->begin(), sm_Modules->end());
272  }
273 }
274 
276  const string& module, CClassTypeInfoBase::TRegClasses& names)
277 {
278  names.clear();
279  CMutexGuard GUARD(s_ClassInfoMutex);
280  TClasses& cc = Classes();
281  ITERATE ( TClasses, i , cc ) {
282  const CClassTypeInfoBase* info = *i;
283  if (info->GetModuleName() == module) {
284  names.insert( info->GetName());
285  }
286  }
287 }
288 
289 void CClassTypeInfoBase::RegisterModule(const string& module)
290 {
291  CMutexGuard GUARD(s_ClassInfoMutex);
292  if (!sm_Modules) {
293  sm_Modules = new set<string>;
294  }
295  sm_Modules->insert(module);
296 }
297 
299 {
300  if ( IsCObject() ) {
301  return static_cast<const CObject*>(objectPtr);
302  }
303  return 0;
304 }
305 
308 {
309  {
311  TContainedTypes* cache = m_ContainedTypes.get();
312  if (cache) {
313  TContainedTypes::iterator ins = cache->find(typeInfo);
314  if (ins != cache->end()) {
315  return ins->second;
316  }
317  }
318  }
320  TContainedTypes* cache = m_ContainedTypes.get();
321  if ( !cache ) {
322  m_ContainedTypes.reset(cache = new TContainedTypes);
323  }
324  pair<TContainedTypes::iterator, bool> ins =
325  cache->insert(TContainedTypes::value_type(typeInfo,
327  if ( !ins.second ) {
328  return ins.first->second;
329  }
330 
331  static int recursion_level = 0;
332  ++recursion_level;
333  EMayContainType ret;
334  try {
335  ret = CalcMayContainType(typeInfo);
336  --recursion_level;
337  if ( ret == eMayContainType_recursion ) {
338  if ( recursion_level == 0 ) {
339  ins.first->second = ret = eMayContainType_no;
340  }
341  else {
342  cache->erase(ins.first);
343  }
344  }
345  else {
346  ins.first->second = ret;
347  }
348  }
349  catch ( ... ) {
350  --recursion_level;
351  cache->erase(ins.first);
352  throw;
353  }
354  return ret;
355 }
356 
359 {
361  // check members
362  for ( TMemberIndex i = GetItems().FirstIndex(),
363  last = GetItems().LastIndex(); i <= last; ++i ) {
365  IsOrMayContainType(typeInfo);
366  if ( contains == eMayContainType_yes ) {
367  return contains;
368  }
369  if ( contains == eMayContainType_recursion ) {
370  ret = contains;
371  }
372  }
373  return ret;
374 }
375 
377 {
379 public:
381 
383 
384  virtual void ReadObject(CObjectIStream& in, const CObjectInfo& object) override;
385 
386 private:
388 };
389 
391  : m_PreRead(func)
392 {
393 }
394 
396  const CObjectInfo& object)
397 {
398  m_PreRead(object.GetTypeInfo(), object.GetObjectPtr());
399  object.GetTypeInfo()->DefaultReadData(in, object.GetObjectPtr());
400 }
401 
403 {
405 public:
407 
409 
410  virtual void ReadObject(CObjectIStream& in, const CObjectInfo& object) override;
411 
412 private:
414 };
415 
417  : m_PostRead(func)
418 {
419 }
420 
422  const CObjectInfo& object)
423 {
424  object.GetTypeInfo()->DefaultReadData(in, object.GetObjectPtr());
425  m_PostRead(object.GetTypeInfo(), object.GetObjectPtr());
426 }
427 
429 {
431 public:
433 
435 
436  virtual void WriteObject(CObjectOStream& out, const CConstObjectInfo& object) override;
437 
438 private:
440 };
441 
443  : m_PreWrite(func)
444 {
445 }
446 
448  const CConstObjectInfo& object)
449 {
450  m_PreWrite(object.GetTypeInfo(), object.GetObjectPtr());
451  object.GetTypeInfo()->DefaultWriteData(out, object.GetObjectPtr());
452 }
453 
455 {
457 public:
459 
461 
462  virtual void WriteObject(CObjectOStream& out, const CConstObjectInfo& object) override;
463 
464 private:
466 };
467 
469  : m_PostWrite(func)
470 {
471 }
472 
474  const CConstObjectInfo& object)
475 {
476  object.GetTypeInfo()->DefaultWriteData(out, object.GetObjectPtr());
477  m_PostWrite(object.GetTypeInfo(), object.GetObjectPtr());
478 }
479 
481 {
482  SetGlobalReadHook(new CPreReadHook(func));
483 }
484 
486 {
487  SetGlobalReadHook(new CPostReadHook(func));
488 }
489 
491 {
493 }
494 
496 {
498 }
499 
CConstObjectInfo –.
Definition: objectinfo.hpp:421
CObjectIStream –.
Definition: objistr.hpp:93
CObjectInfo –.
Definition: objectinfo.hpp:597
CObjectOStream –.
Definition: objostr.hpp:83
CObject –.
Definition: ncbiobj.hpp:180
TPostReadFunction m_PostRead
Definition: classinfob.cpp:413
CReadObjectHook CParent
Definition: classinfob.cpp:404
virtual void ReadObject(CObjectIStream &in, const CObjectInfo &object) override
This method will be called at approriate time when the object of requested type is to be read.
Definition: classinfob.cpp:421
CPostReadHook(TPostReadFunction func)
Definition: classinfob.cpp:416
CClassTypeInfoBase::TPostReadFunction TPostReadFunction
Definition: classinfob.cpp:406
virtual void WriteObject(CObjectOStream &out, const CConstObjectInfo &object) override
This method will be called at approriate time when the object of requested type is to be written.
Definition: classinfob.cpp:473
CClassTypeInfoBase::TPostWriteFunction TPostWriteFunction
Definition: classinfob.cpp:458
TPostWriteFunction m_PostWrite
Definition: classinfob.cpp:465
CPostWriteHook(TPostWriteFunction func)
Definition: classinfob.cpp:468
CWriteObjectHook CParent
Definition: classinfob.cpp:456
CReadObjectHook CParent
Definition: classinfob.cpp:378
TPreReadFunction m_PreRead
Definition: classinfob.cpp:387
virtual void ReadObject(CObjectIStream &in, const CObjectInfo &object) override
This method will be called at approriate time when the object of requested type is to be read.
Definition: classinfob.cpp:395
CClassTypeInfoBase::TPreReadFunction TPreReadFunction
Definition: classinfob.cpp:380
CPreReadHook(TPreReadFunction func)
Definition: classinfob.cpp:390
CWriteObjectHook CParent
Definition: classinfob.cpp:430
CPreWriteHook(TPreWriteFunction func)
Definition: classinfob.cpp:442
TPreWriteFunction m_PreWrite
Definition: classinfob.cpp:439
virtual void WriteObject(CObjectOStream &out, const CConstObjectInfo &object) override
This method will be called at approriate time when the object of requested type is to be written.
Definition: classinfob.cpp:447
CClassTypeInfoBase::TPreWriteFunction TPreWriteFunction
Definition: classinfob.cpp:432
Read hook for a standalone object.
Definition: objhook.hpp:59
Root class for all serialization exceptions.
Definition: exception.hpp:50
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
Write hook for a standalone object.
Definition: objhook.hpp:161
DEFINE_STATIC_MUTEX(s_ClassInfoMutex)
void erase(iterator pos)
Definition: map.hpp:167
container_type::iterator iterator
Definition: map.hpp:54
const_iterator end() const
Definition: map.hpp:152
iterator_bool insert(const value_type &val)
Definition: map.hpp:165
container_type::value_type value_type
Definition: map.hpp:52
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Definition: map.hpp:338
const_iterator_pair equal_range(const key_type &key) const
Definition: map.hpp:296
iterator insert(const value_type &val)
Definition: map.hpp:305
Definition: set.hpp:45
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
const_iterator begin() const
Definition: set.hpp:135
void clear()
Definition: set.hpp:153
void erase(iterator pos)
Definition: set.hpp:151
const_iterator end() const
Definition: set.hpp:136
Include a standard set of the NCBI C++ Toolkit most basic headers.
static const struct name_t names[]
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:51
std::ofstream out("events_result.xml")
main entry point for tests
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
#define NCBI_THROW_FMT(exception_class, err_code, message)
The same as NCBI_THROW but with message processed as output to ostream.
Definition: ncbiexpt.hpp:719
void DataSpec(EDataSpec spec)
Definition: memberlist.cpp:104
const CItemInfo * GetItemInfo(TMemberIndex index) const
TTypeInfo GetTypeInfo(void) const
void AddItem(CItemInfo *item)
Definition: memberlist.cpp:70
void AssignItemsTags(CAsnBinaryDefs::ETagType containerType)
Definition: memberlist.cpp:78
void(* TPreReadFunction)(const CTypeInfo *info, void *object)
Definition: serialbase.hpp:81
void(* TPreWriteFunction)(const CTypeInfo *info, const void *object)
Definition: serialbase.hpp:83
size_t TMemberIndex
Type used for indexing class members and choice variants.
Definition: serialdef.hpp:230
const void * TConstObjectPtr
Definition: serialdef.hpp:59
void(* TPostWriteFunction)(const CTypeInfo *info, const void *object)
Definition: serialbase.hpp:84
#define XSERIAL_TYPEINFO_READLOCK
Definition: serialimpl.hpp:278
ETypeFamily
Type family.
Definition: serialdef.hpp:138
ssize_t TPointerOffsetType
Definition: serialdef.hpp:241
#define XSERIAL_TYPEINFO_WRITELOCK
Definition: serialimpl.hpp:277
EDataSpec
Definition: serialdef.hpp:204
void(* TPostReadFunction)(const CTypeInfo *info, void *object)
Definition: serialbase.hpp:82
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
const type_info * m_Id
Definition: classinfob.hpp:131
const string & GetName(void) const
Get name of this type.
Definition: typeinfo.cpp:249
virtual const string & GetModuleName(void) const
Get module name.
Definition: typeinfo.cpp:254
virtual const CObject * GetCObjectPtr(TConstObjectPtr objectPtr) const override
Definition: classinfob.cpp:298
CMemberInfo * AddMember(const char *memberId, const void *memberPtr, const CTypeRef &memberType)
Definition: classinfob.cpp:99
static TClassesByName * sm_ClassesByName
Definition: classinfob.hpp:145
void(* TPostWriteFunction)(TTypeInfo info, TConstObjectPtr object)
Definition: classinfob.hpp:92
void DataSpec(EDataSpec spec)
Definition: typeinfo.cpp:182
static set< string > * sm_Modules
Definition: classinfob.hpp:146
void AssignItemsTags(void)
Definition: classinfob.cpp:121
void SetPostWriteFunction(TPostWriteFunction func)
Definition: classinfob.cpp:495
virtual CTypeInfo * SetTag(CAsnBinaryDefs::TLongTag tag, CAsnBinaryDefs::ETagClass tagclass=CAsnBinaryDefs::eUniversal, CAsnBinaryDefs::ETagType tagtype=CAsnBinaryDefs::eAutomatic)
Definition: typeinfo.cpp:172
static void GetRegisteredClassNames(const string &module, TRegClasses &names)
Definition: classinfob.cpp:275
const CItemsInfo & GetItems(void) const
CAsnBinaryDefs::ETagType GetTagType(void) const
Definition: typeinfo.hpp:247
void SetGlobalWriteHook(CWriteObjectHook *hook)
Set global (for all input streams) write hook.
Definition: typeinfo.cpp:392
void Deregister(void)
Definition: classinfob.cpp:218
static TTypeInfo GetClassInfoById(const type_info &id)
Definition: classinfob.cpp:232
CClassTypeInfoBase(ETypeFamily typeFamily, size_t size, const char *name, const void *nonCObject, TTypeCreate createFunc, const type_info &ti)
Definition: classinfob.cpp:44
void(* TPostReadFunction)(TTypeInfo info, TObjectPtr object)
Definition: classinfob.hpp:90
bool m_IsCObject
Definition: typeinfo.hpp:304
static TClasses * sm_Classes
Definition: classinfob.hpp:143
static TTypeInfo GetClassInfoByName(const string &name)
Definition: classinfob.cpp:244
bool IsCObject(void) const
Check is this TypeInfo object is kind of CClassTypeInfoBase.
virtual EMayContainType GetMayContainType(TTypeInfo type) const override
Definition: classinfob.cpp:307
void Register(void)
Definition: classinfob.cpp:208
static TClassesById * sm_ClassesById
Definition: classinfob.hpp:144
static TClasses & Classes(void)
Definition: classinfob.cpp:144
void(* TPreWriteFunction)(TTypeInfo info, TConstObjectPtr object)
Definition: classinfob.hpp:91
static TClassesByName & ClassesByName(void)
Definition: classinfob.cpp:186
void SetCreateFunction(TTypeCreate func)
Definition: typeinfo.cpp:355
void SetGlobalReadHook(CReadObjectHook *hook)
Set global (for all input streams) read hook.
Definition: typeinfo.cpp:367
void SetPreWriteFunction(TPreWriteFunction func)
Definition: classinfob.cpp:490
EMayContainType IsOrMayContainType(TTypeInfo type) const
static TClassesById & ClassesById(void)
Definition: classinfob.cpp:158
unique_ptr< TContainedTypes > m_ContainedTypes
Definition: classinfob.hpp:135
static void RegisterModule(const string &module)
Definition: classinfob.cpp:289
void SetPostReadFunction(TPostReadFunction func)
Definition: classinfob.cpp:485
static void GetRegisteredModuleNames(TRegModules &modules)
Definition: classinfob.cpp:265
virtual ~CClassTypeInfoBase(void)
Definition: classinfob.cpp:94
void DataSpec(EDataSpec spec)
Definition: classinfob.cpp:126
set< CClassTypeInfoBase * > TClasses
Definition: classinfob.hpp:138
void(* TPreReadFunction)(TTypeInfo info, TObjectPtr object)
Definition: classinfob.hpp:89
void InitClassTypeInfoBase(const type_info &id)
Definition: classinfob.cpp:132
virtual EMayContainType CalcMayContainType(TTypeInfo typeInfo) const
Definition: classinfob.cpp:358
void SetPreReadFunction(TPreReadFunction func)
Definition: classinfob.cpp:480
@ eMayContainType_recursion
Definition: typeinfo.hpp:185
@ eMayContainType_no
Definition: typeinfo.hpp:184
@ eMayContainType_yes
Definition: typeinfo.hpp:186
int i
static MDB_envinfo info
Definition: mdb_load.c:37
const struct ncbi::grid::netcache::search::fields::SIZE size
Multi-threading – classes, functions, and features.
std::istream & in(std::istream &in_, double &x_)
static const struct type types[]
Definition: type.c:22
Modified on Sat Dec 02 09:19:58 2023 by modify_doxy.py rev. 669887