NCBI C++ ToolKit
typeinfo.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef TYPEINFO__HPP
2 #define TYPEINFO__HPP
3 
4 /* $Id: typeinfo.hpp 99787 2023-05-10 17:54:36Z vasilche $
5 * ===========================================================================
6 *
7 * PUBLIC DOMAIN NOTICE
8 * National Center for Biotechnology Information
9 *
10 * This software/database is a "United States Government Work" under the
11 * terms of the United States Copyright Act. It was written as part of
12 * the author's official duties as a United States Government employee and
13 * thus cannot be copyrighted. This software/database is freely available
14 * to the public for use. The National Library of Medicine and the U.S.
15 * Government have not placed any restriction on its use or reproduction.
16 *
17 * Although all reasonable efforts have been taken to ensure the accuracy
18 * and reliability of the software and data, the NLM and the U.S.
19 * Government do not and cannot warrant the performance or results that
20 * may be obtained by using this software or data. The NLM and the U.S.
21 * Government disclaim all warranties, express or implied, including
22 * warranties of performance, merchantability or fitness for any particular
23 * purpose.
24 *
25 * Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Eugene Vasilchenko
30 *
31 * File Description:
32 * Data type information: members and memory layout
33 */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <serial/serialdef.hpp>
37 #include <serial/impl/hookdata.hpp>
38 #include <serial/impl/hookfunc.hpp>
40 
41 
42 /** @addtogroup TypeInfoCPP
43  *
44  * @{
45  */
46 
47 
49 
50 class CObject;
51 
52 class CObjectIStream;
53 class CObjectOStream;
55 
56 class CClassTypeInfo;
57 
58 class CObjectTypeInfo;
59 class CConstObjectInfo;
60 class CObjectInfo;
61 
62 class CReadObjectHook;
63 class CWriteObjectHook;
64 class CSkipObjectHook;
65 class CCopyObjectHook;
66 
67 class CTypeInfoFunctions;
68 
69 class CNamespaceInfoItem;
70 class CObjectMemoryPool;
71 class CSerialUserOp;
72 
73 /// CTypeInfo class contains all information about C++ types (both basic and
74 /// classes): members and layout in memory.
76 {
77 protected:
78  enum {
79  eNoExplicitTag = -1,
80  };
81  CTypeInfo(ETypeFamily typeFamily, size_t size);
82  CTypeInfo(ETypeFamily typeFamily, size_t size, const char* name);
83  CTypeInfo(ETypeFamily typeFamily, size_t size, const string& name);
84 public:
85  // various function pointers
86  typedef TObjectPtr (*TTypeCreate)(TTypeInfo objectType,
87  CObjectMemoryPool* memoryPool);
88 
89  virtual ~CTypeInfo(void);
90 
92 
93  /// Get name of this type
94  /// @return
95  /// Data type name
96  const string& GetName(void) const;
97 
98  /// Check if data type has namespace name
99  bool HasNamespaceName(void) const;
100  /// Get namespace name
101  const string& GetNamespaceName(void) const;
102  /// Set namespace name
103  const CTypeInfo* SetNamespaceName(const string& ns_name) const;
104  /// Set namespace 'qualified' property
105  const CTypeInfo* SetNsQualified(bool qualified) const;
106  /// Get namespace 'qualified' property
107  ENsQualifiedMode IsNsQualified(void) const;
108 
109  /// Check if data type has namespace prefix
110  bool HasNamespacePrefix(void) const;
111  /// Get namespace prefix
112  const string& GetNamespacePrefix(void) const;
113  /// Set namespace prefix
114  void SetNamespacePrefix(const string& ns_prefix) const;
115 
116  /// Get module name
117  virtual const string& GetModuleName(void) const;
118  /// Set module name
119  void SetModuleName(const string& name);
120  /// Set module name
121  void SetModuleName(const char* name);
122 
123  /// Get size of data object in memory (like sizeof in C)
124  size_t GetSize(void) const;
125 
126  /// Create object of this type on heap (can be deleted by operator delete)
127  TObjectPtr Create(CObjectMemoryPool* memoryPool = 0) const;
128 
129  /// Delete object
130  virtual void Delete(TObjectPtr object) const;
131 
132  // clear object contents so Delete will not leave unused memory allocated
133  // note: object contents is not guaranteed to be in initial state
134  // (as after Create), to do so you should call SetDefault after
135  virtual void DeleteExternalObjects(TObjectPtr object) const;
136 
137  /// Check, whether the object contains default value
138  virtual bool IsDefault(TConstObjectPtr object) const = 0;
139  /// Check if both objects contain the same values
140  virtual bool Equals(TConstObjectPtr object1, TConstObjectPtr object2,
141  ESerialRecursionMode how = eRecursive) const = 0;
142  /// Set object to default value
143  virtual void SetDefault(TObjectPtr dst) const = 0;
144  /// Set object to copy of another one
145  virtual void Assign(TObjectPtr dst, TConstObjectPtr src,
146  ESerialRecursionMode how = eRecursive) const = 0;
147 
148  /// Check is this TypeInfo object is kind of CClassTypeInfoBase
149  bool IsCObject(void) const;
150  virtual const CObject* GetCObjectPtr(TConstObjectPtr objectPtr) const;
151  // return true CTypeInfo of object (redefined in polymorphic classes)
152  virtual TTypeInfo GetRealTypeInfo(TConstObjectPtr object) const;
153 
154  const CSerialUserOp* AsCSerialUserOp(TConstObjectPtr obj) const;
155  CSerialUserOp* AsCSerialUserOp(TObjectPtr obj) const;
156 
157  /// Check if this TypeInfo describes internal unnamed type
158  bool IsInternal(void) const;
159  /// Return internal type access string e.g. Int-fuzz.range
160  const string& GetInternalName(void) const;
161  /// Return internal type's owner module name
162  const string& GetInternalModuleName(void) const;
163  /// Mark this type as internal
164  void SetInternalName(const string& name);
165 
166  /// Return internal or regular name
167  const string& GetAccessName(void) const;
168  /// Return internal or regular module name
169  const string& GetAccessModuleName(void) const;
170 
171  // I/O interface:
172  void ReadData(CObjectIStream& in, TObjectPtr object) const;
174  void CopyData(CObjectStreamCopier& copier) const;
175  void SkipData(CObjectIStream& in) const;
176 
177  virtual bool IsParentClassOf(const CClassTypeInfo* classInfo) const;
178  virtual bool IsType(TTypeInfo type) const;
179 
181 
183  {
185  eMayContainType_recursion, // real value may be yes or no, no caching
186  eMayContainType_yes
187  };
189  virtual EMayContainType GetMayContainType(TTypeInfo type) const;
190 
191  // hooks
192  /// Set global (for all input streams) read hook
193  void SetGlobalReadHook(CReadObjectHook* hook);
194  /// Set local (for a specific input stream) read hook
196  /// Reset global read hooks
197  void ResetGlobalReadHook(void);
198  /// Reset local read hook
199  void ResetLocalReadHook(CObjectIStream& in);
200  /// Set local context-specific read hook
201  void SetPathReadHook(CObjectIStream* in, const string& path,
202  CReadObjectHook* hook);
203 
204  /// Set global (for all input streams) write hook
205  void SetGlobalWriteHook(CWriteObjectHook* hook);
206  /// Set local (for a specific input stream) write hook
208  /// Reset global write hooks
209  void ResetGlobalWriteHook(void);
210  /// Reset local write hook
211  void ResetLocalWriteHook(CObjectOStream& out);
212  /// Set local context-specific write hook
213  void SetPathWriteHook(CObjectOStream* out, const string& path,
214  CWriteObjectHook* hook);
215 
216  /// Set local (for a specific input stream) skip hook
218  /// Reset local skip hook
219  void ResetLocalSkipHook(CObjectIStream& in);
220  /// Set local context-specific skip hook
221  void SetPathSkipHook(CObjectIStream* in, const string& path,
222  CSkipObjectHook* hook);
223 
224  /// Set global (for all input streams) copy hook
225  void SetGlobalCopyHook(CCopyObjectHook* hook);
226  /// Set local (for a specific input stream) copy hook
228  /// Reset global copy hooks
229  void ResetGlobalCopyHook(void);
230  /// Reset local copy hook
231  void ResetLocalCopyHook(CObjectStreamCopier& copier);
232  /// Set local context-specific copy hook
233  void SetPathCopyHook(CObjectStreamCopier* copier, const string& path,
234  CCopyObjectHook* hook);
235 
236  // default methods without checking hook
241 
242 
244  m_TagType = ttype;
245  return this;
246  }
248  return m_TagType;
249  }
250  bool IsTagImplicit(void) const {
251  return m_TagType == CAsnBinaryDefs::eImplicit;
252  }
253  virtual CTypeInfo* SetTag(CAsnBinaryDefs::TLongTag tag,
257  return m_Tag;
258  }
259  bool HasTag(void) const {
260  return m_Tag != eNoExplicitTag;
261  }
263  return m_TagClass;
264  }
266  return m_TagConstructed;
267  }
268  bool IsTagConstructed(void) const {
269  return m_TagConstructed == CAsnBinaryDefs::eConstructed;
270  }
271  void CodeVersion(size_t codever) {
272  m_CodeVer = codever;
273  }
274  size_t GetCodeVersion(void) const {
275  return m_CodeVer;
276  }
277  void DataSpec(EDataSpec spec);
278  EDataSpec GetDataSpec(void) const {
279  return m_DataSpec;
280  }
281  void CopyInfoItem(TTypeInfo other);
282 private:
283  // private constructors to avoid copying
286 
287  // type information
289  size_t m_Size;
290  string m_Name;
291  string m_ModuleName;
293  size_t m_CodeVer;
295 
296 protected:
297  void SetCreateFunction(TTypeCreate func);
298  void SetReadFunction(TTypeReadFunction func);
299  TTypeReadFunction GetReadFunction(void) const;
300  void SetWriteFunction(TTypeWriteFunction func);
301  void SetCopyFunction(TTypeCopyFunction func);
302  void SetSkipFunction(TTypeSkipFunction func);
303 
310  mutable atomic<ETriState> m_CSerialUserOp;
311 
312 private:
313  // type specific function pointers
314  TTypeCreate m_CreateFunction;
315 
320 
321  friend class CTypeInfoFunctions;
322 
323  void x_CreateInfoItemIfNeeded(void) const;
324 };
325 
326 
327 /* @} */
328 
329 
330 #include <serial/impl/typeinfo.inl>
331 
333 
334 #endif /* TYPEINFO__HPP */
info DataSpec(ncbi::EDataSpec::eXSD)
CConstObjectInfo –.
Definition: objectinfo.hpp:421
Copy hook for a standalone object.
Definition: objhook.hpp:254
CObjectIStream –.
Definition: objistr.hpp:93
CObjectInfo –.
Definition: objectinfo.hpp:597
CObjectOStream –.
Definition: objostr.hpp:83
CObjectStreamCopier –.
Definition: objcopy.hpp:71
CObjectTypeInfo –.
Definition: objectinfo.hpp:94
CObject –.
Definition: ncbiobj.hpp:180
Read hook for a standalone object.
Definition: objhook.hpp:59
Base class for user-defined serializable classes to allow for objects assignment and comparison.
Definition: serialbase.hpp:319
Skip hook for a standalone object.
Definition: objhook.hpp:205
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
Include a standard set of the NCBI C++ Toolkit most basic headers.
std::ofstream out("events_result.xml")
main entry point for tests
ENsQualifiedMode
Defines namespace qualification of XML tags.
Definition: serialdef.hpp:198
ESerialRecursionMode
How to assign and compare child sub-objects of serial objects.
Definition: serialdef.hpp:191
void * TObjectPtr
Definition: serialdef.hpp:55
const void * TConstObjectPtr
Definition: serialdef.hpp:59
void SetModuleName(CTypeInfo *info, const char *name)
Definition: serial.cpp:96
ETypeFamily
Type family.
Definition: serialdef.hpp:138
void SetInternalName(CTypeInfo *info, const char *owner_name, const char *member_name=0)
Definition: serial.cpp:106
EDataSpec
Definition: serialdef.hpp:204
@ eRecursive
Recursively.
Definition: serialdef.hpp:192
void(* TTypeWriteFunction)(CObjectOStream &out, const CTypeInfo *objectType, TConstObjectPtr objectPtr)
Definition: hookfunc.hpp:57
void(* TTypeCopyFunction)(CObjectStreamCopier &copier, const CTypeInfo *objectType)
Definition: hookfunc.hpp:60
void(* TTypeReadFunction)(CObjectIStream &in, const CTypeInfo *objectType, TObjectPtr objectPtr)
Definition: hookfunc.hpp:54
void(* TTypeSkipFunction)(CObjectIStream &in, const CTypeInfo *objectType)
Definition: hookfunc.hpp:62
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
void CopyData(CObjectStreamCopier &copier) const
CAsnBinaryDefs::ETagClass GetTagClass(void) const
Definition: typeinfo.hpp:262
size_t m_CodeVer
Definition: typeinfo.hpp:293
CAsnBinaryDefs::ETagConstructed GetTagConstructed(void) const
Definition: typeinfo.hpp:265
CHookData< CReadObjectHook, TTypeReadFunction > m_ReadHookData
Definition: typeinfo.hpp:316
void CodeVersion(size_t codever)
Definition: typeinfo.hpp:271
virtual bool IsDefault(TConstObjectPtr object) const =0
Check, whether the object contains default value.
CHookData< CCopyObjectHook, TTypeCopyFunction > m_CopyHookData
Definition: typeinfo.hpp:319
bool HasTag(void) const
Definition: typeinfo.hpp:259
EDataSpec GetDataSpec(void) const
Definition: typeinfo.hpp:278
CAsnBinaryDefs::ETagType GetTagType(void) const
Definition: typeinfo.hpp:247
void WriteData(CObjectOStream &out, TConstObjectPtr object) const
string m_Name
Definition: typeinfo.hpp:290
ETypeFamily GetTypeFamily(void) const
void DefaultSkipData(CObjectIStream &in) const
CAsnBinaryDefs::ETagClass m_TagClass
Definition: typeinfo.hpp:307
CTypeInfo(const CTypeInfo &)
void DefaultReadData(CObjectIStream &in, TObjectPtr object) const
ETypeFamily m_TypeFamily
Definition: typeinfo.hpp:288
void DefaultWriteData(CObjectOStream &out, TConstObjectPtr object) const
bool m_IsCObject
Definition: typeinfo.hpp:304
TTypeCreate m_CreateFunction
Definition: typeinfo.hpp:314
bool IsCObject(void) const
Check is this TypeInfo object is kind of CClassTypeInfoBase.
CHookData< CWriteObjectHook, TTypeWriteFunction > m_WriteHookData
Definition: typeinfo.hpp:317
CAsnBinaryDefs::ETagType m_TagType
Definition: typeinfo.hpp:308
void ReadData(CObjectIStream &in, TObjectPtr object) const
size_t GetCodeVersion(void) const
Definition: typeinfo.hpp:274
size_t m_Size
Definition: typeinfo.hpp:289
virtual void Assign(TObjectPtr dst, TConstObjectPtr src, ESerialRecursionMode how=eRecursive) const =0
Set object to copy of another one.
atomic< ETriState > m_CSerialUserOp
Definition: typeinfo.hpp:310
bool IsInternal(void) const
Check if this TypeInfo describes internal unnamed type.
CAsnBinaryDefs::TLongTag GetTag(void) const
Definition: typeinfo.hpp:256
CHookData< CSkipObjectHook, TTypeSkipFunction > m_SkipHookData
Definition: typeinfo.hpp:318
bool IsTagConstructed(void) const
Definition: typeinfo.hpp:268
bool m_IsInternal
Definition: typeinfo.hpp:305
string m_ModuleName
Definition: typeinfo.hpp:291
EDataSpec m_DataSpec
Definition: typeinfo.hpp:294
bool IsTagImplicit(void) const
Definition: typeinfo.hpp:250
CTypeInfo & operator=(const CTypeInfo &)
virtual bool Equals(TConstObjectPtr object1, TConstObjectPtr object2, ESerialRecursionMode how=eRecursive) const =0
Check if both objects contain the same values.
EMayContainType IsOrMayContainType(TTypeInfo type) const
bool MayContainType(TTypeInfo type) const
void DefaultCopyData(CObjectStreamCopier &copier) const
CNamespaceInfoItem * m_InfoItem
Definition: typeinfo.hpp:292
size_t GetSize(void) const
Get size of data object in memory (like sizeof in C)
virtual void SetDefault(TObjectPtr dst) const =0
Set object to default value.
CTypeInfo * SetTagType(CAsnBinaryDefs::ETagType ttype)
Definition: typeinfo.hpp:243
CAsnBinaryDefs::TLongTag m_Tag
Definition: typeinfo.hpp:306
void SkipData(CObjectIStream &in) const
CAsnBinaryDefs::ETagConstructed m_TagConstructed
Definition: typeinfo.hpp:309
TObjectPtr Create(CObjectMemoryPool *memoryPool=0) const
Create object of this type on heap (can be deleted by operator delete)
@ eMayContainType_recursion
Definition: typeinfo.hpp:185
@ eMayContainType_no
Definition: typeinfo.hpp:184
#define NCBI_XSERIAL_EXPORT
Definition: ncbi_export.h:1435
const struct ncbi::grid::netcache::search::fields::SIZE size
const char * tag
std::istream & in(std::istream &in_, double &x_)
void SetLocalCopyHook(const CObjectTypeInfo &obj_type_info, CObjectStreamCopier &copier, _Func _func)
void SetLocalSkipHook(const CObjectTypeInfo &obj_type_info, CObjectIStream &istr, _Func _func)
void SetLocalWriteHook(const CObjectTypeInfo &obj_type_info, CObjectOStream &ostr, _Func _func)
void SetLocalReadHook(const CObjectTypeInfo &obj_type_info, CObjectIStream &ostr, _Func _func)
static const string & GetModuleName(const char *moduleName)
Definition: serial.cpp:87
Definition: type.c:6
Modified on Fri Sep 20 14:57:10 2024 by modify_doxy.py rev. 669887