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

Go to the SVN repository for this file.

1 /* $Id: choiceptr.cpp 59586 2013-09-05 15:30:00Z 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 
33 #include <ncbi_pch.hpp>
36 #include <serial/impl/typeref.hpp>
38 #include <serial/objistr.hpp>
39 #include <serial/objostr.hpp>
40 #include <serial/objcopy.hpp>
41 #include <serial/impl/typemap.hpp>
42 #include <serial/impl/ptrinfo.hpp>
44 #include <serial/error_codes.hpp>
45 
46 
47 #define NCBI_USE_ERRCODE_X Serial_TypeInfo
48 
50 
51 
53  : CParent(pointerType->GetSize(),
54  "",
55  TConstObjectPtr(0), &CVoidTypeFunctions::Create, typeid(bool),
56  &GetPtrIndex, &SetPtrIndex, &ResetPtrIndex)
57 {
58  SetPointerType(pointerType);
59 }
60 
62 
64 {
65  return s_ChoicePointerTypeInfo_map->GetTypeInfo(base, &CreateTypeInfo);
66 }
67 
69 {
70  return new CChoicePointerTypeInfo(base);
71 }
72 
74 {
76 
77  if ( base->GetTypeFamily() != eTypeFamilyPointer )
78  NCBI_THROW(CSerialException,eInvalidData,
79  "invalid argument: must be CPointerTypeInfo");
80  const CPointerTypeInfo* ptrType =
82  m_PointerTypeInfo = ptrType;
83 
84  if ( ptrType->GetPointedType()->GetTypeFamily() != eTypeFamilyClass )
85  NCBI_THROW(CSerialException,eInvalidData,
86  "invalid argument: data must be CClassTypeInfo");
87  const CClassTypeInfo* classType =
89  /* Do we really need it to be CObject???
90  if ( !classType->IsCObject() )
91  NCBI_THROW(CSerialException,eInvalidData,
92  "invalid argument:: choice ptr type must be CObject");
93  */
94  const CClassTypeInfo::TSubClasses* subclasses =
95  classType->SubClasses();
96  if ( !subclasses )
97  return;
98 
99  TTypeInfo nullTypeInfo = CNullTypeInfo::GetTypeInfo();
100 
101  for ( CClassTypeInfo::TSubClasses::const_iterator i = subclasses->begin();
102  i != subclasses->end(); ++i ) {
103  TTypeInfo variantType = i->second.Get();
104  if ( !variantType ) {
105  // null
106  variantType = nullTypeInfo;
107  }
108  AddVariant(i->first, 0, variantType)->SetSubClass();
109  TMemberIndex index = GetVariants().LastIndex();
110  if ( variantType == nullTypeInfo ) {
112  m_NullPointerIndex = index;
113  else {
114  ERR_POST_X(1, "double null");
115  }
116  }
117  else {
118  const type_info* id = &CTypeConverter<CClassTypeInfo>::SafeCast(variantType)->GetId();
119  if ( !m_VariantsByType.insert(TVariantsByType::value_type(id, index)).second ) {
120  NCBI_THROW(CSerialException,eInvalidData,
121  "conflict subclasses: "+variantType->GetName());
122  }
123  }
124  }
125 }
126 
129  TConstObjectPtr choicePtr)
130 {
131  const CChoicePointerTypeInfo* choicePtrType =
133 
134  const CPointerTypeInfo* ptrType = choicePtrType->m_PointerTypeInfo;
135  TConstObjectPtr classPtr = ptrType->GetObjectPointer(choicePtr);
136  if ( !classPtr )
137  return choicePtrType->m_NullPointerIndex;
138  const CClassTypeInfo* classType =
140  const TVariantsByType& variants = choicePtrType->m_VariantsByType;
142  variants.find(classType->GetCPlusPlusTypeInfo(classPtr));
143  if ( v == variants.end() )
144  NCBI_THROW(CSerialException,eInvalidData,
145  "incompatible CChoicePointerTypeInfo type");
146  return v->second;
147 }
148 
150  TObjectPtr choicePtr)
151 {
152  const CChoicePointerTypeInfo* choicePtrType =
154 
155  const CPointerTypeInfo* ptrType = choicePtrType->m_PointerTypeInfo;
156  ptrType->SetObjectPointer(choicePtr, 0);
157 }
158 
160  TObjectPtr choicePtr,
161  TMemberIndex index,
162  CObjectMemoryPool* memPool)
163 {
164  const CChoicePointerTypeInfo* choicePtrType =
166 
167  const CPointerTypeInfo* ptrType = choicePtrType->m_PointerTypeInfo;
168  _ASSERT(!ptrType->GetObjectPointer(choicePtr));
169  const CVariantInfo* variantInfo = choicePtrType->GetVariantInfo(index);
170  ptrType->SetObjectPointer(choicePtr,
171  variantInfo->GetTypeInfo()->Create(memPool));
172 }
173 
175 {
176 public:
177  static TObjectPtr Create(TTypeInfo /*typeInfo*/,
178  CObjectMemoryPool* /*memoryPool*/)
179  {
180  return 0;
181  }
182  static void Read(CObjectIStream& in, TTypeInfo ,
183  TObjectPtr objectPtr)
184  {
185  if ( objectPtr != 0 ) {
186  in.ThrowError(in.fInvalidData,
187  "non-null value when reading NULL member");
188  }
189  in.ReadNull();
190  }
192  TConstObjectPtr objectPtr)
193  {
194  if ( objectPtr != 0 ) {
195  out.ThrowError(out.fInvalidData,
196  "non-null value when writing NULL member");
197  }
198  out.WriteNull();
199  }
200  static void Copy(CObjectStreamCopier& copier, TTypeInfo )
201  {
202  copier.In().ReadNull();
203  copier.Out().WriteNull();
204  }
205  static void Skip(CObjectIStream& in, TTypeInfo )
206  {
207  in.SkipNull();
208  }
209 };
210 
212 {
219 }
220 
222 {
223  TTypeInfo typeInfo = new CNullTypeInfo();
224  return typeInfo;
225 }
226 
227 
static CSafeStatic< CTypeInfoMap > s_ChoicePointerTypeInfo_map
Definition: choiceptr.cpp:61
static void Copy(CObjectStreamCopier &copier, TTypeInfo)
Definition: choiceptr.cpp:200
static void Skip(CObjectIStream &in, TTypeInfo)
Definition: choiceptr.cpp:205
static void Write(CObjectOStream &out, TTypeInfo, TConstObjectPtr objectPtr)
Definition: choiceptr.cpp:191
static void Read(CObjectIStream &in, TTypeInfo, TObjectPtr objectPtr)
Definition: choiceptr.cpp:182
static TObjectPtr Create(TTypeInfo, CObjectMemoryPool *)
Definition: choiceptr.cpp:177
CObjectIStream –.
Definition: objistr.hpp:93
CObjectOStream –.
Definition: objostr.hpp:83
CObjectStreamCopier –.
Definition: objcopy.hpp:71
CSafeStatic<>::
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
const_iterator end() const
Definition: map.hpp:152
iterator_bool insert(const value_type &val)
Definition: map.hpp:165
const_iterator find(const key_type &key) const
Definition: map.hpp:153
std::ofstream out("events_result.xml")
main entry point for tests
#define bool
Definition: bool.h:34
#define ERR_POST_X(err_subcode, message)
Error posting with default error code and given error subcode.
Definition: ncbidiag.hpp:550
#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
CVariantInfo * SetSubClass(void)
Definition: variant.cpp:206
TTypeInfo GetTypeInfo(void) const
TMemberIndex LastIndex(void) const
Definition: memberlist.hpp:82
const TMemberIndex kEmptyChoice
Special value for marking empty choice.
Definition: serialdef.hpp:239
void * TObjectPtr
Definition: serialdef.hpp:55
size_t TMemberIndex
Type used for indexing class members and choice variants.
Definition: serialdef.hpp:230
const void * TConstObjectPtr
Definition: serialdef.hpp:59
static const TObjectType * SafeCast(TTypeInfo type)
Definition: serialutil.hpp:76
@ eTypeFamilyClass
Definition: serialdef.hpp:140
@ eTypeFamilyPointer
Definition: serialdef.hpp:143
virtual void ReadNull(void)=0
CObjectIStream & In(void) const
CObjectOStream & Out(void) const
virtual void WriteNull(void)=0
#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 SetWriteFunction(TTypeWriteFunction func)
Definition: typeinfo.cpp:487
const string & GetName(void) const
Get name of this type.
Definition: typeinfo.cpp:249
void SetPointerType(TTypeInfo pointerType)
Definition: choiceptr.cpp:73
void SetObjectPointer(TObjectPtr object, TObjectPtr pointer) const
virtual CTypeInfo * SetTag(CAsnBinaryDefs::TLongTag tag, CAsnBinaryDefs::ETagClass tagclass=CAsnBinaryDefs::eUniversal, CAsnBinaryDefs::ETagType tagtype=CAsnBinaryDefs::eAutomatic)
Definition: typeinfo.cpp:172
ETypeFamily GetTypeFamily(void) const
TTypeInfo GetPointedType(void) const
TMemberIndex m_NullPointerIndex
Definition: choiceptr.hpp:85
const CPointerTypeInfo * m_PointerTypeInfo
Definition: choiceptr.hpp:83
const CItemsInfo & GetVariants(void) const
const CVariantInfo * GetVariantInfo(TMemberIndex index) const
static CTypeInfo * CreateTypeInfo(TTypeInfo base)
Definition: choiceptr.cpp:68
CChoicePointerTypeInfo(TTypeInfo pointerType)
Definition: choiceptr.cpp:52
const TSubClasses * SubClasses(void) const
static void SetPtrIndex(const CChoiceTypeInfo *choiceType, TObjectPtr choicePtr, TMemberIndex index, CObjectMemoryPool *memPool)
Definition: choiceptr.cpp:159
TVariantsByType m_VariantsByType
Definition: choiceptr.hpp:84
list< pair< CMemberId, CTypeRef > > TSubClasses
Definition: classinfo.hpp:86
void SetSkipFunction(TTypeSkipFunction func)
Definition: typeinfo.cpp:497
static TTypeInfo GetTypeInfo(void)
Definition: choiceptr.cpp:221
const type_info * GetCPlusPlusTypeInfo(TConstObjectPtr object) const
void SetCreateFunction(TTypeCreate func)
Definition: typeinfo.cpp:355
TConstObjectPtr GetObjectPointer(TConstObjectPtr object) const
void SetCopyFunction(TTypeCopyFunction func)
Definition: typeinfo.cpp:492
static TMemberIndex GetPtrIndex(const CChoiceTypeInfo *choiceType, TConstObjectPtr choicePtr)
Definition: choiceptr.cpp:128
static void ResetPtrIndex(const CChoiceTypeInfo *choiceType, TObjectPtr choicePtr)
Definition: choiceptr.cpp:149
static TTypeInfo GetTypeInfo(TTypeInfo base)
Definition: choiceptr.cpp:63
CVariantInfo * AddVariant(const char *variantId, const void *variantPtr, const CTypeRef &variantType)
Definition: choice.cpp:152
CNullTypeInfo(void)
Definition: choiceptr.cpp:211
TObjectPtr Create(CObjectMemoryPool *memoryPool=0) const
Create object of this type on heap (can be deleted by operator delete)
void SetReadFunction(TTypeReadFunction func)
Definition: typeinfo.cpp:477
Definition of all error codes used in serial libraries (xser.lib, xcser.lib).
int i
Static variables safety - create on demand, destroy on application termination.
std::istream & in(std::istream &in_, double &x_)
#define _ASSERT
Modified on Mon Jun 17 05:10:18 2024 by modify_doxy.py rev. 669887