blob: 7c02c639839cc8fb40ca2cfa6fd902266ad2747c [file] [log] [blame]
Davide Pesavento17b61b42018-11-21 23:10:48 -05001//
2// Copyright (c) 2016-2018 Martin Moene
3//
4// https://github.com/martinmoene/any-lite
5//
6// Distributed under the Boost Software License, Version 1.0.
7// (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8
9#pragma once
10
11#ifndef NONSTD_ANY_LITE_HPP
12#define NONSTD_ANY_LITE_HPP
13
14#define any_lite_MAJOR 0
15#define any_lite_MINOR 1
16#define any_lite_PATCH 0
17
18#define any_lite_VERSION any_STRINGIFY(any_lite_MAJOR) "." any_STRINGIFY(any_lite_MINOR) "." any_STRINGIFY(any_lite_PATCH)
19
20#define any_STRINGIFY( x ) any_STRINGIFY_( x )
21#define any_STRINGIFY_( x ) #x
22
23// any-lite configuration:
24
25#define any_ANY_DEFAULT 0
26#define any_ANY_NONSTD 1
27#define any_ANY_STD 2
28
29#if !defined( any_CONFIG_SELECT_ANY )
30# define any_CONFIG_SELECT_ANY ( any_HAVE_STD_ANY ? any_ANY_STD : any_ANY_NONSTD )
31#endif
32
33// C++ language version detection (C++20 is speculative):
34// Note: VC14.0/1900 (VS2015) lacks too much from C++14.
35
36#ifndef any_CPLUSPLUS
37# if defined(_MSVC_LANG ) && !defined(__clang__)
38# define any_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG )
39# else
40# define any_CPLUSPLUS __cplusplus
41# endif
42#endif
43
44#define any_CPP98_OR_GREATER ( any_CPLUSPLUS >= 199711L )
45#define any_CPP11_OR_GREATER ( any_CPLUSPLUS >= 201103L )
46#define any_CPP14_OR_GREATER ( any_CPLUSPLUS >= 201402L )
47#define any_CPP17_OR_GREATER ( any_CPLUSPLUS >= 201703L )
48#define any_CPP20_OR_GREATER ( any_CPLUSPLUS >= 202000L )
49
50// Use C++17 std::any if available and requested:
51
52#if any_CPP17_OR_GREATER && defined(__has_include )
53# if __has_include( <any> )
54# define any_HAVE_STD_ANY 1
55# else
56# define any_HAVE_STD_ANY 0
57# endif
58#else
59# define any_HAVE_STD_ANY 0
60#endif
61
62#define any_USES_STD_ANY ( (any_CONFIG_SELECT_ANY == any_ANY_STD) || ((any_CONFIG_SELECT_ANY == any_ANY_DEFAULT) && any_HAVE_STD_ANY) )
63
64//
65// in_place: code duplicated in any-lite, expected-lite, optional-lite, value-ptr-lite, variant-lite:
66//
67
68#ifndef nonstd_lite_HAVE_IN_PLACE_TYPES
69#define nonstd_lite_HAVE_IN_PLACE_TYPES 1
70
71// C++17 std::in_place in <utility>:
72
73#if any_CPP17_OR_GREATER
74
75#include <utility>
76
77namespace nonstd {
78
79using std::in_place;
80using std::in_place_type;
81using std::in_place_index;
82using std::in_place_t;
83using std::in_place_type_t;
84using std::in_place_index_t;
85
86#define nonstd_lite_in_place_t( T) std::in_place_t
87#define nonstd_lite_in_place_type_t( T) std::in_place_type_t<T>
88#define nonstd_lite_in_place_index_t(K) std::in_place_index_t<K>
89
90#define nonstd_lite_in_place( T) std::in_place_t{}
91#define nonstd_lite_in_place_type( T) std::in_place_type_t<T>{}
92#define nonstd_lite_in_place_index(K) std::in_place_index_t<K>{}
93
94} // namespace nonstd
95
96#else // any_CPP17_OR_GREATER
97
98#include <cstddef>
99
100namespace nonstd {
101namespace detail {
102
103template< class T >
104struct in_place_type_tag {};
105
106template< std::size_t K >
107struct in_place_index_tag {};
108
109} // namespace detail
110
111struct in_place_t {};
112
113template< class T >
114inline in_place_t in_place( detail::in_place_type_tag<T> = detail::in_place_type_tag<T>() )
115{
116 return in_place_t();
117}
118
119template< std::size_t K >
120inline in_place_t in_place( detail::in_place_index_tag<K> = detail::in_place_index_tag<K>() )
121{
122 return in_place_t();
123}
124
125template< class T >
126inline in_place_t in_place_type( detail::in_place_type_tag<T> = detail::in_place_type_tag<T>() )
127{
128 return in_place_t();
129}
130
131template< std::size_t K >
132inline in_place_t in_place_index( detail::in_place_index_tag<K> = detail::in_place_index_tag<K>() )
133{
134 return in_place_t();
135}
136
137// mimic templated typedef:
138
139#define nonstd_lite_in_place_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag<T> )
140#define nonstd_lite_in_place_type_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag<T> )
141#define nonstd_lite_in_place_index_t(K) nonstd::in_place_t(&)( nonstd::detail::in_place_index_tag<K> )
142
143#define nonstd_lite_in_place( T) nonstd::in_place_type<T>
144#define nonstd_lite_in_place_type( T) nonstd::in_place_type<T>
145#define nonstd_lite_in_place_index(K) nonstd::in_place_index<K>
146
147} // namespace nonstd
148
149#endif // any_CPP17_OR_GREATER
150#endif // nonstd_lite_HAVE_IN_PLACE_TYPES
151
152//
153// Using std::any:
154//
155
156#if any_USES_STD_ANY
157
158#include <any>
159#include <utility>
160
161namespace nonstd {
162
163 using std::any;
164 using std::any_cast;
165 using std::make_any;
166 using std::swap;
167 using std::bad_any_cast;
168}
169
170#else // any_USES_STD_ANY
171
172#include <typeinfo>
173#include <utility>
174
175// Compiler versions:
176//
177// MSVC++ 6.0 _MSC_VER == 1200 (Visual Studio 6.0)
178// MSVC++ 7.0 _MSC_VER == 1300 (Visual Studio .NET 2002)
179// MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio .NET 2003)
180// MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005)
181// MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008)
182// MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010)
183// MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012)
184// MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013)
185// MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015)
186// MSVC++ 14.1 _MSC_VER >= 1910 (Visual Studio 2017)
187
188#if defined(_MSC_VER ) && !defined(__clang__)
189# define any_COMPILER_MSVC_VER (_MSC_VER )
190# define any_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) )
191#else
192# define any_COMPILER_MSVC_VER 0
193# define any_COMPILER_MSVC_VERSION 0
194#endif
195
196#define any_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) )
197
198#if defined(__clang__)
199# define any_COMPILER_CLANG_VERSION any_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
200#else
201# define any_COMPILER_CLANG_VERSION 0
202#endif
203
204#if defined(__GNUC__) && !defined(__clang__)
205# define any_COMPILER_GNUC_VERSION any_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
206#else
207# define any_COMPILER_GNUC_VERSION 0
208#endif
209
210// half-open range [lo..hi):
211//#define any_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) )
212
213// Presence of language and library features:
214
215#define any_HAVE( feature ) ( any_HAVE_##feature )
216
217#ifdef _HAS_CPP0X
218# define any_HAS_CPP0X _HAS_CPP0X
219#else
220# define any_HAS_CPP0X 0
221#endif
222
223#define any_CPP11_90 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1500)
224#define any_CPP11_100 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1600)
225#define any_CPP11_120 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1800)
226#define any_CPP11_140 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1900)
227
228#define any_CPP14_000 (any_CPP14_OR_GREATER)
229#define any_CPP17_000 (any_CPP17_OR_GREATER)
230
231// Presence of C++11 language features:
232
233#define any_HAVE_CONSTEXPR_11 any_CPP11_140
234#define any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG \
235 any_CPP11_120
236#define any_HAVE_INITIALIZER_LIST any_CPP11_120
237#define any_HAVE_NOEXCEPT any_CPP11_140
238#define any_HAVE_NULLPTR any_CPP11_100
239#define any_HAVE_TYPE_TRAITS any_CPP11_90
240#define any_HAVE_STATIC_ASSERT any_CPP11_100
241#define any_HAVE_ADD_CONST any_CPP11_90
242#define any_HAVE_REMOVE_REFERENCE any_CPP11_90
243
244#define any_HAVE_TR1_ADD_CONST (!! any_COMPILER_GNUC_VERSION )
245#define any_HAVE_TR1_REMOVE_REFERENCE (!! any_COMPILER_GNUC_VERSION )
246#define any_HAVE_TR1_TYPE_TRAITS (!! any_COMPILER_GNUC_VERSION )
247
248// Presence of C++14 language features:
249
250#define any_HAVE_CONSTEXPR_14 any_CPP14_000
251
252// Presence of C++17 language features:
253
254#define any_HAVE_NODISCARD any_CPP17_000
255
256// Presence of C++ library features:
257
258#if any_HAVE_CONSTEXPR_11
259# define any_constexpr constexpr
260#else
261# define any_constexpr /*constexpr*/
262#endif
263
264#if any_HAVE_CONSTEXPR_14
265# define any_constexpr14 constexpr
266#else
267# define any_constexpr14 /*constexpr*/
268#endif
269
270#if any_HAVE_NOEXCEPT
271# define any_noexcept noexcept
272#else
273# define any_noexcept /*noexcept*/
274#endif
275
276#if any_HAVE_NULLPTR
277# define any_nullptr nullptr
278#else
279# define any_nullptr NULL
280#endif
281
282#if any_HAVE_NODISCARD
283# define any_nodiscard [[nodiscard]]
284#else
285# define any_nodiscard /*[[nodiscard]]*/
286#endif
287
288// additional includes:
289
290#if ! any_HAVE_NULLPTR
291# include <cstddef>
292#endif
293
294#if any_HAVE_INITIALIZER_LIST
295# include <initializer_list>
296#endif
297
298#if any_HAVE_TYPE_TRAITS
299# include <type_traits>
300#elif any_HAVE_TR1_TYPE_TRAITS
301# include <tr1/type_traits>
302#endif
303
304//
305// any:
306//
307
308namespace nonstd { namespace any_lite {
309
310namespace detail {
311
312// C++11 emulation:
313
314#if any_HAVE_ADD_CONST
315
316using std::add_const;
317
318#elif any_HAVE_TR1_ADD_CONST
319
320using std::tr1::add_const;
321
322#else
323
324template< class T > struct add_const { typedef const T type; };
325
326#endif // any_HAVE_ADD_CONST
327
328#if any_HAVE_REMOVE_REFERENCE
329
330using std::remove_reference;
331
332#elif any_HAVE_TR1_REMOVE_REFERENCE
333
334using std::tr1::remove_reference;
335
336#else
337
338template< class T > struct remove_reference { typedef T type; };
339template< class T > struct remove_reference<T&> { typedef T type; };
340
341#endif // any_HAVE_REMOVE_REFERENCE
342
343} // namespace detail
344
345class bad_any_cast : public std::bad_cast
346{
347public:
348#if any_CPP11_OR_GREATER
349 virtual const char* what() const any_noexcept
350#else
351 virtual const char* what() const throw()
352#endif
353 {
354 return "any-lite: bad any_cast";
355 }
356};
357
358class any
359{
360public:
361 any_constexpr any() any_noexcept
362 : content( any_nullptr )
363 {}
364
365 any( any const & other )
366 : content( other.content ? other.content->clone() : any_nullptr )
367 {}
368
369#if any_CPP11_OR_GREATER
370
371 any( any && other ) any_noexcept
372 : content( std::move( other.content ) )
373 {
374 other.content = any_nullptr;
375 }
376
377 template<
378 class ValueType, class T = typename std::decay<ValueType>::type
379 , typename = typename std::enable_if< ! std::is_same<T, any>::value >::type
380 >
381 any( ValueType && value ) any_noexcept
382 : content( new holder<T>( std::move( value ) ) )
383 {}
384
385 template<
386 class T, class... Args
387 , typename = typename std::enable_if< std::is_constructible<T, Args...>::value >::type
388 >
389 explicit any( nonstd_lite_in_place_type_t(T), Args&&... args )
390 : content( new holder<T>( T( std::forward<Args>(args)... ) ) )
391 {}
392
393 template<
394 class T, class U, class... Args
395 , typename = typename std::enable_if< std::is_constructible<T, std::initializer_list<U>&, Args...>::value >::type
396 >
397 explicit any( nonstd_lite_in_place_type_t(T), std::initializer_list<U> il, Args&&... args )
398 : content( new holder<T>( T( il, std::forward<Args>(args)... ) ) )
399 {}
400
401#else
402
403 template< class ValueType >
404 any( ValueType const & value )
405 : content( new holder<ValueType>( value ) )
406 {}
407
408#endif // any_CPP11_OR_GREATER
409
410 ~any()
411 {
412 reset();
413 }
414
415 any & operator=( any const & other )
416 {
417 any( other ).swap( *this );
418 return *this;
419 }
420
421#if any_CPP11_OR_GREATER
422
423 any & operator=( any && other ) any_noexcept
424 {
425 any( std::move( other ) ).swap( *this );
426 return *this;
427 }
428
429 template<
430 class ValueType, class T = typename std::decay<ValueType>::type
431 , typename = typename std::enable_if< ! std::is_same<T, any>::value >::type
432 >
433 any & operator=( ValueType && value )
434 {
435 any( std::move( value ) ).swap( *this );
436 return *this;
437 }
438
439 template< class T, class... Args >
440 void emplace( Args && ... args )
441 {
442 any( T( std::forward<Args>(args)... ) ).swap( *this );
443 }
444
445 template<
446 class T, class U, class... Args
447 , typename = typename std::enable_if< std::is_constructible<T, std::initializer_list<U>&, Args...>::value >::type
448 >
449 void emplace( std::initializer_list<U> il, Args&&... args )
450 {
451 any( T( il, std::forward<Args>(args)... ) ).swap( *this );
452 }
453
454#else
455
456 template< class ValueType >
457 any & operator=( ValueType const & value )
458 {
459 any( value ).swap( *this );
460 return *this;
461 }
462
463#endif // any_CPP11_OR_GREATER
464
465 void reset() any_noexcept
466 {
467 delete content; content = any_nullptr;
468 }
469
470 void swap( any & other ) any_noexcept
471 {
472 std::swap( content, other.content );
473 }
474
475 bool has_value() const any_noexcept
476 {
477 return content != any_nullptr;
478 }
479
480 const std::type_info & type() const any_noexcept
481 {
482 return has_value() ? content->type() : typeid( void );
483 }
484
485 //
486 // non-standard:
487 //
488
489 template< class ValueType >
490 const ValueType * to_ptr() const
491 {
492 return &( static_cast<holder<ValueType> *>( content )->held );
493 }
494
495 template< class ValueType >
496 ValueType * to_ptr()
497 {
498 return &( static_cast<holder<ValueType> *>( content )->held );
499 }
500
501private:
502 class placeholder
503 {
504 public:
505 virtual ~placeholder()
506 {
507 }
508
509 virtual std::type_info const & type() const = 0;
510
511 virtual placeholder * clone() const = 0;
512 };
513
514 template< typename ValueType >
515 class holder : public placeholder
516 {
517 public:
518 holder( ValueType const & value )
519 : held( value )
520 {}
521
522#if any_CPP11_OR_GREATER
523 holder( ValueType && value )
524 : held( std::move( value ) )
525 {}
526#endif
527
528 virtual std::type_info const & type() const
529 {
530 return typeid( ValueType );
531 }
532
533 virtual placeholder * clone() const
534 {
535 return new holder( held );
536 }
537
538 ValueType held;
539 };
540
541 placeholder * content;
542};
543
544inline void swap( any & x, any & y ) any_noexcept
545{
546 x.swap( y );
547}
548
549#if any_CPP11_OR_GREATER
550
551template< class T, class ...Args >
552inline any make_any( Args&& ...args )
553{
554 return any( nonstd_lite_in_place_type(T), std::forward<Args>(args)...);
555}
556
557template< class T, class U, class ...Args >
558inline any make_any( std::initializer_list<U> il, Args&& ...args )
559{
560 return any( nonstd_lite_in_place_type(T), il, std::forward<Args>(args)...);
561}
562
563#endif // any_CPP11_OR_GREATER
564
565template<
566 class ValueType
567#if any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG
568 , typename = typename std::enable_if< std::is_reference<ValueType>::value || std::is_copy_constructible<ValueType>::value >::type
569#endif
570>
571any_nodiscard inline ValueType any_cast( any const & operand )
572{
573 const ValueType * result = any_cast< typename detail::add_const< typename detail::remove_reference<ValueType>::type >::type >( &operand );
574
575 if ( ! result )
576 {
577 throw bad_any_cast();
578 }
579
580 return *result;
581}
582
583template<
584 class ValueType
585#if any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG
586 , typename = typename std::enable_if< std::is_reference<ValueType>::value || std::is_copy_constructible<ValueType>::value >::type
587#endif
588>
589any_nodiscard inline ValueType any_cast( any & operand )
590{
591 const ValueType * result = any_cast< typename detail::remove_reference<ValueType>::type >( &operand );
592
593 if ( ! result )
594 {
595 throw bad_any_cast();
596 }
597
598 return *result;
599}
600
601#if any_CPP11_OR_GREATER
602
603template<
604 class ValueType
605#if any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG
606 , typename = typename std::enable_if< std::is_reference<ValueType>::value || std::is_copy_constructible<ValueType>::value >::type
607#endif
608>
609any_nodiscard inline ValueType any_cast( any && operand )
610{
611 const ValueType * result = any_cast< typename detail::remove_reference<ValueType>::type >( &operand );
612
613 if ( ! result )
614 {
615 throw bad_any_cast();
616 }
617
618 return *result;
619}
620
621#endif // any_CPP11_OR_GREATER
622
623template< class ValueType >
624any_nodiscard inline ValueType const * any_cast( any const * operand ) any_noexcept
625{
626 return operand != any_nullptr && operand->type() == typeid(ValueType) ? operand->to_ptr<ValueType>() : any_nullptr;
627}
628
629template<class ValueType >
630any_nodiscard inline ValueType * any_cast( any * operand ) any_noexcept
631{
632 return operand != any_nullptr && operand->type() == typeid(ValueType) ? operand->to_ptr<ValueType>() : any_nullptr;
633}
634
635} // namespace any_lite
636
637using namespace any_lite;
638
639} // namespace nonstd
640
641#endif // any_USES_STD_ANY
642
643#endif // NONSTD_ANY_LITE_HPP