ModelSpace
All Classes Namespaces Functions Variables Enumerations Pages
DataIO.hpp
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16/*
17Data IO Header File
18
19Author: Alex Reynolds
20*/
21#ifndef DATA_IO_HPP
22#define DATA_IO_HPP
23
24#include <type_traits>
25
26#include "DataIOBase.h"
27#include "core/clockwerkerrors.h"
28#include "logging/SimLogger.h"
29
30namespace clockwerk {
31
32 /// @brief Class for inter-object communication
33 ///
34 /// The data IO object is a flexible input/output object for interfacing
35 /// between models, tasks, events, and so on. It is templated to work
36 /// with nearly any data type, provided that type can be passed and returned
37 /// via function.
38 ///
39 /// DataIO operates by abstracting data using a pointer. It holds an internal
40 /// value of type T, and a pointer to the data source to use. By default, that
41 /// data source is itself, but it may point to another DataIO object if
42 /// commanded to do so via the mapTo function. In that case, DataIO will
43 /// recursively call DataIO objects until it finds the root object, then return
44 /// the value held by the root object.
45 template <typename T>
46 class DataIO : public DataIOBase {
47 public:
48 /// @brief Constructor for the DataIO object
49 /// @param data_parent Pointer to parent graph tree object
50 /// @param data_name String name for the DataIO object
51 /// @param initial_value The initial value of the data IO object
52 /// @note Constructor is overloaded -- object can be created from many
53 /// configurations but it is on the developer to ensure it is handled correctly
54 DataIO(GraphTreeObject* data_parent, std::string data_name, T initial_value);
55 DataIO(GraphTreeObject* data_parent, std::string data_name);
56 DataIO(GraphTreeObject* data_parent);
57 DataIO();
58 ~DataIO() {};
59
60 /// @brief Function to map the DataIO object to a pointer, rather than another of its kind
61 /// @param data_source The pointer to which the DataIO will be mapped
62 void mapToPointer(T* data_source) {_data_ptr = data_source; data = this; _writeAllowed = true; _root = true;}
63
64 /// @brief Function to set the value of the DataIO object
65 /// @param new_value Value to set the dataIO object to
66 /// @return Error code according to success/failure
67 int operator()(const T &new_value);
68
69 /// @brief Function to return a handle to the root data held in THIS object
70 /// @return A pointer to root data if write is allowed, nullptr if not
71 /// @note DANGER! This method bypasses a lot of safeties in the signal system and breaks
72 /// traceability on pointer. Using this could allow a write to the wrong location
73 /// or read from the wrong location. It should be not be used in models and should be
74 /// reserved exclusively for built-in functions.
76
77 /// @param Function to break data mapping and reset object to its own internal data
78 /// @note Data will be reset to the value previously held by this signal prior to mapping
79 /// @note Resets write permission to true
81
82 /// @brief Overloaded operator to return value of DataIO object
83 /// @return Value pointed to by data -- could be internal or external
84 const T& operator()();
85
86 /// @brief Function to log data to a logger
87 /// @param logger Pointer to logger
88 /// @return Error code corresponding to success/failure
89 int log(void* logger);
90
91 /// @brief Function to pass headers for this object to the logger
92 /// @param logger Pointer to logger
93 /// @return Error code corresponding to success/failure
94 int header(void* logger);
95 protected:
96 /// Default data location pointed to by the DataIO object. Note that root
97 /// data can change if DataIO is mapped to another dataIO object
98 T _root_data;
99
100 /// Temporary pointer to hold our stuff internally
101 void* tmp_ptr;
102 };
103
104 template <typename T>
105 int DataIO<T>::log(void* logger) {
106 return ((SimLogger*)logger)->writeToBuffer(*getRootDataPointer());
107 }
108
109 template <typename T>
110 int DataIO<T>::header(void* logger) {
111 return ((SimLogger*)logger)->writeHeaders(*getRootDataPointer());
112 }
113
114 template <typename T>
115 int DataIO<T>::operator()(const T &new_value) {
116 /// Get a pointer to the data we're writing to
118
119 /// Now cast to our type and return the value
120 if(tmp_ptr == nullptr) {
122 }
123 *(T*)tmp_ptr = new_value;
124
125 return NO_ERROR;
126 }
127
128 template <typename T>
129 const T& DataIO<T>::operator()() {
130 return *(T*)read();
131 }
132
133 template <typename T>
134 DataIO<T>::DataIO(GraphTreeObject* data_parent, std::string data_name, T initial_value)
135 : _root_data(initial_value) {
136
138
139 /// Assign parent to object
140 DataIOBase::parent(data_parent);
141 DataIOBase::name(data_name);
142
143 /// Now identify our data type
144 _typeID(_root_data);
145
146 _loggable = true;
147 }
148
149 template <typename T>
150 DataIO<T>::DataIO(GraphTreeObject* data_parent, std::string data_name)
151 : _root_data() {
152
154
155 /// Assign parent to object
156 DataIOBase::parent(data_parent);
157 DataIOBase::name(data_name);
158
159 /// Now identify our data type
160 _typeID(_root_data);
161
162 _loggable = true;
163 }
164
165 template <typename T>
166 DataIO<T>::DataIO(GraphTreeObject* data_parent)
167 : _root_data() {
168
170
171 /// Assign parent to object
172 DataIOBase::parent(data_parent);
173
174 /// Now identify our data type
175 _typeID(_root_data);
176
177 if(_type_id == POINTER || _type_id == UNDEFINED) {
178 _loggable = true;
179 } else {
180 _loggable = true;
181 }
182 }
183
184 template <typename T>
185 DataIO<T>::DataIO()
186 : _root_data() {
188
189 /// Now identify our data type
190 _typeID(_root_data);
191
192 _loggable = true;
193 }
194
195 template <typename T>
196 T* DataIO<T>::getRootDataPointer() {
197 return (T*)DataIOBase::read();
198 }
199
200}
201
202#endif
The Data IO Base class is designed to provide the basic interface of the Data IO class (see documenta...
Definition DataIOBase.h:54
void * writePtr()
Function to write value to mapped location – root data or map.
Definition DataIOBase.cpp:20
void resetMap()
Definition DataIOBase.h:72
bool _root
Variable to indicate whether variable in question is root data source or not.
Definition DataIOBase.h:112
bool _writeAllowed
Variable to indicate whether writing is allowed to this variable Set to false upon mapping by default...
Definition DataIOBase.h:108
DataIOBase * data
Pointer to data held by the object – by default, this will point internally to _default_data,...
Definition DataIOBase.h:100
void * _data_ptr
Generic pointer to hold any data type – will be resolved to point to actual object in inherited class...
Definition DataIOBase.h:104
void * read()
Function to read value from mapped location – root data or map.
Definition DataIOBase.cpp:32
dataio_types_e _type_id
Variable to store the type of data that the DataIO object is holding. Valid types are documented in t...
Definition DataIOBase.h:117
Class for inter-object communication.
Definition DataIO.hpp:46
int log(void *logger)
Function to log data to a logger.
Definition DataIO.hpp:105
void resetMap()
Definition DataIO.hpp:80
void mapToPointer(T *data_source)
Function to map the DataIO object to a pointer, rather than another of its kind.
Definition DataIO.hpp:62
DataIO(GraphTreeObject *data_parent, std::string data_name, T initial_value)
Constructor for the DataIO object.
Definition DataIO.hpp:134
const T & operator()()
Overloaded operator to return value of DataIO object.
Definition DataIO.hpp:129
T * getRootDataPointer()
Function to return a handle to the root data held in THIS object.
Definition DataIO.hpp:196
void * tmp_ptr
Temporary pointer to hold our stuff internally.
Definition DataIO.hpp:101
int operator()(const T &new_value)
Function to set the value of the DataIO object.
Definition DataIO.hpp:115
int header(void *logger)
Function to pass headers for this object to the logger.
Definition DataIO.hpp:110
T _root_data
Default data location pointed to by the DataIO object. Note that root data can change if DataIO is ma...
Definition DataIO.hpp:98
Base class for object organization.
Definition GraphTreeObject.h:87
bool _loggable
Variable to indicate whether the selected object is loggable. Set to false by default.
Definition GraphTreeObject.h:257
Class for logging data to a file.
Definition SimLogger.h:67
#define NO_ERROR
Definition clockwerkerrors.h:31
#define ERROR_WRITE_PERMISSION
Definition clockwerkerrors.h:103