mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
[glass] Split DataSource into type-specific variants (#7588)
This commit is contained in:
@@ -6,7 +6,6 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cinttypes>
|
||||
#include <concepts>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <initializer_list>
|
||||
@@ -632,19 +631,53 @@ static void UpdateJsonValueSource(NetworkTablesModel& model,
|
||||
}
|
||||
|
||||
void NetworkTablesModel::ValueSource::UpdateDiscreteSource(
|
||||
std::string_view name, double value, int64_t time, bool digital) {
|
||||
std::string_view name, bool value, int64_t time) {
|
||||
valueChildren.clear();
|
||||
if (!source) {
|
||||
source = std::make_unique<DataSource>(fmt::format("NT:{}", name));
|
||||
auto s = dynamic_cast<BooleanSource*>(source.get());
|
||||
if (!s) {
|
||||
source = std::make_unique<BooleanSource>(fmt::format("NT:{}", name));
|
||||
s = static_cast<BooleanSource*>(source.get());
|
||||
}
|
||||
source->SetValue(value, time);
|
||||
source->SetDigital(digital);
|
||||
s->SetValue(value, time);
|
||||
}
|
||||
|
||||
template <typename T, typename MakeValue>
|
||||
void NetworkTablesModel::ValueSource::UpdateDiscreteSource(
|
||||
std::string_view name, float value, int64_t time) {
|
||||
valueChildren.clear();
|
||||
auto s = dynamic_cast<FloatSource*>(source.get());
|
||||
if (!s) {
|
||||
source = std::make_unique<FloatSource>(fmt::format("NT:{}", name));
|
||||
s = static_cast<FloatSource*>(source.get());
|
||||
}
|
||||
s->SetValue(value, time);
|
||||
}
|
||||
|
||||
void NetworkTablesModel::ValueSource::UpdateDiscreteSource(
|
||||
std::string_view name, double value, int64_t time) {
|
||||
valueChildren.clear();
|
||||
auto s = dynamic_cast<DoubleSource*>(source.get());
|
||||
if (!s) {
|
||||
source = std::make_unique<DoubleSource>(fmt::format("NT:{}", name));
|
||||
s = static_cast<DoubleSource*>(source.get());
|
||||
}
|
||||
s->SetValue(value, time);
|
||||
}
|
||||
|
||||
void NetworkTablesModel::ValueSource::UpdateDiscreteSource(
|
||||
std::string_view name, int64_t value, int64_t time) {
|
||||
valueChildren.clear();
|
||||
auto s = dynamic_cast<IntegerSource*>(source.get());
|
||||
if (!s) {
|
||||
source = std::make_unique<IntegerSource>(fmt::format("NT:{}", name));
|
||||
s = static_cast<IntegerSource*>(source.get());
|
||||
}
|
||||
s->SetValue(value, time);
|
||||
}
|
||||
|
||||
template <bool IsBoolean, typename T, typename MakeValue>
|
||||
void NetworkTablesModel::ValueSource::UpdateDiscreteArray(
|
||||
std::string_view name, std::span<const T> arr, int64_t time,
|
||||
MakeValue makeValue, bool digital) {
|
||||
MakeValue makeValue) {
|
||||
if (valueChildrenMap) {
|
||||
valueChildren.clear();
|
||||
valueChildrenMap = false;
|
||||
@@ -657,7 +690,11 @@ void NetworkTablesModel::ValueSource::UpdateDiscreteArray(
|
||||
child.path = fmt::format("{}{}", name, child.name);
|
||||
}
|
||||
child.value = makeValue(arr[i], time);
|
||||
child.UpdateDiscreteSource(child.path, arr[i], time, digital);
|
||||
if constexpr (IsBoolean) {
|
||||
child.UpdateDiscreteSource(child.path, static_cast<bool>(arr[i]), time);
|
||||
} else {
|
||||
child.UpdateDiscreteSource(child.path, arr[i], time);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
@@ -667,8 +704,7 @@ void NetworkTablesModel::ValueSource::UpdateFromValue(
|
||||
std::string_view typeStr) {
|
||||
switch (value.type()) {
|
||||
case NT_BOOLEAN:
|
||||
UpdateDiscreteSource(name, value.GetBoolean() ? 1 : 0, value.time(),
|
||||
true);
|
||||
UpdateDiscreteSource(name, value.GetBoolean(), value.time());
|
||||
break;
|
||||
case NT_INTEGER:
|
||||
UpdateDiscreteSource(name, value.GetInteger(), value.time());
|
||||
@@ -680,20 +716,20 @@ void NetworkTablesModel::ValueSource::UpdateFromValue(
|
||||
UpdateDiscreteSource(name, value.GetDouble(), value.time());
|
||||
break;
|
||||
case NT_BOOLEAN_ARRAY:
|
||||
UpdateDiscreteArray(name, value.GetBooleanArray(), value.time(),
|
||||
nt::Value::MakeBoolean, true);
|
||||
UpdateDiscreteArray<true>(name, value.GetBooleanArray(), value.time(),
|
||||
nt::Value::MakeBoolean);
|
||||
break;
|
||||
case NT_INTEGER_ARRAY:
|
||||
UpdateDiscreteArray(name, value.GetIntegerArray(), value.time(),
|
||||
nt::Value::MakeInteger);
|
||||
UpdateDiscreteArray<false>(name, value.GetIntegerArray(), value.time(),
|
||||
nt::Value::MakeInteger);
|
||||
break;
|
||||
case NT_FLOAT_ARRAY:
|
||||
UpdateDiscreteArray(name, value.GetFloatArray(), value.time(),
|
||||
nt::Value::MakeFloat);
|
||||
UpdateDiscreteArray<false>(name, value.GetFloatArray(), value.time(),
|
||||
nt::Value::MakeFloat);
|
||||
break;
|
||||
case NT_DOUBLE_ARRAY:
|
||||
UpdateDiscreteArray(name, value.GetDoubleArray(), value.time(),
|
||||
nt::Value::MakeDouble);
|
||||
UpdateDiscreteArray<false>(name, value.GetDoubleArray(), value.time(),
|
||||
nt::Value::MakeDouble);
|
||||
break;
|
||||
case NT_STRING_ARRAY: {
|
||||
auto arr = value.GetStringArray();
|
||||
@@ -729,6 +765,13 @@ void NetworkTablesModel::ValueSource::UpdateFromValue(
|
||||
os << '"';
|
||||
os.write_escaped(value.GetString());
|
||||
os << '"';
|
||||
|
||||
auto s = dynamic_cast<StringSource*>(source.get());
|
||||
if (!s) {
|
||||
source = std::make_unique<StringSource>(fmt::format("NT:{}", name));
|
||||
s = static_cast<StringSource*>(source.get());
|
||||
}
|
||||
s->SetValue(value.GetString(), value.time());
|
||||
}
|
||||
break;
|
||||
case NT_RAW:
|
||||
|
||||
Reference in New Issue
Block a user