Value.hpp 4.9 KB

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