ソースを参照

Merge branch 'master' into extended_trace_eric

gbeurier 7 年 前
コミット
045f3c03ff

+ 1 - 1
CMakeLists.txt

@@ -53,7 +53,7 @@ endif ()
 
 if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
-  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -std=c++11")
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -std=c++14")
 
   if ("${CMAKE_BUILD_TYPE}" EQUAL "Debug" OR "${CMAKE_BUILD_TYPE}" EQUAL "RelWithDebInfo")
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb3")

+ 18 - 24
src/artis/context/Value.hpp

@@ -25,13 +25,21 @@
 
 #include <boost/serialization/serialization.hpp>
 #include <boost/serialization/array.hpp>
+#include <boost/version.hpp>
+
+#if BOOST_VERSION > 106200
+#include <boost/serialization/array_wrapper.hpp>
+#endif
 
 #include <algorithm>
-#include <typeinfo>
+#include <cassert>
 #include <cstring>
+
 #if WIN32
 #include <string>
 #endif
+
+#include <typeinfo>
 #include <vector>
 
 namespace artis { namespace context {
@@ -86,32 +94,32 @@ public:
 
     std::string to_string() const
     {
-        if (is_double()) {
+        if (is_type < double >()) {
             double v;
 
             get_content(v);
             return std::to_string(v);
-        } else if (is_int()) {
+        } else if (is_type < int >()) {
             int v;
 
             get_content(v);
             return std::to_string(v);
-        } else if (is_bool()) {
+        } else if (is_type < bool >()) {
             bool v;
 
             get_content(v);
             return v ? "true" : "false";
-        } if (is_double_vector()) {
+        } if (is_type < std::vector < double > >()) {
             std::vector < double > v;
 
             get_content(v);
             return "";
-        } else if (is_int_vector()) {
+        } else if (is_type < std::vector < int > >()) {
             std::vector < int > v;
 
             get_content(v);
             return "";
-        } else if (is_bool_vector()) {
+        } else if (is_type < std::vector < bool > >()) {
             std::vector < bool > v;
 
             get_content(v);
@@ -121,23 +129,9 @@ public:
         }
     }
 
-    bool is_bool() const
-    { return _type_id == typeid(bool).hash_code(); }
-
-    bool is_bool_vector() const
-    { return _type_id == typeid(std::vector < bool >).hash_code(); }
-
-    bool is_int() const
-    { return _type_id == typeid(int).hash_code(); }
-
-    bool is_int_vector() const
-    { return _type_id == typeid(std::vector < int >).hash_code(); }
-
-    bool is_double() const
-    { return _type_id == typeid(double).hash_code(); }
-
-    bool is_double_vector() const
-    { return _type_id == typeid(std::vector < double >).hash_code(); }
+    template < typename Z >
+    bool is_type() const
+    { return _type_id == typeid(Z).hash_code(); }
 
 private:
     void assign(const void* content, size_t size, size_t type_id)

+ 1 - 1
src/artis/kernel/AbstractAtomicModel.hpp

@@ -125,7 +125,7 @@ public:
         return static_cast < const T* >(this)->*(value.get < T, W >());
     }
 
-    virtual std::string get(const ValueType& value_type, typename U::type t,
+    virtual std::string get(const ValueTypeID& value_type, typename U::type t,
                             unsigned int index) const
     {
         if (type::last_time != t) {

+ 1 - 1
src/artis/kernel/AbstractCoupledModel.hpp

@@ -185,7 +185,7 @@ public:
         }
     }
 
-    virtual std::string get(const ValueType& value_type, typename U::type t,
+    virtual std::string get(const ValueTypeID& value_type, typename U::type t,
                             unsigned int index) const
     {
         typename AbstractCoupledModel::SubModelInternals::const_iterator it =

+ 162 - 8
src/artis/kernel/Any.hpp

@@ -26,11 +26,55 @@
 #include <artis/context/Value.hpp>
 
 #include <string>
+#include <tuple>
+#include <utility>
 #include <vector>
 
+#include <iostream>
+
 namespace artis { namespace kernel {
 
-enum ValueType { DOUBLE, INT, BOOL };
+enum ValueTypeID { DOUBLE, INT, BOOL, DOUBLE_VECTOR, INT_VECTOR, BOOL_VECTOR,
+                   STRING, USER };
+
+// template < typename H, typename T >
+// struct typelist
+// {
+//     typedef H head;
+//     typedef T tail;
+// };
+
+// struct null_typelist
+// { };
+
+// template < typename Fun >
+// struct cons;
+
+// template < typename T1 >
+// struct cons < void (*)(T1)>
+// {
+//     typedef typelist < T1, null_typelist > type;
+
+// };
+
+// template < typename T1, typename T2 >
+// struct cons < void (*)(T1, T2)>
+// {
+//     typedef typelist < T1, typelist < T2, null_typelist > > type;
+
+// };
+
+// template < typename T1, typename T2, typename T3 >
+// struct cons < void (*)(T1, T2, T3)>
+// {
+//     typedef typelist < T1, typelist < T2, typelist < T3,
+//                                                      null_typelist > > > type;
+
+// };
+
+// #define TYPELIST(a) cons < void (*)a >::type
+
+// typedef TYPELIST((double, int, bool)) value_types;
 
 class Any
 {
@@ -54,6 +98,9 @@ private:
 
     base* ptr_;
 
+    template < typename V >
+    friend struct ValueType;
+
 public:
     Any() : ptr_(nullptr)
     { }
@@ -92,32 +139,32 @@ public:
     template < typename T >
     void restore(T* o, const context::Value& value)
     {
-        if (value.is_double()) {
+        if (value.is_type < double >()) {
             double v;
 
             value.get_content(v);
             put(o, v);
-        } else if (value.is_int()) {
+        } else if (value.is_type < int >()) {
             int v;
 
             value.get_content(v);
             put(o, v);
-        } else if (value.is_bool()) {
+        } else if (value.is_type < bool >()) {
             bool v;
 
             value.get_content(v);
             put(o, v);
-        } else if (value.is_double_vector()) {
+        } else if (value.is_type < std::vector < double > >()) {
             std::vector < double > v;
 
             value.get_content(v);
             put(o, v);
-        } else if (value.is_int_vector()) {
+        } else if (value.is_type < std::vector < int > >()) {
             std::vector < int > v;
 
             value.get_content(v);
             put(o, v);
-        } else if (value.is_bool_vector()) {
+        } else if (value.is_type < std::vector < bool > >()) {
             std::vector < bool > v;
 
             value.get_content(v);
@@ -202,7 +249,31 @@ public:
                     if (q_bool) {
                         return o->*(q_bool->value_) ? "true": "false";
                     } else {
-                        return "NA";
+                        data < T, std::vector < double > >* q_double_v =
+                            dynamic_cast < data < T, std::vector <
+                                double > >* >(ptr_);
+
+                        if (q_double_v) {
+                            return "";
+                        } else {
+                            data < T, std::vector < int > >* q_int_v =
+                                dynamic_cast < data < T, std::vector <
+                                    int > >* >(ptr_);
+
+                            if (q_int_v) {
+                                return "";
+                            } else {
+                                data < T, std::vector < bool > >* q_bool_v =
+                                    dynamic_cast < data < T, std::vector <
+                                        bool > >* >(ptr_);
+
+                                if (q_bool_v) {
+                                    return "";
+                                } else {
+                                    return "NA";
+                                }
+                            }
+                        }
                     }
                 }
             }
@@ -212,6 +283,89 @@ public:
     }
 };
 
+// template < typename C,
+//            typename T,
+//            typename Seq = std::make_integer_sequence
+//            < int, std::tuple_size < T >::value > >
+// struct TupleIterator;
+
+// template < typename C,
+//            typename T,
+//            int... S >
+// struct TupleIterator < C, T, std::integer_sequence < int, S... > >
+// {
+//     TupleIterator(C caller, const T& val)
+//     { result = std::make_tuple(caller(std::get < S >(val))...); }
+
+//     std::tuple < context::Value, context::Value, context::Value > result;
+// };
+
+// template < typename C, typename T >
+// TupleIterator < C, T > tuple_iterator(C caller, const T& val)
+// {
+//     return TupleIterator < C, T >(caller, val);
+// }
+
+// template < typename T >
+// struct ToValueCaller
+// {
+//     ToValueCaller(const Any& any, const T* o) : _any(any), _object(o)
+//     { }
+
+//     template < typename Z >
+//     context::Value operator()(const Z& data) const
+//     { return data.to_value(_any, _object); }
+
+//     const Any& _any;
+//     const T*   _object;
+// };
+
+// template < typename V >
+// struct ValueType
+// {
+//     template < typename T >
+//     context::Value to_value(const Any& any, const T* o) const
+//     {
+//         Any::data < T, V >* q = dynamic_cast < Any::data < T, V >* >(any.ptr_);
+
+//         if (q) {
+//             return context::Value(o->*(q->value_));
+//         } else {
+//             return context::Value();
+//         }
+//     }
+// };
+
+// template < typename Tuple, typename F, size_t... Is >
+// constexpr auto apply_impl(Tuple t, F f, std::index_sequence < Is... >)
+// { return f(std::get < Is >(t)...); }
+
+// template < typename Tuple, typename F >
+// constexpr auto apply(Tuple t, F f)
+// { return apply_impl(t, f, std::make_index_sequence <
+//                     std::tuple_size < Tuple >{}>{}); }
+
+// struct find_not_null_value
+// {
+//     context::Value operator()(const context::Value& left,
+//                               const context::Value& right) const;
+// };
+
+// template < typename T >
+// static context::Value to_value(const Any& any, const T* o)
+// {
+//     auto value_types =
+//         std::make_tuple(ValueType < double >(),
+//                         ValueType < int >(),
+//                         ValueType < bool >());
+//     auto list = tuple_iterator(ToValueCaller < T >(any, o), value_types);
+//     auto result = apply(list.result, find_not_null_value{});
+
+//     std::cout << result.to_string() << std::endl;
+
+//     assert(false);
+// }
+
 } }
 
 #endif

+ 1 - 1
src/artis/kernel/CMakeLists.txt

@@ -7,7 +7,7 @@ SET(ARTIS_KERNEL_HPP AbstractAtomicModel.hpp AbstractCoupledModel.hpp
   AbstractModel.hpp Any.hpp Externals.hpp Internals.hpp Macro.hpp Node.hpp
   Simulator.hpp States.hpp)
 
-SET(ARTIS_KERNEL_CPP Simulator.cpp)
+SET(ARTIS_KERNEL_CPP Any.cpp Simulator.cpp)
 
 ADD_SOURCES(artis-lib ${ARTIS_KERNEL_HPP} ${ARTIS_KERNEL_CPP})
 

+ 1 - 1
src/artis/kernel/Node.hpp

@@ -41,7 +41,7 @@ public:
 
     virtual const Any& get(typename U::type t, unsigned int index) const = 0;
 
-    virtual std::string get(const ValueType& value_type, typename U::type t,
+    virtual std::string get(const ValueTypeID& value_type, typename U::type t,
                             unsigned int index) const = 0;
 
     virtual const Node < U >* get_submodel(unsigned int /* index */) const

+ 2 - 2
src/artis/observer/View.hpp

@@ -148,7 +148,7 @@ public:
         }
     }
 
-    void selector(const std::string& name, kernel::ValueType value_type,
+    void selector(const std::string& name, kernel::ValueTypeID value_type,
                   const Selector& chain)
     {
         _selectors[name] = std::make_pair(value_type, chain);
@@ -159,7 +159,7 @@ public:
     { return _values;}
 
 private:
-    typedef std::map < std::string, std::pair < kernel::ValueType,
+    typedef std::map < std::string, std::pair < kernel::ValueTypeID,
                                                 Selector > > Selectors;
 
     Selectors                                    _selectors;

+ 1 - 1
src/test/models.hpp

@@ -92,7 +92,7 @@ public:
         _dx = 0.;
     }
 
-private:
+//private:
     int _ix;
     bool _bx;
     double _dx;

+ 11 - 0
src/test/test.cpp

@@ -87,6 +87,17 @@ int main()
     GlobalParameters globalParameters;
     ModelParameters modelParameters;
 
+    /**********************/
+
+    // AModel a;
+    // artis::kernel::Any v(&AModel::_ix);
+
+    // a.init(0, modelParameters);
+
+    // artis::kernel::to_value < AModel >(v, &a);
+
+    /**********************/
+
     ABuilder builder("{ \"type\": \"RootModel\", "                   \
                      "\"name\": \"root\", "                          \
                      "\"states\": [ "                                \