2008-07-30 03:56:13 +02:00
|
|
|
/*
|
|
|
|
|
UTIL.hpp - metaprogramming helpers and utilities
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2008-07-30 03:56:13 +02:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2008-07-30 03:56:13 +02:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
2008-07-30 03:56:13 +02:00
|
|
|
This program 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 General Public License for more details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2008-07-30 03:56:13 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2008-07-30 03:56:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2011-12-30 03:45:10 +01:00
|
|
|
/** @file util.hpp
|
|
|
|
|
** Simple and lightweight helpers for metaprogramming and type detection.
|
|
|
|
|
** This header is a collection of very basic type detection and metaprogramming utilities.
|
|
|
|
|
** @warning indirectly, this header gets included into the majority of compilation units.
|
|
|
|
|
** Avoid anything here which increases compilation times or adds much debugging info.
|
|
|
|
|
**
|
|
|
|
|
** @see MetaUtils_test
|
|
|
|
|
** @see trait.hpp
|
|
|
|
|
** @see typelist.hpp
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2011-12-03 02:56:50 +01:00
|
|
|
#ifndef LIB_META_UTIL_H
|
|
|
|
|
#define LIB_META_UTIL_H
|
2008-07-30 03:56:13 +02:00
|
|
|
|
2012-01-07 03:11:51 +01:00
|
|
|
|
2016-01-05 20:10:20 +01:00
|
|
|
#include <typeinfo>
|
|
|
|
|
|
|
|
|
|
namespace std { // forward declaration for std::string...
|
|
|
|
|
|
|
|
|
|
template<typename C>
|
|
|
|
|
struct char_traits;
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
class allocator;
|
|
|
|
|
|
|
|
|
|
template<typename c, class TRAIT, class _ALLO>
|
|
|
|
|
class basic_string;
|
|
|
|
|
|
|
|
|
|
using string = basic_string<char, char_traits<char>, allocator<char>>;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-07 03:11:51 +01:00
|
|
|
|
|
|
|
|
|
2011-12-03 02:56:50 +01:00
|
|
|
namespace lib {
|
|
|
|
|
namespace meta {
|
2012-01-07 03:11:51 +01:00
|
|
|
|
|
|
|
|
|
2016-01-05 20:10:20 +01:00
|
|
|
/** helper types to detect the overload resolution chosen by the compiler */
|
2012-01-07 03:11:51 +01:00
|
|
|
|
2008-08-03 16:47:38 +02:00
|
|
|
typedef char Yes_t;
|
2011-12-30 03:45:10 +01:00
|
|
|
struct No_t { char more_than_one[4]; };
|
2012-01-07 03:11:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-30 03:56:13 +02:00
|
|
|
|
2011-12-03 02:56:50 +01:00
|
|
|
|
2011-12-30 03:45:10 +01:00
|
|
|
/** detect possibility of a conversion to string.
|
2013-09-01 17:36:05 +02:00
|
|
|
* Naive implementation just trying the direct conversion.
|
2011-12-30 03:45:10 +01:00
|
|
|
* The embedded constant #value will be true in case this succeeds.
|
|
|
|
|
* Might fail in more tricky situations (references, const, volatile)
|
2016-01-05 20:10:20 +01:00
|
|
|
* @see \ref format-obj.hpp more elaborate solution including lexical_cast
|
2011-12-30 03:45:10 +01:00
|
|
|
*/
|
|
|
|
|
template<typename T>
|
2011-12-30 05:13:27 +01:00
|
|
|
struct can_convertToString
|
2011-12-30 03:45:10 +01:00
|
|
|
{
|
|
|
|
|
static T & probe();
|
|
|
|
|
|
|
|
|
|
static Yes_t check(std::string);
|
|
|
|
|
static No_t check(...);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
static const bool value = (sizeof(Yes_t)==sizeof(check(probe())));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2011-12-31 01:30:07 +01:00
|
|
|
/** strip const from type: naive implementation */
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct UnConst
|
|
|
|
|
{
|
|
|
|
|
typedef T Type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct UnConst<const T>
|
|
|
|
|
{
|
|
|
|
|
typedef T Type;
|
|
|
|
|
};
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct UnConst<const T *>
|
|
|
|
|
{
|
|
|
|
|
typedef T* Type;
|
|
|
|
|
};
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct UnConst<T * const>
|
|
|
|
|
{
|
|
|
|
|
typedef T* Type;
|
|
|
|
|
};
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct UnConst<const T * const>
|
|
|
|
|
{
|
|
|
|
|
typedef T* Type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-03 02:56:50 +01:00
|
|
|
|
|
|
|
|
/** Trait template for detecting a typelist type.
|
|
|
|
|
* For example, this allows to write specialisations with the help of
|
|
|
|
|
* boost::enable_if
|
|
|
|
|
*/
|
|
|
|
|
template<typename TY>
|
|
|
|
|
class is_Typelist
|
|
|
|
|
{
|
|
|
|
|
template<class X>
|
|
|
|
|
static Yes_t check(typename X::List *);
|
|
|
|
|
template<class>
|
|
|
|
|
static No_t check(...);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
static const bool value = (sizeof(Yes_t)==sizeof(check<TY>(0)));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::meta
|
2008-07-30 03:56:13 +02:00
|
|
|
#endif
|