forked from ctlbox/controlbox-cpp
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ValueModels.h
162 lines (133 loc) · 4.21 KB
/
ValueModels.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright 2014-2015 Matthew McGowan.
*
* This file is part of Nice Firmware.
*
* BrewPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BrewPi. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "pointer_scalar.h"
#include "Values.h"
/**
* A streamable value whose data resizes externally.
*/
// TODO - factor ExternalValue, eeprom value and progmem value - all have address and size.
class ExternalValue : public WritableValue
{
void* _pValue;
uint8_t _size;
public:
ExternalValue(void* pValue, uint8_t size) {
_pValue = pValue;
_size = size;
}
void readTo(DataOut& out) {
out.writeBuffer(_pValue, _size);
}
void writeMaskedFrom(DataIn& in, DataIn& mask) {
uint8_t* p = (uint8_t*)_pValue;
for (uint8_t i=0; i<_size; i++) {
*p = nextMaskedByte(*p, in, mask);
p++;
}
}
uint8_t streamSize() {
return _size;
}
};
template <class T> class TransientValue : public WritableValue
{
T value;
public:
void readTo(DataOut& out) {
writePlatformEndianBytes(&value, sizeof(value), out);
}
void writeMaskedFrom(DataIn& in, DataIn& maskIn) {
readPlatformEndianMaskedBytes(&value, sizeof(value), in, maskIn);
}
uint8_t streamSize() { return sizeof(T); }
void setValue(const T& value) {
this->value = value;
}
T getValue() { return value; }
};
/**
* Callback function for use by this container to establish values.
* @param id The index (greater or equal to 0) in the container to return the value of. When negative, returns
* the number of items available.
* @return an object corresponding to the item at the given index, which may be {@code NULL}.
* When {@code id} is negative, returns the number of items, cast as a pointer.
*/
typedef Object* (*ExternalValueFunction)(container_id id);
/**
* Provides a container interface via values sourced externally.
* To avoid overhead of a virtual function table and vptr which require storage for two addresses,
* a single function is used to retrieve.
*/
class ExternalValueContainer : Container {
ExternalValueFunction externalValue;
public:
ExternalValueContainer(ExternalValueFunction fn) {
externalValue = fn;
}
/**
* Returns a transient value item.
*/
Object* item(container_id id) {
return externalValue(id);
}
void returnItem(container_id id, Object* item) {
#if OBJECT_VIRTUAL_DESTRUCTOR
delete item;
#else
delete (uint8_t*)item; // just clear the memory
#endif
}
container_id size() {
return container_id(pointer_scalar(externalValue(-1)));
}
};
/**
* An indirect value is configured with the id chain of an object, and returns that object's value.
* Once created the value pointed to cannot be changed.
* This is used in cases where you have a complex object implemented as a container, and it requires
* values to be inserted into it, yet the value you want to insert is already instantiated elsewhere else.
* It behaves something like a pointer.
*/
class IndirectValue : public EepromValue
{
private:
Value* previous;
void fetchTarget() {
EepromDataIn data;
data.reset(address, eepromAccess.readByte(address-1));
previous = (Value*)lookupUserObject(data);
}
public:
object_t objectType() {
fetchTarget();
return previous==NULL ? otObject : previous->objectType();
}
prepare_t prepare() {
fetchTarget();
return previous==NULL ? 0 : previous->prepare();
}
uint8_t streamSize() { return previous->streamSize(); }
void readTo(DataOut& out) { if (previous) previous->readTo(out); }
void writeMaskedFrom(DataIn& dataIn, DataIn& maskedIn) { if (previous) previous->writeMaskedFrom(dataIn, maskedIn); }
static Object* create(ObjectDefinition& def)
{
return new_object(IndirectValue());
}
};