Bromeon
Tools/SingleDispatcher.hpp
Go to the documentation of this file.
00001 
00002 //
00003 // Thor C++ Library
00004 // Copyright (c) 2011 Jan Haller
00005 // 
00006 // This software is provided 'as-is', without any express or implied
00007 // warranty. In no event will the authors be held liable for any damages
00008 // arising from the use of this software.
00009 // 
00010 // Permission is granted to anyone to use this software for any purpose,
00011 // including commercial applications, and to alter it and redistribute it
00012 // freely, subject to the following restrictions:
00013 // 
00014 // 1. The origin of this software must not be misrepresented; you must not
00015 //    claim that you wrote the original software. If you use this software
00016 //    in a product, an acknowledgment in the product documentation would be
00017 //    appreciated but is not required.
00018 // 
00019 // 2. Altered source versions must be plainly marked as such, and must not be
00020 //    misrepresented as being the original software.
00021 // 
00022 // 3. This notice may not be removed or altered from any source distribution.
00023 //
00025 
00028 
00029 #ifndef THOR_SINGLEDISPATCHER_HPP
00030 #define THOR_SINGLEDISPATCHER_HPP
00031 
00032 #include <Thor/Tools/NonCopyable.hpp>
00033 #include <Thor/Tools/ForEach.hpp>
00034 #include <Thor/Tools/Exceptions.hpp>
00035 #include <Thor/Detail/UnaryFunction.hpp>
00036 #include <Thor/Detail/AssociativeHelpers.hpp>
00037 #include <Thor/Detail/Metaprogramming.hpp>
00038 #include <Thor/Detail/TypeInfo.hpp>
00039 
00040 #include <vector>
00041 #include <algorithm>
00042 #include <cassert>
00043 
00044 
00045 namespace thor
00046 {
00047 
00050 
00061 template <class B, typename R = void>
00062 class SingleDispatcher : private NonCopyable
00063 {
00064     // ---------------------------------------------------------------------------------------------------------------------------
00065     // Static assertions
00066 
00067     // Make sure that B is either T* or T&, where T is a polymorphic base class (containing virtual functions).
00068     THOR_STATIC_ASSERT( 
00069         std::tr1::is_pointer<B>::value && std::tr1::is_polymorphic< typename std::tr1::remove_pointer<B>::type >::value
00070      || std::tr1::is_reference<B>::value && std::tr1::is_polymorphic< typename std::tr1::remove_reference<B>::type >::value )
00071 
00072      
00073     // ---------------------------------------------------------------------------------------------------------------------------
00074     // Public member functions
00075     public:
00078                                     SingleDispatcher();
00079 
00082                                     ~SingleDispatcher();
00083 
00100         template <class D>
00101         void                        Register(R (*globalFunction)( THOR_REPLICATE(B,D) ));
00102 
00124         template <class D, class C>
00125         void                        Register(R (C::*memberFunction)( THOR_REPLICATE(B,D) ), C& object);
00126 
00145         template <class D, typename Fn>
00146         void                        Register(const Fn& functionObject);
00147 
00153         R                           Call(B arg) const;
00154 
00155 
00156     // ---------------------------------------------------------------------------------------------------------------------------
00157     // Private types
00158     private:
00159         typedef detail::TypeInfo                    Key;
00160         typedef detail::UnaryFunctionBase<B, R>*    Value;
00161         typedef detail::KeyValuePair<Key, Value>    Pair;
00162         typedef std::vector<Pair>                   FnMap;
00163 
00164 
00165     // ---------------------------------------------------------------------------------------------------------------------------
00166     // Private member functions
00167     private:
00168         // Registers the type-id key with its associated function value
00169         void                            InternalRegister(Key key, Value value);
00170 
00171         // Finds the key in the map. Returns end() if not found.
00172         typename FnMap::const_iterator  Find(Key key) const;
00173 
00174 
00175     // ---------------------------------------------------------------------------------------------------------------------------
00176     // Private variables
00177     private:
00178         FnMap                       mMap;
00179 };
00180 
00202 
00204 
00205 } // namespace thor
00206 
00207 #include <Thor/Detail/SingleDispatcher.inl>
00208 #endif // THOR_SINGLEDISPATCHER_HPP