JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
optional.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2// Project : VST SDK
3//
4// Category : Helpers
5// Filename : public.sdk/source/vst/utility/optional.h
6// Created by : Steinberg, 08/2016
7// Description : optional helper
8//
9//-----------------------------------------------------------------------------
10// LICENSE
11// (c) 2023, Steinberg Media Technologies GmbH, All Rights Reserved
12//-----------------------------------------------------------------------------
13// Redistribution and use in source and binary forms, with or without modification,
14// are permitted provided that the following conditions are met:
15//
16// * Redistributions of source code must retain the above copyright notice,
17// this list of conditions and the following disclaimer.
18// * Redistributions in binary form must reproduce the above copyright notice,
19// this list of conditions and the following disclaimer in the documentation
20// and/or other materials provided with the distribution.
21// * Neither the name of the Steinberg Media Technologies nor the names of its
22// contributors may be used to endorse or promote products derived from this
23// software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34// OF THE POSSIBILITY OF SUCH DAMAGE.
35//-----------------------------------------------------------------------------
36
37#pragma once
38
39#include <cassert>
40#include <memory>
41#include <utility>
42
43//------------------------------------------------------------------------
44namespace VST3 {
45
46//------------------------------------------------------------------------
47template <typename T>
49{
50 Optional () noexcept : valid (false) {}
51 explicit Optional (const T& v) noexcept : _value (v), valid (true) {}
52 Optional (T&& v) noexcept : _value (std::move (v)), valid (true) {}
53
54 Optional (Optional&& other) noexcept { *this = std::move (other); }
55 Optional& operator= (Optional&& other) noexcept
56 {
57 valid = other.valid;
58 _value = std::move (other._value);
59 return *this;
60 }
61
62 explicit operator bool () const noexcept
63 {
64 setValidationChecked ();
65 return valid;
66 }
67
68 const T& operator* () const noexcept
69 {
70 checkValid ();
71 return _value;
72 }
73
74 const T* operator-> () const noexcept
75 {
76 checkValid ();
77 return &_value;
78 }
79
80 T& operator* () noexcept
81 {
82 checkValid ();
83 return _value;
84 }
85
86 T* operator-> () noexcept
87 {
88 checkValid ();
89 return &_value;
90 }
91
92 T&& value () noexcept
93 {
94 checkValid ();
95 return move (_value);
96 }
97
98 const T& value () const noexcept
99 {
100 checkValid ();
101 return _value;
102 }
103
104 void swap (T& other) noexcept
105 {
106 checkValid ();
107 auto tmp = std::move (other);
108 other = std::move (_value);
109 _value = std::move (tmp);
110 }
111
112private:
113 T _value {};
114 bool valid;
115
116#if !defined(NDEBUG)
117 mutable bool validationChecked {false};
118#endif
119
120 void setValidationChecked () const
121 {
122#if !defined(NDEBUG)
123 validationChecked = true;
124#endif
125 }
126 void checkValid () const
127 {
128#if !defined(NDEBUG)
129 assert (validationChecked);
130#endif
131 }
132};
133
134//------------------------------------------------------------------------
135}
assert