Value.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @file Value.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef COMMON_VALUE
  26. #define COMMON_VALUE 1
  27. #include <boost/serialization/serialization.hpp>
  28. #include <boost/serialization/array.hpp>
  29. #include <cassert>
  30. #include <cstring>
  31. #include <typeinfo>
  32. #include <vector>
  33. namespace artis {
  34. namespace common {
  35. class Value {
  36. public:
  37. Value()
  38. :_content(nullptr), _size(0) { }
  39. template<typename T>
  40. Value(T value) { assign(&value, sizeof(T), typeid(T).hash_code()); }
  41. Value(void* content, size_t size) { assign(content, size, typeid(void*).hash_code()); }
  42. Value(const char* value, unsigned int size)
  43. {
  44. assign(value, size * sizeof(char), typeid(char*).hash_code());
  45. }
  46. Value(const Value& value)
  47. {
  48. if (value._content) {
  49. assign(value._content, value._size, value._type_id);
  50. } else {
  51. _content = nullptr;
  52. _size = 0;
  53. _type_id = 0;
  54. }
  55. }
  56. virtual ~Value() { if (_content != nullptr) delete[] _content; }
  57. bool empty() const { return _content == nullptr; }
  58. template<typename T>
  59. void operator()(T& value) const
  60. {
  61. assert(_type_id == typeid(T).hash_code());
  62. value = *(T*) (_content);
  63. }
  64. template<typename Z>
  65. bool is_type() const { return _type_id == typeid(Z).hash_code(); }
  66. Value& operator=(const Value& value)
  67. {
  68. if (_content != nullptr) {
  69. delete _content;
  70. }
  71. if (value._content) {
  72. assign(value._content, value._size, value._type_id);
  73. } else {
  74. _content = nullptr;
  75. _size = 0;
  76. _type_id = 0;
  77. }
  78. return *this;
  79. }
  80. std::string to_string() const
  81. {
  82. if (is_type<double>()) {
  83. double v;
  84. operator()(v);
  85. return std::to_string(v);
  86. } else if (is_type<int>()) {
  87. int v;
  88. operator()(v);
  89. return std::to_string(v);
  90. } else if (is_type<unsigned int>()) {
  91. unsigned int v;
  92. operator()(v);
  93. return std::to_string(v);
  94. } else if (is_type<bool>()) {
  95. bool v;
  96. operator()(v);
  97. return v ? "true" : "false";
  98. }
  99. if (is_type<std::vector<double> >()) {
  100. std::vector<double> v;
  101. operator()(v);
  102. return "";
  103. } else if (is_type<std::vector<int> >()) {
  104. std::vector<int> v;
  105. operator()(v);
  106. return "";
  107. } else if (is_type<std::vector<bool> >()) {
  108. std::vector<bool> v;
  109. operator()(v);
  110. return "";
  111. } else {
  112. return "<unstringify>";
  113. }
  114. }
  115. private:
  116. void assign(const void* content, size_t size, size_t type_id)
  117. {
  118. _content = new char[size];
  119. std::memcpy(_content, content, size);
  120. _size = size;
  121. _type_id = type_id;
  122. }
  123. friend class boost::serialization::access;
  124. template<class Archive>
  125. void serialize(Archive& ar, const unsigned int version)
  126. {
  127. (void) version;
  128. ar & _size;
  129. if (Archive::is_loading::value) {
  130. assert(_content == nullptr);
  131. _content = new char[_size];
  132. }
  133. ar & boost::serialization::make_array<char>(_content, _size);
  134. ar & _type_id;
  135. }
  136. char* _content;
  137. size_t _size;
  138. size_t _type_id;
  139. };
  140. }
  141. } // namespace artis common
  142. #endif