/usr/include/camp/valuemapper.hpp is in libcamp0.7-dev 0.7.1.1-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | /****************************************************************************
**
** Copyright (C) 2009-2010 TECHNOGERMA Systems France and/or its subsidiary(-ies).
** Contact: Technogerma Systems France Information (contact@technogerma.fr)
**
** This file is part of the CAMP library.
**
** CAMP is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CAMP is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with CAMP. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#ifndef CAMP_VALUEMAPPER_HPP
#define CAMP_VALUEMAPPER_HPP
#include <camp/enum.hpp>
#include <camp/enumobject.hpp>
#include <camp/userobject.hpp>
#include <camp/arraymapper.hpp>
#include <camp/errors.hpp>
#ifndef Q_MOC_RUN
#include <boost/lexical_cast.hpp>
#include <boost/type_traits.hpp>
#include <boost/utility/enable_if.hpp>
#endif
namespace camp_ext
{
template <typename T, typename C = void> struct ValueMapper;
}
namespace camp
{
/**
* \brief Map a C++ type to a CAMP type
*
* This function simply returns the mapping defined by ValueMapper (i.e. \c ValueMapper<T>::type).
*
* \return CAMP type which T maps to
*/
template <typename T>
Type mapType()
{
return static_cast<Type>(camp_ext::ValueMapper<T>::type);
}
} // namespace camp
namespace camp_ext
{
/**
* \class ValueMapper
*
* \brief Template providing a mapping between C++ types/values and CAMP types/values
*
* ValueMapper<T> defines three things in order to make T fully compliant with the system:
*
* \li The abstract CAMP type that T is mapped to
* \li A function to convert from T to the mapped CAMP type
* \li A function to convert from all supported CAMP types to T
*
* ValueMapper is specialized for every supported type, and can be specialized
* for any of your own types in order to extend the system.
*
* Here is an example of mapping for a custom string class:
*
* \code
* namespace camp_ext
* {
* template <>
* struct ValueMapper<MyStringClass>
* {
* // The corresponding CAMP type is "string"
* static const int type = camp::string;
*
* // Convert from MyStringClass to std::string
* static std::string to(const MyStringClass& source)
* {
* return source.to_std_string();
* }
*
* // Convert from any type to MyStringClass
* // Be smart, juste reuse ValueMapper<std::string> :)
* template <typename T>
* static MyStringClass from(const T& source)
* {
* return MyStringClass(ValueMapper<std::string>::from(source));
* }
* };
* }
* \endcode
*/
/*
* Generic version of ValueMapper -- T doesn't match with any specialization
* and is thus treated as a user object
*/
template <typename T, typename C>
struct ValueMapper
{
static const int type = camp::userType;
static camp::UserObject to(const T& source) {return camp::UserObject(source);}
static T from(bool) {CAMP_ERROR(camp::BadType(camp::boolType, camp::mapType<T>()));}
static T from(long) {CAMP_ERROR(camp::BadType(camp::intType, camp::mapType<T>()));}
static T from(double) {CAMP_ERROR(camp::BadType(camp::realType, camp::mapType<T>()));}
static T from(const std::string&) {CAMP_ERROR(camp::BadType(camp::stringType, camp::mapType<T>()));}
static T from(const camp::EnumObject&) {CAMP_ERROR(camp::BadType(camp::enumType, camp::mapType<T>()));}
static T from(const camp::UserObject& source) {return source.get<T>();}
};
/*
* Specialization of ValueMapper for abstract types
*/
template <typename T>
struct ValueMapper<T, typename boost::enable_if<boost::is_abstract<T> >::type>
{
static const int type = camp::userType;
static camp::UserObject to(const T& source) {return camp::UserObject(source);}
};
/*
* Specialization of ValueMapper for booleans
*/
template <>
struct ValueMapper<bool>
{
static const int type = camp::boolType;
static bool to(bool source) {return source;}
static bool from(bool source) {return source;}
static bool from(long source) {return source != 0;}
static bool from(double source) {return source != 0.;}
static bool from(const std::string& source)
{
bool result;
if(source.compare("true") == 0)
{
result = true;
}
else if(source.compare("false") == 0)
{
result = false;
}
else
{
result = ::boost::lexical_cast<bool>(source);
}
return result;
}
static bool from(const camp::EnumObject& source) {return source.value() != 0;}
static bool from(const camp::UserObject& source) {return source.pointer() != 0;}
};
/*
* Specialization of ValueMapper for integers
*/
template <typename T>
struct ValueMapper<T, typename boost::enable_if_c<boost::is_integral<T>::value
&& !boost::is_const<T>::value // to avoid conflict with ValueMapper<const T>
&& !boost::is_reference<T>::value // to avoid conflict with ValueMapper<T&>
>::type>
{
static const int type = camp::intType;
static long to(T source) {return static_cast<long>(source);}
static T from(bool source) {return static_cast<T>(source);}
static T from(long source) {return static_cast<T>(source);}
static T from(double source) {return static_cast<T>(source);}
static T from(const std::string& source) {return boost::lexical_cast<T>(source);}
static T from(const camp::EnumObject& source) {return static_cast<T>(source.value());}
static T from(const camp::UserObject&) {CAMP_ERROR(camp::BadType(camp::userType, camp::intType));}
};
/*
* Specialization of ValueMapper for reals
*/
template <typename T>
struct ValueMapper<T, typename boost::enable_if_c<boost::is_float<T>::value
&& !boost::is_const<T>::value // to avoid conflict with ValueMapper<const T>
&& !boost::is_reference<T>::value // to avoid conflict with ValueMapper<T&>
>::type>
{
static const int type = camp::realType;
static double to(T source) {return static_cast<double>(source);}
static T from(bool source) {return static_cast<T>(source);}
static T from(long source) {return static_cast<T>(source);}
static T from(double source) {return static_cast<T>(source);}
static T from(const std::string& source) {return boost::lexical_cast<T>(source);}
static T from(const camp::EnumObject& source) {return static_cast<T>(source.value());}
static T from(const camp::UserObject&) {CAMP_ERROR(camp::BadType(camp::userType, camp::realType));}
};
/*
* Specialization of ValueMapper for std::string
*/
template <>
struct ValueMapper<std::string>
{
static const int type = camp::stringType;
static const std::string& to(const std::string& source) {return source;}
static std::string from(bool source) {return boost::lexical_cast<std::string>(source);}
static std::string from(long source) {return boost::lexical_cast<std::string>(source);}
static std::string from(double source) {return boost::lexical_cast<std::string>(source);}
static std::string from(const std::string& source) {return source;}
static std::string from(const camp::EnumObject& source) {return source.name();}
static std::string from(const camp::UserObject&) {CAMP_ERROR(camp::BadType(camp::userType, camp::stringType));}
};
/*
* Specialization of ValueMapper for arrays.
* No conversion allowed, only type mapping is provided.
*
* Warning: special case for char[] and const char[], they are strings not arrays
*/
template <typename T>
struct ValueMapper<T, typename boost::enable_if_c<camp::detail::IsArray<T>::value
&& !boost::is_same<typename camp_ext::ArrayMapper<T>::ElementType, char>::value
&& !boost::is_same<typename camp_ext::ArrayMapper<T>::ElementType, const char>::value
>::type>
{
static const int type = camp::arrayType;
};
/*
* Specializations of ValueMapper for char arrays.
* Conversion to char[N] is disabled (can't return an array).
*/
template <int N>
struct ValueMapper<char[N]>
{
static const int type = camp::stringType;
static std::string to(const char source[N]) {return std::string(source);}
};
template <int N>
struct ValueMapper<const char[N]>
{
static const int type = camp::stringType;
static std::string to(const char source[N]) {return std::string(source);}
};
/*
* Specialization of ValueMapper for enum types
*/
template <typename T>
struct ValueMapper<T, typename boost::enable_if_c<boost::is_enum<T>::value
&& !boost::is_const<T>::value // to avoid conflict with ValueMapper<const T>
&& !boost::is_reference<T>::value // to avoid conflict with ValueMapper<T&>
>::type>
{
static const int type = camp::enumType;
static camp::EnumObject to(T source) {return camp::EnumObject(source);}
static T from(bool source) {return static_cast<T>(static_cast<long>(source));}
static T from(long source) {return static_cast<T>(source);}
static T from(double source) {return static_cast<T>(static_cast<long>(source));}
static T from(const camp::EnumObject& source) {return static_cast<T>(source.value());}
static T from(const camp::UserObject&) {CAMP_ERROR(camp::BadType(camp::userType, camp::enumType));}
// The string -> enum conversion involves a little more work:
// we try two different conversions (as a name and as a value)
static T from(const std::string& source)
{
// Get the metaenum of T, if any
const camp::Enum* metaenum = camp::enumByTypeSafe<T>();
// First try as a name
if (metaenum && metaenum->hasName(source))
return static_cast<T>(metaenum->value(source));
// Then try as a number
long value = boost::lexical_cast<long>(source);
if (!metaenum || metaenum->hasValue(value))
return static_cast<T>(value);
// Not a valid enum name or number: throw an error
CAMP_ERROR(camp::BadType(camp::stringType, camp::enumType));
}
};
/*
* Specialization of ValueMapper for EnumObject
*/
template <>
struct ValueMapper<camp::EnumObject>
{
static const int type = camp::enumType;
static const camp::EnumObject& to(const camp::EnumObject& source) {return source;}
static const camp::EnumObject& from(const camp::EnumObject& source) {return source;}
static camp::UserObject from(bool) {CAMP_ERROR(camp::BadType(camp::boolType, camp::enumType));}
static camp::UserObject from(long) {CAMP_ERROR(camp::BadType(camp::intType, camp::enumType));}
static camp::UserObject from(double) {CAMP_ERROR(camp::BadType(camp::realType, camp::enumType));}
static camp::UserObject from(const std::string&) {CAMP_ERROR(camp::BadType(camp::stringType, camp::enumType));}
static camp::UserObject from(const camp::UserObject&) {CAMP_ERROR(camp::BadType(camp::enumType, camp::enumType));}
};
/*
* Specialization of ValueMapper for UserObject
*/
template <>
struct ValueMapper<camp::UserObject>
{
static const int type = camp::userType;
static const camp::UserObject& to(const camp::UserObject& source) {return source;}
static const camp::UserObject& from(const camp::UserObject& source) {return source;}
static camp::UserObject from(bool) {CAMP_ERROR(camp::BadType(camp::boolType, camp::userType));}
static camp::UserObject from(long) {CAMP_ERROR(camp::BadType(camp::intType, camp::userType));}
static camp::UserObject from(double) {CAMP_ERROR(camp::BadType(camp::realType, camp::userType));}
static camp::UserObject from(const std::string&) {CAMP_ERROR(camp::BadType(camp::stringType, camp::userType));}
static camp::UserObject from(const camp::EnumObject&) {CAMP_ERROR(camp::BadType(camp::enumType, camp::userType));}
};
/*
* Specialization of ValueMapper for const T& -- just forward to ValueMapper<T>
*/
template <typename T>
struct ValueMapper<const T&> : public ValueMapper<T>
{
};
/*
* Specialization of ValueMapper for const T -- just forward to ValueMapper<T>
*/
template <typename T>
struct ValueMapper<const T> : public ValueMapper<T>
{
};
/*
* Specialization of ValueMapper for void.
* Conversion to void should never happen, the only aim of this
* specialization is to define the proper type mapping.
*/
template <>
struct ValueMapper<void>
{
static const int type = camp::noType;
};
/*
* Specialization of ValueMapper for NoType.
* Conversion to NoType should never happen, the only aim of this
* specialization is to define the proper mapped type.
*/
template <>
struct ValueMapper<camp::NoType>
{
static const int type = camp::noType;
};
/*
* Specialization of ValueMapper for non-const references.
* Conversions to non-const references are disabled (can't return a temporary by reference)
*/
template <typename T>
struct ValueMapper<T&> : public ValueMapper<T>
{
template <typename U>
static T& from(const U&)
{
// If you get this error, it means you're trying to cast
// a camp::Value to a non-const reference type, which is not allowed
return U::CONVERSION_TO_NON_CONST_REFERENCE_IS_NOT_ALLOWED();
}
};
/*
* Specialization of ValueMapper for const char*.
* Conversions to const char* are disabled (can't return a pointer to a temporary)
*/
template <>
struct ValueMapper<const char*>
{
static const int type = camp::stringType;
static std::string to(const char* source) {return std::string(source);}
template <typename T>
static const char* from(const T&)
{
// If you get this error, it means you're trying to cast
// a camp::Value to a const char*, which is not allowed
return T::CONVERSION_TO_CONST_CHAR_PTR_IS_NOT_ALLOWED();
}
};
} // namespace camp_ext
#endif // CAMP_VALUEMAPPER_HPP
|