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 */
Definition: Option.h:35
t::ret< T > operator*() const
Definition: Option.h:47
bool none() const
Definition: Option.h:43
Option(const Option< T > &opt)
Definition: Option.h:40
Option< T > & operator=(t::in< T > value)
Definition: Option.h:56
Option(t::in< T > value)
Definition: Option.h:39
bool some() const
Definition: Option.h:42
bool isNone() const
Definition: Option.h:71
Option(const OptionalNone &none)
Definition: Option.h:38
bool equals(const OptionalNone &_) const
Definition: Option.h:59
bool operator!=(const Option< T > &o) const
Definition: Option.h:65
bool operator!=(const OptionalNone &_) const
Definition: Option.h:63
const Option< T > & if_else(const F &f) const
Definition: Option.h:51
Option()
Definition: Option.h:37
t::ret< T > value() const
Definition: Option.h:45
const Option< T > & if_one(const F &f) const
Definition: Option.h:49
bool equals(const Option< T > &opt) const
Definition: Option.h:60
Option< T > & operator=(const Option< T > &opt)
Definition: Option.h:54
bool operator==(const Option< T > &o) const
Definition: Option.h:64
bool isOne() const
Definition: Option.h:70
bool operator==(const OptionalNone &_) const
Definition: Option.h:62
Definition: Option.h:30
static void put(T &x, const T &v)
Definition: type_info.h:69
AutoStringStartup & _
Definition: debug_CrashHandler.cpp:235
typename type_info< T >::var_t var
Definition: type_info.h:282
typename type_info< T >::ret_t ret
Definition: type_info.h:285
typename type_info< T >::in_t in
Definition: type_info.h:283
Definition: adapter.h:26
const OptionalNone none
Definition: util_Option.cpp:154
Option< T > some(const T &val)
Definition: Option.h:81