SingleDispatcher.hpp
Go to the documentation of this file.
1 //
3 // Aurora C++ Library
4 // Copyright (c) 2012-2016 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef AURORA_SINGLEDISPATCHER_HPP
30 #define AURORA_SINGLEDISPATCHER_HPP
31 
36 #include <Aurora/Config.hpp>
37 
38 #include <unordered_map>
39 #include <functional>
40 #include <algorithm>
41 #include <cassert>
42 
43 
44 namespace aurora
45 {
46 
49 
115 template <typename Signature, class Traits = RttiDispatchTraits<Signature, 1>>
117 {
118  // ---------------------------------------------------------------------------------------------------------------------------
119  // Public types
120  public:
123  typedef typename FunctionResult<Signature>::Type Result;
124 
127  typedef typename FunctionParam<Signature, 0>::Type Parameter;
128 
131  typedef typename FunctionParam<Signature, 1>::Type UserData;
132 
133 
134  // ---------------------------------------------------------------------------------------------------------------------------
135  // Static assertions
136 
137  // Make sure that B is either T* or T&
138  static_assert(std::is_pointer<Parameter>::value || std::is_lvalue_reference<Parameter>::value,
139  "Function parameter must be a pointer or reference.");
140 
141 
142  // ---------------------------------------------------------------------------------------------------------------------------
143  // Public member functions
144  public:
147 
150 
153 
156 
167  template <typename Id, typename Fn>
168  void bind(const Id& identifier, Fn function);
169 
177  Result call(Parameter arg) const;
178 
189  Result call(Parameter arg, UserData data) const;
190 
196  void fallback(std::function<Signature> function);
197 
198 
199  // ---------------------------------------------------------------------------------------------------------------------------
200  // Private types
201  private:
202  typedef typename Traits::Key Key;
203  typedef std::function<Signature> BaseFunction;
204  typedef std::unordered_map<Key, BaseFunction> FnMap;
205 
206 
207  // ---------------------------------------------------------------------------------------------------------------------------
208  // Private variables
209  private:
210  FnMap mMap;
211  BaseFunction mFallback;
212 };
213 
215 
216 } // namespace aurora
217 
218 #include <Aurora/Dispatch/Detail/SingleDispatcher.inl>
219 #endif // AURORA_SINGLEDISPATCHER_HPP
SingleDispatcher & operator=(SingleDispatcher &&source)
Move assignment operator.
void bind(const Id &identifier, Fn function)
Registers a function bound to a specific key.
Utilities for template metaprogramming.
FunctionResult< Signature >::Type Result
Function return type.
Definition: SingleDispatcher.hpp:123
Result call(Parameter arg) const
Dispatches the key of arg and invokes the corresponding function.
Definition of Aurora exception classes.
FunctionParam< Signature, 1 >::Type UserData
Addition parameter for user data, only useful if Signature contains more than 1 parameter.
Definition: SingleDispatcher.hpp:131
Non-copyable base class.
Definition: NonCopyable.hpp:42
Configuration header of the library.
Class template aurora::DispatchTraits.
~SingleDispatcher()
Destructor.
void fallback(std::function< Signature > function)
Registers a fallback function.
SingleDispatcher()
Default constructor.
Class that is able to perform dynamic dispatch on multiple functions with one parameter.
Definition: SingleDispatcher.hpp:116
Class aurora::NonCopyable.
FunctionParam< Signature, 0 >::Type Parameter
Function parameter type denoting the object used for the dispatch.
Definition: SingleDispatcher.hpp:127
Definition: DispatchTraits.hpp:39