aboutsummaryrefslogtreecommitdiff
path: root/butl/optional
blob: fc34b33fe0787ac604b24cad5836a49e022dbf6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 <typename T>
  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