Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Option.h
1 /*
2  * Option class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2006, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_UTIL_OPTION_H
22 #define ELM_UTIL_OPTION_H
23 
24 #include <elm/assert.h>
25 #include <elm/type_info.h>
26 
27 namespace elm {
28 
29 // OptionalNone value
30 class OptionalNone { };
31 extern const OptionalNone none;
32 
33 
34 // Optional value
35 template <class T> class Option {
36 public:
37  inline Option(): one(false) { }
38  inline Option(const OptionalNone& none): one(false) { }
39  inline Option(t::in<T> value): one(true) { t::put<T>(val, value); }
40  inline Option(const Option<T> &opt): one(opt.one) { t::put<T>(val, t::get<T>(opt.val)); }
41 
42  inline bool some() const { return one; }
43  inline bool none() const { return !one; }
44 
45  inline t::ret<T> value() const
46  { ASSERTP(one, "no value in option"); return t::get<T>(val); }
47  inline t::ret<T> operator*() const { return value(); }
48 
49  template <class F> const Option<T>& if_one(const F& f) const
50  { if(one) f(t::get<T>(val)); return *this; }
51  template <class F> const Option<T>& if_else(const F& f) const
52  { if(!one) f(); return *this; }
53 
54  inline Option<T>& operator=(const Option<T>& opt)
55  { one = opt.one; if(opt.one) t::put<T>(val, opt.val); return *this; }
57  { one = true; type_info<T>::put(val, value); return *this; }
58 
59  inline bool equals(const OptionalNone& _) const { return none(); }
60  inline bool equals(const Option<T> &opt) const
61  { return (this->none() && opt.none()) || (this->some() && opt.some() && val == opt.val); }
62  inline bool operator==(const OptionalNone& _) const { return none(); }
63  inline bool operator!=(const OptionalNone& _) const { return !none(); }
64  inline bool operator==(const Option<T>& o) const { return equals(o); }
65  inline bool operator!=(const Option<T>& o) const { return !equals(o); }
66 
67  // deprecated
68  inline operator bool(void) const { return some(); }
69  inline operator t::ret<T>() const { return value(); }
70  inline bool isOne() const { return one; }
71  inline bool isNone() const { return !one; }
72 
73 private:
74  bool one;
75  typename t::var<T> val;
76 };
77 
78 
79 // Fast Option building
80 template <class T>
81 inline Option<T> some(const T& val) { return Option<T>(val); };
82 
83 } // elm
84 
85 #endif /* ELM_UTIL_OPTION_H */
elm::Option
Definition: Option.h:35
elm::Option::value
t::ret< T > value() const
Definition: Option.h:45
elm::Option::Option
Option(const Option< T > &opt)
Definition: Option.h:40
elm::Option::operator==
bool operator==(const Option< T > &o) const
Definition: Option.h:64
elm::Option::isOne
bool isOne() const
Definition: Option.h:70
elm::Option::operator!=
bool operator!=(const Option< T > &o) const
Definition: Option.h:65
elm::Option::if_one
const Option< T > & if_one(const F &f) const
Definition: Option.h:49
elm::t::var
typename type_info< T >::var_t var
Definition: type_info.h:282
elm::Option::operator==
bool operator==(const OptionalNone &_) const
Definition: Option.h:62
value
elm::some
Option< T > some(const T &val)
Definition: Option.h:81
elm::Option::operator*
t::ret< T > operator*() const
Definition: Option.h:47
elm::Option::equals
bool equals(const Option< T > &opt) const
Definition: Option.h:60
bool
elm::t::in
typename type_info< T >::in_t in
Definition: type_info.h:283
elm::Option::isNone
bool isNone() const
Definition: Option.h:71
elm::Option::none
bool none() const
Definition: Option.h:43
elm::_
AutoStringStartup & _
Definition: debug_CrashHandler.cpp:232
elm::Option::operator=
Option< T > & operator=(t::in< T > value)
Definition: Option.h:56
elm::none
const OptionalNone none
Definition: util_Option.cpp:154
elm
Definition: adapter.h:26
elm::Option::equals
bool equals(const OptionalNone &_) const
Definition: Option.h:59
elm::Option::operator=
Option< T > & operator=(const Option< T > &opt)
Definition: Option.h:54
elm::OptionalNone
Definition: Option.h:30
elm::Option::some
bool some() const
Definition: Option.h:42
elm::type_info::put
static void put(T &x, const T &v)
Definition: type_info.h:69
elm::Option::Option
Option(const OptionalNone &none)
Definition: Option.h:38
elm::Option::if_else
const Option< T > & if_else(const F &f) const
Definition: Option.h:51
elm::t::ret
typename type_info< T >::ret_t ret
Definition: type_info.h:285
elm::Option::Option
Option(t::in< T > value)
Definition: Option.h:39
elm::Option::operator!=
bool operator!=(const OptionalNone &_) const
Definition: Option.h:63
elm::Option::Option
Option()
Definition: Option.h:37