36 #define CHAR_PTR(c) ((char*)c)
38 template <
class ObjType>
39 Handle<ObjType>::Handle() {
40 typeInfo.setup<ObjType>();
44 template <
class ObjType>
45 int16_t Handle<ObjType>::getTypeCode() {
46 return typeInfo.getTypeCode();
49 template <
class ObjType>
50 int32_t Handle<ObjType>::getExactTypeInfoValue()
const {
51 return typeInfo.getExactTypeInfoValue();
54 template <
class ObjType>
55 void Handle<ObjType>::setExactTypeInfoValue(int32_t toMe) {
59 template <
class ObjType>
60 Handle<ObjType>::~Handle() {
63 if (!isNullPtr() &&
getAllocator().isManaged(getTarget())) {
64 getTarget()->decRefCount(typeInfo);
68 template <
class ObjType>
69 unsigned Handle<ObjType>::getRefCount() {
72 if (!isNullPtr() &&
getAllocator().isManaged(getTarget())) {
73 return getTarget()->getRefCount();
80 template <
class ObjType>
91 std::cout <<
"This seems bad. I don't think that you should be emptying the containing "
92 "block for a nullptr.\n";
96 template <
class ObjType>
97 Handle<ObjType>::Handle(GenericHandle rhs) {
98 typeInfo = rhs.getMyBase();
103 template <
class ObjType>
104 Handle<ObjType>::Handle(
const std::nullptr_t rhs) {
105 typeInfo.setup<Nothing>();
109 template <
class ObjType>
110 Handle<ObjType> Handle<ObjType>::copyTargetToCurrentAllocationBlock() {
118 Handle<ObjType> returnVal;
121 #ifdef DEBUG_OBJECT_MODEL
128 if (space ==
nullptr) {
129 PDB_COUT <<
"ERROR: Not enough memory when invoking copyTargetToCurrentAllocationBlock() "
132 returnVal.offset = -1;
138 returnVal.getTarget()->setRefCount(1);
141 returnVal.typeInfo = typeInfo;
143 if (typeInfo.getTypeCode() == 0) {
144 PDB_COUT <<
"copyTargetToCurrentAllocationBlock: typeInfo = 0 before "
145 "setUpAndCopyFromConstituentObject"
148 typeInfo.setUpAndCopyFromConstituentObject(returnVal.getTarget()->getObject(),
149 getTarget()->getObject());
150 }
catch (NotEnoughSpace& n) {
151 PDB_COUT <<
"ERROR: Not enough memory when invoking copyTargetToCurrentAllocationBlock() "
154 returnVal.getTarget()->decRefCount(typeInfo);
155 returnVal.offset = -1;
162 #define GET_OLD_TARGET \
163 RefCountedObject<ObjType>* oldTarget = nullptr; \
165 oldTarget = getTarget(); \
166 PDBTemplateBase oldTypeInfo = typeInfo;
167 #define DEC_OLD_REF_COUNT \
168 if (oldTarget != nullptr) \
169 oldTarget->decRefCount(oldTypeInfo)
171 template <
class ObjType>
172 Handle<ObjType>& Handle<ObjType>::operator=(
const std::nullptr_t rhs) {
179 template <
class ObjType>
180 bool Handle<ObjType>::isNullPtr()
const {
181 return (offset == -1);
205 template <
class ObjType>
206 Handle<ObjType>::Handle(
const RefCountedObject<ObjType>* fromMe) {
209 typeInfo.setup<ObjType>();
211 if (fromMe ==
nullptr) {
219 #ifdef DEBUG_OBJECT_MODEL
222 typeInfo.getTypeCode());
227 if (space ==
nullptr) {
228 std::cout <<
"ERROR: Not enough memory when invoking Handle (const RefCountedObject "
229 "<ObjType>) in Handle.cc"
237 getTarget()->setRefCount(1);
241 typeInfo.setUpAndCopyFromConstituentObject(getTarget()->getObject(),
242 fromMe->getObject());
243 }
catch (NotEnoughSpace& n) {
244 PDB_COUT <<
"ERROR: Not enough memory when invoking Handle(const "
245 "RefCountedObject<ObjType>) in Handle.cc"
247 getTarget()->decRefCount(typeInfo);
259 getTarget()->incRefCount();
265 template <
class ObjType,
class ObjTypeTwo>
266 auto convert(ObjType*, ObjTypeTwo*) -> std::enable_if_t<
267 std::is_base_of<ObjType, ObjTypeTwo>::value ||
268 (std::is_base_of<ObjType, Object>::value && std::is_base_of<String, ObjTypeTwo>::value) ||
269 (std::is_base_of<ObjType, Object>::value && std::is_base_of<HandleBase, ObjTypeTwo>::value),
274 template <
class ObjType>
275 template <
class ObjTypeTwo>
276 Handle<ObjType>::Handle(
const RefCountedObject<ObjTypeTwo>* fromMe) {
278 ObjType* one =
nullptr;
279 ObjTypeTwo* two =
nullptr;
283 typeInfo.setup<ObjTypeTwo>();
285 if (fromMe ==
nullptr) {
295 #ifdef DEBUG_OBJECT_MODEL
297 typeInfo.getTypeCode());
302 if (space ==
nullptr) {
303 std::cout <<
"ERROR: Not enough memory when invoking Handle (const RefCountedObject "
304 "<ObjTypeTwo>) in Handle.cc"
312 getTarget()->setRefCount(1);
317 typeInfo.setUpAndCopyFromConstituentObject(getTarget()->getObject(),
318 fromMe->getObject());
319 }
catch (NotEnoughSpace& n) {
320 PDB_COUT <<
"ERROR: Not enough memory when invoking Handle(const "
321 "RefCountedObject<ObjTypeTwo>) in Handle.cc"
323 getTarget()->decRefCount(typeInfo);
335 getTarget()->incRefCount();
343 template <
class ObjType>
344 Handle<ObjType>::Handle(
const Handle<ObjType>& fromMe) {
347 if (fromMe.isNullPtr()) {
352 typeInfo = fromMe.typeInfo;
359 #ifdef DEBUG_OBJECT_MODEL
362 typeInfo.getSizeOfConstituentObject(fromMe.getTarget()->getObject()),
363 typeInfo.getTypeCode());
367 typeInfo.getSizeOfConstituentObject(fromMe.getTarget()->getObject()));
370 if (space ==
nullptr) {
371 std::cout <<
"ERROR: Not enough memory when invoking Handle(const Handle<ObjType>) in "
381 getTarget()->setRefCount(1);
386 typeInfo.setUpAndCopyFromConstituentObject(getTarget()->getObject(),
387 fromMe.getTarget()->getObject());
388 }
catch (NotEnoughSpace& n) {
389 PDB_COUT <<
"ERROR: Not enough memory when invoking Handle(const Handle<ObjType>) in "
392 getTarget()->decRefCount(typeInfo);
403 getTarget()->incRefCount();
407 template <
class ObjType>
408 template <
class ObjTypeTwo>
409 Handle<ObjType>::Handle(
const Handle<ObjTypeTwo>& fromMe) {
411 ObjType* one =
nullptr;
412 ObjTypeTwo* two =
nullptr;
416 if (fromMe.isNullPtr()) {
421 typeInfo = fromMe.typeInfo;
428 #ifdef DEBUG_OBJECT_MODEL
431 typeInfo.getSizeOfConstituentObject(fromMe.getTarget()->getObject()),
432 typeInfo.getTypeCode());
436 typeInfo.getSizeOfConstituentObject(fromMe.getTarget()->getObject()));
439 if (space ==
nullptr) {
440 std::cout <<
"ERROR: Not enough memory when invoking Handle(const Handle<ObjTypeTwo>) "
449 getTarget()->setRefCount(1);
454 typeInfo.setUpAndCopyFromConstituentObject(getTarget()->getObject(),
455 fromMe.getTarget()->getObject());
456 }
catch (NotEnoughSpace& n) {
457 PDB_COUT <<
"ERROR: Not enough memory when invoking Handle(const Handle<ObjTypeTwo>) "
460 getTarget()->decRefCount(typeInfo);
471 getTarget()->incRefCount();
483 template <
class ObjType>
484 Handle<ObjType>& Handle<ObjType>::operator=(
const RefCountedObject<ObjType>* fromMe) {
489 typeInfo.setup<ObjType>();
491 if (fromMe ==
nullptr) {
492 std::cout <<
"ERROR: fromMe is nullptr when invoking operator = (const RefCountedObject "
493 "<ObjType>*) in Handle.cc"
505 #ifdef DEBUG_OBJECT_MODEL
507 typeInfo.getTypeCode());
512 if (space ==
nullptr) {
513 std::cout <<
"ERROR: not enough memory when invoking operator = (const "
514 "RefCountedObject <ObjType>*) in Handle.cc"
524 getTarget()->setRefCount(1);
529 if (typeInfo.getTypeCode() == 0) {
530 PDB_COUT <<
"operator=: typeInfo = 0 before setUpAndCopyFromConstituentObject"
533 #ifdef DEBUG_DEEP_COPY
534 if (typeInfo.getTypeCode() > 126) {
535 PDB_COUT <<
"typeInfo=" << typeInfo.getTypeCode() << std::endl;
538 typeInfo.setUpAndCopyFromConstituentObject(getTarget()->getObject(),
539 fromMe->getObject());
540 }
catch (NotEnoughSpace& n) {
541 PDB_COUT <<
"ERROR: Not enough memory when invoking operator = (const "
542 "RefCountedObject<ObjType>*) in Handle.cc"
545 getTarget()->decRefCount(typeInfo);
554 #ifdef DEBUG_OBJECT_MODEL
556 std::cout <<
"#################################################" << std::endl;
557 std::cout <<
"Both LHS and RHS are not in current block" << std::endl;
558 std::cout <<
"RHS typeinfo =" << typeInfo.getTypeCode() << std::endl;
559 std::cout <<
"#################################################" << std::endl;
570 getTarget()->incRefCount();
579 template <
class ObjType>
580 template <
class ObjTypeTwo>
581 Handle<ObjType>& Handle<ObjType>::operator=(
const RefCountedObject<ObjTypeTwo>* fromMe) {
583 ObjType* one =
nullptr;
584 ObjTypeTwo* two =
nullptr;
591 typeInfo.setup<ObjTypeTwo>();
593 if (fromMe ==
nullptr) {
604 #ifdef DEBUG_OBJECT_MODEL
606 typeInfo.getTypeCode());
611 if (space ==
nullptr) {
612 std::cout <<
"ERROR: Not enough memory when invoking operator = (const "
613 "RefCountedObject<ObjTypeTwo>*) in Handle.cc"
623 getTarget()->setRefCount(1);
628 typeInfo.setUpAndCopyFromConstituentObject(getTarget()->getObject(),
629 fromMe->getObject());
630 }
catch (NotEnoughSpace& n) {
631 PDB_COUT <<
"ERROR: Not enough memory when invoking operator = (const "
632 "RefCountedObject<ObjTypeTwo>*) in Handle.cc"
634 getTarget()->decRefCount(typeInfo);
644 #ifdef DEBUG_OBJECT_MODEL
646 std::cout <<
"#################################################" << std::endl;
647 std::cout <<
"Both LHS and RHS are not in current block" << std::endl;
648 std::cout <<
"RHS typeinfo =" << typeInfo.getTypeCode() << std::endl;
649 std::cout <<
"#################################################" << std::endl;
659 getTarget()->incRefCount();
670 template <
class ObjType>
671 Handle<ObjType>& Handle<ObjType>::operator=(
const Handle<ObjType>& fromMe) {
677 if (fromMe ==
nullptr) {
683 typeInfo = fromMe.typeInfo;
690 RefCountedObject<ObjType>* refCountedObject = fromMe.getTarget();
691 ObjType*
object = refCountedObject->getObject();
692 #ifdef DEBUG_OBJECT_MODEL
695 typeInfo.getSizeOfConstituentObject(
697 typeInfo.getTypeCode());
701 typeInfo.getSizeOfConstituentObject(
705 if (space ==
nullptr) {
706 std::cout <<
"Not enough memory when doing a deep copy with TypeId="
707 << typeInfo.getTypeCode() << std::endl;
716 getTarget()->setRefCount(1);
721 #ifdef DEBUG_DEEP_COPY
722 int typeId = typeInfo.getTypeCode();
723 if ((typeId == 0) || (typeId > 126)) {
724 PDB_COUT <<
"Handle operator=: typeInfo=" << typeId
725 <<
" before setUpAndCopyFromConstituentObject" << std::endl;
728 typeInfo.setUpAndCopyFromConstituentObject(getTarget()->getObject(),
729 fromMe.getTarget()->getObject());
730 }
catch (NotEnoughSpace& n) {
731 PDB_COUT <<
"Not enough memory when doing a deep copy with TypeId="
732 << typeInfo.getTypeCode() << std::endl;
733 getTarget()->decRefCount(typeInfo);
747 #ifdef DEBUG_OBJECT_MODEL
749 std::cout <<
"#################################################" << std::endl;
750 std::cout <<
"Both LHS and RHS are not in current block" << std::endl;
751 std::cout <<
"RHS typeinfo =" << typeInfo.getTypeCode() << std::endl;
752 std::cout <<
"#################################################" << std::endl;
761 getTarget()->incRefCount();
768 template <
class ObjType>
769 template <
class ObjTypeTwo>
770 Handle<ObjType>& Handle<ObjType>::operator=(
const Handle<ObjTypeTwo>& fromMe) {
772 ObjType* one =
nullptr;
773 ObjTypeTwo* two =
nullptr;
780 if (fromMe ==
nullptr) {
786 typeInfo = fromMe.typeInfo;
787 if (typeInfo.getTypeCode() == 0) {
788 PDB_COUT <<
"Handle operator = ObjTypeTwo: typeInfo = 0 before getSizeConstituentObject"
796 #ifdef DEBUG_OBJECT_MODEL
799 typeInfo.getSizeOfConstituentObject(fromMe.getTarget()->getObject()),
800 typeInfo.getTypeCode());
804 typeInfo.getSizeOfConstituentObject(fromMe.getTarget()->getObject()));
807 if (space ==
nullptr) {
808 std::cout <<
"Not enough memory when doing a deep copy with TypeId="
809 << typeInfo.getTypeCode() << std::endl;
818 getTarget()->setRefCount(1);
823 typeInfo.setUpAndCopyFromConstituentObject(getTarget()->getObject(),
824 fromMe.getTarget()->getObject());
825 }
catch (NotEnoughSpace& n) {
826 PDB_COUT <<
"Not enough memory when doing a deep copy with TypeId="
827 << typeInfo.getTypeCode() << std::endl;
828 getTarget()->decRefCount(typeInfo);
842 #ifdef DEBUG_OBJECT_MODEL
844 std::cout <<
"#################################################" << std::endl;
845 std::cout <<
"Both LHS and RHS are not in current block" << std::endl;
846 std::cout <<
"RHS typeinfo =" << typeInfo.getTypeCode() << std::endl;
847 std::cout <<
"#################################################" << std::endl;
856 getTarget()->incRefCount();
864 template <
class ObjType>
865 ObjType* Handle<ObjType>::operator->()
const {
869 typeInfo.setVTablePtr(getTarget()->getObject());
871 return getTarget()->getObject();
874 template <
class ObjType>
875 void Handle<ObjType>::setOffset(int64_t toMe) {
879 template <
class ObjType>
880 int64_t Handle<ObjType>::getOffset()
const {
884 template <
class ObjType>
885 ObjType& Handle<ObjType>::operator*()
const {
888 typeInfo.setVTablePtr(getTarget()->getObject());
889 return *(getTarget()->getObject());
894 template <
class ObjType>
895 RefCountedObject<ObjType>* Handle<ObjType>::getTarget()
const {
896 return (RefCountedObject<ObjType>*)(
CHAR_PTR(
this) + offset);
902 template <
class ObjType>
903 Handle<ObjType>& Handle<ObjType>::shallowCopyToCurrentAllocationBlock(
904 const Handle<ObjType>& copyMe) {
910 if (copyMe ==
nullptr) {
916 typeInfo = copyMe.typeInfo;
924 getTarget()->incRefCount();
#define REF_COUNT_PREAMBLE_SIZE
auto convert(ObjType *, ObjTypeTwo *) -> std::enable_if_t< std::is_base_of< ObjType, ObjTypeTwo >::value||(std::is_base_of< ObjType, Object >::value &&std::is_base_of< String, ObjTypeTwo >::value)||(std::is_base_of< ObjType, Object >::value &&std::is_base_of< HandleBase, ObjTypeTwo >::value), int >
Allocator & getAllocator()
#define DEC_OLD_REF_COUNT
NotEnoughSpace myException
bool contains(void *whereIn)
void * getRAM(size_t howMuch)
void emptyOutBlock(void *here)
void emptyOutContainingBlock(void *forMe)