2020-03-08 15:26:17 +00:00
|
|
|
//
|
2020-03-20 03:24:06 +00:00
|
|
|
// Struct.hpp
|
2020-03-08 15:26:17 +00:00
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 06/03/2020.
|
|
|
|
// Copyright © 2020 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2020-03-08 15:26:17 +00:00
|
|
|
|
2020-05-27 03:07:26 +00:00
|
|
|
#include <cassert>
|
2020-03-18 01:44:04 +00:00
|
|
|
#include <cstdarg>
|
2020-03-14 04:17:58 +00:00
|
|
|
#include <cstring>
|
2021-05-04 00:33:25 +00:00
|
|
|
#include <cstddef>
|
2023-05-25 20:06:13 +00:00
|
|
|
#include <cstdint>
|
2020-03-08 15:26:17 +00:00
|
|
|
#include <string>
|
2020-03-09 01:23:29 +00:00
|
|
|
#include <typeindex>
|
|
|
|
#include <typeinfo>
|
2020-05-24 05:20:48 +00:00
|
|
|
#include <type_traits>
|
2020-03-08 15:26:17 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-03-20 03:24:06 +00:00
|
|
|
#include "Enum.hpp"
|
2020-06-27 20:26:01 +00:00
|
|
|
#include "TypeInfo.hpp"
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-08 15:26:17 +00:00
|
|
|
namespace Reflection {
|
|
|
|
|
2020-03-12 03:25:29 +00:00
|
|
|
#define DeclareField(Name) declare(&Name, #Name)
|
|
|
|
|
2024-12-06 21:03:24 +00:00
|
|
|
#define BEGIN_DECLARATIONS void declare_fields() {
|
|
|
|
#define END_DECLARATIONS } \
|
2024-12-06 21:00:03 +00:00
|
|
|
struct Declarer { Declarer() { Target t; t.declare_fields(); } }; \
|
|
|
|
static Declarer declarer;
|
|
|
|
|
|
|
|
|
2020-03-13 00:56:02 +00:00
|
|
|
struct Struct {
|
2020-03-30 04:24:49 +00:00
|
|
|
virtual std::vector<std::string> all_keys() const = 0;
|
|
|
|
virtual const std::type_info *type_of(const std::string &name) const = 0;
|
2020-05-15 02:59:44 +00:00
|
|
|
virtual size_t count_of(const std::string &name) const = 0;
|
2020-05-27 02:55:55 +00:00
|
|
|
virtual void set(const std::string &name, const void *value, size_t offset = 0) = 0;
|
2020-05-26 03:39:00 +00:00
|
|
|
virtual void *get(const std::string &name) = 0;
|
2020-05-27 02:55:55 +00:00
|
|
|
virtual const void *get(const std::string &name) const {
|
|
|
|
return const_cast<Struct *>(this)->get(name);
|
|
|
|
}
|
2020-03-30 04:24:49 +00:00
|
|
|
virtual std::vector<std::string> values_for(const std::string &name) const = 0;
|
2024-02-17 02:47:23 +00:00
|
|
|
virtual ~Struct() = default;
|
2020-03-30 04:24:49 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns A string describing this struct. This string has no guaranteed layout, may not be
|
|
|
|
sufficiently formed for a formal language parser, etc.
|
|
|
|
*/
|
|
|
|
std::string description() const;
|
2020-05-15 03:58:17 +00:00
|
|
|
|
2020-05-22 04:31:40 +00:00
|
|
|
/*!
|
2020-05-26 03:39:00 +00:00
|
|
|
Serialises this struct in BSON format.
|
|
|
|
|
|
|
|
Supported field types:
|
|
|
|
|
|
|
|
* [u]int[8/16/32/64]_t;
|
|
|
|
* float and double;
|
|
|
|
* bool;
|
|
|
|
* std::string;
|
|
|
|
* plain C-style arrays of any of the above;
|
|
|
|
* other reflective structs;
|
|
|
|
* std::vector<uint8_t> as raw binary data.
|
|
|
|
|
|
|
|
TODO: vector of string, possibly? Or more general vector support?
|
|
|
|
|
|
|
|
@returns The BSON serialisation.
|
2020-05-22 04:31:40 +00:00
|
|
|
*/
|
|
|
|
std::vector<uint8_t> serialise() const;
|
|
|
|
|
|
|
|
/*!
|
2020-05-26 03:39:00 +00:00
|
|
|
Applies as many fields as possible from the incoming BSON. Supports the same types
|
|
|
|
as @c serialise.
|
2020-05-22 04:31:40 +00:00
|
|
|
*/
|
|
|
|
bool deserialise(const std::vector<uint8_t> &bson);
|
|
|
|
|
|
|
|
/*!
|
2020-05-26 03:39:00 +00:00
|
|
|
Called to determine whether @c key should be included in the serialisation of this struct.
|
2020-05-22 04:31:40 +00:00
|
|
|
*/
|
2020-05-30 04:37:06 +00:00
|
|
|
virtual bool should_serialise([[maybe_unused]] const std::string &key) const { return true; }
|
2020-05-22 04:31:40 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
private:
|
|
|
|
void append(std::ostringstream &stream, const std::string &key, const std::type_info *type, size_t offset) const;
|
|
|
|
bool deserialise(const uint8_t *bson, size_t size);
|
2020-03-14 03:38:29 +00:00
|
|
|
};
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-14 03:38:29 +00:00
|
|
|
/*!
|
|
|
|
Attempts to set the property @c name to @c value ; will perform limited type conversions.
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-16 01:50:43 +00:00
|
|
|
@returns @c true if the property was successfully set; @c false otherwise.
|
2020-03-14 03:38:29 +00:00
|
|
|
*/
|
2020-05-27 02:55:55 +00:00
|
|
|
template <typename Type> bool set(Struct &target, const std::string &name, Type value, size_t offset = 0);
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-14 03:38:29 +00:00
|
|
|
/*!
|
|
|
|
Setting an int:
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-14 03:38:29 +00:00
|
|
|
* to an int copies the int;
|
2020-05-26 03:39:00 +00:00
|
|
|
* to a smaller type, truncates the int;
|
2020-03-14 03:38:29 +00:00
|
|
|
* to an int64_t promotes the int; and
|
|
|
|
* to a registered enum, copies the int.
|
|
|
|
*/
|
2020-05-27 02:55:55 +00:00
|
|
|
template <> bool set(Struct &target, const std::string &name, int64_t value, size_t offset);
|
|
|
|
template <> bool set(Struct &target, const std::string &name, int value, size_t offset);
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-14 03:38:29 +00:00
|
|
|
/*!
|
|
|
|
Setting a string:
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-14 03:38:29 +00:00
|
|
|
* to an enum, if the string names a member of the enum, sets the value.
|
|
|
|
*/
|
2020-05-27 02:55:55 +00:00
|
|
|
template <> bool set(Struct &target, const std::string &name, const std::string &value, size_t offset);
|
|
|
|
template <> bool set(Struct &target, const std::string &name, const char *value, size_t offset);
|
2020-03-13 00:56:02 +00:00
|
|
|
|
2020-03-19 02:31:32 +00:00
|
|
|
/*!
|
|
|
|
Setting a bool:
|
|
|
|
|
|
|
|
* to a bool, copies the value.
|
|
|
|
*/
|
2020-05-27 02:55:55 +00:00
|
|
|
template <> bool set(Struct &target, const std::string &name, bool value, size_t offset);
|
2020-03-19 02:31:32 +00:00
|
|
|
|
2020-03-16 01:50:43 +00:00
|
|
|
|
2020-05-27 02:55:55 +00:00
|
|
|
template <> bool set(Struct &target, const std::string &name, float value, size_t offset);
|
|
|
|
template <> bool set(Struct &target, const std::string &name, double value, size_t offset);
|
2020-05-26 03:39:00 +00:00
|
|
|
|
2020-03-16 01:50:43 +00:00
|
|
|
/*!
|
|
|
|
Fuzzy-set attempts to set any property based on a string value. This is intended to allow input provided by the user.
|
|
|
|
|
|
|
|
Amongst other steps, it might:
|
|
|
|
* if the target is a bool, map true, false, yes, no, y, n, etc;
|
|
|
|
* if the target is an integer, parse like strtrol;
|
|
|
|
* if the target is a float, parse like strtod; or
|
|
|
|
* if the target is a reflective enum, attempt to match to enum members (possibly doing so in a case insensitive fashion).
|
|
|
|
|
|
|
|
This method reserves the right to perform more or fewer attempted mappings, using any other logic it
|
|
|
|
decides is appropriate.
|
|
|
|
|
|
|
|
@returns @c true if the property was successfully set; @c false otherwise.
|
|
|
|
*/
|
|
|
|
bool fuzzy_set(Struct &target, const std::string &name, const std::string &value);
|
|
|
|
|
2020-03-16 03:48:53 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Attempts to get the property @c name to @c value ; will perform limited type conversions.
|
|
|
|
|
|
|
|
@returns @c true if the property was successfully read; @c false otherwise.
|
|
|
|
*/
|
2020-05-15 03:58:17 +00:00
|
|
|
template <typename Type> bool get(const Struct &target, const std::string &name, Type &value, size_t offset = 0);
|
2020-03-30 04:24:49 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Attempts to get the property @c name to @c value ; will perform limited type conversions.
|
2020-03-16 03:48:53 +00:00
|
|
|
|
2020-03-30 04:24:49 +00:00
|
|
|
@returns @c true if the property was successfully read; a default-constructed instance of Type otherwise.
|
|
|
|
*/
|
2020-05-15 03:58:17 +00:00
|
|
|
template <typename Type> Type get(const Struct &target, const std::string &name, size_t offset = 0);
|
2020-03-16 03:48:53 +00:00
|
|
|
|
2020-03-14 02:42:37 +00:00
|
|
|
template <typename Owner> class StructImpl: public Struct {
|
2024-12-02 02:44:14 +00:00
|
|
|
public:
|
|
|
|
/*!
|
|
|
|
@returns the value of type @c Type that is loaded from the offset registered for the field @c name.
|
2020-03-12 02:06:16 +00:00
|
|
|
It is the caller's responsibility to provide an appropriate type of data.
|
2024-12-02 02:44:14 +00:00
|
|
|
*/
|
|
|
|
void *get(const std::string &name) final {
|
|
|
|
const auto iterator = contents_.find(name);
|
|
|
|
if(iterator == contents_.end()) return nullptr;
|
|
|
|
return reinterpret_cast<uint8_t *>(this) + iterator->second.offset;
|
|
|
|
}
|
2020-03-08 15:26:17 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
/*!
|
|
|
|
Stores the @c value of type @c Type to the offset registered for the field @c name.
|
2020-03-08 15:26:17 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
It is the caller's responsibility to provide an appropriate type of data.
|
|
|
|
*/
|
|
|
|
void set(const std::string &name, const void *value, size_t offset) final {
|
|
|
|
const auto iterator = contents_.find(name);
|
|
|
|
if(iterator == contents_.end()) return;
|
|
|
|
assert(offset < iterator->second.count);
|
|
|
|
memcpy(reinterpret_cast<uint8_t *>(this) + iterator->second.offset + offset * iterator->second.size, value, iterator->second.size);
|
|
|
|
}
|
2020-05-15 02:59:44 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
/*!
|
|
|
|
@returns @c type_info for the field @c name.
|
|
|
|
*/
|
|
|
|
const std::type_info *type_of(const std::string &name) const final {
|
|
|
|
const auto iterator = contents_.find(name);
|
|
|
|
if(iterator == contents_.end()) return nullptr;
|
|
|
|
return iterator->second.type;
|
|
|
|
}
|
2020-03-18 01:44:04 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
/*!
|
|
|
|
@returns The number of instances of objects of the same type as @c name that sit consecutively in memory.
|
|
|
|
*/
|
|
|
|
size_t count_of(const std::string &name) const final {
|
|
|
|
const auto iterator = contents_.find(name);
|
|
|
|
if(iterator == contents_.end()) return 0;
|
|
|
|
return iterator->second.count;
|
|
|
|
}
|
2020-03-18 01:44:04 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
/*!
|
|
|
|
@returns a list of the valid enum value names for field @c name if it is a declared enum field of this struct;
|
|
|
|
the empty list otherwise.
|
|
|
|
*/
|
|
|
|
std::vector<std::string> values_for(const std::string &name) const final {
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
// Return an empty vector if this field isn't declared.
|
|
|
|
const auto type = type_of(name);
|
|
|
|
if(!type) return result;
|
|
|
|
|
|
|
|
// Also return an empty vector if this field isn't a registered enum.
|
|
|
|
const auto all_values = Enum::all_values(*type);
|
|
|
|
if(all_values.empty()) return result;
|
|
|
|
|
|
|
|
// If no restriction is stored, return all values.
|
|
|
|
const auto permitted_values = permitted_enum_values_.find(name);
|
|
|
|
if(permitted_values == permitted_enum_values_.end()) return all_values;
|
|
|
|
|
|
|
|
// Compile a vector of only those values the stored set indicates.
|
|
|
|
auto value = all_values.begin();
|
|
|
|
auto flag = permitted_values->second.begin();
|
|
|
|
while(value != all_values.end() && flag != permitted_values->second.end()) {
|
|
|
|
if(*flag) {
|
|
|
|
result.push_back(*value);
|
2020-03-08 15:26:17 +00:00
|
|
|
}
|
2024-12-02 02:44:14 +00:00
|
|
|
++flag;
|
|
|
|
++value;
|
2020-03-08 15:26:17 +00:00
|
|
|
}
|
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
return result;
|
|
|
|
}
|
2020-05-15 02:59:44 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
/*!
|
|
|
|
@returns A vector of all declared fields for this struct.
|
|
|
|
*/
|
|
|
|
std::vector<std::string> all_keys() const final {
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
for(const auto &pair: contents_) {
|
|
|
|
keys.push_back(pair.first);
|
2020-03-09 01:23:29 +00:00
|
|
|
}
|
2024-12-02 02:44:14 +00:00
|
|
|
return keys;
|
|
|
|
}
|
2020-03-09 01:23:29 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
protected:
|
|
|
|
/*
|
|
|
|
This interface requires reflective structs to declare all fields;
|
|
|
|
specifically they should call:
|
2020-03-18 01:44:04 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
declare_field(&field1, "field1");
|
|
|
|
declare_field(&field2, "field2");
|
2020-03-18 01:44:04 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
Fields are registered in class storage. So callers can use needs_declare()
|
|
|
|
to determine whether a class of this type has already established the
|
|
|
|
reflective fields.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Exposes the field pointed to by @c t for reflection as @c name. If @c t is itself a Reflection::Struct,
|
|
|
|
it'll be the struct that's exposed.
|
|
|
|
*/
|
|
|
|
template <typename Type> void declare(Type *t, const std::string &name) {
|
|
|
|
// If the declared item is a class, see whether it can be dynamically cast
|
|
|
|
// to a reflectable for emplacement. If so, exit early.
|
|
|
|
if constexpr (std::is_class<Type>()) {
|
|
|
|
if(declare_reflectable(t, name)) return;
|
2020-03-08 15:26:17 +00:00
|
|
|
}
|
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
// If the declared item is an array, record it as a pointer to the
|
|
|
|
// first element plus a size.
|
|
|
|
if constexpr (std::is_array<Type>()) {
|
|
|
|
declare_emplace(&(*t)[0], name, sizeof(*t) / sizeof(*t[0]));
|
|
|
|
return;
|
|
|
|
}
|
2020-03-18 01:44:04 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
declare_emplace(t, name);
|
|
|
|
}
|
2020-03-18 01:44:04 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
/*!
|
|
|
|
If @c t is a previously-declared field that links to a declared enum then the variable
|
|
|
|
arguments provide a list of the acceptable values for that field. The list should be terminated
|
|
|
|
with a value of -1.
|
|
|
|
*/
|
|
|
|
template <typename Type> void limit_enum(Type *t, ...) {
|
|
|
|
const auto name = name_of(t);
|
|
|
|
if(name.empty()) return;
|
|
|
|
|
|
|
|
// The default vector size of '8' isn't especially scientific,
|
|
|
|
// but I feel like it's a good choice.
|
|
|
|
std::vector<bool> permitted_values(8);
|
|
|
|
|
|
|
|
va_list list;
|
|
|
|
va_start(list, t);
|
|
|
|
while(true) {
|
|
|
|
const int next = va_arg(list, int);
|
|
|
|
if(next < 0) break;
|
|
|
|
|
|
|
|
if(permitted_values.size() <= size_t(next)) {
|
|
|
|
permitted_values.resize(permitted_values.size() << 1);
|
2020-03-18 01:44:04 +00:00
|
|
|
}
|
2024-12-02 02:44:14 +00:00
|
|
|
permitted_values[size_t(next)] = true;
|
2020-03-18 01:44:04 +00:00
|
|
|
}
|
2024-12-02 02:44:14 +00:00
|
|
|
va_end(list);
|
2020-03-18 01:44:04 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
permitted_enum_values_.emplace(name, permitted_values);
|
|
|
|
}
|
2020-03-30 04:24:49 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
/*!
|
|
|
|
@returns @c true if this subclass of @c Struct has not yet declared any fields.
|
|
|
|
*/
|
2024-12-06 21:00:03 +00:00
|
|
|
bool needs_declare() const {
|
2024-12-02 02:44:14 +00:00
|
|
|
return contents_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Performs a reverse lookup from field to name.
|
|
|
|
*/
|
|
|
|
std::string name_of(void *field) {
|
|
|
|
const ssize_t offset = reinterpret_cast<uint8_t *>(field) - reinterpret_cast<uint8_t *>(this);
|
|
|
|
|
|
|
|
auto iterator = contents_.begin();
|
|
|
|
while(iterator != contents_.end()) {
|
|
|
|
if(iterator->second.offset == offset) break;
|
|
|
|
++iterator;
|
2020-03-30 04:24:49 +00:00
|
|
|
}
|
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
if(iterator != contents_.end()) {
|
|
|
|
return iterator->first;
|
|
|
|
} else {
|
|
|
|
return std::string();
|
2020-03-30 04:24:49 +00:00
|
|
|
}
|
2024-12-02 02:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename Type> bool declare_reflectable([[maybe_unused]] Type *t, const std::string &name) {
|
|
|
|
if constexpr (std::is_base_of<Reflection::Struct, Type>::value) {
|
|
|
|
Reflection::Struct *const str = static_cast<Reflection::Struct *>(t);
|
|
|
|
declare_emplace(str, name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type> void declare_emplace(Type *t, const std::string &name, size_t count = 1) {
|
|
|
|
contents_.emplace(
|
|
|
|
std::make_pair(
|
|
|
|
name,
|
|
|
|
Field(typeid(Type), reinterpret_cast<uint8_t *>(t) - reinterpret_cast<uint8_t *>(this), sizeof(Type), count)
|
|
|
|
));
|
|
|
|
}
|
2020-03-30 04:24:49 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
struct Field {
|
|
|
|
const std::type_info *type;
|
|
|
|
ssize_t offset;
|
|
|
|
size_t size;
|
|
|
|
size_t count;
|
|
|
|
Field(const std::type_info &type, ssize_t offset, size_t size, size_t count) :
|
|
|
|
type(&type), offset(offset), size(size), count(count) {}
|
|
|
|
};
|
|
|
|
static inline std::unordered_map<std::string, Field> contents_;
|
|
|
|
static inline std::unordered_map<std::string, std::vector<bool>> permitted_enum_values_;
|
2020-03-08 15:26:17 +00:00
|
|
|
};
|
|
|
|
|
2020-06-27 20:26:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - IMPLEMENTATION DETAILS BELOW
|
|
|
|
// DO NOT RELY ON THE IMPLEMENTATIONS BELOW.
|
|
|
|
// Please use the documentation above.
|
|
|
|
|
|
|
|
// MARK: - Getters
|
|
|
|
|
|
|
|
template <typename Type> bool get(const Struct &target, const std::string &name, Type &value, size_t offset) {
|
|
|
|
const auto target_type = target.type_of(name);
|
|
|
|
if(!target_type) return false;
|
|
|
|
|
|
|
|
// If type is a direct match, copy.
|
|
|
|
if(*target_type == typeid(Type)) {
|
|
|
|
const auto address = reinterpret_cast<const uint8_t *>(target.get(name)) + offset * sizeof(Type);
|
|
|
|
value = *reinterpret_cast<const Type *>(address);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the type is a registered enum and the value type is int, copy.
|
|
|
|
if constexpr (std::is_integral<Type>::value && sizeof(Type) == sizeof(int)) {
|
|
|
|
if(!Enum::name(*target_type).empty()) {
|
|
|
|
memcpy(&value, target.get(name), sizeof(int));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the type is an int that is larger than the stored type and matches the signedness, cast upward.
|
|
|
|
if constexpr (std::is_integral<Type>::value) {
|
|
|
|
if(TypeInfo::is_integral(target_type)) {
|
|
|
|
const bool target_is_signed = TypeInfo::is_signed(target_type);
|
|
|
|
const size_t target_size = TypeInfo::size(target_type);
|
|
|
|
|
|
|
|
// An unsigned type can map to any larger type, signed or unsigned;
|
|
|
|
// a signed type can map to a larger type only if it also is signed.
|
|
|
|
if(sizeof(Type) > target_size && (!target_is_signed || std::is_signed<Type>::value)) {
|
|
|
|
const auto address = reinterpret_cast<const uint8_t *>(target.get(name)) + offset * target_size;
|
|
|
|
|
|
|
|
#define Map(x) if(*target_type == typeid(x)) { value = static_cast<Type>(*reinterpret_cast<const x *>(address)); }
|
|
|
|
ForAllInts(Map);
|
|
|
|
#undef Map
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the type is a double and stored type is a float, cast upward.
|
|
|
|
if constexpr (std::is_floating_point<Type>::value) {
|
|
|
|
constexpr size_t size = sizeof(Type);
|
|
|
|
const bool target_is_floating_point = TypeInfo::is_floating_point(target_type);
|
|
|
|
const size_t target_size = TypeInfo::size(target_type);
|
|
|
|
|
|
|
|
if(size > target_size && target_is_floating_point) {
|
|
|
|
const auto address = reinterpret_cast<const uint8_t *>(target.get(name)) + offset * target_size;
|
|
|
|
|
|
|
|
#define Map(x) if(*target_type == typeid(x)) { value = static_cast<Type>(*reinterpret_cast<const x *>(address)); }
|
|
|
|
ForAllFloats(Map);
|
|
|
|
#undef Map
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type> Type get(const Struct &target, const std::string &name, size_t offset) {
|
|
|
|
Type value{};
|
|
|
|
get(target, name, value, offset);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-03-08 15:26:17 +00:00
|
|
|
}
|