Explorar el Código

Improve Value class and fix bug in scheduler

Eric Ramat hace 6 años
padre
commit
231c1f58c6
Se han modificado 2 ficheros con 58 adiciones y 10 borrados
  1. 40 3
      src/paradevs/common/Value.hpp
  2. 18 7
      src/paradevs/common/scheduler/HeapScheduler.hpp

+ 40 - 3
src/paradevs/common/Value.hpp

@@ -32,6 +32,7 @@
 
 #include <cstring>
 #include <typeinfo>
+#include <vector>
 
 #include <iostream>
 
@@ -71,13 +72,17 @@ public:
     { return _content == nullptr; }
 
     template < typename T >
-    T get_content() const
+    void get_content(T& value) const
     {
         assert(_type_id == typeid(T).hash_code());
 
-        return *(T*)(_content);
+        value = *(T*)(_content);
     }
 
+    template < typename Z >
+    bool is_type() const
+    { return _type_id == typeid(Z).hash_code(); }
+
     void operator=(const Value& value)
     {
         if (_content) {
@@ -95,7 +100,39 @@ public:
 
     std::string to_string() const
     {
-        return std::string();
+        if (is_type < double >()) {
+            double v;
+
+            get_content(v);
+            return std::to_string(v);
+        } else if (is_type < int >()) {
+            int v;
+
+            get_content(v);
+            return std::to_string(v);
+        } else if (is_type < bool >()) {
+            bool v;
+
+            get_content(v);
+            return v ? "true" : "false";
+        } if (is_type < std::vector < double > >()) {
+            std::vector < double > v;
+
+            get_content(v);
+            return "";
+        } else if (is_type < std::vector < int > >()) {
+            std::vector < int > v;
+
+            get_content(v);
+            return "";
+        } else if (is_type < std::vector < bool > >()) {
+            std::vector < bool > v;
+
+            get_content(v);
+            return "";
+        } else {
+            assert(false);
+        }
     }
 
 private:

+ 18 - 7
src/paradevs/common/scheduler/HeapScheduler.hpp

@@ -69,10 +69,19 @@ public:
     models_type get_current_models(typename Time::type time) const
     {
         models_type models;
+        typename models_type::iterator it;
 
         for (typename type::ordered_iterator it = type::ordered_begin();
              it != type::ordered_end() and it->get_time() == time; ++it) {
-            models.push_back(it->get_model());
+            std::string str = it->get_model()->get_name();
+            auto it2 = find_if(models.begin(), models.end(),
+                               [&str](const model_type* obj) {
+                                   return obj->get_name() == str;
+                               });
+
+            if (it2 == models.end()) {
+                models.push_back(it->get_model());
+            }
         }
         return models;
     }
@@ -92,11 +101,13 @@ public:
         typename Time::type previous_time =
             (*model->handle()._handle).get_time();
 
-        (*model->handle()._handle).set_time(time);
-        if (previous_time < time) {
-            type::decrease(model->handle()._handle);
-        } else if (previous_time > time) {
-            type::increase(model->handle()._handle);
+        if (previous_time != time) {
+            (*model->handle()._handle).set_time(time);
+            if (previous_time < time) {
+                type::decrease(model->handle()._handle);
+            } else if (previous_time > time) {
+                type::increase(model->handle()._handle);
+            }
         }
     }
 
@@ -110,7 +121,7 @@ public:
             ss << "(" << it->get_time() << " -> " << it->get_model()->get_name()
                << ") ";
         }
-        ss << "}";
+        ss << "} [" << type::size() << "]";
         return ss.str();
     }
 };