/[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 457 - (hide annotations) (download) (as text)
Mon Mar 14 22:29:46 2005 UTC (19 years, 1 month ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 4171 byte(s)
fixed tiny exception bug

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     }
75    
76     optional& operator =(const T& arg) {
77     this->data = arg;
78     initialized = true;
79     }
80    
81     const T& operator *() const throw (LinuxSamplerException) { return get(); }
82     T& operator *() throw (LinuxSamplerException) { return get(); }
83    
84     const T* operator ->() const throw (LinuxSamplerException) {
85     if (!initialized) throw LinuxSamplerException("optional variable not initialized");
86     return &data;
87     }
88    
89     T* operator ->() throw (LinuxSamplerException) {
90     if (!initialized) throw LinuxSamplerException("optional variable not initialized");
91     return &data;
92     }
93    
94     operator bool() const { return initialized; }
95 senkov 134 bool operator !() const { return !initialized; }
96 schoenebeck 123
97     protected:
98     T data;
99     bool initialized;
100     };
101    
102     } // namespace LinuxSampler
103    
104     #endif // __LS_OPTIONAL_H__

  ViewVC Help
Powered by ViewVC