Audio Processing Framework (APF) version 0.5.0
misc.h
Go to the documentation of this file.
1/******************************************************************************
2 Copyright (c) 2012-2016 Institut für Nachrichtentechnik, Universität Rostock
3 Copyright (c) 2006-2012 Quality & Usability Lab
4 Deutsche Telekom Laboratories, TU Berlin
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23*******************************************************************************/
24
25// https://AudioProcessingFramework.github.io/
26
29
30#ifndef APF_MISC_H
31#define APF_MISC_H
32
33#include <utility> // for std::forward
34#include <type_traits> // for std::is_base_of
35
36namespace apf
37{
38
41template<typename Derived>
42class CRTP
43{
44 public:
45 Derived& derived()
46 {
47 static_assert(std::is_base_of<CRTP, Derived>::value
48 , "Derived must be derived from CRTP (as the name suggests)!");
49
50 return *static_cast<Derived*>(this);
51 }
52
53 protected:
54 CRTP() = default;
55 ~CRTP() = default;
56};
57
60{
61 protected:
62 NonCopyable() = default;
63 ~NonCopyable() = default;
64
65 public:
67
68 NonCopyable(const NonCopyable&) = delete;
69 NonCopyable& operator=(const NonCopyable&) = delete;
71
73
74 NonCopyable(NonCopyable&&) = default;
75 NonCopyable& operator=(NonCopyable&&) = default;
77};
78
97template<typename T>
99{
100 public:
102 template<typename... Args>
103 explicit BlockParameter(Args&&... args)
104 : _current(args...)
105 , _old(std::forward<Args>(args)...)
106 {}
107
110 template<typename Arg>
111 T& operator=(Arg&& arg)
112 {
113#ifndef NDEBUG
114 ++_assignments;
115#endif
116
117 _old = std::move(_current);
118 return _current = std::forward<Arg>(arg);
119 }
120
121 const T& get() const { return _current; }
122 const T& old() const { return _old; }
123
125 operator const T&() const { return this->get(); }
126
128 bool changed() const { return this->get() != this->old(); }
129
132 BlockParameter& operator+=(const T& rhs) { _current += rhs; return *this; }
133 BlockParameter& operator-=(const T& rhs) { _current -= rhs; return *this; }
134 BlockParameter& operator*=(const T& rhs) { _current *= rhs; return *this; }
135 BlockParameter& operator/=(const T& rhs) { _current /= rhs; return *this; }
136 BlockParameter& operator%=(const T& rhs) { _current %= rhs; return *this; }
137 BlockParameter& operator&=(const T& rhs) { _current &= rhs; return *this; }
138 BlockParameter& operator|=(const T& rhs) { _current |= rhs; return *this; }
139 BlockParameter& operator<<=(const T& rhs){ _current <<= rhs; return *this; }
140 BlockParameter& operator>>=(const T& rhs){ _current >>= rhs; return *this; }
141
142 BlockParameter& operator++() { ++_current; return *this; }
143 BlockParameter& operator--() { --_current; return *this; }
144 T operator++(int) { return _current++; }
145 T operator--(int) { return _current--; }
147
148 private:
149 class both_proxy
150 {
151 public:
152 explicit both_proxy(const BlockParameter& param) : _p(param) {}
153
154#define APF_BLOCKPARAMETER_BOTH_PROXY_OPERATORS(opstring) \
155 friend bool operator opstring(const both_proxy& lhs, const T& rhs) { \
156 return lhs._p.get() opstring rhs && lhs._p.old() opstring rhs; } \
157 friend bool operator opstring(const T& lhs, const both_proxy& rhs) { \
158 return lhs opstring rhs._p.get() && lhs opstring rhs._p.old(); }
159
160 APF_BLOCKPARAMETER_BOTH_PROXY_OPERATORS(==)
161 APF_BLOCKPARAMETER_BOTH_PROXY_OPERATORS(!=)
162 APF_BLOCKPARAMETER_BOTH_PROXY_OPERATORS(<)
163 APF_BLOCKPARAMETER_BOTH_PROXY_OPERATORS(>)
164 APF_BLOCKPARAMETER_BOTH_PROXY_OPERATORS(<=)
165 APF_BLOCKPARAMETER_BOTH_PROXY_OPERATORS(>=)
166#undef APF_BLOCKPARAMETER_BOTH_PROXY_OPERATORS
167
168 private:
169 const BlockParameter& _p;
170 };
171
172 public:
173
174 both_proxy both() const { return both_proxy(*this); }
175
176#ifndef NDEBUG
177 bool no_multiple_assignments() const
178 {
179 auto result = (_assignments <= 1);
180 _assignments = 0;
181 return result;
182 }
183
184 bool exactly_one_assignment() const
185 {
186 auto result = (_assignments == 1);
187 _assignments = 0;
188 return result;
189 }
190#endif
191
192 private:
193 T _current, _old;
194
195#ifndef NDEBUG
196 mutable int _assignments = 0;
197#endif
198};
199
200} // namespace apf
201
202#endif
Hold current and old value of any type.
Definition: misc.h:99
T & operator=(Arg &&arg)
Assignment operator.
Definition: misc.h:111
const T & old() const
Get old value.
Definition: misc.h:122
BlockParameter(Args &&... args)
Constructor. Any arguments are forwarded to both old and current value.
Definition: misc.h:103
const T & get() const
Get current value.
Definition: misc.h:121
bool changed() const
Check if value has changed between before the last assignment and now.
Definition: misc.h:128
Curiously Recurring Template Pattern (CRTP) base class.
Definition: misc.h:43
~CRTP()=default
Protected destructor to avoid base class pointers.
Classes derived from this class cannot be copied (but still moved).
Definition: misc.h:60
~NonCopyable()=default
Protected non-virtual destructor.
NonCopyable()=default
Protected default constructor.
Audio Processing Framework.
Definition: iterator.h:61