2018-02-09 21:31:05 +00:00
|
|
|
//
|
|
|
|
// MultiConfigurable.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 09/02/2018.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2018 Thomas Harte. All rights reserved.
|
2018-02-09 21:31:05 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "MultiConfigurable.hpp"
|
|
|
|
|
2018-02-19 13:13:41 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-02-09 21:31:05 +00:00
|
|
|
using namespace Analyser::Dynamic;
|
|
|
|
|
2020-03-20 01:02:14 +00:00
|
|
|
namespace {
|
2018-02-09 21:31:05 +00:00
|
|
|
|
2020-03-20 01:02:14 +00:00
|
|
|
class MultiStruct: public Reflection::Struct {
|
|
|
|
public:
|
|
|
|
MultiStruct(const std::vector<Configurable::Device *> &devices) : devices_(devices) {
|
|
|
|
for(auto device: devices) {
|
|
|
|
options_.emplace_back(device->get_options());
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 03:48:53 +00:00
|
|
|
|
2020-03-20 01:02:14 +00:00
|
|
|
void apply() {
|
|
|
|
auto options = options_.begin();
|
|
|
|
for(auto device: devices_) {
|
|
|
|
device->set_options(*options);
|
|
|
|
++options;
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 03:48:53 +00:00
|
|
|
|
2020-03-30 04:24:49 +00:00
|
|
|
std::vector<std::string> all_keys() const final {
|
2020-03-20 01:02:14 +00:00
|
|
|
std::set<std::string> keys;
|
|
|
|
for(auto &options: options_) {
|
|
|
|
const auto new_keys = options->all_keys();
|
|
|
|
keys.insert(new_keys.begin(), new_keys.end());
|
|
|
|
}
|
|
|
|
return std::vector<std::string>(keys.begin(), keys.end());
|
|
|
|
}
|
2018-02-09 21:31:05 +00:00
|
|
|
|
2020-03-30 04:24:49 +00:00
|
|
|
std::vector<std::string> values_for(const std::string &name) const final {
|
2020-03-20 01:02:14 +00:00
|
|
|
std::set<std::string> values;
|
|
|
|
for(auto &options: options_) {
|
|
|
|
const auto new_values = options->values_for(name);
|
|
|
|
values.insert(new_values.begin(), new_values.end());
|
2018-02-09 21:31:05 +00:00
|
|
|
}
|
2020-03-20 01:02:14 +00:00
|
|
|
return std::vector<std::string>(values.begin(), values.end());
|
|
|
|
}
|
|
|
|
|
2020-03-30 04:24:49 +00:00
|
|
|
const std::type_info *type_of(const std::string &name) const final {
|
2020-03-20 01:02:14 +00:00
|
|
|
for(auto &options: options_) {
|
|
|
|
auto info = options->type_of(name);
|
|
|
|
if(info) return info;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2018-02-09 21:31:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 02:59:44 +00:00
|
|
|
size_t count_of(const std::string &name) const final {
|
|
|
|
for(auto &options: options_) {
|
|
|
|
auto info = options->type_of(name);
|
|
|
|
if(info) return options->count_of(name);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-30 04:24:49 +00:00
|
|
|
const void *get(const std::string &name) const final {
|
2020-03-20 01:02:14 +00:00
|
|
|
for(auto &options: options_) {
|
|
|
|
auto value = options->get(name);
|
|
|
|
if(value) return value;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-26 03:39:00 +00:00
|
|
|
void *get(const std::string &name) final {
|
|
|
|
for(auto &options: options_) {
|
|
|
|
auto value = options->get(name);
|
|
|
|
if(value) return value;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-27 02:55:55 +00:00
|
|
|
void set(const std::string &name, const void *value, size_t offset) final {
|
2020-03-20 01:02:14 +00:00
|
|
|
const auto safe_type = type_of(name);
|
|
|
|
if(!safe_type) return;
|
|
|
|
|
|
|
|
// Set this property only where the child's type is the same as that
|
|
|
|
// which was returned from here for type_of.
|
|
|
|
for(auto &options: options_) {
|
|
|
|
const auto type = options->type_of(name);
|
|
|
|
if(!type) continue;
|
|
|
|
|
|
|
|
if(*type == *safe_type) {
|
2020-05-27 02:55:55 +00:00
|
|
|
options->set(name, value, offset);
|
2020-03-20 01:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::vector<Configurable::Device *> &devices_;
|
|
|
|
std::vector<std::unique_ptr<Reflection::Struct>> options_;
|
|
|
|
};
|
|
|
|
|
2018-02-09 21:31:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 01:02:14 +00:00
|
|
|
MultiConfigurable::MultiConfigurable(const std::vector<std::unique_ptr<::Machine::DynamicMachine>> &machines) {
|
|
|
|
for(const auto &machine: machines) {
|
|
|
|
Configurable::Device *device = machine->configurable_device();
|
|
|
|
if(device) devices_.push_back(device);
|
2018-02-09 21:31:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 01:02:14 +00:00
|
|
|
void MultiConfigurable::set_options(const std::unique_ptr<Reflection::Struct> &str) {
|
|
|
|
const auto options = dynamic_cast<MultiStruct *>(str.get());
|
|
|
|
options->apply();
|
2018-02-09 21:31:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 01:02:14 +00:00
|
|
|
std::unique_ptr<Reflection::Struct> MultiConfigurable::get_options() {
|
|
|
|
return std::make_unique<MultiStruct>(devices_);
|
2018-02-09 21:31:05 +00:00
|
|
|
}
|