File: UnitPorts.cpp

package info (click to toggle)
dyssol 1.5.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,204 kB
  • sloc: cpp: 53,870; sh: 85; python: 59; makefile: 11
file content (330 lines) | stat: -rw-r--r-- 9,233 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* Copyright (c) 2020, Dyssol Development Team.
 * Copyright (c) 2024, DyssolTEC GmbH.
 * All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#include "UnitPorts.h"

#include "ContainerFunctions.h"
#include "DyssolStringConstants.h"
#include "DyssolUtilities.h"
#include "H5Handler.h"

////////////////////////////////////////////////////////////////////////////////
// CUnitPort
//

CUnitPort::CUnitPort(std::string _name, EUnitPort _type) :
	m_name{ std::move(_name) },
	m_type{ _type }
{
}

const std::string& CUnitPort::GetName() const
{
	return m_name;
}

void CUnitPort::SetName(const std::string& _name)
{
	m_name = _name;
}

EUnitPort CUnitPort::GetType() const
{
	return m_type;
}

void CUnitPort::SetType(EUnitPort _type)
{
	m_type = _type;
}

const std::string& CUnitPort::GetStreamKey() const
{
	return m_streamKey;
}

void CUnitPort::SetStreamKey(const std::string& _key)
{
	m_streamKey = _key;
}

CStream* CUnitPort::GetStream() const
{
	return m_stream;
}

void CUnitPort::SetStream(CStream* _stream)
{
	m_stream = _stream;
}

void CUnitPort::SaveToFile(CH5Handler& _h5File, const std::string& _path) const
{
	if (!_h5File.IsValid()) return;

	// current version of save procedure
	_h5File.WriteAttribute(_path, StrConst::H5AttrSaveVersion, m_saveVersion);

	_h5File.WriteData(_path, StrConst::UPort_H5Name,      m_name);
	_h5File.WriteData(_path, StrConst::UPort_H5Type,      E2I(m_type));
	_h5File.WriteData(_path, StrConst::UPort_H5StreamKey, m_streamKey);
}

void CUnitPort::LoadFromFile(CH5Handler& _h5File, const std::string& _path)
{
	if (!_h5File.IsValid()) return;

	// current version of save procedure
	//const int version = _h5File.ReadAttribute(_path, StrConst::H5AttrSaveVersion);

	_h5File.ReadData(_path, StrConst::UPort_H5Name,      m_name);
	_h5File.ReadData(_path, StrConst::UPort_H5StreamKey, m_streamKey);
}

////////////////////////////////////////////////////////////////////////////////
// CPortsManager
//

CPortsManager::CPortsManager(const CPortsManager& _other)
{
	m_ports = DeepCopy(_other.m_ports);
}

CPortsManager::CPortsManager(CPortsManager&& _other) noexcept
{
	swap(*this, _other);
}

CPortsManager& CPortsManager::operator=(CPortsManager _other)
{
	swap(*this, _other);
	return *this;
}

CPortsManager& CPortsManager::operator=(CPortsManager&& _other) noexcept
{
	CPortsManager tmp{ std::move(_other) };
	swap(*this, tmp);
	return *this;
}

void swap(CPortsManager& _first, CPortsManager& _second) noexcept
{
	std::swap(_first.m_ports, _second.m_ports);
}

CUnitPort* CPortsManager::AddPort(const std::string& _name, EUnitPort _type)
{
	if (GetPort(_name)) return nullptr;
	m_ports.emplace_back(new CUnitPort{ _name, _type });
	return m_ports.back().get();
}

const CUnitPort* CPortsManager::GetPort(const std::string& _name) const
{
	for (const auto& p : m_ports)
		if (p->GetName() == _name)
			return p.get();
	return nullptr;
}

CUnitPort* CPortsManager::GetPort(const std::string& _name)
{
	return const_cast<CUnitPort*>(const_cast<const CPortsManager&>(*this).GetPort(_name));
}

const CUnitPort* CPortsManager::GetPort(size_t _index) const
{
	if (_index >= m_ports.size()) return {};
	return m_ports[_index].get();
}

CUnitPort* CPortsManager::GetPort(size_t _index)
{
	return const_cast<CUnitPort*>(const_cast<const CPortsManager&>(*this).GetPort(_index));
}

std::vector<CUnitPort*> CPortsManager::GetAllPorts()
{
	std::vector<CUnitPort*> res;
	for (auto& p : m_ports)
		res.push_back(p.get());
	return res;
}

std::vector<const CUnitPort*> CPortsManager::GetAllPorts() const
{
	std::vector<const CUnitPort*> res;
	for (const auto& p : m_ports)
		res.push_back(p.get());
	return res;
}

std::vector<CUnitPort*> CPortsManager::GetAllInputPorts()
{
	std::vector<CUnitPort*> res;
	for (auto& p : m_ports)
		if (p->GetType() == EUnitPort::INPUT)
			res.push_back(p.get());
	return res;
}

std::vector<const CUnitPort*> CPortsManager::GetAllInputPorts() const
{
	std::vector<const CUnitPort*> res;
	for (const auto& p : m_ports)
		if (p->GetType() == EUnitPort::INPUT)
			res.push_back(p.get());
	return res;
}

std::vector<CUnitPort*> CPortsManager::GetAllOutputPorts()
{
	std::vector<CUnitPort*> res;
	for (auto& p : m_ports)
		if (p->GetType() == EUnitPort::OUTPUT)
			res.push_back(p.get());
	return res;
}

std::vector<const CUnitPort*> CPortsManager::GetAllOutputPorts() const
{
	std::vector<const CUnitPort*> res;
	for (const auto& p : m_ports)
		if (p->GetType() == EUnitPort::OUTPUT)
			res.push_back(p.get());
	return res;
}

size_t CPortsManager::GetPortsNumber() const
{
	return m_ports.size();
}

std::vector<std::string> CPortsManager::GetInputPortsNames() const
{
	std::vector<std::string> res;
	for (const auto& p : m_ports)
		if (p->GetType() == EUnitPort::INPUT)
			res.push_back(p->GetName());
	return res;
}

std::vector<std::string> CPortsManager::GetOutputPortsNames() const
{
	std::vector<std::string> res;
	for (const auto& p : m_ports)
		if (p->GetType() == EUnitPort::OUTPUT)
			res.push_back(p->GetName());
	return res;
}

void CPortsManager::Clear()
{
	m_ports.clear();
}

void CPortsManager::SaveToFile(CH5Handler& _h5File, const std::string& _path) const
{
	if (!_h5File.IsValid()) return;

	// current version of save procedure
	_h5File.WriteAttribute(_path, StrConst::H5AttrSaveVersion, m_saveVersion);

	_h5File.WriteData(_path, StrConst::UPort_H5PortsNames, AllPortNames());
	_h5File.WriteData(_path, StrConst::UPort_H5PortsKeys, AllPortStreamKeys());

	_h5File.WriteAttribute(_path, StrConst::UPort_H5AttrPortsNum, static_cast<int>(m_ports.size()));
	for (size_t i = 0; i < m_ports.size(); ++i)
	{
		const std::string portPath = _h5File.CreateGroup(_path, StrConst::UPort_H5GroupPortName + std::to_string(i));
		m_ports[i]->SaveToFile(_h5File, portPath);
	}
}

void CPortsManager::LoadFromFile(CH5Handler& _h5File, const std::string& _path)
{
	if (!_h5File.IsValid()) return;

	// version of save procedure
	//const int version = _h5File.ReadAttribute(_path, StrConst::H5AttrSaveVersion);

	/* complex loading procedure with names, while users can change ports during the development of a unit.
	 * this approach allows to properly load even if the order or names of ports are changed by a developer. */
	std::vector<std::string> names, keys;
	_h5File.ReadData(_path, StrConst::UPort_H5PortsNames, names);
	_h5File.ReadData(_path, StrConst::UPort_H5PortsKeys, keys);
	std::vector<bool> portsLoaded(m_ports.size(), false);	// whether an existing port is already loaded
	std::vector<bool> portsReaded(names.size(), false);		// whether a saved port is already used to load an existing port
	const std::string portPath = _path + "/" + StrConst::UPort_H5GroupPortName;
	// try to load by names
	for (size_t iExist = 0; iExist < m_ports.size(); ++iExist)
		for (size_t iSaved = 0; iSaved < names.size(); ++iSaved)
			if (m_ports[iExist]->GetName() == names[iSaved])
			{
				m_ports[iExist]->LoadFromFile(_h5File, portPath + std::to_string(iSaved));
				portsReaded[iSaved] = true;
				portsLoaded[iExist] = true;
				break;
			}
	// load rest by positions
	for (size_t i = 0; i < m_ports.size(); ++i)
		if (!portsLoaded[i] && i < portsReaded.size() && !portsReaded[i])
		{
			const std::string name = m_ports[i]->GetName();
			m_ports[i]->LoadFromFile(_h5File, portPath + std::to_string(i));
			m_ports[i]->SetName(name);
		}
}

void CPortsManager::LoadFromFile_v0(const CH5Handler& _h5File, const std::string& _path)
{
	if (!_h5File.IsValid()) return;

	/* complex loading procedure with names, while users can change ports during the development of a unit.
	* this approach allows to properly load even if the order or names of ports are changed by a developer. */
	std::vector<std::string> names, keys;
	_h5File.ReadData(_path, StrConst::UPort_H5PortsNames, names);
	_h5File.ReadData(_path, StrConst::UPort_H5PortsKeys, keys);
	std::vector<bool> portsLoaded(m_ports.size(), false);	// whether an existing port is already loaded
	std::vector<bool> portsReaded(names.size(), false);		// whether a saved port is already used to load an existing port
	// try to load by names
	for (size_t iExist = 0; iExist < m_ports.size(); ++iExist)
		for (size_t iSaved = 0; iSaved < names.size(); ++iSaved)
			if (m_ports[iExist]->GetName() == names[iSaved])
			{
				m_ports[iExist]->SetStreamKey(keys[iSaved]);
				portsReaded[iSaved] = true;
				portsLoaded[iExist] = true;
				break;
			}
	// load rest by positions
	for (size_t i = 0; i < m_ports.size(); ++i)
		if (!portsLoaded[i] && i < portsReaded.size() && !portsReaded[i])
			m_ports[i]->SetStreamKey(keys[i]);
}

void CPortsManager::LoadFromFile_v00(const CH5Handler& _h5File, const std::string& _path)
{
	std::vector<std::string> keys;
	_h5File.ReadData(_path, StrConst::BUnit_H5UnitPorts, keys);
	for (size_t i = 0; i < keys.size() && i < m_ports.size(); ++i)
		m_ports[i]->SetStreamKey(keys[i]);
}

std::vector<std::string> CPortsManager::AllPortNames() const
{
	std::vector<std::string> res;
	for (const auto& p : m_ports)
		res.push_back(p->GetName());
	return res;
}

std::vector<std::string> CPortsManager::AllPortStreamKeys() const
{
	std::vector<std::string> res;
	for (const auto& p : m_ports)
		res.push_back(p->GetStreamKey());
	return res;
}