JSON Voorhees
Killer JSON for C++
optional.hpp
Go to the documentation of this file.
1 /** \file jsonv/optional.hpp
2  * Pulls in an implementation of \c optional.
3  *
4  * Copyright (c) 2016 by Travis Gockel. All rights reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
7  * as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
8  * version.
9  *
10  * \author Travis Gockel (travis@gockelhut.com)
11 **/
12 #ifndef __JSONV_OPTIONAL_HPP_INCLUDED__
13 #define __JSONV_OPTIONAL_HPP_INCLUDED__
14 
15 #include <jsonv/config.hpp>
16 
17 #ifndef JSONV_OPTIONAL_USE_STD
18 # define JSONV_OPTIONAL_USE_STD 0
19 #endif
20 
21 #ifndef JSONV_OPTIONAL_USE_STD_EXPERIMENTAL
22 # define JSONV_OPTIONAL_USE_STD_EXPERIMENTAL 0
23 #endif
24 
25 #ifndef JSONV_OPTIONAL_USE_BOOST
26 # define JSONV_OPTIONAL_USE_BOOST 0
27 #endif
28 
29 // Attempt to guess between JSONV_OPTIONAL_USE_STD and JSONV_OPTIONAL_USE_STD_EXPERIMENTAL based on C++ language version
30 #if !defined(JSONV_OPTIONAL_TYPE) && !JSONV_OPTIONAL_USE_STD && !JSONV_OPTIONAL_USE_STD_EXPERIMENTAL && !JSONV_OPTIONAL_USE_BOOST
31 # if __cplusplus > 201700L
32 # undef JSONV_OPTIONAL_USE_STD
33 # define JSONV_OPTIONAL_USE_STD 1
34 # else
35 # undef JSONV_OPTIONAL_USE_STD_EXPERIMENTAL
36 # define JSONV_OPTIONAL_USE_STD_EXPERIMENTAL 1
37 # endif
38 #endif
39 
40 /** \def JSONV_OPTIONAL_TYPE
41  * The type to use for \c jsonv::optional. By default, this is \c std::optional or \c std::experimental::optional,
42  * depending on the C++ language version you are compiling with.
43  *
44  * \def JSONV_NULLOPT_VALUE
45  * The value to use for \c jsonv::nullopt By default, this is \c std::nullopt or \c std::experimental::nullopt (by the
46  * same rules as \c JSONV_OPTIONAL_TYPE). If you define \c JSONV_OPTIONAL_TYPE, you must also define this.
47  *
48  * \def JSONV_OPTIONAL_INCLUDE
49  * The file to include to get the implementation for \c optional. If you define \c JSONV_OPTIONAL_TYPE, you must
50  * also define this.
51  *
52  * \def JSONV_OPTIONAL_USE_STD
53  * Set this to 1 to use \c std::optional as the backing type for \c jsonv::optional.
54  *
55  * \def JSONV_OPTIONAL_USE_STD_EXPERIMENTAL
56  * Set this to 1 to use \c std::experimental::optional as the backing type for \c jsonv::optional.
57  *
58  * \def JSONV_OPTIONAL_USE_BOOST
59  * Set this to 1 to use \c boost::optional as the backing type for \c jsonv::optional.
60 **/
61 #ifndef JSONV_OPTIONAL_TYPE
62 # if defined(JSONV_OPTIONAL_USE_STD_EXPERIMENTAL) && JSONV_OPTIONAL_USE_STD_EXPERIMENTAL
63 # define JSONV_OPTIONAL_TYPE std::experimental::optional
64 # define JSONV_NULLOPT_VALUE std::experimental::nullopt
65 # define JSONV_OPTIONAL_INCLUDE <experimental/optional>
66 # elif defined(JSONV_OPTIONAL_USE_BOOST) && JSONV_OPTIONAL_USE_BOOST
67 # define JSONV_OPTIONAL_TYPE boost::optional
68 # define JSONV_NULLOPT_VALUE boost::none
69 # define JSONV_OPTIONAL_INCLUDE <boost/optional.hpp>
70 # else
71 # define JSONV_OPTIONAL_TYPE std::optional
72 # define JSONV_NULLOPT_VALUE std::nullopt
73 # define JSONV_OPTIONAL_INCLUDE <optional>
74 # endif
75 #endif
76 
77 #include JSONV_OPTIONAL_INCLUDE
78 
79 namespace jsonv
80 {
81 
82 /** Represents a value that may or may not be present.
83  *
84  * \see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html
85 **/
86 template <typename T>
87 using optional = JSONV_OPTIONAL_TYPE<T>;
88 
89 namespace
90 {
91 
92 constexpr auto& nullopt JSONV_UNUSED = JSONV_NULLOPT_VALUE;
93 
94 }
95 
96 }
97 
98 #endif/*__JSONV_OPTIONAL_HPP_INCLUDED__*/
Copyright (c) 2014-2019 by Travis Gockel.
#define JSONV_NULLOPT_VALUE
The value to use for jsonv::nullopt By default, this is std::nullopt or std::experimental::nullopt (b...
Definition: optional.hpp:64
#define JSONV_UNUSED
Note that you know the variable is unused, but make the compiler stop complaining about it...
Definition: config.hpp:115
JSONV_OPTIONAL_TYPE< T > optional
Represents a value that may or may not be present.
Definition: optional.hpp:87