2020-03-14 03:38:29 +00:00
|
|
|
//
|
|
|
|
// Struct.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 13/03/2020.
|
|
|
|
// Copyright © 2020 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2020-03-20 03:24:06 +00:00
|
|
|
#include "Struct.hpp"
|
2020-03-14 03:38:29 +00:00
|
|
|
|
2020-03-19 02:31:32 +00:00
|
|
|
#include <algorithm>
|
2020-03-30 04:31:25 +00:00
|
|
|
#include <iomanip>
|
2020-03-30 04:24:49 +00:00
|
|
|
#include <sstream>
|
2020-03-19 02:31:32 +00:00
|
|
|
|
2020-03-16 03:48:53 +00:00
|
|
|
// MARK: - Setters
|
|
|
|
|
2020-03-14 03:38:29 +00:00
|
|
|
template <> bool Reflection::set(Struct &target, const std::string &name, int value) {
|
|
|
|
const auto target_type = target.type_of(name);
|
|
|
|
if(!target_type) return false;
|
|
|
|
|
|
|
|
// No need to convert an int or a registered enum.
|
|
|
|
if(*target_type == typeid(int) || !Reflection::Enum::name(*target_type).empty()) {
|
|
|
|
target.set(name, &value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Promote to an int64_t.
|
|
|
|
if(*target_type == typeid(int64_t)) {
|
|
|
|
const auto int64 = int64_t(value);
|
|
|
|
target.set(name, &int64);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <> bool Reflection::set(Struct &target, const std::string &name, const std::string &value) {
|
|
|
|
const auto target_type = target.type_of(name);
|
|
|
|
if(!target_type) return false;
|
|
|
|
|
|
|
|
if(Reflection::Enum::name(*target_type).empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-19 02:31:32 +00:00
|
|
|
const int enum_value = Reflection::Enum::from_string(*target_type, value);
|
|
|
|
if(enum_value < 0) {
|
2020-03-14 03:38:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-19 02:31:32 +00:00
|
|
|
target.set(name, &enum_value);
|
2020-03-14 03:38:29 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <> bool Reflection::set(Struct &target, const std::string &name, const char *value) {
|
|
|
|
const std::string string(value);
|
|
|
|
return set<const std::string &>(target, name, string);
|
|
|
|
}
|
2020-03-16 01:50:43 +00:00
|
|
|
|
2020-03-19 02:31:32 +00:00
|
|
|
template <> bool Reflection::set(Struct &target, const std::string &name, bool value) {
|
|
|
|
const auto target_type = target.type_of(name);
|
|
|
|
if(!target_type) return false;
|
|
|
|
|
|
|
|
if(*target_type == typeid(bool)) {
|
|
|
|
target.set(name, &value);;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-16 03:48:53 +00:00
|
|
|
// MARK: - Fuzzy setter
|
|
|
|
|
2020-03-16 01:50:43 +00:00
|
|
|
bool Reflection::fuzzy_set(Struct &target, const std::string &name, const std::string &value) {
|
2020-03-19 02:31:32 +00:00
|
|
|
const auto target_type = target.type_of(name);
|
|
|
|
if(!target_type) return false;
|
|
|
|
|
|
|
|
// If the target is a registered enum, ttry to convert the value. Failing that,
|
|
|
|
// try to match without case sensitivity.
|
|
|
|
if(Reflection::Enum::size(*target_type)) {
|
|
|
|
const int from_string = Reflection::Enum::from_string(*target_type, value);
|
|
|
|
if(from_string >= 0) {
|
|
|
|
target.set(name, &from_string);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto all_values = Reflection::Enum::all_values(*target_type);
|
|
|
|
const auto value_location = std::find_if(all_values.begin(), all_values.end(),
|
|
|
|
[&value] (const auto &entry) {
|
|
|
|
if(value.size() != entry.size()) return false;
|
|
|
|
const char *v = value.c_str();
|
|
|
|
const char *e = entry.c_str();
|
|
|
|
while(*v) {
|
|
|
|
if(tolower(*v) != tolower(*e)) return false;
|
|
|
|
++v;
|
|
|
|
++e;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if(value_location != all_values.end()) {
|
|
|
|
const int offset = int(value_location - all_values.begin());
|
|
|
|
target.set(name, &offset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-16 01:50:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-16 03:48:53 +00:00
|
|
|
|
|
|
|
// MARK: - Getters
|
|
|
|
|
2020-03-30 04:24:49 +00:00
|
|
|
template <typename Type> bool Reflection::get(const Struct &target, const std::string &name, Type &value) {
|
2020-03-16 03:48:53 +00:00
|
|
|
const auto target_type = target.type_of(name);
|
|
|
|
if(!target_type) return false;
|
|
|
|
|
2020-03-30 04:24:49 +00:00
|
|
|
if(*target_type == typeid(Type)) {
|
|
|
|
memcpy(&value, target.get(name), sizeof(Type));
|
2020-03-16 03:48:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-30 04:24:49 +00:00
|
|
|
|
|
|
|
template <typename Type> Type Reflection::get(const Struct &target, const std::string &name) {
|
|
|
|
Type value;
|
|
|
|
get(target, name, value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Description
|
|
|
|
|
|
|
|
std::string Reflection::Struct::description() const {
|
|
|
|
std::ostringstream stream;
|
|
|
|
|
|
|
|
stream << "{";
|
|
|
|
|
|
|
|
bool is_first = true;
|
|
|
|
for(const auto &key: all_keys()) {
|
|
|
|
if(!is_first) stream << ", ";
|
|
|
|
is_first = false;
|
|
|
|
stream << key << ": ";
|
|
|
|
|
|
|
|
const auto type = type_of(key);
|
|
|
|
|
|
|
|
// Output Bools as yes/no.
|
|
|
|
if(*type == typeid(bool)) {
|
2020-04-12 18:40:32 +00:00
|
|
|
stream << ::Reflection::get<bool>(*this, key);
|
2020-03-30 04:24:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output Ints of all sizes as hex.
|
2020-03-30 04:31:25 +00:00
|
|
|
#define OutputIntC(int_type, cast_type) if(*type == typeid(int_type)) { stream << std::setfill('0') << std::setw(sizeof(int_type)*2) << std::hex << cast_type(::Reflection::get<int_type>(*this, key)); continue; }
|
2020-03-30 04:24:49 +00:00
|
|
|
#define OutputInt(int_type) OutputIntC(int_type, int_type)
|
|
|
|
OutputIntC(int8_t, int16_t);
|
|
|
|
OutputIntC(uint8_t, uint16_t);
|
|
|
|
OutputInt(int16_t);
|
|
|
|
OutputInt(uint16_t);
|
|
|
|
OutputInt(int32_t);
|
|
|
|
OutputInt(uint32_t);
|
|
|
|
OutputInt(int64_t);
|
|
|
|
OutputInt(uint64_t);
|
|
|
|
#undef OutputInt
|
|
|
|
|
|
|
|
// Output floats and strings natively.
|
|
|
|
#define OutputNative(val_type) if(*type == typeid(val_type)) { stream << ::Reflection::get<val_type>(*this, key); continue; }
|
|
|
|
OutputNative(float);
|
|
|
|
OutputNative(double);
|
|
|
|
OutputNative(char *);
|
|
|
|
OutputNative(std::string);
|
|
|
|
#undef OutputNAtive
|
|
|
|
|
|
|
|
// Output the current value of any enums.
|
|
|
|
if(!Enum::name(*type).empty()) {
|
|
|
|
const int value = ::Reflection::get<int>(*this, key);
|
|
|
|
stream << Enum::to_string(*type, value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse to deal with embedded objects.
|
|
|
|
if(*type == typeid(Reflection::Struct)) {
|
|
|
|
const Reflection::Struct *const child = reinterpret_cast<const Reflection::Struct *>(get(key));
|
|
|
|
stream << child->description();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream << "}";
|
|
|
|
|
|
|
|
return stream.str();
|
|
|
|
}
|