1 |
schoenebeck |
123 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
|
|
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
schoenebeck |
880 |
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
7 |
schoenebeck |
123 |
* * |
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 |
schoenebeck |
880 |
#include "Exception.h" |
28 |
schoenebeck |
123 |
|
29 |
|
|
namespace LinuxSampler { |
30 |
|
|
|
31 |
|
|
class optional_base { |
32 |
|
|
public: |
33 |
|
|
class nothing_t { public: nothing_t() {} }; |
34 |
|
|
|
35 |
|
|
static const nothing_t nothing; |
36 |
|
|
}; |
37 |
|
|
|
38 |
|
|
template<class T> |
39 |
|
|
class optional : public optional_base { |
40 |
|
|
public: |
41 |
|
|
optional() { |
42 |
|
|
initialized = false; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
optional(T data) { |
46 |
|
|
this->data = data; |
47 |
|
|
initialized = true; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
optional(nothing_t) { |
51 |
|
|
initialized = false; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
template <class T_inner> |
55 |
|
|
optional(T_inner data) { |
56 |
|
|
this->data = T(data); |
57 |
|
|
initialized = true; |
58 |
|
|
} |
59 |
|
|
|
60 |
schoenebeck |
880 |
const T& get() const throw (Exception) { |
61 |
|
|
if (!initialized) throw Exception("optional variable not initialized"); |
62 |
schoenebeck |
123 |
return data; |
63 |
|
|
} |
64 |
|
|
|
65 |
schoenebeck |
880 |
T& get() throw (Exception) { |
66 |
|
|
if (!initialized) throw Exception("optional variable not initialized"); |
67 |
schoenebeck |
123 |
return data; |
68 |
|
|
} |
69 |
|
|
|
70 |
schoenebeck |
880 |
optional& operator =(const optional& arg) throw (Exception) { |
71 |
|
|
if (!arg.initialized) throw Exception("optional variable not initialized"); |
72 |
schoenebeck |
123 |
this->data = arg.data; |
73 |
|
|
initialized = true; |
74 |
schoenebeck |
810 |
return *this; |
75 |
schoenebeck |
123 |
} |
76 |
|
|
|
77 |
|
|
optional& operator =(const T& arg) { |
78 |
|
|
this->data = arg; |
79 |
|
|
initialized = true; |
80 |
schoenebeck |
810 |
return *this; |
81 |
schoenebeck |
123 |
} |
82 |
|
|
|
83 |
schoenebeck |
880 |
const T& operator *() const throw (Exception) { return get(); } |
84 |
|
|
T& operator *() throw (Exception) { return get(); } |
85 |
schoenebeck |
123 |
|
86 |
schoenebeck |
880 |
const T* operator ->() const throw (Exception) { |
87 |
|
|
if (!initialized) throw Exception("optional variable not initialized"); |
88 |
schoenebeck |
123 |
return &data; |
89 |
|
|
} |
90 |
|
|
|
91 |
schoenebeck |
880 |
T* operator ->() throw (Exception) { |
92 |
|
|
if (!initialized) throw Exception("optional variable not initialized"); |
93 |
schoenebeck |
123 |
return &data; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
operator bool() const { return initialized; } |
97 |
senkov |
134 |
bool operator !() const { return !initialized; } |
98 |
schoenebeck |
123 |
|
99 |
|
|
protected: |
100 |
|
|
T data; |
101 |
|
|
bool initialized; |
102 |
|
|
}; |
103 |
|
|
|
104 |
|
|
} // namespace LinuxSampler |
105 |
|
|
|
106 |
|
|
#endif // __LS_OPTIONAL_H__ |