Value.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include <iostream>
  34. namespace artis {
  35. namespace common {
  36. class Value {
  37. public:
  38. Value()
  39. :_content(nullptr), _size(0) { }
  40. template<typename T>
  41. Value(T value) { assign(&value, sizeof(T), typeid(T).hash_code()); }
  42. Value(void* content, size_t size) { assign(content, size, typeid(void*).hash_code()); }
  43. Value(const char* value, unsigned int size)
  44. {
  45. assign(value, size * sizeof(char), typeid(char*).hash_code());
  46. }
  47. Value(const Value& value)
  48. {
  49. if (value._content) {
  50. assign(value._content, value._size, value._type_id);
  51. } else {
  52. _content = nullptr;
  53. _size = 0;
  54. _type_id = 0;
  55. }
  56. }
  57. virtual ~Value() { if (_content != nullptr) delete[] _content; }
  58. bool empty() const { return _content == nullptr; }
  59. template<typename T>
  60. void operator()(T& value) const
  61. {
  62. assert(_type_id == typeid(T).hash_code());
  63. value = *(T*) (_content);
  64. }
  65. template<typename Z>
  66. bool is_type() const { return _type_id == typeid(Z).hash_code(); }
  67. Value& operator=(const Value& value)
  68. {
  69. if (_content != nullptr) {
  70. delete _content;
  71. }
  72. if (value._content) {
  73. assign(value._content, value._size, value._type_id);
  74. } else {
  75. _content = nullptr;
  76. _size = 0;
  77. _type_id = 0;
  78. }
  79. return *this;
  80. }
  81. std::string to_string() const
  82. {
  83. if (is_type<double>()) {
  84. double v;
  85. operator()(v);
  86. return std::to_string(v);
  87. } else if (is_type<int>()) {
  88. int v;
  89. operator()(v);
  90. return std::to_string(v);
  91. } else if (is_type<bool>()) {
  92. bool v;
  93. operator()(v);
  94. return v ? "true" : "false";
  95. }
  96. if (is_type<std::vector<double> >()) {
  97. std::vector<double> v;
  98. operator()(v);
  99. return "";
  100. } else if (is_type<std::vector<int> >()) {
  101. std::vector<int> v;
  102. operator()(v);
  103. return "";
  104. } else if (is_type<std::vector<bool> >()) {
  105. std::vector<bool> v;
  106. operator()(v);
  107. return "";
  108. } else {
  109. return "<unstringify>";
  110. }
  111. }
  112. private:
  113. void assign(const void* content, size_t size, size_t type_id)
  114. {
  115. _content = new char[size];
  116. std::memcpy(_content, content, size);
  117. _size = size;
  118. _type_id = type_id;
  119. }
  120. friend class boost::serialization::access;
  121. template<class Archive>
  122. void serialize(Archive& ar, const unsigned int version)
  123. {
  124. (void) version;
  125. ar & _size;
  126. if (Archive::is_loading::value) {
  127. assert(_content == nullptr);
  128. _content = new char[_size];
  129. }
  130. ar & boost::serialization::make_array<char>(_content, _size);
  131. ar & _type_id;
  132. }
  133. char* _content;
  134. size_t _size;
  135. size_t _type_id;
  136. };
  137. }
  138. } // namespace artis common
  139. #endif