Bromeon
Tools/SingleDispatcher.hpp
Go to the documentation of this file.
00001 
00002 //
00003 // Thor C++ Library
00004 // Copyright (c) 2011-2012 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/Exceptions.hpp>
00034 #include <Thor/Tools/ForEach.hpp>
00035 #include <Thor/Tools/Rtti.hpp>
00036 #include <Thor/SmartPtr/CopiedPtr.hpp>
00037 #include <Thor/Detail/UnaryFunction.hpp>
00038 #include <Thor/Detail/AssociativeHelpers.hpp>
00039 #include <Thor/Detail/Metaprogramming.hpp>
00040 #include <Thor/Detail/TypeInfo.hpp>
00041 
00042 #include <vector>
00043 #include <algorithm>
00044 #include <cassert>
00045 
00046 
00047 namespace thor
00048 {
00049 
00052 
00063 template <class B, typename R = void>
00064 class SingleDispatcher : private NonCopyable
00065 {
00066     // ---------------------------------------------------------------------------------------------------------------------------
00067     // Static assertions
00068 
00069     // Make sure that B is either T* or T&, where T is a polymorphic base class (containing virtual functions).
00070     THOR_STATIC_ASSERT( 
00071         std::tr1::is_pointer<B>::value && std::tr1::is_polymorphic< typename std::tr1::remove_pointer<B>::type >::value
00072      || std::tr1::is_reference<B>::value && std::tr1::is_polymorphic< typename std::tr1::remove_reference<B>::type >::value )
00073 
00074      
00075     // ---------------------------------------------------------------------------------------------------------------------------
00076     // Public member functions
00077     public:
00083         explicit                    SingleDispatcher(bool supportDerivedToBase = false);
00084 
00101         template <class D>
00102         void                        Register(R (*globalFunction)( THOR_REPLICATE(B,D) ));
00103 
00125         template <class D, class C>
00126         void                        Register(R (C::*memberFunction)( THOR_REPLICATE(B,D) ), C& object);
00127 
00146         template <class D, typename Fn>
00147         void                        Register(const Fn& functionObject);
00148 
00156         R                           Call(B arg) const;
00157 
00158 
00159     // ---------------------------------------------------------------------------------------------------------------------------
00160     // Private types
00161     private:
00162         typedef detail::TypeInfo                                            TypeInfo;
00163         typedef TypeInfo                                                    Key;
00164         typedef CopiedPtr<detail::UnaryFunctionBase<B, R>, DynamicCopy>     Value;
00165         typedef detail::KeyValuePair<Key, Value>                            Pair;
00166         typedef std::vector<Pair>                                           FnMap;
00167 
00168 
00169     // ---------------------------------------------------------------------------------------------------------------------------
00170     // Private member functions
00171     private:
00172         // Registers the type-id key with its associated function value
00173         void                            InternalRegister(FnMap& fnMap, TypeInfo key, Value value) const;
00174 
00175         // Finds the key in the map. Returns end() if not found.
00176         typename FnMap::const_iterator  Find(TypeInfo key, bool useCache = false) const;
00177 
00178         // Make sure the cached map is updated
00179         void                            EnsureCacheUpdate() const;
00180 
00181 
00182     // ---------------------------------------------------------------------------------------------------------------------------
00183     // Private variables
00184     private:
00185         FnMap                       mMap;
00186         mutable FnMap               mCachedMap;
00187         mutable bool                mNeedsCacheUpdate;
00188         bool                        mDerivedToBase;
00189 };
00190 
00212 
00214 
00215 } // namespace thor
00216 
00217 #include <Thor/Detail/SingleDispatcher.inl>
00218 #endif // THOR_SINGLEDISPATCHER_HPP