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.6KB

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