Bromeon
Resources/ResourceKeyTraits.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_RESOURCEKEYTRAITS_HPP
00030 #define THOR_RESOURCEKEYTRAITS_HPP
00031 
00032 #include <Thor/Config.hpp>
00033 #include <Thor/Detail/Metaprogramming.hpp>
00034 
00035 #include THOR_TR1_HEADER(type_traits)
00036 #include <string>
00037 
00038 
00039 namespace sf
00040 {
00041 
00042     class Image;
00043     class Font;
00044     class Shader;
00045     class Music;
00046     class SoundBuffer;
00047 
00048 } // namespace sf
00049 
00050 
00051 namespace thor
00052 {   
00053 
00054 template <typename T>
00055 class MovedPtr;
00056 
00057 namespace Resources
00058 {
00059 
00060     class ImageKey;
00061     class FontKey;
00062     class ShaderKey;
00063     class SoundBufferKey;
00064     class MusicKey;
00065 
00066 } // namespace Resources
00067 } // namespace thor
00068 
00069 // ---------------------------------------------------------------------------------------------------------------------------
00070 
00071 
00072 namespace thor
00073 {
00074 
00077 
00078 namespace Resources
00079 {
00080 
00094     template <class Resource>
00095     struct KeyTraits;
00096 
00097 } // namespace Resources
00098 
00099 
00105 #define THOR_MAP_RESOURCEKEY(Resource, ResourceKey)     \
00106 namespace thor                                          \
00107 {                                                       \
00108 namespace Resources                                     \
00109 {                                                       \
00110     template <>                                         \
00111     struct KeyTraits< Resource >                        \
00112     {                                                   \
00113         typedef ResourceKey Type;                       \
00114     };                                                  \
00115 }                                                       \
00116 } 
00117 
00119 
00120 } // namespace thor
00121 
00122 // Map SFML resources to built-in keys
00123 THOR_MAP_RESOURCEKEY(sf::Image,         ImageKey)
00124 THOR_MAP_RESOURCEKEY(sf::Font,          FontKey)
00125 THOR_MAP_RESOURCEKEY(sf::Shader,        ShaderKey)
00126 THOR_MAP_RESOURCEKEY(sf::SoundBuffer,   SoundBufferKey)
00127 THOR_MAP_RESOURCEKEY(sf::Music,         MusicKey)
00128 
00129 // ---------------------------------------------------------------------------------------------------------------------------
00130 
00131 
00132 // Helper functions to access key strings
00133 namespace thor
00134 {
00135 namespace detail
00136 {
00137     // Resource key string accessors for built-in keys
00138     template <bool BuiltIn>
00139     struct KeyInfoAccessor
00140     {
00141         template <class Key>
00142         static const std::string& Get(const Key& key)
00143         {
00144             return key.mKey;
00145         }
00146     };
00147 
00148     // Resource key string accessors for other keys
00149     template <>
00150     struct KeyInfoAccessor<false>
00151     {
00152         template <class Key>
00153         static const std::string& Get(const Key&)
00154         {
00155             static const std::string emptyString = "";
00156             return emptyString;
00157         }
00158     };
00159 
00160     // Access the resource key string
00161     template <class Key>
00162     const std::string& GetKeyInfo(const Key& key)
00163     {
00164         using std::tr1::is_same;
00165 
00166         const bool builtin =
00167             is_same<Key, Resources::ImageKey>::value
00168          || is_same<Key, Resources::FontKey>::value
00169          || is_same<Key, Resources::ShaderKey>::value
00170          || is_same<Key, Resources::SoundBufferKey>::value
00171          || is_same<Key, Resources::MusicKey>::value;
00172 
00173         return KeyInfoAccessor<builtin>::Get(key);
00174     }
00175 
00176 } // namespace detail
00177 } // namespace thor
00178 
00179 #endif // THOR_RESOURCEKEYTRAITS_HPP