1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2010 Christian Schoenebeck * |
7 |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
12 |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
17 |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#ifndef __LS_OPTIONAL_H__ |
25 |
#define __LS_OPTIONAL_H__ |
26 |
|
27 |
#include "Exception.h" |
28 |
|
29 |
namespace LinuxSampler { |
30 |
|
31 |
/** |
32 |
* Base class of template class optional, not meant to be instantiated |
33 |
* directly. It just provides the optional<T>::nothing member. |
34 |
*/ |
35 |
class optional_base { |
36 |
public: |
37 |
class nothing_t { public: nothing_t() {} }; |
38 |
|
39 |
static const nothing_t nothing; |
40 |
}; |
41 |
|
42 |
/** |
43 |
* This class can be used for any variable that might not have a value |
44 |
* set. E.g. as a return value type of a function, since in the case of |
45 |
* return values of functions it's often difficult to return a pointer |
46 |
* variable which might do the trick of an optional return value. It |
47 |
* behaves pretty much like a pointer though. That is, it can be checked |
48 |
* against NULL and the actual value can be dereferenced with the |
49 |
* typical C pointer dereference operators. |
50 |
*/ |
51 |
template<class T> |
52 |
class optional : public optional_base { |
53 |
public: |
54 |
optional() { |
55 |
initialized = false; |
56 |
} |
57 |
|
58 |
optional(T data) { |
59 |
this->data = data; |
60 |
initialized = true; |
61 |
} |
62 |
|
63 |
optional(nothing_t) { |
64 |
initialized = false; |
65 |
} |
66 |
|
67 |
template <class T_inner> |
68 |
optional(T_inner data) { |
69 |
this->data = T(data); |
70 |
initialized = true; |
71 |
} |
72 |
|
73 |
const T& get() const throw (Exception) { |
74 |
if (!initialized) throw Exception("optional variable not initialized"); |
75 |
return data; |
76 |
} |
77 |
|
78 |
T& get() throw (Exception) { |
79 |
if (!initialized) throw Exception("optional variable not initialized"); |
80 |
return data; |
81 |
} |
82 |
|
83 |
optional& operator =(const optional& arg) { |
84 |
this->data = arg.data; |
85 |
initialized = arg.initialized; |
86 |
return *this; |
87 |
} |
88 |
|
89 |
optional& operator =(const T& arg) { |
90 |
this->data = arg; |
91 |
initialized = true; |
92 |
return *this; |
93 |
} |
94 |
|
95 |
const T& operator *() const throw (Exception) { return get(); } |
96 |
T& operator *() throw (Exception) { return get(); } |
97 |
|
98 |
const T* operator ->() const throw (Exception) { |
99 |
if (!initialized) throw Exception("optional variable not initialized"); |
100 |
return &data; |
101 |
} |
102 |
|
103 |
T* operator ->() throw (Exception) { |
104 |
if (!initialized) throw Exception("optional variable not initialized"); |
105 |
return &data; |
106 |
} |
107 |
|
108 |
operator bool() const { return initialized; } |
109 |
bool operator !() const { return !initialized; } |
110 |
|
111 |
protected: |
112 |
T data; |
113 |
bool initialized; |
114 |
}; |
115 |
|
116 |
} // namespace LinuxSampler |
117 |
|
118 |
#endif // __LS_OPTIONAL_H__ |