Browse Source

Remove std::vector to value and add pointer

Eric Ramat 1 year ago
parent
commit
b76101a616
1 changed files with 49 additions and 10 deletions
  1. 49 10
      src/artis-star/common/Value.hpp

+ 49 - 10
src/artis-star/common/Value.hpp

@@ -32,6 +32,7 @@
 
 #include <cassert>
 #include <cstring>
+#include <numeric>
 #include <typeinfo>
 #include <vector>
 
@@ -46,6 +47,12 @@ namespace artis {
             template<typename T>
             Value(T value) { assign(&value, sizeof(T), typeid(T).hash_code()); }
 
+            template<typename T>
+            Value(T* value, size_t size)
+            {
+                assign(value, sizeof(T) * size, typeid(T*).hash_code());
+            }
+
             Value(void* content, size_t size) { assign(content, size, typeid(void*).hash_code()); }
 
             Value(const char* value, unsigned int size)
@@ -76,6 +83,14 @@ namespace artis {
                 value = *(T*) (_content);
             }
 
+            template<typename T>
+            void operator()(T*& value) const
+            {
+                assert(_type_id == typeid(T*).hash_code());
+
+                value = (T*) (_content);
+            }
+
             template<typename Z>
             bool is_type() const { return _type_id == typeid(Z).hash_code(); }
 
@@ -118,22 +133,46 @@ namespace artis {
 
                     operator()(v);
                     return v ? "true" : "false";
-                }
-                if (is_type<std::vector<double> >()) {
-                    std::vector<double> v;
+                } else if (is_type<double*>()) {
+                    double* v;
+                    std::string str;
+                    size_t size = _size / sizeof(double);
+
+                    operator()(v);
+                    for (size_t i = 0; i < size; ++i) {
+                        str += std::to_string(v[i]) + std::string(" ");
+                    }
+                    return str;
+                } else if (is_type<int*>()) {
+                    int* v;
+                    std::string str;
+                    size_t size = _size / sizeof(int);
 
                     operator()(v);
-                    return "";
-                } else if (is_type<std::vector<int> >()) {
-                    std::vector<int> v;
+                    for (size_t i = 0; i < size; ++i) {
+                        str += std::to_string(v[i]) + std::string(" ");
+                    }
+                    return str;
+                } else if (is_type<unsigned int*>()) {
+                    unsigned int* v;
+                    std::string str;
+                    size_t size = _size / sizeof(unsigned int);
 
                     operator()(v);
-                    return "";
-                } else if (is_type<std::vector<bool> >()) {
-                    std::vector<bool> v;
+                    for (size_t i = 0; i < size; ++i) {
+                        str += std::to_string(v[i]) + std::string(" ");
+                    }
+                    return str;
+                } else if (is_type<bool*>()) {
+                    bool* v;
+                    std::string str;
+                    size_t size = _size / sizeof(bool);
 
                     operator()(v);
-                    return "";
+                    for (size_t i = 0; i < size; ++i) {
+                        str += std::to_string(v[i]) + std::string(" ");
+                    }
+                    return str;
                 } else {
                     return "<unstringify>";
                 }