fw4spl
Dispatcher.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
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_DISPATCHER_HPP__
8 #define __FWTOOLS_DISPATCHER_HPP__
9 
10 #include <stdexcept>
11 #include <boost/mpl/if.hpp>
12 #include <boost/mpl/empty.hpp>
13 #include <boost/mpl/front.hpp>
14 #include <boost/mpl/pop_front.hpp>
15 
16 #include <fwCore/macros.hpp>
17 
18 #include "fwTools/TypeMapping.hpp"
19 #include "fwTools/Stringizer.hpp"
20 
21 
22 namespace fwTools
23 {
24 
25 
34 {
35 
37  static void invoke()
38  {
39  }
40 
42  template< class KeyType>
43  static void invoke(const KeyType &keytype)
44  {
45  std::string msg = ::fwTools::getString(keytype) +
46  " : KeyType value incorrect : no corresponding Type in typelist";
47  throw std::invalid_argument(msg);
48  }
49 
51  template< class KeyType,class Parameter>
52  static void invoke( const KeyType &keytype,const Parameter &param )
53  {
54  FwCoreNotUsedMacro(param);
55  std::string msg = ::fwTools::getString(keytype) +
56  " : KeyType value incorrect : no corresponding Type in typelist";
57  throw std::invalid_argument(msg);
58  }
59 
61  template< class BaseClass, class KeyType>
62  static BaseClass *instanciate(const KeyType &keytype)
63  {
64  std::string msg = ::fwTools::getString(keytype) +
65  " : KeyType value incorrect : no corresponding Type in typelist";
66  throw std::invalid_argument(msg);
67  return NULL;
68  }
69 
70 };
71 
72 
73 
74 
84 template< class TSEQ, class FUNCTOR >
85 struct Dispatcher
86 {
87 
88  private:
89  typedef BOOST_DEDUCED_TYPENAME boost::mpl::pop_front<TSEQ>::type Tail;
90  typedef BOOST_DEDUCED_TYPENAME boost::mpl::front<TSEQ>::type Head;
91 
92  public:
93 
94 
95 
99  static void invoke()
100  {
101  namespace mpl = boost::mpl;
102 
103  // create the functor then excute it
104  FUNCTOR f;
105 #ifdef _WIN32
106  f.operator()<Head>();
107 #else
108  f.template operator()<Head>();
109 #endif
110 
111 
112  // recursively call other element in the list
113  typedef BOOST_DEDUCED_TYPENAME mpl::if_<
114  mpl::empty<Tail>,
117  >::type typex;
118  typex::invoke();
119  }
120 
121 
122 
126  template< class KeyType >
127  static void invoke( const KeyType &keytype )
128  {
129  namespace mpl = boost::mpl;
130 
131  if ( isMapping< Head>(keytype) )
132  {
133  // create the functor then excute it
134  FUNCTOR f;
135 #ifdef _WIN32
136  f.operator()<Head>();
137 #else
138  f.template operator()<Head>();
139 #endif
140  }
141  else
142  {
143  // recursively call other element in the list
144  typedef BOOST_DEDUCED_TYPENAME mpl::if_<
145  mpl::empty<Tail>,
148  >::type typex;
149  typex::invoke(keytype);
150 
151  }
152  }
153  // NOTE gcc seems unable to explicit call of static template fonction member :/
154  // all arguments needs to be present specicied template seems ignored
155 
156 
161  template< class KeyType,class Parameter >
162  static void invoke( const KeyType &keytype, Parameter &param )
163  {
164  namespace mpl = boost::mpl;
165 
166  if ( isMapping< Head>(keytype) )
167  {
168  // create the functor then excute it
169  FUNCTOR f;
170 #ifdef _WIN32
171  f.operator()<Head>(param);
172 #else
173  f.template operator()<Head>(param);
174 #endif
175 
176  }
177  else
178  {
179  // recursively call other element in the list
180  typedef BOOST_DEDUCED_TYPENAME mpl::if_<
181  mpl::empty<Tail>,
184  >::type typex;
185  typex::invoke(keytype,param);
186 
187  }
188  }
189 
190 
191 };
192 
193 
194 } //end namespace fwTools
195 
196 #endif /*__FWTOOLS_DISPATCHER_HPP__*/
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
This file defines fwCore base macros.
static void invoke()
Perform nothing see Dispatcher<>::invoke()
Definition: Dispatcher.hpp:37
static void invoke(const KeyType &keytype)
Throw an exception to inform end-user that KeyType value have no correspondance in type list...
Definition: Dispatcher.hpp:43
static void invoke(const KeyType &keytype)
Invoke only the specified Type only.
Definition: Dispatcher.hpp:127
static void invoke()
Instanciate and invoke all functors.
Definition: Dispatcher.hpp:99
static void invoke(const KeyType &keytype, const Parameter &param)
Throw an exception to inform end-user that KeyType value have no correspondance in type list...
Definition: Dispatcher.hpp:52
Create an automatic template instancier exple Dispatcher< TYPESEQUENCE , FUNCTOR>::invoke("int");.
Definition: Dispatcher.hpp:85
static BaseClass * instanciate(const KeyType &keytype)
Throw an exception to inform end-user that KeyType value have no correspondance in type list...
Definition: Dispatcher.hpp:62
Limit cases for empty typelist.
Definition: Dispatcher.hpp:33
static void invoke(const KeyType &keytype, Parameter &param)
Invoke only the specified Type only with a fixed parameter.
Definition: Dispatcher.hpp:162