Value.hpp 4.1 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-2018 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 <cstring>
  30. #include <typeinfo>
  31. #include <vector>
  32. #include <iostream>
  33. namespace artis { namespace common {
  34. class Value
  35. {
  36. public:
  37. Value() : _content(nullptr), _size(0)
  38. { }
  39. template < typename T >
  40. Value(T value)
  41. { assign(&value, sizeof(T), typeid(T).hash_code()); }
  42. Value(void* content, size_t size)
  43. { assign(content, size, typeid(void*).hash_code()); }
  44. Value(const char* value, unsigned int size)
  45. { assign(value, size * sizeof(char), typeid(char*).hash_code()); }
  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()
  57. { if (_content) delete _content; }
  58. bool empty() const
  59. { return _content == nullptr; }
  60. template < typename T >
  61. void operator()(T& value) const
  62. {
  63. assert(_type_id == typeid(T).hash_code());
  64. value = *(T*)(_content);
  65. }
  66. template < typename Z >
  67. bool is_type() const
  68. { return _type_id == typeid(Z).hash_code(); }
  69. void operator=(const Value& value)
  70. {
  71. if (_content) {
  72. delete _content;
  73. }
  74. if (value._content) {
  75. assign(value._content, value._size, value._type_id);
  76. } else {
  77. _content = nullptr;
  78. _size = 0;
  79. _type_id = 0;
  80. }
  81. }
  82. std::string to_string() const
  83. {
  84. if (is_type < double >()) {
  85. double v;
  86. operator()(v);
  87. return std::to_string(v);
  88. } else if (is_type < int >()) {
  89. int v;
  90. operator()(v);
  91. return std::to_string(v);
  92. } else if (is_type < bool >()) {
  93. bool v;
  94. operator()(v);
  95. return v ? "true" : "false";
  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. } } // namespace artis common
  138. #endif