|
|
@@ -14,7 +14,7 @@ namespace sophiar {
|
|
|
struct sophiar_pool::impl {
|
|
|
|
|
|
struct variable_type_info {
|
|
|
- using creator_func_type = void *(*)();
|
|
|
+ using creator_func_type = void *(*)(const nlohmann::json &);
|
|
|
|
|
|
size_t binary_length;
|
|
|
std::type_index type = typeid(void);
|
|
|
@@ -40,6 +40,43 @@ namespace sophiar {
|
|
|
|
|
|
named_vector<variable_index_type, variable_info_impl> variable_pool;
|
|
|
|
|
|
+ template<typename SmallObjType>
|
|
|
+ static void *create_variable_pointer(const nlohmann::json &config) {
|
|
|
+ auto placeholder = new SmallObjType::pointer{};
|
|
|
+ if (!config.contains("value")) {
|
|
|
+ return (void *) placeholder;
|
|
|
+ }
|
|
|
+
|
|
|
+ // load default value
|
|
|
+ using elem_type = basic_obj_traits<SmallObjType>::element_type;
|
|
|
+ static constexpr auto elem_num = basic_obj_traits<SmallObjType>::element_size;
|
|
|
+
|
|
|
+ struct elem_provider {
|
|
|
+ std::array<elem_type, elem_num> vals = {};
|
|
|
+ size_t pos = 0;
|
|
|
+
|
|
|
+ auto &operator>>(elem_type &_val) {
|
|
|
+ _val = vals[pos++];
|
|
|
+ return *this;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ auto provider = elem_provider{};
|
|
|
+ const auto &value_conf = config["value"];
|
|
|
+ if (!value_conf.is_array()) {
|
|
|
+ assert(elem_num == 1);
|
|
|
+ provider.vals[0] = value_conf.get<elem_type>();
|
|
|
+ } else {
|
|
|
+ assert(elem_num == value_conf.size());
|
|
|
+ for (int i = 0; i < elem_num; ++i) {
|
|
|
+ provider.vals[i] = value_conf[i].get<elem_type>();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ *placeholder = SmallObjType::new_instance();
|
|
|
+ (*placeholder)->fill_from(provider);
|
|
|
+ return (void *) placeholder;
|
|
|
+ }
|
|
|
+
|
|
|
template<typename SmallObjType>
|
|
|
void register_variable_type(std::string_view type_name) {
|
|
|
static_assert(SmallObjType::binary_length() <= std::numeric_limits<uint8_t>::max());
|
|
|
@@ -58,7 +95,7 @@ namespace sophiar {
|
|
|
type_info.binary_length = SmallObjType::binary_length();
|
|
|
type_info.type = type;
|
|
|
type_info.type_name = type_name;
|
|
|
- type_info.creator_func = []() { return (void *) new SmallObjType::pointer; };
|
|
|
+ type_info.creator_func = create_variable_pointer<SmallObjType>;
|
|
|
|
|
|
// create IO functions
|
|
|
#define REGISTER_WRITER_FUNC(writer_type) { \
|
|
|
@@ -109,7 +146,8 @@ namespace sophiar {
|
|
|
}
|
|
|
|
|
|
void register_variable(std::string_view name,
|
|
|
- std::string_view type_name) {
|
|
|
+ std::string_view type_name,
|
|
|
+ const nlohmann::json &config = {}) {
|
|
|
auto index = variable_pool.new_elem(name);
|
|
|
auto &info = variable_pool[index];
|
|
|
|
|
|
@@ -117,7 +155,7 @@ namespace sophiar {
|
|
|
auto var_type_index = variable_type_name_index_map.query(type_name);
|
|
|
const auto &type_info = variable_type_info_pool[var_type_index];
|
|
|
|
|
|
- info.placeholder = type_info.creator_func();
|
|
|
+ info.placeholder = type_info.creator_func(config);
|
|
|
info.update_signal = new coro_signal2{};
|
|
|
info.type_info = &type_info;
|
|
|
info.last_update_ts = 0;
|
|
|
@@ -135,7 +173,7 @@ namespace sophiar {
|
|
|
for (auto &var_config: config["variable_list"]) {
|
|
|
auto var_name = LOAD_STRING_ITEM2(var_config, "name");
|
|
|
auto type_name = LOAD_STRING_ITEM2(var_config, "type");
|
|
|
- register_variable(var_name, type_name);
|
|
|
+ register_variable(var_name, type_name, var_config);
|
|
|
}
|
|
|
}
|
|
|
|