Lemma is an Electromagnetics API
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LemmaObject.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.h 199 2014-12-29 19:25:20Z tirons $
  10. **/
  11. #ifndef __LEMMAOBJECT_H
  12. #define __LEMMAOBJECT_H
  13. #include "helper.h"
  14. #include "lemma.h"
  15. #ifdef HAVE_YAMLCPP
  16. #include "yaml-cpp/yaml.h"
  17. #endif
  18. namespace Lemma {
  19. // ==========================================================================
  20. // Class: LemmaObject
  21. /** \brief Abstract class providing simple reference counting and memory
  22. * management.
  23. * \details Since objects can be members of other objects, and may be
  24. * of multiple objects it becomes useful to count the number of
  25. * Classes an object is a member of. Objects cannot be deleted that
  26. * contain references to other objects.
  27. */
  28. // ==========================================================================
  29. /** Base class for all of Lemma. Reference counted object provides simple
  30. * reference counting. This allows for much easier maintainance of shared
  31. * objects among classes.
  32. */
  33. class LemmaObject {
  34. /** Recursively prints information about this object
  35. */
  36. friend std::ostream &operator<<(std::ostream &stream, const LemmaObject &ob);
  37. #ifdef HAVE_YAMLCPP
  38. friend YAML::Emitter& operator << (YAML::Emitter& out, const LemmaObject &ob) ;
  39. #endif
  40. friend class DeleteObjectWithReferences;
  41. public:
  42. // Needed because some derived classes have Eigen vectors as members,
  43. // causing alignment issues when vectorisation is enabled.
  44. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  45. // ==================== OPERATORS ==============================
  46. // ==================== OPERATIONS ==============================
  47. /**
  48. * Deletes this objects self reference. If there are no remaining
  49. * references, Release is called freeing all memory. Otherwise Release
  50. * is called if/when the last reference is removed.
  51. */
  52. virtual void Delete()=0;
  53. /** Attaches this object to another object */
  54. void AttachTo(LemmaObject *);
  55. /** Detaches this object from another object */
  56. void DetachFrom(LemmaObject *);
  57. // ==================== ACCESS ==============================
  58. // ==================== INQUIRY ==============================
  59. /** Returns the number of objects attached to this. */
  60. unsigned int GetNumberOfReferences();
  61. /** Returns address(s) to class(es) of which this is a member
  62. */
  63. std::vector<LemmaObject*> GetReferences();
  64. /** Returns name of this model */
  65. std::string GetName();
  66. #ifdef HAVE_YAMLCPP
  67. /**
  68. * Uses YAML to serialize this object.
  69. * @return a YAML::Node
  70. * FOR NOT LemmaObject does not write out any YAML info,
  71. * in the future the reference count could be logged? But the utility
  72. * of that is minimal.
  73. * @note Not every Lemma class needs to be Serializable, for instance HankelTransform
  74. * classes will never need to be Serialized. There may be a need to differentiate these
  75. * two families in to a LemmaInternalClass without serializing and perhaps this class for
  76. * all external classes that might need to be serialized.
  77. */
  78. virtual YAML::Node Serialize() const{
  79. return YAML::Node();
  80. };
  81. #endif
  82. protected:
  83. // ==================== LIFECYCLE ==============================
  84. /** Protected default constructor. This is an abstract class and
  85. * cannot be instantiated.
  86. */
  87. LemmaObject (const std::string &name);
  88. #ifdef HAVE_YAMLCPP
  89. /** Protected DeDerializing constructor, use factory DeSerialize method*/
  90. LemmaObject (const YAML::Node& node);
  91. #endif
  92. // ==================== DATA MEMBERS ==============================
  93. /** Protected default destructor. This is an abstract class and
  94. * cannot be instantiated.
  95. */
  96. virtual ~LemmaObject ();
  97. /**
  98. * Releases all memory back to the heap. Just calls the protected
  99. * destructor at the derived class level.
  100. */
  101. virtual void Release()=0;
  102. // ==================== OPERATIONS ==============================
  103. // ==================== DATA MEMBERS ==============================
  104. /** Number of other classes claiming this as a member. */
  105. unsigned int NumberOfReferences;
  106. /** List of the addresses of the other classes referencing this as a
  107. * member
  108. */
  109. std::vector<LemmaObject *> RefPtrList;
  110. /** Stores an ASCII string representation of the class name */
  111. std::string Name;
  112. }; // ----- end of class LemmaObject -----
  113. /////////////////////////////////////////////////////////////////
  114. // BOILERPLATE MACROS
  115. #define DESERIALIZECHECK( node, Object ) { \
  116. if (node.Tag() != Object->GetName()) { \
  117. throw DeSerializeTypeMismatch(Object, node.Tag()) ; \
  118. } } \
  119. /////////////////////////////////////////////////////////////////
  120. // Error Classes
  121. /** Error called when ReferenceCounting breaks. If an object is
  122. * deleted that has refences still, this is thrown.
  123. */
  124. class DeleteObjectWithReferences : public std::runtime_error {
  125. public:
  126. /** Call this method internally when an object is deleted with
  127. * references.
  128. */
  129. DeleteObjectWithReferences(LemmaObject *ptr);
  130. /** @deprecated this error may be thrown, but the error message is much
  131. * less insightful than
  132. * DeleteObjectWithReferences(LemmaObject* ptr).
  133. */
  134. DeleteObjectWithReferences( );
  135. };
  136. /** Error called when DeSerializing breaks. If the node type is not the expected one
  137. * this error is thown.
  138. */
  139. class DeSerializeTypeMismatch : public std::runtime_error {
  140. public:
  141. DeSerializeTypeMismatch(LemmaObject *ptr, const std::string& got);
  142. };
  143. /** If an assignment is made that is out of bounts, throw this.
  144. */
  145. class AssignmentOutOfBounds : public std::runtime_error {
  146. public:
  147. /** Throw when an assignment is out of bounds.
  148. * @param[in] ptr is a pointer to the class throwing the exception.
  149. */
  150. AssignmentOutOfBounds(LemmaObject *ptr);
  151. };
  152. /** If a pointer to a class is requested, but it is NULL valued, throw this
  153. */
  154. class RequestToReturnNullPointer : public std::runtime_error {
  155. public:
  156. /** Thrown when the pointer is NULL
  157. * @param[in] ptr is a pointer to the class throwing the exception.
  158. */
  159. RequestToReturnNullPointer(LemmaObject *ptr);
  160. };
  161. /** If an error in opening a .mat file is encountered, throw this.
  162. */
  163. class MatFileCannotBeOpened : public std::runtime_error {
  164. /** thown when a mat file fails to be opened.
  165. */
  166. public: MatFileCannotBeOpened();
  167. };
  168. /** Generic file I/O error. */
  169. class GenericFileIOError : public std::runtime_error {
  170. public: GenericFileIOError(LemmaObject *ptr, const std::string &filename);
  171. };
  172. }
  173. #endif // __LEMMAOBJECT_H