NCBI C++ ToolKit
serial.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: serial.cpp 84111 2018-10-17 15:43:58Z 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 * Serialization classes.
30 *
31 */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbistd.hpp>
35 #include <corelib/ncbimtx.hpp>
36 #include <serial/serial.hpp>
37 #include <serial/serialimpl.hpp>
38 #include <serial/serialbase.hpp>
39 #include <serial/impl/ptrinfo.hpp>
41 #include <serial/impl/choice.hpp>
42 #include <serial/objostr.hpp>
43 #include <serial/objistr.hpp>
44 #include <serial/objectiter.hpp>
47 
49 
51 {
53 }
54 
56 {
57  out.Write(object, type.Get());
58 }
59 
60 void Read(CObjectIStream& in, TObjectPtr object, const CTypeRef& type)
61 {
62  //type.Get()->SetDefault(object);
63  in.Read(object, type.Get());
64 }
65 
67 {
68  out.Write(object, type);
69 }
70 
72 {
73  in.Read(object, type);
74 }
75 
77 {
78  if (!in.EndOfData()) {
79  in.Skip(ctype);
80  return true;
81  }
82  return false;
83 }
84 
85 DEFINE_STATIC_FAST_MUTEX(s_ModuleNameMutex);
86 
87 static const string& GetModuleName(const char* moduleName)
88 {
89  CFastMutexGuard GUARD(s_ModuleNameMutex);
90  static CSafeStatic< set<string> > s_ModuleNames;
91  const string& s = *s_ModuleNames.Get().insert(moduleName).first;
93  return s;
94 }
95 
96 void SetModuleName(CTypeInfo* info, const char* moduleName)
97 {
98  info->SetModuleName(GetModuleName(moduleName));
99 }
100 
101 void SetModuleName(CEnumeratedTypeValues* info, const char* moduleName)
102 {
103  info->SetModuleName(GetModuleName(moduleName));
104 }
105 
107  const char* owner_name, const char* member_name)
108 {
109  string name = owner_name;
110  if ( member_name && *member_name ) {
111  name += '.';
112  name += member_name;
113  }
114  info->SetInternalName(name);
115 }
116 
118  const char* owner_name, const char* member_name)
119 {
120  string name = owner_name;
121  if ( member_name && *member_name ) {
122  name += '.';
123  name += member_name;
124  }
125  info->SetInternalName(name);
126 }
127 
128 // add member functions
130  const void* member,
131  const CTypeRef& r)
132 {
133  return info->AddMember(name, member, r);
134 }
136  const void* member,
137  TTypeInfo t)
138 {
139  return AddMember(info, name, member, CTypeRef(t));
140 }
142  const void* member,
144 {
145  return AddMember(info, name, member, CTypeRef(f));
146 }
147 
148 // two arguments:
150  const void* member,
151  TTypeInfoGetter1 f1,
152  const CTypeRef& r)
153 {
154  return AddMember(info, name, member, CTypeRef(f1, r));
155 }
157  const void* member,
158  TTypeInfoGetter1 f1,
159  TTypeInfo t)
160 {
161  return AddMember(info, name, member, f1, CTypeRef(t));
162 }
164  const void* member,
165  TTypeInfoGetter1 f1,
167 {
168  return AddMember(info, name, member, f1, CTypeRef(f));
169 }
170 
171 // three arguments:
173  const void* member,
174  TTypeInfoGetter1 f2,
175  TTypeInfoGetter1 f1,
176  const CTypeRef& r)
177 {
178  return AddMember(info, name, member, f2, CTypeRef(f1, r));
179 }
181  const void* member,
182  TTypeInfoGetter1 f2,
183  TTypeInfoGetter1 f1,
184  TTypeInfo t)
185 {
186  return AddMember(info, name, member, f2, f1, CTypeRef(t));
187 }
189  const void* member,
190  TTypeInfoGetter1 f2,
191  TTypeInfoGetter1 f1,
193 {
194  return AddMember(info, name, member, f2, f1, CTypeRef(f));
195 }
196 
197 // four arguments:
199  const void* member,
200  TTypeInfoGetter1 f3,
201  TTypeInfoGetter1 f2,
202  TTypeInfoGetter1 f1,
203  const CTypeRef& r)
204 {
205  return AddMember(info, name, member, f3, f2, CTypeRef(f1, r));
206 }
208  const void* member,
209  TTypeInfoGetter1 f3,
210  TTypeInfoGetter1 f2,
211  TTypeInfoGetter1 f1,
212  TTypeInfo t)
213 {
214  return AddMember(info, name, member, f3, f2, f1, CTypeRef(t));
215 }
217  const void* member,
218  TTypeInfoGetter1 f3,
219  TTypeInfoGetter1 f2,
220  TTypeInfoGetter1 f1,
222 {
223  return AddMember(info, name, member, f3, f2, f1, CTypeRef(f));
224 }
225 
226 // five arguments:
228  const void* member,
229  TTypeInfoGetter1 f4,
230  TTypeInfoGetter1 f3,
231  TTypeInfoGetter1 f2,
232  TTypeInfoGetter1 f1,
233  const CTypeRef& r)
234 {
235  return AddMember(info, name, member, f4, f3, f2, CTypeRef(f1, r));
236 }
238  const void* member,
239  TTypeInfoGetter1 f4,
240  TTypeInfoGetter1 f3,
241  TTypeInfoGetter1 f2,
242  TTypeInfoGetter1 f1,
243  TTypeInfo t)
244 {
245  return AddMember(info, name, member, f4, f3, f2, f1, CTypeRef(t));
246 }
248  const void* member,
249  TTypeInfoGetter1 f4,
250  TTypeInfoGetter1 f3,
251  TTypeInfoGetter1 f2,
252  TTypeInfoGetter1 f1,
254 {
255  return AddMember(info, name, member, f4, f3, f2, f1, CTypeRef(f));
256 }
257 
258 // add variant functions
260  const void* member,
261  const CTypeRef& r)
262 {
263  return info->AddVariant(name, member, r);
264 }
266  const void* member,
267  TTypeInfo t)
268 {
269  return AddVariant(info, name, member, CTypeRef(t));
270 }
272  const void* member,
274 {
275  return AddVariant(info, name, member, CTypeRef(f));
276 }
277 
278 // two arguments:
280  const void* member,
281  TTypeInfoGetter1 f1,
282  const CTypeRef& r)
283 {
284  return AddVariant(info, name, member, CTypeRef(f1, r));
285 }
287  const void* member,
288  TTypeInfoGetter1 f1,
289  TTypeInfo t)
290 {
291  return AddVariant(info, name, member, f1, CTypeRef(t));
292 }
294  const void* member,
295  TTypeInfoGetter1 f1,
297 {
298  return AddVariant(info, name, member, f1, CTypeRef(f));
299 }
300 
301 // three arguments:
303  const void* member,
304  TTypeInfoGetter1 f2,
305  TTypeInfoGetter1 f1,
306  const CTypeRef& r)
307 {
308  return AddVariant(info, name, member, f2, CTypeRef(f1, r));
309 }
311  const void* member,
312  TTypeInfoGetter1 f2,
313  TTypeInfoGetter1 f1,
314  TTypeInfo t)
315 {
316  return AddVariant(info, name, member, f2, f1, CTypeRef(t));
317 }
319  const void* member,
320  TTypeInfoGetter1 f2,
321  TTypeInfoGetter1 f1,
323 {
324  return AddVariant(info, name, member, f2, f1, CTypeRef(f));
325 }
326 
327 // four arguments:
329  const void* member,
330  TTypeInfoGetter1 f3,
331  TTypeInfoGetter1 f2,
332  TTypeInfoGetter1 f1,
333  const CTypeRef& r)
334 {
335  return AddVariant(info, name, member, f3, f2, CTypeRef(f1, r));
336 }
338  const void* member,
339  TTypeInfoGetter1 f3,
340  TTypeInfoGetter1 f2,
341  TTypeInfoGetter1 f1,
342  TTypeInfo t)
343 {
344  return AddVariant(info, name, member, f3, f2, f1, CTypeRef(t));
345 }
347  const void* member,
348  TTypeInfoGetter1 f3,
349  TTypeInfoGetter1 f2,
350  TTypeInfoGetter1 f1,
352 {
353  return AddVariant(info, name, member, f3, f2, f1, CTypeRef(f));
354 }
355 
356 // five arguments:
358  const void* member,
359  TTypeInfoGetter1 f4,
360  TTypeInfoGetter1 f3,
361  TTypeInfoGetter1 f2,
362  TTypeInfoGetter1 f1,
363  const CTypeRef& r)
364 {
365  return AddVariant(info, name, member, f4, f3, f2, CTypeRef(f1, r));
366 }
368  const void* member,
369  TTypeInfoGetter1 f4,
370  TTypeInfoGetter1 f3,
371  TTypeInfoGetter1 f2,
372  TTypeInfoGetter1 f1,
373  TTypeInfo t)
374 {
375  return AddVariant(info, name, member, f4, f3, f2, f1, CTypeRef(t));
376 }
378  const void* member,
379  TTypeInfoGetter1 f4,
380  TTypeInfoGetter1 f3,
381  TTypeInfoGetter1 f2,
382  TTypeInfoGetter1 f1,
384 {
385  return AddVariant(info, name, member, f4, f3, f2, f1, CTypeRef(f));
386 }
387 
388 
389 
392  const void* nonCObject,
393  TCreateFunction createFunc,
394  const type_info& ti,
395  TWhichFunction whichFunc,
396  TSelectFunction selectFunc,
397  TResetFunction resetFunc)
398 {
399  return new CChoiceTypeInfo(size, name, nonCObject, createFunc,
400  ti, whichFunc, selectFunc, resetFunc);
401 }
402 
405  const CObject* cObject,
406  TCreateFunction createFunc,
407  const type_info& ti,
408  TWhichFunction whichFunc,
409  TSelectFunction selectFunc,
410  TResetFunction resetFunc)
411 {
412  return new CChoiceTypeInfo(size, name, cObject, createFunc,
413  ti, whichFunc, selectFunc, resetFunc);
414 }
415 
418  const void* nonCObject,
419  TCreateFunction createFunc,
420  const type_info& id,
421  TGetTypeIdFunction idFunc)
422 {
423  return new CClassTypeInfo(size, name, nonCObject, createFunc, id, idFunc);
424 }
425 
428  const CObject* cObject,
429  TCreateFunction createFunc,
430  const type_info& id,
431  TGetTypeIdFunction idFunc)
432 {
433  return new CClassTypeInfo(size, name, cObject, createFunc, id, idFunc);
434 }
435 
437 {
438  info->SetPreWriteFunction(func);
439 }
440 
442 {
443  info->SetPostWriteFunction(func);
444 }
445 
447 {
448  info->SetPreReadFunction(func);
449 }
450 
452 {
453  info->SetPostReadFunction(func);
454 }
455 
457 {
458  info->SetPreWriteFunction(func);
459 }
460 
462 {
463  info->SetPostWriteFunction(func);
464 }
465 
467 {
468  info->SetPreReadFunction(func);
469 }
470 
472 {
473  info->SetPostReadFunction(func);
474 }
475 
476 static void s_ResolveItems(CTypeInfo*& info, const char*& name,
477  ETypeFamily req_family)
478 {
479  TTypeInfo info0 = info;
480  const char* name0 = name;
481  while ( const char* dot = strchr(name, '.') ) {
482  CTempString item_name(name, dot-name);
484  switch ( info->GetTypeFamily() ) {
485  case eTypeFamilyClass:
486  new_info = dynamic_cast<CClassTypeInfo*>(info)
487  ->GetMemberInfo(item_name)->GetTypeInfo();
488  break;
489  case eTypeFamilyChoice:
490  new_info = dynamic_cast<CChoiceTypeInfo*>(info)
491  ->GetVariantInfo(item_name)->GetTypeInfo();
492  break;
494  if ( item_name != "E" ) {
495  NCBI_THROW_FMT(CSerialException,eInvalidData,
496  info0->GetName()<<'.'<<
497  CTempString(name0, name-name0)<<
498  ": element name must be 'E'");
499  }
500  new_info = dynamic_cast<CContainerTypeInfo*>(info)
501  ->GetElementType();
502  break;
503  default:
504  new_info = info;
505  break;
506  }
507  // skip all pointers (CRef<>)
508  while ( new_info->GetTypeFamily() == eTypeFamilyPointer ) {
509  new_info = dynamic_cast<const CPointerTypeInfo*>(new_info)
510  ->GetPointedType();
511  }
512  info = const_cast<CTypeInfo*>(new_info);
513  name = dot+1;
514  }
515  if ( info->GetTypeFamily() != req_family ) {
516  NCBI_THROW_FMT(CSerialException,eInvalidData,
517  info0->GetName()<<'.'<<
518  CTempString(name0, name-name0)<<
519  ": not a "<<
520  (req_family == eTypeFamilyClass? "class": "choice"));
521  }
522 }
523 
525  const char* member_names,
526  CReadClassMemberHook* hook_ptr)
527 {
528  CRef<CReadClassMemberHook> hook(hook_ptr);
529  s_ResolveItems(start_info, member_names, eTypeFamilyClass);
530  dynamic_cast<CClassTypeInfo*>(start_info)
531  ->SetGlobalHook(member_names, hook);
532 }
533 
535  const char* variant_names,
536  CReadChoiceVariantHook* hook_ptr)
537 {
538  CRef<CReadChoiceVariantHook> hook(hook_ptr);
539  s_ResolveItems(start_info, variant_names, eTypeFamilyChoice);
540  dynamic_cast<CChoiceTypeInfo*>(start_info)
541  ->SetGlobalHook(variant_names, hook);
542 }
543 
545 {
546  return member.GetClassObject().GetObjectPtr();
547 }
548 
550 {
551  return variant.GetChoiceObject().GetObjectPtr();
552 }
553 
554 // Functions preventing memory leaks due to undestroyed type info objects
555 
557 {
558 /*
559  typedef AutoPtr<CEnumeratedTypeValues> TEnumTypePtr;
560  typedef list<TEnumTypePtr> TEnumTypeList;
561 
562  static CSafeStaticPtr<TEnumTypeList> s_EnumTypeList;
563 
564  TEnumTypePtr ap(object);
565  s_EnumTypeList.Get().push_back(ap);
566 */
567 }
568 
570 {
571 /*
572  typedef AutoPtr<CTypeInfo> TTypeInfoPtr;
573  typedef list<TTypeInfoPtr> TTypeInfoList;
574 
575  static CSafeStaticPtr<TTypeInfoList> s_TypeInfoList;
576 
577  TTypeInfoPtr ap(object);
578  s_TypeInfoList.Get().push_back(ap);
579 */
580 }
581 
CObjectIStream –.
Definition: objistr.hpp:93
CObjectInfoCV –.
Definition: objectiter.hpp:588
CObjectInfoMI –.
Definition: objectiter.hpp:432
CObjectOStream –.
Definition: objostr.hpp:83
CObjectTypeInfo –.
Definition: objectinfo.hpp:94
CObject –.
Definition: ncbiobj.hpp:180
Read hook for a choice variant (CHOICE)
Definition: objhook.hpp:117
Read hook for data member of a containing object (eg, SEQUENCE)
Definition: objhook.hpp:78
CRef –.
Definition: ncbiobj.hpp:618
CSafeStatic<>::
T & Get(void)
Create the variable if not created yet, return the reference.
Root class for all serialization exceptions.
Definition: exception.hpp:50
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
Include a standard set of the NCBI C++ Toolkit most basic headers.
std::ofstream out("events_result.xml")
main entry point for tests
#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 SetGlobalReadMemberHook(CTypeInfo *start_info, const char *member_names, CReadClassMemberHook *hook_ptr)
Definition: serial.cpp:524
void(* TPreReadFunction)(const CTypeInfo *info, void *object)
Definition: serialbase.hpp:81
void SetGlobalReadVariantHook(CTypeInfo *start_info, const char *variant_names, CReadChoiceVariantHook *hook_ptr)
Definition: serial.cpp:534
void SetPostRead(CClassTypeInfo *info, TPostReadFunction func)
Definition: serial.cpp:451
static CChoiceTypeInfo * CreateChoiceInfo(const char *name, size_t size, const void *nonCObject, TCreateFunction createFunc, const type_info &ti, TWhichFunction whichFunc, TSelectFunction selectFunc, TResetFunction resetFunc)
Definition: serial.cpp:391
TObjectPtr GetChoiceObjectPtr(const CObjectInfoCV &variant)
Definition: serial.cpp:549
void * TObjectPtr
Definition: serialdef.hpp:55
void SetPreWrite(CClassTypeInfo *info, TPreWriteFunction func)
Definition: serial.cpp:436
TTypeInfo CPointerTypeInfoGetTypeInfo(TTypeInfo type)
Definition: serial.cpp:50
void(* TPreWriteFunction)(const CTypeInfo *info, const void *object)
Definition: serialbase.hpp:83
TTypeInfo(* TTypeInfoGetter)(void)
Definition: serialdef.hpp:63
const void * TConstObjectPtr
Definition: serialdef.hpp:59
void RegisterEnumTypeValuesObject(CEnumeratedTypeValues *)
Definition: serial.cpp:556
void SetPreRead(CClassTypeInfo *info, TPreReadFunction func)
Definition: serial.cpp:446
void Read(CObjectIStream &in, TObjectPtr object, const CTypeRef &type)
Definition: serial.cpp:60
void(* TPostWriteFunction)(const CTypeInfo *info, const void *object)
Definition: serialbase.hpp:84
TTypeInfo(* TTypeInfoGetter1)(TTypeInfo)
Definition: serialdef.hpp:64
void SetModuleName(CTypeInfo *info, const char *moduleName)
Definition: serial.cpp:96
void Write(CObjectOStream &out, TConstObjectPtr object, const CTypeRef &type)
Definition: serial.cpp:55
static CClassTypeInfo * CreateClassInfo(const char *name, size_t size, const void *nonCObject, TCreateFunction createFunc, const type_info &id, TGetTypeIdFunction func)
Definition: serial.cpp:417
void SetPostWrite(CClassTypeInfo *info, TPostWriteFunction func)
Definition: serial.cpp:441
ETypeFamily
Type family.
Definition: serialdef.hpp:138
TObjectPtr GetClassObjectPtr(const CObjectInfoMI &member)
Definition: serial.cpp:544
void RegisterTypeInfoObject(CTypeInfo *)
Definition: serial.cpp:569
CMemberInfo * AddMember(CClassTypeInfoBase *info, const char *name, const void *member, const CTypeRef &r)
Definition: serial.cpp:129
void SetInternalName(CTypeInfo *info, const char *owner_name, const char *member_name)
Definition: serial.cpp:106
CTypeInfo::TTypeCreate TCreateFunction
CVariantInfo * AddVariant(CChoiceTypeInfo *info, const char *name, const void *member, const CTypeRef &r)
Definition: serial.cpp:259
void(* TPostReadFunction)(const CTypeInfo *info, void *object)
Definition: serialbase.hpp:82
@ eTypeFamilyClass
Definition: serialdef.hpp:140
@ eTypeFamilyContainer
Definition: serialdef.hpp:142
@ eTypeFamilyChoice
Definition: serialdef.hpp:141
@ eTypeFamilyPointer
Definition: serialdef.hpp:143
const CObjectInfo & GetChoiceObject(void) const
Get containing choice.
TObjectPtr GetObjectPtr(void) const
Get pointer to object.
const CObjectInfo & GetClassObject(void) const
Get containing class data.
bool Serial_FilterSkip(CObjectIStream &in, const CObjectTypeInfo &ctype)
Definition: serial.cpp:76
#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 string & GetName(void) const
Get name of this type.
Definition: typeinfo.cpp:249
static TTypeInfo GetTypeInfo(TTypeInfo base)
Definition: ptrinfo.cpp:124
static void RegisterModule(const string &module)
Definition: classinfob.cpp:289
static MDB_envinfo info
Definition: mdb_load.c:37
const struct ncbi::grid::netcache::search::fields::SIZE size
EIPRangeType t
Definition: ncbi_localip.c:101
Static variables safety - create on demand, destroy on application termination.
Multi-threading – mutexes; rw-locks; semaphore.
std::istream & in(std::istream &in_, double &x_)
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
static int new_info(pcre *re, pcre_extra *study, int option, void *ptr)
Definition: pcretest.c:2421
DEFINE_STATIC_FAST_MUTEX(s_ModuleNameMutex)
static void s_ResolveItems(CTypeInfo *&info, const char *&name, ETypeFamily req_family)
Definition: serial.cpp:476
static const string & GetModuleName(const char *moduleName)
Definition: serial.cpp:87
Definition: type.c:6
Modified on Tue Apr 16 20:07:40 2024 by modify_doxy.py rev. 669887