Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
trait.hpp
Go to the documentation of this file.
1/*
2 TRAIT.hpp - type handling and type detection helpers
3
4 Copyright (C)
5 2009, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
14
49#ifndef LIB_META_TRAIT_H
50#define LIB_META_TRAIT_H
51
52
53#include "lib/meta/util.hpp"
55
56#include <type_traits>
57
58
59//Forward declarations for the Unwrap helper....
60namespace boost{
61 template<class X> class reference_wrapper;
62}
63namespace std {
64 template<class X> class reference_wrapper;
65 template<class X> class shared_ptr;
66 template<class X, class D> class unique_ptr;
67 template<class C, class T> class basic_filebuf;
68 template<class C> class char_traits;
69}
70namespace lib{
71 template<class X, class B> class P;
72
73 namespace hash {
74 class LuidH;
75 }
76 namespace time {
77 class TimeValue;
78 class Duration;
79}}
80namespace steam {
81namespace mobject{
82 template<class X, class B> class Placement;
83}}
84
85
86namespace lib {
87namespace meta {
88
89 using std::remove_cv_t;
90 using std::remove_pointer_t;
91 using std::remove_reference_t;
92 using std::conditional_t;
93 using std::is_reference_v;
94 using std::is_lvalue_reference_v;
95 using std::is_rvalue_reference_v;
96 using std::is_pointer;
97 using std::is_base_of;
98 using std::is_convertible;
99 using std::is_constructible;
100 using std::is_floating_point;
101 using std::is_arithmetic;
102 using std::is_unsigned;
103 using std::is_signed;
104 using std::is_class;
105 using std::is_same;
106 using std::__not_;
107 using std::__and_;
108 using std::__or_;
109
110 template<typename T>
111 static constexpr bool isConst_v = std::is_const_v<remove_reference_t<T>>;
112 template<typename T>
113 static constexpr bool isLRef_v = std::is_lvalue_reference_v<T>;
114 template<typename T>
115 static constexpr bool isRRef_v = std::is_rvalue_reference_v<T>;
116 template<typename T>
117 static constexpr bool isRef_v = std::is_reference_v<T>;
118
119
128 template<typename X>
129 struct Unwrap
130 : std::false_type
131 {
132 using Type = X;
133
134 static X&
135 extract (X const& x)
136 {
137 return const_cast<X&> (x);
138 }
139 };
140
141 template<>
142 struct Unwrap<void>
143 : std::false_type
144 {
145 using Type = void;
146 };
147
148 template<typename X>
149 struct Unwrap<X*>
150 : std::true_type
151 {
153
154 static Type&
155 extract (const X* ptr)
156 {
157 ASSERT (ptr);
158 return const_cast<Type&> (*ptr);
159 }
160 };
161
162 template<typename X>
163 struct Unwrap<boost::reference_wrapper<X>>
164 : std::true_type
165 {
166 using Type = X;
167
168 static X&
173 };
174
175 template<typename X>
176 struct Unwrap<std::reference_wrapper<X>>
177 : std::true_type
178 {
179 using Type = X;
180
181 static X&
186 };
187
188 template<typename X, class D>
189 struct Unwrap<std::unique_ptr<X,D>>
190 : std::true_type
191 {
192 using Type = X;
193
194 static X&
195 extract (std::unique_ptr<X,D> const& ptr)
196 {
197 ASSERT (ptr);
198 return *ptr;
199 }
200 };
201
202 template<typename X>
203 struct Unwrap<std::shared_ptr<X>>
204 : std::true_type
205 {
206 using Type = X;
207
208 static X&
209 extract (std::shared_ptr<X> const& ptr)
210 {
211 ASSERT (ptr);
212 return *ptr;
213 }
214 };
215
216 template<typename X, class B>
217 struct Unwrap<P<X, B>>
218 : std::true_type
219 {
220 using Type = X;
221
222 static X&
224 {
225 ASSERT (ptr);
226 return *ptr;
227 }
228 };
229
230
238 template<typename X>
240 unwrap (X const& wrapped)
241 {
243 }
244
245
246
247
249 template<typename X>
263
264
265
266
274 template<typename TY>
276 {
277 typedef TY Value;
278 typedef TY* Pointer;
279 typedef TY& Reference;
280 };
281
282 template<typename TY>
283 struct RefTraits<TY *>
284 {
285 typedef TY* Value;
286 typedef TY** Pointer;
287 typedef TY*& Reference;
288 };
289
290 template<typename TY>
291 struct RefTraits<TY &>
292 {
293 typedef TY Value;
294 typedef TY* Pointer;
295 typedef TY& Reference;
296 };
297
298 template<typename TY>
299 struct RefTraits<TY &&>
300 {
301 typedef TY Value;
302 typedef TY* Pointer;
303 typedef TY& Reference;
304 };
305
306
307
308
309
310
311
312 /* ==== Traits ==== */
313
315 template<typename T, typename U>
317 : is_same <typename Strip<T>::TypeReferred
318 ,typename Strip<U>::TypeReferred>
319 { };
320
322 template<typename S, typename I>
324 : __or_< __and_< is_class<I>
325 , is_class<S>
326 , is_base_of<I,S>
327 >
328 , is_same<I,S>
329 >
330 { };
331
333 template<typename S, typename I>
335 : is_Subclass <typename Strip<S>::TypeReferred
336 ,typename Strip<I>::TypeReferred>
337 { };
338
340 template<typename S, typename G>
342 : std::is_convertible<typename RefTraits<S>::Reference
343 ,typename RefTraits<G>::Reference
344 >
345 { };
346
348 template<typename X>
350 : __or_< is_basically<X, std::string>
351 , is_basically<X, std::string_view>
352 , is_convertible<X, const char*>
353 >
354 { };
355
372 template<typename X>
374 : __or_< is_arithmetic<X>
375 , is_StringLike<X>
376 >
377 { };
378
379 template<typename X>
381 : __and_<can_lexical2string<X>
382 ,__not_<can_convertToString<X>>
383 >
384 { };
385
386 // need to exclude files and input streams from automatic string conversion
387 template<typename X>
389 : is_same<X, std::basic_filebuf<char, std::char_traits<char> > >
390 { };
391
393 template<typename X>
395 : __and_<is_class<typename Strip<X>::TypePlain>
396 ,__not_<is_pointer<X>>
397 ,__not_<can_lexical2string<X>>
398 ,__not_<is_StreamSource<X>>
399 >
400 { };
401
402
404 template<typename X>
406 : std::false_type
407 { };
408
409 template<typename T>
410 struct is_smart_ptr<std::shared_ptr<T>>
411 : std::true_type
412 { };
413
414 template <typename T, typename D>
415 struct is_smart_ptr<std::unique_ptr<T,D>>
416 : std::true_type
417 { };
418
419
420
421
422
423
424 template<typename NUM>
426 : __and_<is_arithmetic<NUM>
427 ,__not_<is_floating_point<NUM>>
428 >
429 { };
430
447 template<typename SRC, typename TAR>
449 : __or_<__and_<is_unsigned<SRC>, is_signed<TAR>>
450 ,__and_<is_signed<SRC>, is_unsigned<TAR>>
451 ,__and_<is_nonFloat<SRC>, is_floating_point<TAR>>
452 ,__and_<is_floating_point<SRC>, is_nonFloat<TAR>>
453 ,__not_<is_constructible<TAR, SRC>>
454 >
455 { };
456
457 template<typename TAR>
459 : __or_<is_arithmetic<TAR>
460 ,is_floating_point<TAR>
461 >
462 { };
463
464#define TRAIT_IS_NARROWING(_SRC_, _TAR_) \
465 template<> \
466 struct is_narrowingInit<_SRC_, _TAR_> \
467 : std::true_type \
468 { };
469
480
488
489 TRAIT_IS_NARROWING (double, float)
490
493
494#undef TRAIT_IS_NARROWING
495
496
497
498
499
500
501 /* ====== generic iteration support ====== */
502
509 template<typename T>
525
526
527
533 template<typename T>
548
549
550
554 template<typename T>
556 {
558
560 {
562 META_DETECT_FUNCTION(typename X::iterator, begin,(void));
563 META_DETECT_FUNCTION(typename X::iterator, end ,(void));
564
568 };
569 };
570
572 {
574 META_DETECT_FUNCTION(typename X::iterator, begin,(void) noexcept);
575 META_DETECT_FUNCTION(typename X::iterator, end ,(void) noexcept);
576
580 };
581 };
582
584 {
585 META_DETECT_NESTED(const_iterator);
586 META_DETECT_FUNCTION(typename X::const_iterator, begin,(void) const);
587 META_DETECT_FUNCTION(typename X::const_iterator, end ,(void) const);
588
592 };
593 };
594
596 {
597 META_DETECT_NESTED(const_iterator);
598 META_DETECT_FUNCTION(typename X::const_iterator, begin,(void) const noexcept);
599 META_DETECT_FUNCTION(typename X::const_iterator, end ,(void) const noexcept);
600
604 };
605 };
606
607
608 public:
613 };
614 };
615
616
618 template<typename T>
620 {
622
624 {
625 META_DETECT_NESTED(reverse_iterator);
626 META_DETECT_FUNCTION(typename X::reverse_iterator, rbegin,(void));
627 META_DETECT_FUNCTION(typename X::reverse_iterator, rend ,(void));
628
632 };
633 };
634
636 {
637 META_DETECT_NESTED(reverse_iterator);
638 META_DETECT_FUNCTION(typename X::reverse_iterator, rbegin,(void) noexcept);
639 META_DETECT_FUNCTION(typename X::reverse_iterator, rend ,(void) noexcept);
640
644 };
645 };
646
648 {
649 META_DETECT_NESTED(const_reverse_iterator);
650 META_DETECT_FUNCTION(typename X::const_reverse_iterator, rbegin,(void) const);
651 META_DETECT_FUNCTION(typename X::const_reverse_iterator, rend ,(void) const);
652
656 };
657 };
658
660 {
661 META_DETECT_NESTED(const_reverse_iterator);
662 META_DETECT_FUNCTION(typename X::const_reverse_iterator, rbegin,(void) const noexcept);
663 META_DETECT_FUNCTION(typename X::const_reverse_iterator, rend ,(void) const noexcept);
664
668 };
669 };
670
671
672 public:
677 };
678 };
679
680
681
682}} // namespace lib::meta
683#endif
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition p.hpp:77
Hash implementation based on a lumiera unique object id (LUID) When invoking the default ctor,...
Trait template to detect a type usable immediately as "Lumiera Forward Iterator" in a specialised for...
Definition trait.hpp:511
Strip< T >::Type Type
Definition trait.hpp:512
Trait template to detect a type usable with the STL for-each loop.
Definition trait.hpp:556
Strip< T >::Type Type
Definition trait.hpp:557
Trait template to detect a type also supporting STL-style backwards iteration.
Definition trait.hpp:620
Trait template to detect a type exposing a »state core« API.
Definition trait.hpp:535
META_DETECT_FUNCTION_ARGLESS(checkPoint)
META_DETECT_FUNCTION_ARGLESS(iterNext)
Strip< T >::Type Type
Definition trait.hpp:536
Duration is the internal Lumiera time metric.
basic constant internal time value.
A refcounting Handle to an MObject of type MO, used to constrain or explicitly specify the location w...
Metaprogramming helpers to check for specific properties of a type in question.
unsigned short int ushort
Definition integral.hpp:34
Simple and lightweight helpers for metaprogramming and type detection.
remove_reference_t< TypeUnconst > TypeReferred
Definition trait.hpp:257
static constexpr bool isRRef_v
Definition trait.hpp:115
conditional_t< is_reference_v< X >, conditional_t< is_rvalue_reference_v< X >, remove_cv_t< remove_reference_t< X > > &&, remove_cv_t< remove_reference_t< X > > & >, remove_cv_t< X > > TypeUnconst
Definition trait.hpp:256
enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition meta/util.hpp:87
static constexpr bool isRef_v
Definition trait.hpp:117
Unwrap< TypePlain >::Type Type
Definition trait.hpp:261
static constexpr bool isLRef_v
Definition trait.hpp:113
remove_cv_t< TypePointee > TypePlain
Definition trait.hpp:259
static constexpr bool isConst_v
Definition trait.hpp:111
remove_pointer_t< TypeReferred > TypePointee
Definition trait.hpp:258
Unwrap< X >::Type & unwrap(X const &wrapped)
convenience shortcut: unwrapping free function.
Definition trait.hpp:240
Type definition helper for pointer and reference types.
Definition trait.hpp:276
Helper for type analysis: tries to strip all kinds of type adornments.
Definition trait.hpp:251
Implementation namespace for support and library code.
STL namespace.
Steam-Layer implementation namespace root.
static X & extract(P< X, B > ptr)
Definition trait.hpp:223
remove_cv_t< X > Type
Definition trait.hpp:152
static Type & extract(const X *ptr)
Definition trait.hpp:155
static X & extract(boost::reference_wrapper< X > wrapped)
Definition trait.hpp:169
static X & extract(std::reference_wrapper< X > wrapped)
Definition trait.hpp:182
static X & extract(std::shared_ptr< X > const &ptr)
Definition trait.hpp:209
static X & extract(std::unique_ptr< X, D > const &ptr)
Definition trait.hpp:195
Helper for type analysis and convenience accessors: attempts to extract a base type from various wrap...
Definition trait.hpp:131
static X & extract(X const &x)
Definition trait.hpp:135
META_DETECT_FUNCTION(typename X::const_iterator, begin,(void) const)
META_DETECT_FUNCTION(typename X::const_iterator, end,(void) const)
META_DETECT_FUNCTION(typename X::const_iterator, begin,(void) const noexcept)
META_DETECT_FUNCTION(typename X::const_iterator, end,(void) const noexcept)
META_DETECT_FUNCTION(typename X::iterator, begin,(void))
META_DETECT_FUNCTION(typename X::iterator, end,(void))
META_DETECT_FUNCTION(typename X::iterator, begin,(void) noexcept)
META_DETECT_FUNCTION(typename X::iterator, end,(void) noexcept)
META_DETECT_FUNCTION(typename X::reverse_iterator, rbegin,(void))
META_DETECT_FUNCTION(typename X::reverse_iterator, rend,(void))
META_DETECT_FUNCTION(typename X::const_reverse_iterator, rend,(void) const)
META_DETECT_FUNCTION(typename X::const_reverse_iterator, rbegin,(void) const)
META_DETECT_FUNCTION(typename X::const_reverse_iterator, rend,(void) const noexcept)
META_DETECT_FUNCTION(typename X::const_reverse_iterator, rbegin,(void) const noexcept)
META_DETECT_FUNCTION(typename X::reverse_iterator, rend,(void) noexcept)
META_DETECT_FUNCTION(typename X::reverse_iterator, rbegin,(void) noexcept)
verify the first (special) type can stand-in for the second
Definition trait.hpp:345
types able to be lexically converted to string representation
Definition trait.hpp:377
detect various flavours of string / text data
Definition trait.hpp:354
verify compliance to an interface by subtype check
Definition trait.hpp:330
compare unadorned types, disregarding const and references
Definition trait.hpp:319
compare for unadorned base type, disregarding const and references
Definition trait.hpp:337
temporary workaround for GCC Bug-63723, necessary until CGG-5
Definition trait.hpp:455
detect smart pointers
Definition trait.hpp:407
when to use custom string conversions for output streams
Definition trait.hpp:400
Primary class template for std::hash.
#define TRAIT_IS_NARROWING(_SRC_, _TAR_)
Definition trait.hpp:464