Lemma is an Electromagnetics API
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LemmaObject.h 7.2KB

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