Main Lemma Repository
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /**
  19. * \ingroup LemmaCore
  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. /**
  32. * Streams class information as YAML::Node
  33. */
  34. friend YAML::Emitter& operator << (YAML::Emitter& out, const LemmaObject &ob) ;
  35. friend class LemmaObjectDeleter;
  36. public:
  37. // ==================== LIFECYCLE ==============================
  38. // Needed because many derived classes have Eigen vectors as members,
  39. // causing alignment issues when vectorisation is enabled.
  40. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  41. // ==================== OPERATORS ==============================
  42. // ==================== OPERATIONS ==============================
  43. // ==================== ACCESS ==============================
  44. // ==================== INQUIRY ==============================
  45. /** Returns the name of the underlying class; Run-time type information (RTTI). This approach
  46. Was chosen over typeid due to name mangling among various compilers, and the need for consistency
  47. in Serialized objects.
  48. */
  49. virtual inline std::string GetName() const {
  50. return this->CName;
  51. }
  52. /**
  53. * Uses YAML to serialize this object.
  54. * @return a YAML::Node
  55. * FOR NOT LemmaObject does not write out any YAML info,
  56. * in the future the reference count could be logged? But the utility
  57. * of that is minimal.
  58. * @note Not every Lemma class needs to be Serializable, for instance HankelTransform
  59. * classes will never need to be Serialized. There may be a need to differentiate these
  60. * two families in to a LemmaInternalClass without serializing and perhaps this class for
  61. * all external classes that might need to be serialized.
  62. */
  63. virtual YAML::Node Serialize() const {
  64. YAML::Node node = YAML::Node();
  65. node.SetTag( GetName() );
  66. std::time_t now = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() );
  67. node["Serialized"] = std::ctime(&now);
  68. return node;
  69. };
  70. protected:
  71. // ==================== LIFECYCLE ==============================
  72. /** Protected default constructor. This is an abstract class and
  73. * cannot be instantiated.
  74. */
  75. LemmaObject ( );
  76. /** Protected DeDerializing constructor, use factory DeSerialize method*/
  77. LemmaObject (const YAML::Node& node);
  78. /** Protected default destructor. This is an abstract class and
  79. * cannot be instantiated. Virtual is necessary so that if base class destructor is
  80. * called, we get the right behaviour.
  81. */
  82. virtual ~LemmaObject();
  83. private:
  84. // ==================== DATA MEMBERS ==============================
  85. /** ASCII string representation of the class name */
  86. static constexpr auto CName = "LemmaObject";
  87. }; // ----- end of class LemmaObject -----
  88. class LemmaObjectDeleter
  89. {
  90. public:
  91. void operator()(LemmaObject* p) { delete p; }
  92. };
  93. /////////////////////////////////////////////////////////////////
  94. // Error Classes
  95. /** Error called when DeSerializing breaks. If the node type is not the expected one
  96. * this error is thown.
  97. */
  98. class DeSerializeTypeMismatch : public std::runtime_error {
  99. public:
  100. DeSerializeTypeMismatch(const std::string& expected, const std::string& got);
  101. };
  102. /** If an assignment is made that is out of bounts, throw this.
  103. */
  104. class AssignmentOutOfBounds : public std::runtime_error {
  105. public:
  106. /** Throw when an assignment is out of bounds.
  107. * @param[in] ptr is a pointer to the class throwing the exception.
  108. */
  109. AssignmentOutOfBounds(LemmaObject *ptr);
  110. };
  111. /** If a pointer to a class is requested, but it is NULL valued, throw this
  112. */
  113. class RequestToReturnNullPointer : public std::runtime_error {
  114. public:
  115. /** Thrown when the pointer is NULL
  116. * @param[in] ptr is a pointer to the class throwing the exception.
  117. */
  118. RequestToReturnNullPointer(LemmaObject *ptr);
  119. };
  120. /** If an error in opening a .mat file is encountered, throw this.
  121. */
  122. class MatFileCannotBeOpened : public std::runtime_error {
  123. /** thown when a mat file fails to be opened.
  124. */
  125. public: MatFileCannotBeOpened();
  126. };
  127. /** Generic file I/O error. */
  128. class GenericFileIOError : public std::runtime_error {
  129. public: GenericFileIOError(LemmaObject *ptr, const std::string &filename);
  130. };
  131. }
  132. #endif // __LEMMAOBJECT_H