2009-07-19 08:03:54 +02:00
|
|
|
/*
|
|
|
|
|
TRAIT.hpp - type handling and type detection helpers
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-07-19 08:03:54 +02:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-07-19 08:03:54 +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.
|
|
|
|
|
|
2009-07-19 08:03:54 +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
|
|
|
|
2009-07-19 08:03:54 +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
|
|
|
|
2009-07-19 08:03:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2016-01-06 02:41:58 +01:00
|
|
|
/** @file trait.hpp
|
|
|
|
|
** Helpers for type detection, type rewriting and metaprogramming.
|
|
|
|
|
** This header is a collection of frequently used templates for working with types.
|
|
|
|
|
** It incurs only modest header inclusion overhead (be sure not to jeopardise that!).
|
|
|
|
|
**
|
|
|
|
|
** \par unwrapping
|
|
|
|
|
** Strip away all kinds of type adornments, like const, reference, pointer, smart-ptr.
|
2017-04-02 04:22:51 +02:00
|
|
|
** The accompanying \ref lib::meta::unwrap() function can be used to accept "stuff
|
|
|
|
|
** packaged in various forms". The \ref Strip template packages this ability in various
|
2016-01-06 02:41:58 +01:00
|
|
|
** degrees for metaprogramming
|
|
|
|
|
** @warning these helpers can be quite dangerous, as they silently break
|
|
|
|
|
** any protective barriers (including lifecycle managing smart-ptrs)
|
|
|
|
|
**
|
|
|
|
|
** \par string conversion
|
|
|
|
|
** a set of trait templates to categorise arbitrary types with respect to
|
|
|
|
|
** the ability for string conversions
|
|
|
|
|
**
|
|
|
|
|
** \par ability to iterate
|
2018-09-21 13:46:42 +02:00
|
|
|
** these traits [can be used](\ref util-foreach.hpp) to build the notion of a
|
2016-01-06 02:41:58 +01:00
|
|
|
** generic container -- basically anything that can be enumerated.
|
|
|
|
|
** Within Lumiera, we frequently use our own concept of "iterability",
|
2018-09-21 13:46:42 +02:00
|
|
|
** known as ["Lumiera Forward Iterator"](\ref iter-adapter.hpp). These
|
2016-01-06 02:41:58 +01:00
|
|
|
** helpers here allow to unify this concept with the "Range"
|
|
|
|
|
** concept from the standard library (`begin()` and `end()`)
|
|
|
|
|
**
|
|
|
|
|
** @see MetaUtils_test
|
2017-04-02 04:22:51 +02:00
|
|
|
** @see format-obj.hpp string representation for _anything_
|
|
|
|
|
** @see meta/util.hpp very basic metaprogramming helpers
|
2016-01-06 02:41:58 +01:00
|
|
|
** @see typelist.hpp
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2010-01-04 10:08:14 +01:00
|
|
|
#ifndef LIB_META_TRAIT_H
|
|
|
|
|
#define LIB_META_TRAIT_H
|
2009-07-19 08:03:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/meta/util.hpp"
|
2010-01-04 10:08:14 +01:00
|
|
|
#include "lib/meta/duck-detector.hpp"
|
2009-07-19 08:03:54 +02:00
|
|
|
|
2016-01-06 02:41:58 +01:00
|
|
|
#include <type_traits>
|
2011-12-27 01:25:09 +01:00
|
|
|
|
2009-07-19 08:03:54 +02:00
|
|
|
|
2010-01-07 03:10:02 +01:00
|
|
|
//Forward declarations for the Unwrap helper....
|
|
|
|
|
namespace boost{
|
|
|
|
|
template<class X> class reference_wrapper;
|
|
|
|
|
}
|
|
|
|
|
namespace std {
|
2011-12-27 01:25:09 +01:00
|
|
|
template<class X> class reference_wrapper;
|
2010-01-07 03:10:02 +01:00
|
|
|
template<class X> class shared_ptr;
|
2014-04-03 22:42:48 +02:00
|
|
|
}
|
2011-12-02 16:10:03 +01:00
|
|
|
namespace lib{
|
2011-12-27 01:25:09 +01:00
|
|
|
template<class X, class B> class P;
|
2016-01-27 12:38:16 +01:00
|
|
|
|
|
|
|
|
namespace hash {
|
|
|
|
|
class LuidH;
|
2016-02-07 02:20:01 +01:00
|
|
|
}
|
|
|
|
|
namespace time {
|
|
|
|
|
class TimeValue;
|
|
|
|
|
class Duration;
|
2016-01-27 12:38:16 +01:00
|
|
|
}}
|
2018-11-15 23:55:13 +01:00
|
|
|
namespace steam {
|
2010-01-07 03:10:02 +01:00
|
|
|
namespace mobject{
|
2011-12-27 01:25:09 +01:00
|
|
|
template<class X, class B> class Placement;
|
2016-01-27 12:38:16 +01:00
|
|
|
}}
|
2010-01-07 03:10:02 +01:00
|
|
|
|
2009-10-05 04:29:06 +02:00
|
|
|
|
2010-01-04 10:08:14 +01:00
|
|
|
namespace lib {
|
|
|
|
|
namespace meta {
|
2009-10-05 04:29:06 +02:00
|
|
|
|
2016-01-06 02:41:58 +01:00
|
|
|
using std::remove_cv;
|
|
|
|
|
using std::remove_pointer;
|
|
|
|
|
using std::remove_reference;
|
2018-03-22 21:43:19 +01:00
|
|
|
using std::is_pointer;
|
|
|
|
|
using std::is_base_of;
|
2016-01-06 02:41:58 +01:00
|
|
|
using std::is_convertible;
|
2016-01-27 12:38:16 +01:00
|
|
|
using std::is_constructible;
|
|
|
|
|
using std::is_floating_point;
|
2016-01-06 02:41:58 +01:00
|
|
|
using std::is_arithmetic;
|
2016-01-27 12:38:16 +01:00
|
|
|
using std::is_unsigned;
|
|
|
|
|
using std::is_signed;
|
2018-03-22 21:43:19 +01:00
|
|
|
using std::is_class;
|
2016-01-06 02:41:58 +01:00
|
|
|
using std::is_same;
|
|
|
|
|
using std::__not_;
|
|
|
|
|
using std::__and_;
|
|
|
|
|
using std::__or_;
|
2009-08-28 20:33:20 +02:00
|
|
|
|
2009-07-19 08:03:54 +02:00
|
|
|
|
2010-01-07 03:10:02 +01:00
|
|
|
/**
|
2016-01-06 02:41:58 +01:00
|
|
|
* Helper for type analysis and convenience accessors:
|
|
|
|
|
* attempts to extract a base type from various wrappers.
|
2010-01-07 04:40:10 +01:00
|
|
|
* Additionally allows to extract/deref the wrapped element.
|
|
|
|
|
* @warning strips away any const
|
2016-01-06 02:41:58 +01:00
|
|
|
* @warning also strips away smart-ptrs and lifecycle managers!
|
2010-01-07 03:10:02 +01:00
|
|
|
*/
|
|
|
|
|
template<typename X>
|
|
|
|
|
struct Unwrap
|
|
|
|
|
{
|
|
|
|
|
typedef X Type;
|
|
|
|
|
|
|
|
|
|
static X&
|
|
|
|
|
extract (X const& x)
|
|
|
|
|
{
|
|
|
|
|
return const_cast<X&> (x);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-01-06 02:41:58 +01:00
|
|
|
template<>
|
|
|
|
|
struct Unwrap<void> ///< @note we can't unwrap void!
|
|
|
|
|
{
|
|
|
|
|
typedef void Type;
|
|
|
|
|
};
|
|
|
|
|
|
2010-01-07 03:10:02 +01:00
|
|
|
template<typename X>
|
|
|
|
|
struct Unwrap<X*>
|
|
|
|
|
{
|
2016-01-06 02:41:58 +01:00
|
|
|
typedef typename std::remove_cv<X>::type Type;
|
2010-01-07 03:10:02 +01:00
|
|
|
|
2010-01-07 04:40:10 +01:00
|
|
|
static Type&
|
2010-01-07 03:10:02 +01:00
|
|
|
extract (const X* ptr)
|
|
|
|
|
{
|
|
|
|
|
ASSERT (ptr);
|
2010-01-07 04:40:10 +01:00
|
|
|
return const_cast<Type&> (*ptr);
|
2010-01-07 03:10:02 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename X>
|
2016-12-23 04:23:03 +01:00
|
|
|
struct Unwrap<boost::reference_wrapper<X>>
|
2010-01-07 03:10:02 +01:00
|
|
|
{
|
|
|
|
|
typedef X Type;
|
|
|
|
|
|
|
|
|
|
static X&
|
|
|
|
|
extract (boost::reference_wrapper<X> wrapped)
|
|
|
|
|
{
|
|
|
|
|
return wrapped;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename X>
|
2016-12-23 04:23:03 +01:00
|
|
|
struct Unwrap<std::reference_wrapper<X>>
|
2010-01-07 03:10:02 +01:00
|
|
|
{
|
|
|
|
|
typedef X Type;
|
|
|
|
|
|
|
|
|
|
static X&
|
2014-04-03 22:42:48 +02:00
|
|
|
extract (std::reference_wrapper<X> wrapped)
|
2010-01-07 03:10:02 +01:00
|
|
|
{
|
|
|
|
|
return wrapped;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename X>
|
2016-12-23 04:23:03 +01:00
|
|
|
struct Unwrap<std::shared_ptr<X>>
|
2010-01-07 03:10:02 +01:00
|
|
|
{
|
|
|
|
|
typedef X Type;
|
|
|
|
|
|
|
|
|
|
static X&
|
2014-04-03 22:42:48 +02:00
|
|
|
extract (std::shared_ptr<X> ptr)
|
2010-01-07 03:10:02 +01:00
|
|
|
{
|
|
|
|
|
ASSERT (ptr);
|
|
|
|
|
return *ptr;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename X, class B>
|
2016-12-23 04:23:03 +01:00
|
|
|
struct Unwrap<P<X, B>>
|
2010-01-07 03:10:02 +01:00
|
|
|
{
|
|
|
|
|
typedef X Type;
|
|
|
|
|
|
|
|
|
|
static X&
|
2011-12-02 16:10:03 +01:00
|
|
|
extract (P<X,B> ptr)
|
2010-01-07 03:10:02 +01:00
|
|
|
{
|
|
|
|
|
ASSERT (ptr);
|
|
|
|
|
return *ptr;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2010-01-07 04:40:10 +01:00
|
|
|
|
|
|
|
|
/** convenience shortcut: unwrapping free function.
|
|
|
|
|
* @return reference to the bare element.
|
|
|
|
|
* @warning this function is dangerous: it strips away
|
|
|
|
|
* any managing smart-ptr and any const!
|
|
|
|
|
* You might even access and return a
|
|
|
|
|
* reference to an anonymous temporary.
|
|
|
|
|
*/
|
2010-01-07 03:10:02 +01:00
|
|
|
template<typename X>
|
|
|
|
|
typename Unwrap<X>::Type&
|
|
|
|
|
unwrap (X const& wrapped)
|
|
|
|
|
{
|
|
|
|
|
return Unwrap<X>::extract(wrapped);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-07 04:40:10 +01:00
|
|
|
|
2010-01-07 03:10:02 +01:00
|
|
|
/** Helper for type analysis: tries to strip all kinds of type adornments */
|
|
|
|
|
template<typename X>
|
|
|
|
|
struct Strip
|
|
|
|
|
{
|
2016-01-06 02:41:58 +01:00
|
|
|
typedef typename std::remove_cv<X> ::type TypeUnconst;
|
|
|
|
|
typedef typename std::remove_reference<TypeUnconst>::type TypeReferred;
|
|
|
|
|
typedef typename std::remove_pointer<TypeReferred> ::type TypePointee;
|
|
|
|
|
typedef typename std::remove_cv<TypePointee> ::type TypePlain;
|
2010-01-07 03:10:02 +01:00
|
|
|
|
|
|
|
|
typedef typename Unwrap<TypePlain> ::Type Type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-01-07 04:40:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Type definition helper for pointer and reference types.
|
|
|
|
|
* Allows to create a member field and to get the basic type
|
|
|
|
|
* irrespective if the given type is plain, pointer or reference
|
|
|
|
|
*/
|
|
|
|
|
template<typename TY>
|
|
|
|
|
struct RefTraits
|
|
|
|
|
{
|
|
|
|
|
typedef TY* pointer;
|
|
|
|
|
typedef TY& reference;
|
|
|
|
|
typedef TY value_type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename TY>
|
|
|
|
|
struct RefTraits<TY *>
|
|
|
|
|
{
|
|
|
|
|
typedef TY* pointer;
|
|
|
|
|
typedef TY& reference;
|
|
|
|
|
typedef TY value_type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename TY>
|
|
|
|
|
struct RefTraits<TY &>
|
|
|
|
|
{
|
|
|
|
|
typedef TY* pointer;
|
|
|
|
|
typedef TY& reference;
|
|
|
|
|
typedef TY value_type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-06 02:41:58 +01:00
|
|
|
/* ==== Traits ==== */
|
2010-01-07 04:40:10 +01:00
|
|
|
|
2016-01-06 02:41:58 +01:00
|
|
|
/** compare unadorned types, disregarding const and references */
|
|
|
|
|
template<typename T, typename U>
|
|
|
|
|
struct is_basically
|
2017-12-02 03:37:21 +01:00
|
|
|
: is_same <typename Strip<T>::TypeReferred
|
|
|
|
|
,typename Strip<U>::TypeReferred>
|
2016-01-06 02:41:58 +01:00
|
|
|
{ };
|
2010-01-07 04:40:10 +01:00
|
|
|
|
2018-03-22 21:43:19 +01:00
|
|
|
/** verify compliance to an interface by subtype check */
|
|
|
|
|
template<typename S, typename I>
|
|
|
|
|
struct is_Subclass
|
|
|
|
|
: __or_< __and_< is_class<I>
|
|
|
|
|
, is_class<S>
|
|
|
|
|
, is_base_of<I,S>
|
|
|
|
|
>
|
|
|
|
|
, is_same<I,S>
|
|
|
|
|
>
|
|
|
|
|
{ };
|
|
|
|
|
|
2016-01-06 02:41:58 +01:00
|
|
|
/** detect various flavours of string / text data */
|
|
|
|
|
template<typename X>
|
|
|
|
|
struct is_StringLike
|
|
|
|
|
: __or_< is_basically<X, std::string>
|
|
|
|
|
, is_convertible<X, const char*>
|
|
|
|
|
>
|
|
|
|
|
{ };
|
|
|
|
|
|
|
|
|
|
/** types able to be lexically converted to string representation
|
|
|
|
|
* @note this compile-time trait can't predict if such an conversion
|
|
|
|
|
* to string will be successful at runtime; indeed it may throw,
|
|
|
|
|
* so you should additionally guard the invocation with try-catch!
|
|
|
|
|
* @remarks this template is relevant for guarding `lexical_cast<..>` expressions.
|
|
|
|
|
* Such an expression won't even compile for some types, because of missing or
|
|
|
|
|
* ambiguous output operator(s). Ideally, there would be some automatic detection
|
|
|
|
|
* (relying on the existence of an `operator<<` for the given type. But at my
|
|
|
|
|
* first attempt in 2009 (commit 1533e5bd0) I couldn't make this work, so I
|
|
|
|
|
* fell back on just declaring all classes of types which are known to work
|
|
|
|
|
* with lexical_cast to string.
|
|
|
|
|
* @remarks Meanwhile (2016) I think this is an adequate and robust solution
|
|
|
|
|
* and here to stay. Based on this, I'll add a generic overload for the
|
|
|
|
|
* output stream inserter `operator<<` to use custom string conversions;
|
|
|
|
|
* this trait is essential to exclude types which can be printed as-is.
|
2009-08-28 20:33:20 +02:00
|
|
|
*/
|
|
|
|
|
template<typename X>
|
|
|
|
|
struct can_lexical2string
|
2016-01-06 02:41:58 +01:00
|
|
|
: __or_< is_arithmetic<X>
|
|
|
|
|
, is_StringLike<X>
|
|
|
|
|
>
|
|
|
|
|
{ };
|
|
|
|
|
|
2016-01-08 01:00:05 +01:00
|
|
|
template<typename X>
|
|
|
|
|
struct use_LexicalConversion
|
|
|
|
|
: __and_<can_lexical2string<X>
|
|
|
|
|
,__not_<can_convertToString<X>>
|
|
|
|
|
>
|
|
|
|
|
{ };
|
|
|
|
|
|
|
|
|
|
/** when to use custom string conversions for output streams */
|
|
|
|
|
template<typename X>
|
|
|
|
|
struct use_StringConversion4Stream
|
2018-03-22 21:43:19 +01:00
|
|
|
: __and_<is_class<typename Strip<X>::TypePlain>
|
|
|
|
|
,__not_<is_pointer<X>>
|
2016-01-08 01:00:05 +01:00
|
|
|
,__not_<can_lexical2string<X>>
|
|
|
|
|
>
|
|
|
|
|
{ };
|
|
|
|
|
|
2016-01-06 02:41:58 +01:00
|
|
|
|
2009-08-28 20:33:20 +02:00
|
|
|
|
2016-07-30 21:54:16 +02:00
|
|
|
/** detect smart pointers */
|
|
|
|
|
template<typename X>
|
|
|
|
|
struct is_smart_ptr
|
|
|
|
|
: std::false_type
|
|
|
|
|
{ };
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct is_smart_ptr<std::shared_ptr<T>>
|
|
|
|
|
: std::true_type
|
|
|
|
|
{ };
|
|
|
|
|
|
|
|
|
|
template <typename T, typename D>
|
|
|
|
|
struct is_smart_ptr<std::unique_ptr<T,D>>
|
|
|
|
|
: std::true_type
|
|
|
|
|
{ };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-19 08:03:54 +02:00
|
|
|
|
2009-12-19 03:55:02 +01:00
|
|
|
|
2016-01-27 12:38:16 +01:00
|
|
|
|
|
|
|
|
template<typename NUM>
|
|
|
|
|
struct is_nonFloat
|
|
|
|
|
: __and_<is_arithmetic<NUM>
|
|
|
|
|
,__not_<is_floating_point<NUM>>
|
|
|
|
|
>
|
|
|
|
|
{ };
|
|
|
|
|
|
|
|
|
|
/** temporary workaround for GCC [Bug-63723], necessary until CGG-5
|
|
|
|
|
* @remarks The problem is that GCC emits a warning on narrowing conversion,
|
|
|
|
|
* instead of letting the SFINAE substitution fail. This can be considered
|
|
|
|
|
* questionable behaviour, since the usual implementation of a `is_convertible`
|
|
|
|
|
* trait uses initialisation from a brace enclosed list, where C++11 prohibits
|
|
|
|
|
* narrowing conversions. Now the problem is, that we'll use such traits checks
|
|
|
|
|
* to remove such _impossble_ cases from generated trampoline tables or visitor
|
|
|
|
|
* double dispatch implementations. Thus, for one we get lots of warnings at that
|
|
|
|
|
* point when generating those trampoline tables (at initialisation), while it
|
|
|
|
|
* is not clear we'll trigger those cases, and, when we do, we'll get narrowing
|
|
|
|
|
* conversions in a context where we're unable to cope with them or protect
|
|
|
|
|
* ourselves against spurious conversions.
|
|
|
|
|
* What follows is a quick-n-dirty hack to remove such unwanted conversions.
|
|
|
|
|
*
|
|
|
|
|
* [Bug-63723]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63723
|
|
|
|
|
*/
|
|
|
|
|
template<typename SRC, typename TAR>
|
|
|
|
|
struct is_narrowingInit
|
|
|
|
|
: __or_<__and_<is_unsigned<SRC>, is_signed<TAR>>
|
|
|
|
|
,__and_<is_signed<SRC>, is_unsigned<TAR>>
|
|
|
|
|
,__and_<is_nonFloat<SRC>, is_floating_point<TAR>>
|
|
|
|
|
,__and_<is_floating_point<SRC>, is_nonFloat<TAR>>
|
|
|
|
|
,__not_<is_constructible<TAR, SRC>>
|
|
|
|
|
>
|
|
|
|
|
{ };
|
|
|
|
|
|
2016-01-28 21:21:41 +01:00
|
|
|
template<typename TAR>
|
|
|
|
|
struct is_narrowingInit<lib::hash::LuidH, TAR>
|
|
|
|
|
: __or_<is_arithmetic<TAR>
|
|
|
|
|
,is_floating_point<TAR>
|
|
|
|
|
>
|
|
|
|
|
{ };
|
|
|
|
|
|
2016-01-27 12:38:16 +01:00
|
|
|
#define TRAIT_IS_NARROWING(_SRC_, _TAR_) \
|
|
|
|
|
template<> \
|
|
|
|
|
struct is_narrowingInit<_SRC_, _TAR_> \
|
|
|
|
|
: std::true_type \
|
|
|
|
|
{ };
|
|
|
|
|
|
|
|
|
|
TRAIT_IS_NARROWING (int64_t, int32_t)
|
|
|
|
|
TRAIT_IS_NARROWING (int64_t, int16_t)
|
|
|
|
|
TRAIT_IS_NARROWING (int64_t, int8_t)
|
2016-01-29 00:59:34 +01:00
|
|
|
TRAIT_IS_NARROWING (int64_t, char)
|
2016-01-27 12:38:16 +01:00
|
|
|
TRAIT_IS_NARROWING (int32_t, int16_t)
|
|
|
|
|
TRAIT_IS_NARROWING (int32_t, int8_t)
|
2016-01-29 00:59:34 +01:00
|
|
|
TRAIT_IS_NARROWING (int32_t, char)
|
2016-01-27 12:38:16 +01:00
|
|
|
TRAIT_IS_NARROWING (int16_t, int8_t)
|
|
|
|
|
TRAIT_IS_NARROWING (int16_t, short)
|
|
|
|
|
TRAIT_IS_NARROWING (int16_t, char)
|
|
|
|
|
|
|
|
|
|
TRAIT_IS_NARROWING (uint64_t, uint32_t)
|
|
|
|
|
TRAIT_IS_NARROWING (uint64_t, uint16_t)
|
|
|
|
|
TRAIT_IS_NARROWING (uint64_t, uint8_t)
|
|
|
|
|
TRAIT_IS_NARROWING (uint32_t, uint16_t)
|
|
|
|
|
TRAIT_IS_NARROWING (uint32_t, uint8_t)
|
|
|
|
|
TRAIT_IS_NARROWING (uint16_t, uint8_t)
|
|
|
|
|
TRAIT_IS_NARROWING (uint16_t, ushort)
|
|
|
|
|
|
|
|
|
|
TRAIT_IS_NARROWING (double, float)
|
|
|
|
|
|
2016-02-07 02:20:01 +01:00
|
|
|
TRAIT_IS_NARROWING (double, lib::time::TimeValue)
|
|
|
|
|
TRAIT_IS_NARROWING (double, lib::time::Duration)
|
|
|
|
|
|
2016-01-27 12:38:16 +01:00
|
|
|
#undef TRAIT_IS_NARROWING
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-06 02:41:58 +01:00
|
|
|
/* ====== generic iteration support ====== */
|
2009-12-19 03:55:02 +01:00
|
|
|
|
2013-01-13 16:39:43 +01:00
|
|
|
/** Trait template to detect a type usable immediately as
|
|
|
|
|
* "Lumiera Forward Iterator" in a specialised for-each loop
|
|
|
|
|
* This is just a heuristic, based on some common properties
|
|
|
|
|
* of such iterators; it is enough to distinguish it from an
|
|
|
|
|
* STL container, but can certainly be refined.
|
|
|
|
|
*/
|
|
|
|
|
template<typename T>
|
|
|
|
|
class can_IterForEach
|
|
|
|
|
{
|
|
|
|
|
typedef typename Strip<T>::Type Type;
|
|
|
|
|
|
|
|
|
|
META_DETECT_NESTED(value_type);
|
|
|
|
|
META_DETECT_OPERATOR_DEREF();
|
|
|
|
|
META_DETECT_OPERATOR_INC();
|
|
|
|
|
|
|
|
|
|
public:
|
2017-12-03 04:24:02 +01:00
|
|
|
enum{ value = std::is_constructible<bool, Type>::value
|
2013-01-13 16:39:43 +01:00
|
|
|
&& HasNested_value_type<Type>::value
|
|
|
|
|
&& HasOperator_deref<Type>::value
|
|
|
|
|
&& HasOperator_inc<Type>::value
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-04 11:19:01 +01:00
|
|
|
/** Trait template to detect a type usable with the STL for-each loop.
|
|
|
|
|
* Basically we're looking for the functions to get the begin/end iterator
|
|
|
|
|
*/
|
|
|
|
|
template<typename T>
|
|
|
|
|
class can_STL_ForEach
|
|
|
|
|
{
|
2010-01-07 03:10:02 +01:00
|
|
|
typedef typename Strip<T>::Type Type;
|
|
|
|
|
|
2010-01-04 11:19:01 +01:00
|
|
|
struct is_iterable
|
|
|
|
|
{
|
|
|
|
|
META_DETECT_NESTED(iterator);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::iterator, begin,(void));
|
|
|
|
|
META_DETECT_FUNCTION(typename X::iterator, end ,(void));
|
|
|
|
|
|
2010-01-07 03:10:02 +01:00
|
|
|
enum { value = HasNested_iterator<Type>::value
|
|
|
|
|
&& HasFunSig_begin<Type>::value
|
|
|
|
|
&& HasFunSig_end<Type>::value
|
2010-01-04 11:19:01 +01:00
|
|
|
};
|
|
|
|
|
};
|
C++17: fix detector for STL container iterability
the reason for the failure, as it turned out,
is that 'noexcept' is part of the function signature since C++17
And, since typically a STL container has const and non-const variants
of the begin() and end() function, the match to a member function pointer
became ambuguous, when probing with a signature without 'noexcept'
However, we deliberately want to support "any STL container like" types,
and this IMHO should include types with a possibly throwing iterator.
The rationale is, sometimes we want to expose some element *generator*
behind a container-like interface.
At this point I did an investigation if we can emulate something
in the way of a Concept -- i.e. rather than checking for the presence
of some functions on the interface, better try to cover the necessary
behaviour, like in a type class.
Unfortunately, while doable, this turns out to become quite technical;
and this highlights why the C++20 concepts are such an important addition
to the language.
So for the time being, we'll amend the existing solution
and look ahead to C++20
2020-02-21 18:45:51 +01:00
|
|
|
|
|
|
|
|
struct is_noexcept_iterable
|
|
|
|
|
{
|
|
|
|
|
META_DETECT_NESTED(iterator);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::iterator, begin,(void) noexcept);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::iterator, end ,(void) noexcept);
|
|
|
|
|
|
|
|
|
|
enum { value = HasNested_iterator<Type>::value
|
|
|
|
|
&& HasFunSig_begin<Type>::value
|
|
|
|
|
&& HasFunSig_end<Type>::value
|
|
|
|
|
};
|
|
|
|
|
};
|
2010-01-04 11:19:01 +01:00
|
|
|
|
|
|
|
|
struct is_const_iterable
|
|
|
|
|
{
|
|
|
|
|
META_DETECT_NESTED(const_iterator);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::const_iterator, begin,(void) const);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::const_iterator, end ,(void) const);
|
|
|
|
|
|
2010-01-07 03:10:02 +01:00
|
|
|
enum { value = HasNested_const_iterator<Type>::value
|
|
|
|
|
&& HasFunSig_begin<Type>::value
|
|
|
|
|
&& HasFunSig_end<Type>::value
|
2010-01-04 11:19:01 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
C++17: fix detector for STL container iterability
the reason for the failure, as it turned out,
is that 'noexcept' is part of the function signature since C++17
And, since typically a STL container has const and non-const variants
of the begin() and end() function, the match to a member function pointer
became ambuguous, when probing with a signature without 'noexcept'
However, we deliberately want to support "any STL container like" types,
and this IMHO should include types with a possibly throwing iterator.
The rationale is, sometimes we want to expose some element *generator*
behind a container-like interface.
At this point I did an investigation if we can emulate something
in the way of a Concept -- i.e. rather than checking for the presence
of some functions on the interface, better try to cover the necessary
behaviour, like in a type class.
Unfortunately, while doable, this turns out to become quite technical;
and this highlights why the C++20 concepts are such an important addition
to the language.
So for the time being, we'll amend the existing solution
and look ahead to C++20
2020-02-21 18:45:51 +01:00
|
|
|
struct is_const_noexcept_iterable
|
|
|
|
|
{
|
|
|
|
|
META_DETECT_NESTED(const_iterator);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::const_iterator, begin,(void) const noexcept);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::const_iterator, end ,(void) const noexcept);
|
|
|
|
|
|
|
|
|
|
enum { value = HasNested_const_iterator<Type>::value
|
|
|
|
|
&& HasFunSig_begin<Type>::value
|
|
|
|
|
&& HasFunSig_end<Type>::value
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2010-01-04 11:19:01 +01:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
enum { value = is_iterable::value
|
C++17: fix detector for STL container iterability
the reason for the failure, as it turned out,
is that 'noexcept' is part of the function signature since C++17
And, since typically a STL container has const and non-const variants
of the begin() and end() function, the match to a member function pointer
became ambuguous, when probing with a signature without 'noexcept'
However, we deliberately want to support "any STL container like" types,
and this IMHO should include types with a possibly throwing iterator.
The rationale is, sometimes we want to expose some element *generator*
behind a container-like interface.
At this point I did an investigation if we can emulate something
in the way of a Concept -- i.e. rather than checking for the presence
of some functions on the interface, better try to cover the necessary
behaviour, like in a type class.
Unfortunately, while doable, this turns out to become quite technical;
and this highlights why the C++20 concepts are such an important addition
to the language.
So for the time being, we'll amend the existing solution
and look ahead to C++20
2020-02-21 18:45:51 +01:00
|
|
|
or is_const_iterable::value
|
|
|
|
|
or is_noexcept_iterable::value
|
|
|
|
|
or is_const_noexcept_iterable::value
|
2010-01-04 11:19:01 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-01-13 16:39:43 +01:00
|
|
|
/** Trait template to detect a type also supporting STL-style backwards iteration */
|
2010-01-04 11:19:01 +01:00
|
|
|
template<typename T>
|
2013-01-13 16:39:43 +01:00
|
|
|
class can_STL_backIteration
|
2010-01-04 11:19:01 +01:00
|
|
|
{
|
2010-01-07 03:10:02 +01:00
|
|
|
typedef typename Strip<T>::Type Type;
|
2013-01-13 16:39:43 +01:00
|
|
|
|
|
|
|
|
struct is_backIterable
|
|
|
|
|
{
|
|
|
|
|
META_DETECT_NESTED(reverse_iterator);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::reverse_iterator, rbegin,(void));
|
|
|
|
|
META_DETECT_FUNCTION(typename X::reverse_iterator, rend ,(void));
|
|
|
|
|
|
|
|
|
|
enum { value = HasNested_reverse_iterator<Type>::value
|
|
|
|
|
&& HasFunSig_rbegin<Type>::value
|
|
|
|
|
&& HasFunSig_rend<Type>::value
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
C++17: fix detector for STL container iterability
the reason for the failure, as it turned out,
is that 'noexcept' is part of the function signature since C++17
And, since typically a STL container has const and non-const variants
of the begin() and end() function, the match to a member function pointer
became ambuguous, when probing with a signature without 'noexcept'
However, we deliberately want to support "any STL container like" types,
and this IMHO should include types with a possibly throwing iterator.
The rationale is, sometimes we want to expose some element *generator*
behind a container-like interface.
At this point I did an investigation if we can emulate something
in the way of a Concept -- i.e. rather than checking for the presence
of some functions on the interface, better try to cover the necessary
behaviour, like in a type class.
Unfortunately, while doable, this turns out to become quite technical;
and this highlights why the C++20 concepts are such an important addition
to the language.
So for the time being, we'll amend the existing solution
and look ahead to C++20
2020-02-21 18:45:51 +01:00
|
|
|
struct is_noexcept_backIterable
|
|
|
|
|
{
|
|
|
|
|
META_DETECT_NESTED(reverse_iterator);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::reverse_iterator, rbegin,(void) noexcept);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::reverse_iterator, rend ,(void) noexcept);
|
|
|
|
|
|
|
|
|
|
enum { value = HasNested_reverse_iterator<Type>::value
|
|
|
|
|
&& HasFunSig_rbegin<Type>::value
|
|
|
|
|
&& HasFunSig_rend<Type>::value
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2013-01-13 16:39:43 +01:00
|
|
|
struct is_const_backIterable
|
|
|
|
|
{
|
|
|
|
|
META_DETECT_NESTED(const_reverse_iterator);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::const_reverse_iterator, rbegin,(void) const);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::const_reverse_iterator, rend ,(void) const);
|
|
|
|
|
|
|
|
|
|
enum { value = HasNested_const_reverse_iterator<Type>::value
|
|
|
|
|
&& HasFunSig_rbegin<Type>::value
|
|
|
|
|
&& HasFunSig_rend<Type>::value
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
C++17: fix detector for STL container iterability
the reason for the failure, as it turned out,
is that 'noexcept' is part of the function signature since C++17
And, since typically a STL container has const and non-const variants
of the begin() and end() function, the match to a member function pointer
became ambuguous, when probing with a signature without 'noexcept'
However, we deliberately want to support "any STL container like" types,
and this IMHO should include types with a possibly throwing iterator.
The rationale is, sometimes we want to expose some element *generator*
behind a container-like interface.
At this point I did an investigation if we can emulate something
in the way of a Concept -- i.e. rather than checking for the presence
of some functions on the interface, better try to cover the necessary
behaviour, like in a type class.
Unfortunately, while doable, this turns out to become quite technical;
and this highlights why the C++20 concepts are such an important addition
to the language.
So for the time being, we'll amend the existing solution
and look ahead to C++20
2020-02-21 18:45:51 +01:00
|
|
|
struct is_const_noexcept_backIterable
|
|
|
|
|
{
|
|
|
|
|
META_DETECT_NESTED(const_reverse_iterator);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::const_reverse_iterator, rbegin,(void) const noexcept);
|
|
|
|
|
META_DETECT_FUNCTION(typename X::const_reverse_iterator, rend ,(void) const noexcept);
|
|
|
|
|
|
|
|
|
|
enum { value = HasNested_const_reverse_iterator<Type>::value
|
|
|
|
|
&& HasFunSig_rbegin<Type>::value
|
|
|
|
|
&& HasFunSig_rend<Type>::value
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2010-01-04 11:19:01 +01:00
|
|
|
|
|
|
|
|
public:
|
2013-01-13 16:39:43 +01:00
|
|
|
enum { value = is_backIterable::value
|
C++17: fix detector for STL container iterability
the reason for the failure, as it turned out,
is that 'noexcept' is part of the function signature since C++17
And, since typically a STL container has const and non-const variants
of the begin() and end() function, the match to a member function pointer
became ambuguous, when probing with a signature without 'noexcept'
However, we deliberately want to support "any STL container like" types,
and this IMHO should include types with a possibly throwing iterator.
The rationale is, sometimes we want to expose some element *generator*
behind a container-like interface.
At this point I did an investigation if we can emulate something
in the way of a Concept -- i.e. rather than checking for the presence
of some functions on the interface, better try to cover the necessary
behaviour, like in a type class.
Unfortunately, while doable, this turns out to become quite technical;
and this highlights why the C++20 concepts are such an important addition
to the language.
So for the time being, we'll amend the existing solution
and look ahead to C++20
2020-02-21 18:45:51 +01:00
|
|
|
or is_const_backIterable::value
|
|
|
|
|
or is_noexcept_backIterable::value
|
|
|
|
|
or is_const_noexcept_backIterable::value
|
2013-01-13 16:39:43 +01:00
|
|
|
};
|
2010-01-04 11:19:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-01-13 16:39:43 +01:00
|
|
|
|
2010-01-04 10:08:14 +01:00
|
|
|
}} // namespace lib::meta
|
2009-07-19 08:03:54 +02:00
|
|
|
#endif
|