dds-fmu 0.5.1
DDS-FMU communication integration
DataMapper.hpp
1#pragma once
2
3/*
4 Copyright 2023, SINTEF Ocean
5 This Source Code Form is subject to the terms of the Mozilla Public
6 License, v. 2.0. If a copy of the MPL was not distributed with this
7 file, You can obtain one at http://mozilla.org/MPL/2.0/.
8*/
9
10#include <filesystem>
11#include <map>
12#include <queue>
13#include <string>
14#include <tuple>
15
16#include <xtypes/DynamicData.hpp>
17#include <xtypes/idl/idl.hpp>
18
19#include "visitors.hpp"
20
21namespace ddsfmu {
22
37public:
42 enum class Direction {
43 Read,
44 Write,
45 Parameter
46 };
47
54 typedef std::tuple<std::int32_t, std::int32_t, std::int32_t, std::int32_t> IndexOffsets;
55
56 DataMapper() = default;
57 DataMapper(const DataMapper&) = delete;
58 DataMapper& operator=(const DataMapper&) = delete;
59
68 void reset(const std::filesystem::path& fmu_resources);
69
70 inline void set_double(const std::int32_t value_ref, const double& value) {
71 m_real_writer.at(value_ref)(value);
72 }
73 inline void get_double(const std::int32_t value_ref, double& value) const {
74 m_real_reader.at(value_ref)(value);
75 }
76 inline void set_int(const std::int32_t value_ref, const std::int32_t& value) {
77 m_int_writer.at(value_ref)(value);
78 }
79 inline void get_int(const std::int32_t value_ref, std::int32_t& value) const {
80 m_int_reader.at(value_ref)(value);
81 }
82 inline void set_bool(const std::int32_t value_ref, const bool& value) {
83 m_bool_writer.at(value_ref)(value);
84 }
85 inline void get_bool(const std::int32_t value_ref, bool& value) const {
86 m_bool_reader.at(value_ref)(value);
87 }
88 inline void set_string(const std::int32_t value_ref, const std::string& value) {
89 m_string_writer.at(value_ref)(value);
90 }
91 inline void get_string(const std::int32_t value_ref, std::string& value) const {
92 m_string_reader.at(value_ref)(value);
93 }
94
95 inline eprosima::xtypes::DynamicData& data_ref(const std::string& topic, Direction read_write_param) {
96 return m_data_store.at(std::make_tuple(topic, read_write_param));
97 }
98 inline const eprosima::xtypes::DynamicData&
99 data_ref(const std::string& topic, Direction read_write_param) const {
100 return m_data_store.at(std::make_tuple(topic, read_write_param));
101 }
102
103 inline eprosima::xtypes::idl::Context& idl_context() { return m_context; }
104
105 inline IndexOffsets index_offsets(const std::string& topic, Direction read_write_param) const {
106 // We have the same number of readers and writers, so the same index applies to both
107 return m_offsets.at(std::make_tuple(topic, read_write_param));
108 }
109
110 inline void queue_for_key_parameter(const std::string& topic_name, const std::string& topic_type){
111 m_potential_keys.push(std::make_pair(topic_name, topic_type));
112 }
113
114 void process_key_queue();
115
116private:
117 typedef std::tuple<std::string, Direction> StoreKey;
118 void add(const std::string& topic_name, const std::string& topic_type, Direction read_write_param);
119 void clear();
120 std::int32_t m_int_offset, m_real_offset, m_bool_offset, m_string_offset;
121 std::map<StoreKey, IndexOffsets> m_offsets;
122 std::queue<std::pair<std::string, std::string>> m_potential_keys;
123 std::vector<std::function<void(const std::int32_t&)>> m_int_writer;
124 std::vector<std::function<void(std::int32_t&)>> m_int_reader;
125 std::vector<std::function<void(const double&)>> m_real_writer;
126 std::vector<std::function<void(double&)>> m_real_reader;
127 std::vector<std::function<void(const bool&)>> m_bool_writer;
128 std::vector<std::function<void(bool&)>> m_bool_reader;
129 std::vector<std::function<void(const std::string&)>> m_string_writer;
130 std::vector<std::function<void(std::string&)>> m_string_reader;
131 std::map<StoreKey, eprosima::xtypes::DynamicData> m_data_store;
132 eprosima::xtypes::idl::Context m_context;
133};
134
135}
Manages mapping between FMU signals and xtypes data storage.
Definition: DataMapper.hpp:36
DataMapper()=default
void get_bool(const std::int32_t value_ref, bool &value) const
Definition: DataMapper.hpp:85
void set_int(const std::int32_t value_ref, const std::int32_t &value)
Definition: DataMapper.hpp:76
void set_bool(const std::int32_t value_ref, const bool &value)
Definition: DataMapper.hpp:82
void get_double(const std::int32_t value_ref, double &value) const
Definition: DataMapper.hpp:73
DataMapper & operator=(const DataMapper &)=delete
Copy assignment.
void get_int(const std::int32_t value_ref, std::int32_t &value) const
Definition: DataMapper.hpp:79
void get_string(const std::int32_t value_ref, std::string &value) const
Definition: DataMapper.hpp:91
const eprosima::xtypes::DynamicData & data_ref(const std::string &topic, Direction read_write_param) const
Definition: DataMapper.hpp:99
std::tuple< std::int32_t, std::int32_t, std::int32_t, std::int32_t > IndexOffsets
Definition: DataMapper.hpp:54
void reset(const std::filesystem::path &fmu_resources)
Clears and repopulates internal data structures.
Definition: DataMapper.cpp:36
void queue_for_key_parameter(const std::string &topic_name, const std::string &topic_type)
Definition: DataMapper.hpp:110
Direction
Definition: DataMapper.hpp:42
eprosima::xtypes::idl::Context & idl_context()
Definition: DataMapper.hpp:103
DataMapper(const DataMapper &)=delete
Copy constructor.
eprosima::xtypes::DynamicData & data_ref(const std::string &topic, Direction read_write_param)
Definition: DataMapper.hpp:95
IndexOffsets index_offsets(const std::string &topic, Direction read_write_param) const
Definition: DataMapper.hpp:105
void set_double(const std::int32_t value_ref, const double &value)
Definition: DataMapper.hpp:70
void process_key_queue()
Definition: DataMapper.cpp:106
void set_string(const std::int32_t value_ref, const std::string &value)
Definition: DataMapper.hpp:88
Definition: auxiliaries.cpp:26