-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomponent.h
95 lines (82 loc) · 2.86 KB
/
component.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/** Component
* Copyright (C) Madura A.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02111-1307, USA.
*/
#pragma once
#include <atomic>
#include <string>
#include <vector>
namespace vrok {
enum class ComponentType { Decoder, Effect, Driver, Player, None };
enum class PropertyType { INT, FLT, DBL };
struct PropertyInfo {
double range_from = 0.0;
double range_to = 0.0;
double increment = 0.0; // 0.0 means any
double default_value = 0.0;
std::vector<std::string> enum_names;
PropertyInfo() = default;
PropertyInfo(double range_from_, double range_to_, double increment_, double default_value_,
std::initializer_list<std::string> enum_names_) {
range_from = range_from_;
range_to = range_to_;
increment = increment_;
default_value = default_value_;
enum_names.insert(enum_names.end(), enum_names_.begin(), enum_names_.end());
}
};
class PropertyBase {
protected:
std::string _name;
PropertyInfo _info;
public:
void SetName(std::string name);
std::string GetName();
virtual PropertyType GetType() = 0;
virtual void Get(void *ptr) = 0;
virtual void Set(void *ptr) = 0;
uint32_t Size();
void SetPropertyInfo(const PropertyInfo &info);
const PropertyInfo &GetPropertyInfo();
};
template <typename T>
class Property : public PropertyBase {
private:
PropertyType _type;
std::atomic<T> _data;
T _default;
public:
Property();
PropertyType GetType() { return _type; }
void Get(void *ptr) { *((T *)ptr) = _data.load(std::memory_order_relaxed); }
T Get() { return _data.load(std::memory_order_relaxed); }
void Set(T val) { _data.store(val, std::memory_order_relaxed); }
void Set(void *ptr) { _data.store(*((T *)ptr), std::memory_order_relaxed); }
};
class Component {
public:
Component();
virtual vrok::ComponentType ComponentType() = 0;
virtual Component *CreateSelf() = 0;
virtual const char *ComponentName() = 0;
virtual const char *Description() { return ""; }
virtual const char *Author() { return ""; }
virtual const char *License() { return ""; }
virtual void PropertyChanged(PropertyBase *property) { }
virtual ~Component();
};
}