Explorar el Código

Add Value class

Eric Ramat hace 9 años
padre
commit
387f7477c6

+ 1 - 1
src/paradevs/common/CMakeLists.txt

@@ -11,7 +11,7 @@ LINK_DIRECTORIES(
 
 SET(COMMON_HPP Bag.hpp Coordinator.hpp ExternalEvent.hpp InternalEvent.hpp
   Links.hpp Model.hpp Node.hpp Parameters.hpp RootCoordinator.hpp Scheduler.hpp
-  Simulator.hpp)
+  Simulator.hpp Value.hpp)
 
 INSTALL(FILES ${COMMON_HPP} DESTINATION ${PARADEVS_INCLUDE_DIRS}/common)
 

+ 12 - 7
src/paradevs/common/ExternalEvent.hpp

@@ -29,8 +29,10 @@
 
 #include <paradevs/common/Model.hpp>
 #include <paradevs/common/Node.hpp>
+#include <paradevs/common/Value.hpp>
 
 #include <boost/serialization/serialization.hpp>
+#include <boost/serialization/nvp.hpp>
 
 #include <sstream>
 #include <string>
@@ -47,11 +49,11 @@ template < class Time >
 class ExternalEvent
 {
 public:
-    ExternalEvent(const std::string& port_name, void* content) :
+    ExternalEvent(const std::string& port_name, const Value& content) :
         _port_name(port_name), _model(0), _content(content)
     { }
 
-    ExternalEvent(const Node < Time >& node, void* content) :
+    ExternalEvent(const Node < Time >& node, const Value& content) :
         _port_name(node.get_port_name()),
         _model(node.get_model()),
         _content(content)
@@ -63,13 +65,13 @@ public:
     virtual ~ExternalEvent()
     { }
 
-    void* get_content() const
+    const Value& get_content() const
     { return _content; }
 
     const std::string& get_port_name() const
     { return _port_name; }
 
-    void set_content(void* content)
+    void set_content(const Value& content)
     { _content = content; }
 
     Model < Time >* get_model() const
@@ -88,8 +90,8 @@ public:
         ss << "( " << _port_name << " , "
            << (_model ? _model->get_name() : "<>")
            << " , ";
-        if (_content) {
-            ss << *(double*)_content;
+        if (not _content.empty()) {
+            ss << _content.to_string();
         } else {
             ss << "null";
         }
@@ -106,12 +108,15 @@ private:
         (void) version;
 
         ar & _port_name;
+        _model = 0;
+        // ar & _model;
+        ar & _content;
         // ar & _model->get_name();
     }
 
     std::string     _port_name;
     Model < Time >* _model;
-    void*           _content;
+    Value           _content;
 };
 
 } } // namespace paradevs common

+ 105 - 0
src/paradevs/common/Value.hpp

@@ -0,0 +1,105 @@
+/**
+ * @file Value.hpp
+ * @author The PARADEVS Development Team
+ * See the AUTHORS or Authors.txt file
+ */
+
+/*
+ * PARADEVS - the multimodeling and simulation environment
+ * This file is a part of the PARADEVS environment
+ *
+ * Copyright (C) 2013-2015 ULCO http://www.univ-litoral.fr
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef COMMON_VALUE
+#define COMMON_VALUE 1
+
+#include <boost/serialization/serialization.hpp>
+#include <boost/serialization/array.hpp>
+
+#include <cstring>
+#include <typeinfo>
+
+namespace paradevs { namespace common {
+
+class Value
+{
+public:
+    Value() : _content(nullptr), _size(0)
+    { }
+
+    template < typename T >
+    Value(T value)
+    { assign(&value, sizeof(T), typeid(T).hash_code()); }
+
+    Value(void* content, size_t size)
+    { assign(content, size, typeid(void*).hash_code()); }
+
+    Value(const char* value, unsigned int size)
+    { assign(value, size * sizeof(char), typeid(char*).hash_code()); }
+
+    virtual ~Value()
+    { /* if (_content) delete[] _content; */ }
+
+    bool empty() const
+    { return _content == nullptr; }
+
+    template < typename T >
+    T get_content() const
+    {
+        assert(_type_id == typeid(T).hash_code());
+
+        return *(T*)(_content);
+    }
+
+    std::string to_string() const
+    {
+        return std::string();
+    }
+
+private:
+    void assign(const void* content, size_t size, size_t type_id)
+    {
+        _content = new char[size];
+        std::memcpy(_content, content, size);
+        _size = size;
+        _type_id = type_id;
+    }
+
+    friend class boost::serialization::access;
+
+    template<class Archive>
+    void serialize(Archive & ar, const unsigned int version)
+    {
+        (void) version;
+
+        ar & _size;
+        if (Archive::is_loading::value) {
+            assert(_content == nullptr);
+            _content = new char[_size];
+        }
+        ar & boost::serialization::make_array < char >(_content, _size);
+        ar & _type_id;
+    }
+
+    char*  _content;
+    size_t _size;
+    size_t _type_id;
+};
+
+} } // namespace paradevs common
+
+#endif

+ 2 - 1
src/paradevs/kernel/pdevs/GraphManager.hpp

@@ -105,13 +105,14 @@ public:
     }
 
     virtual void dispatch_events_to_parent(common::Node < Time > node,
-                                           void* content,
+                                           const common::Value& content,
                                            typename Time::type t)
     {
         common::Bag < Time > ymessages;
 
         ymessages.push_back(
             common::ExternalEvent <Time >(node, content));
+
         dynamic_cast < common::Coordinator < Time >* >(
             _coordinator->get_parent())->dispatch_events(
                 ymessages, t);

+ 2 - 13
src/paradevs/kernel/pdevs/mpi/GraphManager.hpp

@@ -48,20 +48,9 @@ public:
     { }
 
     virtual void dispatch_events_to_parent(common::Node < Time > node,
-                                           void* content,
+                                           const common::Value& content,
                                            typename Time::type t)
-    {
-/*        common::Bag < Time > ymessages;
-
-        ymessages.push_back(
-            common::ExternalEvent <Time >(node, content));
-        dynamic_cast < common::Coordinator < Time >* >(
-            _coordinator->get_parent())->dispatch_events(
-            ymessages, t); */
-
-        _logical_processor->dispatch_events_to_parent(node, content, t);
-
-    }
+    { _logical_processor->dispatch_events_to_parent(node, content, t); }
 
     void set_logical_processor(LogicalProcessor < Time >* logical_processor)
     { _logical_processor = logical_processor; }

+ 5 - 4
src/paradevs/kernel/pdevs/mpi/LogicalProcessor.hpp

@@ -49,12 +49,13 @@ public:
     { }
 
     void dispatch_events_to_parent(common::Node < Time > node,
-                                   void* content,
+                                   const common::Value& content,
                                    typename Time::type t)
     {
         (void) t;
 
-        _output_bag.push_back(common::ExternalEvent <Time >(node, content));
+        _output_bag.push_back(
+            common::ExternalEvent <Time >(node, content));
     }
 
     void loop()
@@ -105,8 +106,8 @@ private:
     int _rank;
     int _parent;
     boost::mpi::communicator _communicator;
-    common::Model < Time >* _model;
-    common::Bag < Time > _output_bag;
+    common::Model < Time >*  _model;
+    common::Bag < Time >     _output_bag;
 };
 
 } } } // namespace paradevs pdevs mpi