Parcourir la source

Add size method to Value class

Eric Ramat il y a 11 mois
Parent
commit
52b16b0dfb
2 fichiers modifiés avec 26 ajouts et 6 suppressions
  1. 18 6
      src/artis-star/common/event/Value.hpp
  2. 8 0
      tests/common_value.cpp

+ 18 - 6
src/artis-star/common/event/Value.hpp

@@ -62,6 +62,8 @@ class Value {
 
     virtual bool operator==(const Base &other) const = 0;
 
+    virtual size_t size() const = 0;
+
     virtual std::string to_string() const = 0;
 
     virtual const std::type_info &type() const noexcept = 0;
@@ -124,6 +126,16 @@ class Value {
       return ss.str();
     }
 
+    size_t size() const override {
+      if (type() == typeid(std::vector<T>)) {
+        return std::any_cast<const std::vector<T> &>(*this).size();
+      } else if (type() == typeid(std::string)) {
+        return std::any_cast<const std::string &>(*this).size();
+      } else if (type() == typeid(std::valarray<T>)) {
+        return std::any_cast<const std::valarray<T> &>(*this).size();
+      } else { return 1; }
+    }
+
     const std::type_info &type() const noexcept override { return std::any::type(); }
 
   private:
@@ -133,7 +145,7 @@ class Value {
 public:
   Value() : _data(nullptr) {}
 
-  Value(const Value& value) : _data(value._data) {}
+  Value(const Value &value) : _data(value._data) {}
 
   template<typename T, std::enable_if_t<not HasToString<T>::Has, bool> = true>
   Value(const T &value) : _data(new Data<T>(value)) {}
@@ -155,7 +167,7 @@ public:
   template<typename Z>
   bool is_type() const { return _data->type() == typeid(Z); }
 
-  Value& operator=(const Value &other) {
+  Value &operator=(const Value &other) {
     _data = other._data;
     return *this;
   }
@@ -179,9 +191,9 @@ public:
     dynamic_cast<Data<T> *>(_data.get())->operator()(value);
   }
 
-  std::string to_string() const {
-    return _data->to_string();
-  }
+  size_t size() const { return _data->size(); }
+
+  std::string to_string() const { return _data->to_string(); }
 
 private:
   friend class boost::serialization::access;
@@ -194,7 +206,7 @@ private:
   std::shared_ptr<Base> _data;
 };
 
-std::ostream& operator<<(std::ostream& o, const artis::common::event::Value& value);
+std::ostream &operator<<(std::ostream &o, const artis::common::event::Value &value);
 
 } // namespace artis common event
 

+ 8 - 0
tests/common_value.cpp

@@ -95,6 +95,12 @@ BOOST_AUTO_TEST_CASE(Common_Value_TestCase_4)
   BOOST_REQUIRE_EQUAL(double_values.to_string(), "0.5 0.6 1.2 ");
   BOOST_REQUIRE_EQUAL(bool_values.to_string(), "true false true false ");
   BOOST_REQUIRE_EQUAL(char_values.to_string(), "a b c ");
+
+  BOOST_REQUIRE_EQUAL(int_values.size(), 3);
+  BOOST_REQUIRE_EQUAL(unsigned_int_values.size(), 3);
+  BOOST_REQUIRE_EQUAL(double_values.size(), 3);
+  BOOST_REQUIRE_EQUAL(bool_values.size(), 4);
+  BOOST_REQUIRE_EQUAL(char_values.size(), 3);
 }
 
 BOOST_AUTO_TEST_CASE(Common_Value_TestCase_5)
@@ -106,6 +112,7 @@ BOOST_AUTO_TEST_CASE(Common_Value_TestCase_5)
   artis::common::event::Value value{s};
 
   BOOST_REQUIRE_EQUAL(value.to_string(), "s");
+  BOOST_REQUIRE_EQUAL(value.size(), 1);
 }
 
 BOOST_AUTO_TEST_CASE(Common_Value_TestCase_6)
@@ -118,6 +125,7 @@ BOOST_AUTO_TEST_CASE(Common_Value_TestCase_6)
   artis::common::event::Value value{vs};
 
   BOOST_REQUIRE_EQUAL(value.to_string(), "s s s s ");
+  BOOST_REQUIRE_EQUAL(value.size(), 4);
 }
 
 BOOST_AUTO_TEST_CASE(Common_Value_TestCase_7)