/[svn]/linuxsampler/trunk/src/common/optional.h
ViewVC logotype

Annotation of /linuxsampler/trunk/src/common/optional.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 810 - (hide annotations) (download) (as text)
Tue Nov 22 11:49:40 2005 UTC (18 years, 5 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 4231 byte(s)
* src/common/optional.h: fixed '=' operator which did not return anything

1 schoenebeck 123 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 457 * Copyright (C) 2005 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     #include "LinuxSamplerException.h"
28    
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     const T& get() const throw (LinuxSamplerException) {
61     if (!initialized) throw LinuxSamplerException("optional variable not initialized");
62     return data;
63     }
64    
65     T& get() throw (LinuxSamplerException) {
66     if (!initialized) throw LinuxSamplerException("optional variable not initialized");
67     return data;
68     }
69    
70     optional& operator =(const optional& arg) throw (LinuxSamplerException) {
71 schoenebeck 457 if (!arg.initialized) throw LinuxSamplerException("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     const T& operator *() const throw (LinuxSamplerException) { return get(); }
84     T& operator *() throw (LinuxSamplerException) { return get(); }
85    
86     const T* operator ->() const throw (LinuxSamplerException) {
87     if (!initialized) throw LinuxSamplerException("optional variable not initialized");
88     return &data;
89     }
90    
91     T* operator ->() throw (LinuxSamplerException) {
92     if (!initialized) throw LinuxSamplerException("optional variable not initialized");
93     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__

  ViewVC Help
Powered by ViewVC