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.h 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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-13-2016
  9. **/
  10. #ifndef __LEMMAOBJECT_H
  11. #define __LEMMAOBJECT_H
  12. #include "helper.h"
  13. #include "lemma.h"
  14. #ifdef HAVE_YAMLCPP
  15. #include "yaml-cpp/yaml.h"
  16. #endif
  17. #include <chrono>
  18. #include <memory>
  19. namespace Lemma {
  20. /** \brief Abstract class providing common interface for Lemma Objects.
  21. * \details Lemma objects can be members of other Lemma, and may be members
  22. * of multiple objects. Since updates should be atomic, and objects
  23. * can be large, it becomes useful to count the number of
  24. * Classes an object is a member of.
  25. * Before C++-11, this was done internally in Lemma, with the inclusion of
  26. * more sophisticated smart pointers, this logic has been offloaded to the
  27. * standard. All Lemma objects should be created as C++-11 Smart pointers, using
  28. * the supplied New method. Calls to Delete are no longer necessary or available.
  29. */
  30. class LemmaObject {
  31. /** Recursively prints information about this object
  32. */
  33. friend std::ostream &operator<<(std::ostream &stream, const LemmaObject &ob);
  34. #ifdef HAVE_YAMLCPP
  35. friend YAML::Emitter& operator << (YAML::Emitter& out, const LemmaObject &ob) ;
  36. #endif
  37. friend class LemmaObjectDeleter;
  38. public:
  39. // ==================== LIFECYCLE ==============================
  40. // Needed because many derived classes have Eigen vectors as members,
  41. // causing alignment issues when vectorisation is enabled.
  42. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  43. // ==================== OPERATORS ==============================
  44. // ==================== OPERATIONS ==============================
  45. // ==================== ACCESS ==============================
  46. // ==================== INQUIRY ==============================
  47. /** Returns the name of the class, similiar to Python's type */
  48. std::string GetName() const;
  49. #ifdef HAVE_YAMLCPP
  50. /**
  51. * Uses YAML to serialize this object.
  52. * @return a YAML::Node
  53. * FOR NOT LemmaObject does not write out any YAML info,
  54. * in the future the reference count could be logged? But the utility
  55. * of that is minimal.
  56. * @note Not every Lemma class needs to be Serializable, for instance HankelTransform
  57. * classes will never need to be Serialized. There may be a need to differentiate these
  58. * two families in to a LemmaInternalClass without serializing and perhaps this class for
  59. * all external classes that might need to be serialized.
  60. */
  61. virtual YAML::Node Serialize() const {
  62. YAML::Node node = YAML::Node();
  63. std::time_t now = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() );
  64. node["Serialized"] = std::ctime(&now);
  65. return node;
  66. };
  67. #endif
  68. protected:
  69. // ==================== LIFECYCLE ==============================
  70. /** Protected default constructor. This is an abstract class and
  71. * cannot be instantiated.
  72. */
  73. LemmaObject (const std::string &name);
  74. #ifdef HAVE_YAMLCPP
  75. /** Protected DeDerializing constructor, use factory DeSerialize method*/
  76. LemmaObject (const YAML::Node& node);
  77. #endif
  78. /** Protected default destructor. This is an abstract class and
  79. * cannot be instantiated.
  80. */
  81. virtual ~LemmaObject();
  82. private:
  83. // ==================== DATA MEMBERS ==============================
  84. /** Stores an ASCII string representation of the class name */
  85. std::string Name;
  86. }; // ----- end of class LemmaObject -----
  87. class LemmaObjectDeleter
  88. {
  89. public:
  90. void operator()(LemmaObject* p) { delete p; }
  91. };
  92. /////////////////////////////////////////////////////////////////
  93. // BOILERPLATE MACROS
  94. #define DESERIALIZECHECK( node, Object ) { \
  95. if (node.Tag() != Object->GetName()) { \
  96. throw DeSerializeTypeMismatch(Object, node.Tag()) ; \
  97. } } \
  98. /////////////////////////////////////////////////////////////////
  99. // Error Classes
  100. /** Error called when DeSerializing breaks. If the node type is not the expected one
  101. * this error is thown.
  102. */
  103. class DeSerializeTypeMismatch : public std::runtime_error {
  104. public:
  105. DeSerializeTypeMismatch(LemmaObject *ptr, const std::string& got);
  106. };
  107. /** If an assignment is made that is out of bounts, throw this.
  108. */
  109. class AssignmentOutOfBounds : public std::runtime_error {
  110. public:
  111. /** Throw when an assignment is out of bounds.
  112. * @param[in] ptr is a pointer to the class throwing the exception.
  113. */
  114. AssignmentOutOfBounds(LemmaObject *ptr);
  115. };
  116. /** If a pointer to a class is requested, but it is NULL valued, throw this
  117. */
  118. class RequestToReturnNullPointer : public std::runtime_error {
  119. public:
  120. /** Thrown when the pointer is NULL
  121. * @param[in] ptr is a pointer to the class throwing the exception.
  122. */
  123. RequestToReturnNullPointer(LemmaObject *ptr);
  124. };
  125. /** If an error in opening a .mat file is encountered, throw this.
  126. */
  127. class MatFileCannotBeOpened : public std::runtime_error {
  128. /** thown when a mat file fails to be opened.
  129. */
  130. public: MatFileCannotBeOpened();
  131. };
  132. /** Generic file I/O error. */
  133. class GenericFileIOError : public std::runtime_error {
  134. public: GenericFileIOError(LemmaObject *ptr, const std::string &filename);
  135. };
  136. }
  137. #endif // __LEMMAOBJECT_H