A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PDBTemplateBase.cc
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 
19 #include "Object.h"
20 #include "VTableMap.h"
21 #include "PDBString.h"
22 #include "Handle.h"
23 #include "BuiltInObjectTypeIDs.h"
24 
25 #ifndef TEMPLATE_BASE_CC
26 #define TEMPLATE_BASE_CC
27 
28 namespace pdb {
29 
31  info = 0;
32 }
33 
34 inline int16_t PDBTemplateBase::getTypeCode() const {
35  if (info > 0)
36  return (int16_t)info;
37  else
38  return -1;
39 }
40 
41 inline int32_t PDBTemplateBase::getExactTypeInfoValue() const {
42  return info;
43 }
44 
45 
46 inline PDBTemplateBase& PDBTemplateBase::operator=(const PDBTemplateBase& toMe) {
47  info = toMe.info;
48  return *this;
49 }
50 
51 // set up the type
52 inline void PDBTemplateBase::set(int32_t toMe) {
53  info = toMe;
54 }
55 
56 // set up the type
57 template <class ObjType>
59 
60  // if we descend from Object, then get the type code
61  if (std::is_base_of<Object, ObjType>::value) {
62  info = (int16_t)getTypeID<ObjType>();
63  // std :: cout << "I'm a pdb Object with info="<< info << std :: endl;
64  } else if (std::is_base_of<String, ObjType>::value) {
65  info = String_TYPEID;
66  // std :: cout << "I am a String object with info=" << info << std :: endl;
67  } else if (std::is_base_of<HandleBase, ObjType>::value) {
68  info = Handle_TYPEID;
69  // std :: cout << "I am a Handle object with info=" << info << std :: endl;
70  } else {
71  // we could not recognize this type, so just record the size
72  info = -((int32_t)sizeof(ObjType));
73  // std :: cout << "I am a C++ object with info=" << info << std :: endl;
74  }
75 }
76 
77 // this deletes an object of type ObjType
78 inline void PDBTemplateBase::deleteConstituentObject(void* deleteMe) const {
79 
80  // if we are a string
81  if (info == String_TYPEID) {
82  ((String*)deleteMe)->~String();
83 
84  // if we are a Handle
85  } else if (info == Handle_TYPEID) {
86  ((Handle<Nothing>*)deleteMe)->~Handle();
87 
88  // else if we are not derived from Object, do nothing
89  } else if (info > 0) {
90  // we are going to install the vTable pointer for an object of type ObjType into temp
91  void* temp = nullptr;
92  // std :: cout << "to getVTablePtr for deleteConsitituentObject" << info << std :: endl;
93  ((Object*)&temp)->setVTablePtr(VTableMap::getVTablePtr((int16_t)info));
94 
95  // now call the deleter for that object type
96  if (temp != nullptr) {
97  ((Object*)&temp)->deleteObject(deleteMe);
98 
99  // in the worst case, we could not fix the vTable pointer, so try to use the object's
100  // vTable pointer
101  } else {
102  ((Object*)deleteMe)->deleteObject(deleteMe);
103  }
104  }
105 }
106 
107 inline void PDBTemplateBase::setUpAndCopyFromConstituentObject(void* target, void* source) const {
108 
109  // if we are a string
110  if (info == String_TYPEID) {
111  new (target) String();
112  *((String*)target) = *((String*)source);
113 
114  // if we are a Handle
115  } else if (info == Handle_TYPEID) {
116  new (target) Handle<Nothing>();
117  *((Handle<Nothing>*)target) = *((Handle<Nothing>*)source);
118 
119  // if we are derived from Object, use the virtual function
120  } else if (info > 0) {
121 
122  // we are going to install the vTable pointer for an object of type ObjType into temp
123  void* temp = nullptr;
124  // std :: cout << "to getVTablePtr for setUpAndCopyFromConsitituentObject" << info << std ::
125  // endl;
126  ((Object*)&temp)->setVTablePtr(VTableMap::getVTablePtr((int16_t)info));
127 
128  // now call the construct-and-copy operation for that object type
129  if (temp != nullptr) {
130  ((Object*)&temp)->setUpAndCopyFrom(target, source);
131 
132  // in the worst case, we could not fix the vTable pointer, so try to use the object's
133  // vTable pointer
134  } else {
135  ((Object*)source)->setUpAndCopyFrom(target, source);
136  }
137 
138  } else {
139 
140  // just do a memmove
141  memmove(target, source, -info);
142  }
143 }
144 
145 inline size_t PDBTemplateBase::getSizeOfConstituentObject(void* ofMe) const {
146 
147  // if we are a string
148  if (info == String_TYPEID) {
149  return sizeof(String);
150 
151  // else if we are a handle
152  } else if (info == Handle_TYPEID) {
153  return sizeof(Handle<Nothing>);
154 
155  // if we are derived from Object, use the virtual function
156  } else if (info > 0) {
157  // we are going to install the vTable pointer for an object of type ObjType into temp
158  void* temp = nullptr;
159  ((Object*)&temp)->setVTablePtr(VTableMap::getVTablePtr((int16_t)info));
160 
161  // now get the size
162  if (temp != nullptr) {
163  return ((Object*)&temp)->getSize(ofMe);
164 
165  // in the worst case, we could not fix the vTable pointer, so try to use the object's
166  // vTable pointer
167  } else {
168  return ((Object*)ofMe)->getSize(ofMe);
169  }
170  } else {
171  return -info;
172  }
173 }
174 
175 inline void PDBTemplateBase::setVTablePtr(void* forMe) const {
176 
177  // if we are derived from Object, then we set the v table pointer; otherwise, no need
178  if (info > Handle_TYPEID)
179  ((Object*)forMe)->setVTablePtr(VTableMap::getVTablePtr((int16_t)info));
180 }
181 
182 inline bool PDBTemplateBase::descendsFromObject() const {
183  return info > 0;
184 }
185 }
186 
187 #include "PDBString.cc"
188 
189 #endif
size_t getSizeOfConstituentObject(void *forMe) const
void deleteConstituentObject(void *deleteMe) const
void set(int32_t toMe)
static void * getVTablePtr(int16_t objectTypeID)
Definition: VTableMap.cc:224
int32_t getExactTypeInfoValue() const
void setVTablePtr(void *forMe) const
int16_t getTypeCode() const
void setUpAndCopyFromConstituentObject(void *target, void *source) const
PDBTemplateBase & operator=(const PDBTemplateBase &toMe)
bool descendsFromObject() const