fw4spl
TypeMapping.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWTOOLS_TYPEMAPPING_HPP__
8 #define __FWTOOLS_TYPEMAPPING_HPP__
9 
10 #include <boost/mpl/bool.hpp>
11 #include <boost/mpl/empty.hpp>
12 #include <boost/mpl/front.hpp>
13 #include <boost/mpl/if.hpp>
14 #include <boost/mpl/is_sequence.hpp>
15 #include <boost/mpl/pop_front.hpp>
16 #include <boost/mpl/size.hpp>
17 #include <boost/static_assert.hpp>
18 
19 #include <iterator>
20 #include <stdexcept>
21 
22 namespace fwTools
23 {
24 
25 // forward declaration
26 template< class TSEQ, class KeyTypeContainer >
28 
29 template< class T, class KeyType >
31 
32 //
55 template< class TSingle_or_TSEQ, class KeyType_or_KeyTypeContainer >
56 bool isMapping(const KeyType_or_KeyTypeContainer& key)
57 {
58  namespace mpl = ::boost::mpl;
59  typedef BOOST_DEDUCED_TYPENAME mpl::if_<
60  mpl::is_sequence< TSingle_or_TSEQ >,
63  >::type typex;
64  return typex::evaluate(key);
65 
66 }
67 
75 template< class T, class KeyType >
77 {
78 
80  static bool evaluate(const KeyType& key)
81  {
82  FwCoreNotUsedMacro(key);
83  BOOST_STATIC_ASSERT(sizeof(T) == 0); // note its a compilator workaround of BOOST_STATIC_ASSERT(false);
84  // ** if the compilation trap here its because you have not specialized
85  // ** isMapping<MySingleType,MyCorrespondingKeyType>(keytypevalue)
86  std::string msg("isMapping<TYPE>::(const KEYTYPE &key) not specializated for TYPE and/or KEYTYPE!!!");
87  throw std::invalid_argument(msg);
88  return false;
89  }
90 };
91 
97 template< class TSEQ, class KeyTypeContainer >
98 bool isMappingMulti(const KeyTypeContainer& keys)
99 {
101 }
102 
106 template< class KeyTypeContainer >
108 {
109  //------------------------------------------------------------------------------
110 
111  static bool evaluate(typename KeyTypeContainer::const_iterator& begin,
112  typename KeyTypeContainer::const_iterator& end)
113  {
114  assert( begin == end ); // assertion fails iff TypeList & KeyType container does not have the same size
115  return true; // an empty typelist with an emty keyType matches
116  }
117 };
118 
124 template< class TSEQ, class KeyTypeContainer >
125 struct
127 {
128  static bool evaluate(typename KeyTypeContainer::const_iterator& begin,
129  typename KeyTypeContainer::const_iterator& end);
130 
131  //------------------------------------------------------------------------------
132 
133  static bool evaluate(const KeyTypeContainer& keys)
134  {
135 
136  namespace mpl = ::boost::mpl;
137 
138  if ( keys.size() != static_cast<unsigned long>(mpl::size<TSEQ>::value) )
139  {
140  std::string msg("isMappingMulti TypeList & KeyType container does not have the same size !!!");
141  throw std::invalid_argument(msg);
142  return false;
143  }
144 
145  typename KeyTypeContainer::const_iterator begin = keys.begin(); // needed to have cste ptr
146  typename KeyTypeContainer::const_iterator end = keys.end();
148 
149  }
150 
151 };
152 
153 //------------------------------------------------------------------------------
154 
155 template< class TSEQ, class KeyTypeContainer >
156 bool
157 isMappingMultiMPLHelper<TSEQ, KeyTypeContainer>::evaluate(typename KeyTypeContainer::const_iterator& begin,
158  typename KeyTypeContainer::const_iterator& end)
159 {
160  namespace mpl = ::boost::mpl;
161 
162  typedef BOOST_DEDUCED_TYPENAME mpl::front<TSEQ>::type Head;
163  typedef BOOST_DEDUCED_TYPENAME mpl::pop_front<TSEQ>::type Tail;
164 
165  typedef BOOST_DEDUCED_TYPENAME mpl::if_<
166  mpl::empty<Tail>,
169  >::type typex;
170 
171  bool firstKeyIsOK = isMapping< Head >( *begin ); // call a isMapping with a single key
172 
173  if ( firstKeyIsOK == false ) // OPTIMISATION
174  {
175  return false; // the first key doesn't match : do not try to test other
176  }
177 
178  bool otherKeys = typex::evaluate( ++begin, end );
179  return firstKeyIsOK && otherKeys;
180 
181 }
182 
183 } // namespace fwTools
184 
185 #endif /*__FWTOOLS_TYPEMAPPING_HPP__*/
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
bool isMappingMulti(const KeyTypeContainer &keys)
Test whatever a typelist is mapping a container of KeyType.
Definition: TypeMapping.hpp:98
an helper to isMapping() using iterator
bool isMapping(const KeyType_or_KeyTypeContainer &key)
Create a type (T) binding/mapping with a KeyType ( std::string, ipop::PixelType etc...
Definition: TypeMapping.hpp:56
static bool evaluate(const KeyType &key)
this function is called iff TSingle_or_TSEQ is not a sequence and isMapping<SingleType> ...
Definition: TypeMapping.hpp:80
an isMapping() helper : This function is called iff TSingle_or_TSEQ is not a sequence and isMapping<S...
Definition: TypeMapping.hpp:30
an helper to isMapping() using iterator : this class is called when TSEQ is a sequence. it is recursivelly called with head element removed from TSEQ
Definition: TypeMapping.hpp:27