-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a minimal Optional for C++11
- Loading branch information
1 parent
76de222
commit d821f11
Showing
1 changed file
with
29 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,40 @@ | ||
#ifdef __has_include // Check if __has_include is present | ||
# if __has_include(<optional>) // Check for a standard library | ||
# if __has_include(<optional>) // Check for a standard version | ||
# include <optional> | ||
# if defined(__cpp_lib_optional) // C++ >= 17 | ||
namespace LOG4CXX_NS { template< class T > using Optional = std::optional<T>; } | ||
#define LOG4CXX_HAS_STD_OPTIONAL 1 | ||
#endif | ||
# elif __has_include(<experimental/optional>) // Check for an experimental version | ||
# include <experimental/optional> | ||
namespace LOG4CXX_NS { template< class T > using Optional = std::experimental::optional<T>; } | ||
#define LOG4CXX_HAS_STD_OPTIONAL 1 | ||
# elif __has_include(<boost/optional.hpp>) // Try with an external library | ||
# include <boost/optional.hpp> | ||
namespace LOG4CXX_NS { template< class T > using Optional = boost::optional<T>; } | ||
#define LOG4CXX_HAS_STD_OPTIONAL 1 | ||
# else // Not found at all | ||
# error "Missing <optional>" | ||
#define LOG4CXX_HAS_STD_OPTIONAL 0 | ||
# endif | ||
#endif | ||
#endif | ||
|
||
#if !LOG4CXX_HAS_STD_OPTIONAL // Implement a minimal Optional? | ||
namespace LOG4CXX_NS | ||
{ | ||
template< class T > | ||
class Optional : private std::pair<bool, T> | ||
{ | ||
using BaseType = std::pair<bool, T>; | ||
public: | ||
Optional() : BaseType(false, T()) {} | ||
Optional& operator=(const T& value) | ||
{ | ||
this->first = true; | ||
this->second = value; | ||
return *this; | ||
} | ||
bool has_value() const { return this->first; } | ||
const T& value() const { return this->second; } | ||
}; | ||
} // namespace LOG4CXX_NS | ||
#endif |