nux-1.14.0
PropertyTraits.h
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright 2011 Inalogic® Inc.
00004  *
00005  * This program is free software: you can redistribute it and/or modify it
00006  * under the terms of the GNU Lesser General Public License, as
00007  * published by the  Free Software Foundation; either version 2.1 or 3.0
00008  * of the License.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranties of
00012  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00013  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00014  * License for more details.
00015  *
00016  * You should have received a copy of both the GNU Lesser General Public
00017  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00018  *
00019  * Authored by: Tim Penhey <tim.penhey@canonical.com>
00020  *
00021  */
00022 #ifndef NUXCORE_PROPERTY_TRAITS_H
00023 #define NUXCORE_PROPERTY_TRAITS_H
00024 
00025 #include <string>
00026 #include <boost/lexical_cast.hpp>
00027 #include <boost/type_traits/is_enum.hpp>
00028 
00029 #include "Color.h"
00030 
00031 
00032 namespace nux {
00033 namespace type {
00034 
00041 template <typename T, bool = boost::is_enum<T>::value>
00042 struct PropertyTrait
00043 {
00060 };
00061 
00062 
00069 template <typename T>
00070 struct serializable_impl
00071 {
00072   typedef std::pair<T, bool> ResultType;
00073 
00074   static std::string to_string_impl(T const& value)
00075   {
00076     return boost::lexical_cast<std::string>(value);
00077   }
00078 
00079   static ResultType from_string_impl(std::string const& serialized_form)
00080   {
00081     try {
00082       return ResultType(boost::lexical_cast<T>(serialized_form), true);
00083     }
00084 #if defined(NUX_OS_WINDOWS)
00085     catch (boost::bad_lexical_cast const& ) {
00086 #else
00087     catch (boost::bad_lexical_cast const& e) {
00088 #endif
00089       return std::make_pair<T, bool>(T(), false);
00090     }
00091   }
00092 
00093 };
00094 
00095 
00096 template <typename ENUM>
00097 struct PropertyTrait<ENUM, true>
00098 {
00099   typedef ENUM ValueType;
00100   typedef serializable_impl<unsigned> Serialiser;
00101   typedef std::pair<ENUM, bool> ResultType;
00102 
00103   static std::string to_string(ENUM value)
00104   {
00105       return Serialiser::to_string_impl(value);
00106   }
00107 
00108   static std::pair<ENUM, bool> from_string(std::string const& serialized_form)
00109   {
00110     Serialiser::ResultType result = Serialiser::from_string_impl(serialized_form);
00111     return ResultType(static_cast<ENUM>(result.first), result.second);
00112   }
00113 };
00114 
00115 
00116 template <>
00117 struct PropertyTrait<int>
00118 {
00119   typedef int ValueType;
00120 
00121   static std::string to_string(int value)
00122   {
00123       return serializable_impl<int>::to_string_impl(value);
00124   }
00125 
00126   static std::pair<int, bool> from_string(std::string const& serialized_form)
00127   {
00128       return serializable_impl<int>::from_string_impl(serialized_form);
00129   }
00130 };
00131 
00132 template <>
00133 struct PropertyTrait<unsigned>
00134 {
00135   typedef unsigned ValueType;
00136 
00137   static std::string to_string(unsigned value)
00138   {
00139       return serializable_impl<unsigned>::to_string_impl(value);
00140   }
00141 
00142   static std::pair<unsigned, bool> from_string(std::string const& serialized_form)
00143   {
00144       return serializable_impl<unsigned>::from_string_impl(serialized_form);
00145   }
00146 };
00147 
00148 template <>
00149 struct PropertyTrait<float>
00150 {
00151   typedef float ValueType;
00152 
00153   static std::string to_string(float value)
00154   {
00155       return serializable_impl<float>::to_string_impl(value);
00156   }
00157 
00158   static std::pair<float, bool> from_string(std::string const& serialized_form)
00159   {
00160       return serializable_impl<float>::from_string_impl(serialized_form);
00161   }
00162 };
00163 
00164 
00165 template <>
00166 struct PropertyTrait<double>
00167 {
00168   typedef double ValueType;
00169 
00170   static std::string to_string(double value)
00171   {
00172       return serializable_impl<double>::to_string_impl(value);
00173   }
00174 
00175   static std::pair<double, bool> from_string(std::string const& serialized_form)
00176   {
00177       return serializable_impl<double>::from_string_impl(serialized_form);
00178   }
00179 };
00180 
00181 
00182 template <>
00183 struct PropertyTrait<bool>
00184 {
00185   typedef bool ValueType;
00186 
00187   static std::string to_string(bool value)
00188   {
00189       return value ? "true" : "false";
00190   }
00191 
00192   static std::pair<bool, bool> from_string(std::string const& serialized_form)
00193   {
00194     if (serialized_form == "true") {
00195       return std::make_pair(true, true);
00196     } else if (serialized_form == "false") {
00197       return std::make_pair(false, true);
00198     } else {
00199       return std::make_pair(false, false);
00200     }
00201   }
00202 };
00203 
00204 
00205 template <>
00206 struct PropertyTrait<std::string>
00207 {
00208   typedef std::string ValueType;
00209 
00210   static std::string to_string(std::string const& value)
00211   {
00212       return value;
00213   }
00214 
00215   static std::pair<std::string, bool> from_string(std::string const& serialized_form)
00216   {
00217     return std::make_pair(serialized_form, true);
00218   }
00219 };
00220 
00221 
00222 // template <>
00223 // struct PropertyTrait<Color>
00224 // {
00225 //   typedef Color ValueType;
00226 
00227 //   static std::string to_string(Color value)
00228 //   {
00229 //     std::string str = std::string("[") +
00230 //       serializable_impl<float>::to_string_impl(value.red) + std::string(" ") +
00231 //       serializable_impl<float>::to_string_impl(value.green) + std::string(" ") +
00232 //       serializable_impl<float>::to_string_impl(value.blue) + std::string(" ") +
00233 //       serializable_impl<float>::to_string_impl(value.alpha) + std::string(" ") +
00234 //       std::string("]");
00235 
00236 //     return str;
00237 //   }
00238 
00239 //   static std::pair<Color, bool> from_string(std::string const& serialized_form)
00240 //   {
00241 //     // todo
00242 //     return std::make_pair<Color, bool>(color::Red, true);
00243 //   }
00244 // };
00245 
00246 }
00247 }
00248 
00249 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends