Main Lemma Repository
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LemmaObject.h 6.0KB

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