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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include <iostream>
  18. #include <iomanip>
  19. #include <codecvt>
  20. namespace Lemma {
  21. /**
  22. * \ingroup LemmaCore
  23. * \brief Abstract class providing common interface for Lemma Objects.
  24. * \details Lemma objects can be members of other Lemma, and may be members
  25. * of multiple objects. Since updates should be atomic, and objects
  26. * can be large, it becomes useful to count the number of
  27. * Classes an object is a member of.
  28. * Before C++-11, this was done internally in Lemma, with the inclusion of
  29. * more sophisticated smart pointers, this logic has been offloaded to the
  30. * standard. All Lemma objects should be created as C++-11 Smart pointers, using
  31. * the supplied New method. Calls to Delete are no longer necessary or available.
  32. */
  33. class LemmaObject {
  34. /**
  35. * Streams class information as YAML::Node
  36. */
  37. //friend YAML::Emitter& operator << (YAML::Emitter& out, const LemmaObject &ob) ;
  38. protected:
  39. struct ctor_key{};
  40. public:
  41. // Needed because many derived classes have Eigen vectors as members,
  42. // causing alignment issues when vectorisation is enabled.
  43. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  44. // ==================== LIFECYCLE ==============================
  45. /**
  46. * Uses YAML to serialize this object.
  47. * @return a YAML::Node
  48. * FOR NOT LemmaObject does not write out any YAML info,
  49. * in the future the reference count could be logged? But the utility
  50. * of that is minimal.
  51. * @note Not every Lemma class needs to be Serializable, for instance HankelTransform
  52. * classes will never need to be Serialized. There may be a need to differentiate these
  53. * two families in to a LemmaInternalClass without serializing and perhaps this class for
  54. * all external classes that might need to be serialized.
  55. */
  56. virtual YAML::Node Serialize() const {
  57. std::cout.precision( 20 );
  58. YAML::Node node = YAML::Node();
  59. //node.SetStyle(YAML::EmitterStyle::Flow);
  60. node.SetTag( GetName() );
  61. // ctime throws warning with MSVC and may not look right in certain locales
  62. //std::time_t now = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() );
  63. //std::string ser_time = std::string( std::ctime(&now) );
  64. //ser_time.pop_back();
  65. //node["Serialized"] = ser_time;
  66. // Alternative formulation
  67. std::time_t now = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() );
  68. std::stringstream out;
  69. // use locale format
  70. //out.imbue(std::locale("")); // use whatever is on the system
  71. //out << std::put_time(std::localtime(& now), L"%c") ; // locale on system
  72. // ISO-8601 format;
  73. out << std::put_time(std::localtime(& now), "%F %T %z");
  74. node["Serialized"] = out.str();
  75. node["Lemma_VERSION"] = LEMMA_VERSION;
  76. return node;
  77. };
  78. // ==================== OPERATORS ==============================
  79. // ==================== OPERATIONS ==============================
  80. // ==================== ACCESS ==============================
  81. // ==================== INQUIRY ==============================
  82. /** Returns the name of the underlying class; Run-time type information (RTTI). This approach
  83. Was chosen over typeid due to name mangling among various compilers, and the need for consistency
  84. in Serialized objects.
  85. */
  86. virtual std::string GetName() const;
  87. protected:
  88. // ==================== LIFECYCLE ==============================
  89. /** Protected default constructor. This is an abstract class and
  90. * cannot be instantiated.
  91. */
  92. LemmaObject ( const ctor_key& );
  93. /** Protected DeSerializing constructor */
  94. LemmaObject ( const YAML::Node& node, const ctor_key& );
  95. /** Disable copying Lemma Object */
  96. LemmaObject( const LemmaObject& ) = delete;
  97. /** Protected default destructor. This is an abstract class and
  98. * cannot be instantiated. Virtual is necessary so that if base class destructor is
  99. * called, we get the right behaviour.
  100. */
  101. virtual ~LemmaObject();
  102. private:
  103. // ==================== DATA MEMBERS ==============================
  104. /** ASCII string representation of the class name */
  105. static constexpr auto CName = "LemmaObject";
  106. }; // ----- end of class LemmaObject -----
  107. /////////////////////////////////////////////////////////////////
  108. // Error Classes
  109. /** Error called when DeSerializing breaks. If the node type is not the expected one
  110. * this error is thown.
  111. */
  112. class DeSerializeTypeMismatch : public std::runtime_error {
  113. public:
  114. DeSerializeTypeMismatch(const std::string& expected, const std::string& got);
  115. };
  116. /** If an assignment is made that is out of bounts, throw this.
  117. */
  118. class AssignmentOutOfBounds : public std::runtime_error {
  119. public:
  120. /** Throw when an assignment is out of bounds.
  121. * @param[in] ptr is a pointer to the class throwing the exception.
  122. */
  123. AssignmentOutOfBounds(LemmaObject *ptr);
  124. };
  125. /** If a pointer to a class is requested, but it is NULL valued, throw this
  126. */
  127. class RequestToReturnNullPointer : public std::runtime_error {
  128. public:
  129. /** Thrown when the pointer is NULL
  130. * @param[in] ptr is a pointer to the class throwing the exception.
  131. */
  132. RequestToReturnNullPointer(LemmaObject *ptr);
  133. };
  134. /** If an error in opening a .mat file is encountered, throw this.
  135. */
  136. class MatFileCannotBeOpened : public std::runtime_error {
  137. /** thown when a mat file fails to be opened.
  138. */
  139. public: MatFileCannotBeOpened();
  140. };
  141. /** Generic file I/O error. */
  142. class GenericFileIOError : public std::runtime_error {
  143. public: GenericFileIOError(LemmaObject *ptr, const std::string &filename);
  144. };
  145. }
  146. #endif // __LEMMAOBJECT_H