From f1f338c3d40f0c86744e7e4c70d386771f94ad27 Mon Sep 17 00:00:00 2001 From: Patrick Flick Date: Fri, 19 Aug 2016 14:44:36 -0700 Subject: [PATCH] changes to type and type traits --- include/mxx/datatypes.hpp | 44 ++++++++++++++++++------------------- include/mxx/type_traits.hpp | 8 +++---- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/include/mxx/datatypes.hpp b/include/mxx/datatypes.hpp index f699fdf..545e87a 100644 --- a/include/mxx/datatypes.hpp +++ b/include/mxx/datatypes.hpp @@ -580,8 +580,9 @@ class value_datatype_builder : public datatype_builder_base -size_t offset_of(M T::* m) { +template +typename std::enable_if::value, size_t>::type +offset_of(M Base::* m) { return reinterpret_cast(&(((T*)nullptr)->*m)); } @@ -593,19 +594,16 @@ class static_datatype_builder : public datatype_builder_base void add_member(M T::*m) { - this->template add_member_by_offset(offset_of(m)); + this->template add_member_by_offset(offset_of(m)); } -}; - - -// non-static datatype construction using variadic templates and only a subset of members -template -datatype built_custom_datatype(T* val, Args&...args) { - value_datatype_builder builder(*val); - builder.add_members(args...); - return builder.get_datatype(); -} + // support adding members of base classes + template + typename std::enable_if::value, void>::type + add_member(M Base::*m) { + this->template add_member_by_offset(offset_of(m)); + } +}; /* @@ -674,7 +672,7 @@ struct is_trivial_type -typename std::enable_if&)>::value, datatype>::type +inline typename std::enable_if&)>::value, datatype>::type build_datatype() { //static_assert(!has_static_member_datatype&)>::value, "needs static datatype() function"); //T val; @@ -684,7 +682,7 @@ build_datatype() { } template -typename std::enable_if< +inline typename std::enable_if< !has_static_member_datatype&)>::value && has_member_datatype&)>::value , datatype>::type @@ -695,42 +693,42 @@ build_datatype() { return builder.get_datatype(); } +// TODO: enable_if specializations for this function template -datatype build_datatype(const T&) { +inline datatype build_datatype(const T&) { datatype dt(datatype_builder::get_type(), is_builtin_type::value); return dt; } -// TODO: use is_builtin etc instead of `has_builder` after removing the custom `datatype_builder` -// specializations +// if datatype_builder exists: template -typename std::enable_if::value, datatype>::type +inline typename std::enable_if::value, datatype>::type build_datatype() { datatype dt(datatype_builder::get_type(), is_builtin_type::value); return dt; } template -typename std::enable_if::value, datatype>::type +inline typename std::enable_if::value, datatype>::type build_datatype() { // static assert the opposite to trigger the static assertion failure static_assert(is_trivial_type::value, "Type `T` is not a `trivial` type and is thus not supported for mxx send/recv operations. " "This type needs one of the following to be supported as trivial datatype: " - "custom datatype builder, member function `datatype` or global function `make_datatype(Layout& l, T&)`"); + "specialized build_datatype, a member function `datatype`, or global function `make_datatype(Layout& l, T&)`"); return datatype(); } template -datatype get_datatype() { +inline datatype get_datatype() { // TODO: retrieve cached datatype return build_datatype(); } template -datatype get_datatype(const T& t) { +inline datatype get_datatype(const T& t) { // TODO: retrieve cached datatype return build_datatype(t); } diff --git a/include/mxx/type_traits.hpp b/include/mxx/type_traits.hpp index 6dffddc..9b89f86 100644 --- a/include/mxx/type_traits.hpp +++ b/include/mxx/type_traits.hpp @@ -5,7 +5,7 @@ #include #include -//namespace mxx { +namespace mxx { // source for testing if member functions are available: http://stackoverflow.com/a/16824239/4639394 #define MXX_DEFINE_HAS_MEMBER(member) \ @@ -22,7 +22,7 @@ private: \ decltype( std::declval(). member ( std::declval()... ) ), \ Ret /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */ \ >::type; /* attempt to call it and see if the return type is correct */ \ - template \ + template \ static constexpr std::false_type check(...); \ typedef decltype(check(0)) type; \ public: \ @@ -95,11 +95,9 @@ MXX_DEFINE_HAS_STATIC_MEMBER(datatype) MXX_DEFINE_HAS_MEMBER(datatype) -namespace mxx { // TODO: build this into the mxx/datatype structures template struct has_datatype : std::false_type {}; -} @@ -178,6 +176,6 @@ template struct is_flat_type : std::integral_constant::value> {}; -//} // namespace mxx +} // namespace mxx #endif // MXX_TYPE_TRAITS