Back to index

nux  3.0.0
PropertyTraits.h
Go to the documentation of this file.
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 
00023 #if defined(NUX_OS_WINDOWS)
00024   #pragma warning(disable : 4355) // warning C4355: 'this' : used in base member initializer list
00025 #endif
00026 
00027 #ifndef NUXCORE_PROPERTY_TRAITS_H
00028 #define NUXCORE_PROPERTY_TRAITS_H
00029 
00030 #include <string>
00031 #include <boost/lexical_cast.hpp>
00032 #include <boost/type_traits/is_enum.hpp>
00033 
00034 #include "Color.h"
00035 
00036 
00037 namespace nux {
00038 namespace type {
00039 
00046 template <typename T, bool = boost::is_enum<T>::value>
00047 struct PropertyTrait
00048 {
00065 };
00066 
00067 
00074 template <typename T>
00075 struct serializable_impl
00076 {
00077   typedef std::pair<T, bool> ResultType;
00078 
00079   static std::string to_string_impl(T const& value)
00080   {
00081     return boost::lexical_cast<std::string>(value);
00082   }
00083 
00084   static ResultType from_string_impl(std::string const& serialized_form)
00085   {
00086     try {
00087       return ResultType(boost::lexical_cast<T>(serialized_form), true);
00088     }
00089 #if defined(NUX_OS_WINDOWS)
00090     catch (boost::bad_lexical_cast const& ) {
00091 #else
00092     catch (boost::bad_lexical_cast const& e) {
00093 #endif
00094       return std::make_pair<T, bool>(T(), false);
00095     }
00096   }
00097 
00098 };
00099 
00100 
00101 template <typename ENUM>
00102 struct PropertyTrait<ENUM, true>
00103 {
00104   typedef ENUM ValueType;
00105   typedef serializable_impl<unsigned> Serialiser;
00106   typedef std::pair<ENUM, bool> ResultType;
00107 
00108   static std::string to_string(ENUM value)
00109   {
00110       return Serialiser::to_string_impl(value);
00111   }
00112 
00113   static std::pair<ENUM, bool> from_string(std::string const& serialized_form)
00114   {
00115     Serialiser::ResultType result = Serialiser::from_string_impl(serialized_form);
00116     return ResultType(static_cast<ENUM>(result.first), result.second);
00117   }
00118 };
00119 
00120 
00121 template <>
00122 struct PropertyTrait<int>
00123 {
00124   typedef int ValueType;
00125 
00126   static std::string to_string(int value)
00127   {
00128       return serializable_impl<int>::to_string_impl(value);
00129   }
00130 
00131   static std::pair<int, bool> from_string(std::string const& serialized_form)
00132   {
00133       return serializable_impl<int>::from_string_impl(serialized_form);
00134   }
00135 };
00136 
00137 template <>
00138 struct PropertyTrait<unsigned>
00139 {
00140   typedef unsigned ValueType;
00141 
00142   static std::string to_string(unsigned value)
00143   {
00144       return serializable_impl<unsigned>::to_string_impl(value);
00145   }
00146 
00147   static std::pair<unsigned, bool> from_string(std::string const& serialized_form)
00148   {
00149       return serializable_impl<unsigned>::from_string_impl(serialized_form);
00150   }
00151 };
00152 
00153 template <>
00154 struct PropertyTrait<float>
00155 {
00156   typedef float ValueType;
00157 
00158   static std::string to_string(float value)
00159   {
00160       return serializable_impl<float>::to_string_impl(value);
00161   }
00162 
00163   static std::pair<float, bool> from_string(std::string const& serialized_form)
00164   {
00165       return serializable_impl<float>::from_string_impl(serialized_form);
00166   }
00167 };
00168 
00169 
00170 template <>
00171 struct PropertyTrait<double>
00172 {
00173   typedef double ValueType;
00174 
00175   static std::string to_string(double value)
00176   {
00177       return serializable_impl<double>::to_string_impl(value);
00178   }
00179 
00180   static std::pair<double, bool> from_string(std::string const& serialized_form)
00181   {
00182       return serializable_impl<double>::from_string_impl(serialized_form);
00183   }
00184 };
00185 
00186 
00187 template <>
00188 struct PropertyTrait<bool>
00189 {
00190   typedef bool ValueType;
00191 
00192   static std::string to_string(bool value)
00193   {
00194       return value ? "true" : "false";
00195   }
00196 
00197   static std::pair<bool, bool> from_string(std::string const& serialized_form)
00198   {
00199     if (serialized_form == "true") {
00200       return std::make_pair(true, true);
00201     } else if (serialized_form == "false") {
00202       return std::make_pair(false, true);
00203     } else {
00204       return std::make_pair(false, false);
00205     }
00206   }
00207 };
00208 
00209 
00210 template <>
00211 struct PropertyTrait<std::string>
00212 {
00213   typedef std::string ValueType;
00214 
00215   static std::string to_string(std::string const& value)
00216   {
00217       return value;
00218   }
00219 
00220   static std::pair<std::string, bool> from_string(std::string const& serialized_form)
00221   {
00222     return std::make_pair(serialized_form, true);
00223   }
00224 };
00225 
00226 
00227 // template <>
00228 // struct PropertyTrait<Color>
00229 // {
00230 //   typedef Color ValueType;
00231 
00232 //   static std::string to_string(Color value)
00233 //   {
00234 //     std::string str = std::string("[") +
00235 //       serializable_impl<float>::to_string_impl(value.red) + std::string(" ") +
00236 //       serializable_impl<float>::to_string_impl(value.green) + std::string(" ") +
00237 //       serializable_impl<float>::to_string_impl(value.blue) + std::string(" ") +
00238 //       serializable_impl<float>::to_string_impl(value.alpha) + std::string(" ") +
00239 //       std::string("]");
00240 
00241 //     return str;
00242 //   }
00243 
00244 //   static std::pair<Color, bool> from_string(std::string const& serialized_form)
00245 //   {
00246 //     // todo
00247 //     return std::make_pair<Color, bool>(color::Red, true);
00248 //   }
00249 // };
00250 
00251 }
00252 }
00253 
00254 #endif