Lemma is an Electromagnetics API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LemmaObject.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* This file is part of Lemma, a geophysical modelling and inversion API */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. @file
  7. @author Trevor Irons
  8. @date 06/25/2009
  9. @version $Id: LemmaObject.cpp 266 2015-04-01 03:24:00Z tirons $
  10. **/
  11. #include "LemmaObject.h"
  12. namespace Lemma {
  13. std::ostream &operator<<(std::ostream &stream,
  14. const LemmaObject &ob) {
  15. stream << "Class name= " << ob.Name << "\n";
  16. return stream;
  17. }
  18. #ifdef HAVE_YAMLCPP
  19. YAML::Emitter& operator << (YAML::Emitter& out, const LemmaObject& ob) {
  20. out << YAML::BeginMap;
  21. out << YAML::Key <<"Class Name" << YAML::Value << ob.Name;
  22. return out;
  23. }
  24. #endif
  25. // ==================== LIFECYCLE ==============================
  26. // Constructor
  27. LemmaObject::LemmaObject(const std::string& name) :
  28. NumberOfReferences(0), Name(name) {
  29. }
  30. #ifdef HAVE_YAMLCPP
  31. LemmaObject::LemmaObject(const YAML::Node &node) :
  32. NumberOfReferences(0), Name(node.Tag()) {
  33. }
  34. #endif
  35. // Destructor
  36. LemmaObject::~LemmaObject() {
  37. if (this->NumberOfReferences != 0) {
  38. throw DeleteObjectWithReferences(this);
  39. }
  40. }
  41. // ==================== OPERATIONS ==============================
  42. void LemmaObject::AttachTo (LemmaObject* ptrIn) {
  43. this->NumberOfReferences++;
  44. this->RefPtrList.push_back(ptrIn);
  45. }
  46. void LemmaObject::DetachFrom (LemmaObject* ptrIn) {
  47. bool found(false);
  48. std::vector<LemmaObject*>::iterator iter = this->RefPtrList.begin();
  49. while (iter != this->RefPtrList.end() ) {
  50. if (*iter == ptrIn) {
  51. this->RefPtrList.erase(iter);
  52. this->NumberOfReferences--;
  53. found = true;
  54. break;
  55. }
  56. ++iter;
  57. }
  58. if (!found) {
  59. std::cout << *this;
  60. std::cerr << "Function call DetachFrom Failed \n"
  61. << "Caller name " << this->Name << " address=" << this <<
  62. " detaching from " << ptrIn->Name << " address= " << ptrIn<< "\n"
  63. << "This class was not listed as attached to this\n";
  64. exit(EXIT_FAILURE);
  65. }
  66. // If there are no remaining references, free up memory.
  67. if (NumberOfReferences == 0) {
  68. this->Release();
  69. }
  70. }
  71. // ==================== INQUIRY ==============================
  72. unsigned int LemmaObject::GetNumberOfReferences() {
  73. return this->NumberOfReferences;
  74. }
  75. std::vector<LemmaObject*> LemmaObject::GetReferences() {
  76. return RefPtrList;
  77. }
  78. std::string LemmaObject::GetName() {
  79. return Name;
  80. }
  81. // ==================== ACCESS ==============================
  82. // ==================== ACCESS ==============================
  83. // ==================== OPERATORS ==============================
  84. //////////////////////////////////////////////////////////////////////
  85. //////////////////////////////////////////////////////////////////////
  86. // Exception classes
  87. DeleteObjectWithReferences::DeleteObjectWithReferences() :
  88. runtime_error("DELETED OBJECT WITH REFERENCES") {
  89. }
  90. DeleteObjectWithReferences::
  91. DeleteObjectWithReferences(LemmaObject* ptr) :
  92. runtime_error("DELETED OBJECT WITH REFERENCES") {
  93. std::cerr << "DELETED OBJECT WITH REFERENCES THROWN BY INSTANCE OF "
  94. << ptr->GetName() << std::endl;
  95. std::cerr << ptr->RefPtrList.size() << " connection(s) remain\n";
  96. for (unsigned int i=0; i<ptr->RefPtrList.size(); ++i) {
  97. std::cerr << "\tConnection " << i << " is " << ptr->RefPtrList[i]->GetName();
  98. if (ptr->RefPtrList[i]->GetName().empty())
  99. std::cerr << "a deleted object! PLEASE REPORT BUG!\n";
  100. else
  101. std::cerr << "\n" << *ptr->RefPtrList[i] << std::endl;
  102. }
  103. }
  104. DeSerializeTypeMismatch::DeSerializeTypeMismatch(LemmaObject *ptr, const std::string& got) :
  105. runtime_error("DESERIALIZE TYPE MISMATCH") {
  106. std::cerr << "Expected " << ptr->GetName() << " got " << got << std::endl;
  107. }
  108. RequestToReturnNullPointer::
  109. RequestToReturnNullPointer(LemmaObject *ptr) :
  110. runtime_error("REQUEST TO RETURN NULL POINTER"){
  111. std::cout << "Thrown by instance of "
  112. << ptr->GetName() << std::endl;
  113. }
  114. MatFileCannotBeOpened::
  115. MatFileCannotBeOpened() :
  116. runtime_error("MATFILE CANNOT BE OPENED"){}
  117. AssignmentOutOfBounds::
  118. AssignmentOutOfBounds(LemmaObject *ptr) :
  119. runtime_error("ASSIGNMENT OUT OF BOUNDS"){
  120. std::cout << "Thrown by instance of "
  121. << ptr->GetName() << std::endl;
  122. }
  123. GenericFileIOError::
  124. GenericFileIOError(LemmaObject *ptr, const std::string &filename) : runtime_error("FILE I/O ERROR"){
  125. std::cout << std::endl;
  126. std::cout << "FILE I/O ERROR" << std::endl;
  127. std::cout << std::endl;
  128. std::cout << "Thrown by instance of "
  129. << ptr->GetName() << std::endl;
  130. std::cout << " while trying to access " << filename << std::endl;
  131. std::cout << std::endl;
  132. }
  133. } // end of namespace Lemma