Bromeon
Resources/ResourceKeyTraits.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_RESOURCEKEYTRAITS_HPP
00030 #define THOR_RESOURCEKEYTRAITS_HPP
00031 
00032 #include <Thor/Config.hpp>
00033 #include <Thor/Detail/Metaprogramming.hpp>
00034 
00035 #include <string>
00036 
00037 
00038 namespace sf
00039 {
00040 
00041     class Image;
00042     class Texture;
00043     class Font;
00044     class Shader;
00045     class Music;
00046     class SoundBuffer;
00047 
00048     class InputStream;
00049 
00050 } // namespace sf
00051 
00052 
00053 namespace thor
00054 {   
00055 namespace Resources
00056 {
00057 
00058     class ImageKey;
00059     class TextureKey;
00060     class FontKey;
00061     class ShaderKey;
00062     class SoundBufferKey;
00063     class MusicKey;
00064 
00065 } // namespace Resources
00066 } // namespace thor
00067 
00068 // ---------------------------------------------------------------------------------------------------------------------------
00069 
00070 
00071 namespace thor
00072 {
00073 
00076 
00077 namespace Resources
00078 {
00079 
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::Texture,       TextureKey)
00125 THOR_MAP_RESOURCEKEY(sf::Font,          FontKey)
00126 THOR_MAP_RESOURCEKEY(sf::Shader,        ShaderKey)
00127 THOR_MAP_RESOURCEKEY(sf::SoundBuffer,   SoundBufferKey)
00128 THOR_MAP_RESOURCEKEY(sf::Music,         MusicKey)
00129 
00130 // ---------------------------------------------------------------------------------------------------------------------------
00131 
00132 
00133 // Helper functions to access key strings
00134 namespace thor
00135 {
00136 namespace detail
00137 {
00138     // Class to access the strings of resource keys
00139     struct KeyInfoAccessor
00140     {
00141         // Resource key string accessors for built-in keys
00142         template <class Key>
00143         static const std::string& Get(const Key& key, Int2Type<true>)
00144         {
00145             return key.mKey;
00146         }
00147 
00148         // Resource key string accessors for other keys
00149         template <class Key>
00150         static const std::string& Get(const Key&, Int2Type<false>)
00151         {
00152             static const std::string emptyString = "";
00153             return emptyString;
00154         }
00155     };
00156 
00157     // Access the resource key string
00158     template <class Key>
00159     const std::string& GetKeyInfo(const Key& key)
00160     {
00161         using std::tr1::is_same;
00162 
00163         const bool builtin =
00164             is_same<Key, Resources::ImageKey>::value
00165          || is_same<Key, Resources::TextureKey>::value
00166          || is_same<Key, Resources::FontKey>::value
00167          || is_same<Key, Resources::ShaderKey>::value
00168          || is_same<Key, Resources::SoundBufferKey>::value
00169          || is_same<Key, Resources::MusicKey>::value;
00170 
00171         return KeyInfoAccessor::Get(key, Int2Type<builtin>());
00172     }
00173 
00174 } // namespace detail
00175 } // namespace thor
00176 
00177 #endif // THOR_RESOURCEKEYTRAITS_HPP