From faf53e5fdda42a40959dda689532af6f030d9fa2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 11 Jun 2015 18:36:04 +0200 Subject: Add optional implementation --- butl/optional | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 butl/optional (limited to 'butl/optional') diff --git a/butl/optional b/butl/optional new file mode 100644 index 0000000..fc34b33 --- /dev/null +++ b/butl/optional @@ -0,0 +1,39 @@ +// file : butl/optional -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef BUTL_OPTIONAL +#define BUTL_OPTIONAL + +namespace butl +{ + // Simple optional class template while waiting for std::optional. + // + template + class optional + { + public: + typedef T value_type; + + optional (): null_ (true) {} + optional (const T& v): value_ (v), null_ (false) {} + optional& operator= (const T& v) {value_ = v; null_ = false; return *this;} + + T& value () {return value_;} + const T& value () const {return value_;} + + T* operator-> () {return &value_;} + const T* operator-> () const {return &value_;} + + T& operator* () {return value_;} + const T& operator* () const {return value_;} + + explicit operator bool () const {return !null_;} + + private: + T value_; + bool null_; + }; +} + +#endif // BUTL_OPTIONAL -- cgit v1.1