Audio Processing Framework (APF) version 0.5.0
shareddata.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_SHAREDDATA_H
31#define APF_SHAREDDATA_H
32
33#include "apf/commandqueue.h"
34
35namespace apf
36{
37
38template<typename X>
39class SharedData
40{
41 public:
42 class SetCommand;
43
44 explicit SharedData(CommandQueue& fifo, X&& def = X())
45 : _fifo(fifo)
46 , _data(std::forward<X>(def))
47 {}
48
50 const X& get() const { return _data; }
51
52 operator const X&() const { return this->get(); }
53
54 void operator=(const X& rhs)
55 {
56 _fifo.push(new SetCommand(&_data, rhs));
57 }
58
59 void operator=(X&& rhs)
60 {
61 _fifo.push(new SetCommand(&_data, std::forward<X>(rhs)));
62 }
63
64 void set_from_rt_thread(X&& data)
65 {
66 _data = std::forward<X>(data);
67 }
68
69 friend bool
70 operator==(const SharedData& lhs, const X& rhs) { return lhs._data == rhs; }
71 friend bool
72 operator==(const X& lhs, const SharedData& rhs) { return lhs == rhs._data; }
73 friend bool
74 operator!=(const SharedData& lhs, const X& rhs) { return lhs._data != rhs; }
75 friend bool
76 operator!=(const X& lhs, const SharedData& rhs) { return lhs != rhs._data; }
77
78 private:
79 CommandQueue& _fifo;
80 X _data;
81};
82
83template<typename X>
84class SharedData<X>::SetCommand : public CommandQueue::Command
85{
86 public:
87 SetCommand(X* pointer, const X& data)
88 : _pointer(pointer)
89 , _data(data)
90 {
91 assert(pointer != nullptr);
92 }
93
94 SetCommand(X* pointer, X&& data)
95 : _pointer(pointer)
96 , _data(std::forward<X>(data))
97 {
98 assert(pointer != nullptr);
99 }
100
101 private:
102 virtual void execute()
103 {
104 using std::swap;
105 swap(*_pointer, _data);
106 }
107
108 virtual void cleanup() {}
109
110 X* _pointer;
111 X _data;
112};
113
114} // namespace apf
115
116#endif
Command queue.
Audio Processing Framework.
Definition: iterator.h:61