Skip to content

Commit e6b015a

Browse files
committed
fix(node-opal-orchestra): Use shared pointers for DDF items
Signed-off-by: Steffen Vogel <steffen.vogel@opal-rt.com>
1 parent 922ed11 commit e6b015a

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

include/villas/nodes/opal_orchestra/ddf.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,27 @@ class DataItem : public Item {
4848

4949
class BusItem : public Item {
5050
public:
51-
std::map<std::string, Item *> items;
51+
std::map<std::string, std::shared_ptr<Item>> items;
5252
std::string name;
5353

5454
BusItem(const std::string &name) : items(), name(name) {}
5555

56-
DataItem *upsertItem(std::vector<std::string> pathComponents, bool &inserted);
56+
std::shared_ptr<DataItem> upsertItem(std::vector<std::string> pathComponents,
57+
bool &inserted);
5758

5859
void toXml(xmlNode *parent, bool withDefault) const override;
5960
};
6061

6162
class DataSet {
6263
public:
63-
std::map<std::string, Item *> items;
64+
std::map<std::string, std::shared_ptr<Item>> items;
6465
std::string name;
6566

6667
DataSet(const std::string &name) : items(), name(name) {}
6768

68-
DataItem *upsertItem(const std::string &path, bool &inserted);
69-
DataItem *upsertItem(std::vector<std::string> pathComponents, bool &inserted);
69+
std::shared_ptr<DataItem> upsertItem(const std::string &path, bool &inserted);
70+
std::shared_ptr<DataItem> upsertItem(std::vector<std::string> pathComponents,
71+
bool &inserted);
7072

7173
void toXml(xmlNode *parent, bool withDefault) const;
7274
};
@@ -77,7 +79,7 @@ class Connection {
7779
virtual std::string getDetails() const = 0;
7880
virtual void parse(json_t *json) = 0;
7981

80-
static Connection *fromJson(json_t *json);
82+
static std::shared_ptr<Connection> fromJson(json_t *json);
8183
};
8284

8385
class ConnectionLocal : public Connection {
@@ -124,7 +126,7 @@ class ConnectionDolphin : public Connection {
124126
class Domain {
125127
public:
126128
std::string name;
127-
Connection *connection;
129+
std::shared_ptr<Connection> connection;
128130
bool synchronous;
129131
bool states;
130132
bool multiplePublishAllowed;

lib/nodes/opal_orchestra.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ using namespace villas::node::orchestra;
3838
// An OpalOrchestraMapping maps one or more VILLASnode signals to an Orchestra data item.
3939
class OpalOrchestraMapping {
4040
public:
41-
DataItem *item;
41+
std::shared_ptr<DataItem> item;
4242
std::string path;
4343
std::vector<Signal::Ptr> signals;
4444

@@ -53,7 +53,7 @@ class OpalOrchestraMapping {
5353
unsigned int typeSize; // sizeof() of the signal type. See RTSignalType.
5454
unsigned short length;
5555

56-
OpalOrchestraMapping(DataItem *item, const std::string &path)
56+
OpalOrchestraMapping(std::shared_ptr<DataItem> item, const std::string &path)
5757
: item(item), path(path), signals(), signalList(), indices() {}
5858

5959
void addSignal(Signal::Ptr signal, std::optional<unsigned> orchestraIdx) {
@@ -237,8 +237,8 @@ class OpalOrchestraNode : public Node {
237237

238238
Domain domain; // The domain to which the node belongs.
239239

240-
std::map<DataItem *, OpalOrchestraMapping> subscribeMappings;
241-
std::map<DataItem *, OpalOrchestraMapping> publishMappings;
240+
std::map<std::shared_ptr<DataItem>, OpalOrchestraMapping> subscribeMappings;
241+
std::map<std::shared_ptr<DataItem>, OpalOrchestraMapping> publishMappings;
242242

243243
double rate;
244244
std::optional<std::filesystem::path> dataDefinitionFilename;
@@ -332,8 +332,9 @@ class OpalOrchestraNode : public Node {
332332
skipWaitToGo(false), dataDefinitionFileOverwrite(false),
333333
dataDefinitionFileWriteOnly(false) {}
334334

335-
void parseSignals(json_t *json, SignalList::Ptr signals, DataSet &dataSet,
336-
std::map<DataItem *, OpalOrchestraMapping> &mappings) {
335+
void parseSignals(
336+
json_t *json, SignalList::Ptr signals, DataSet &dataSet,
337+
std::map<std::shared_ptr<DataItem>, OpalOrchestraMapping> &mappings) {
337338
if (!json_is_array(json)) {
338339
throw ConfigError(json, "node-config-node-opal-orchestra-signals",
339340
"Signals must be an array");
@@ -373,7 +374,7 @@ class OpalOrchestraNode : public Node {
373374
auto orchestraName = nme ? nme : signal->name;
374375

375376
bool inserted = false;
376-
auto *item = dataSet.upsertItem(orchestraName, inserted);
377+
auto item = dataSet.upsertItem(orchestraName, inserted);
377378

378379
if (inserted) {
379380
item->type = orchestraType;
@@ -552,7 +553,8 @@ class OpalOrchestraNode : public Node {
552553
throw RTError(ret, "Failed to check ready to go");
553554
}
554555

555-
if (auto *c = dynamic_cast<ConnectionRemote *>(domain.connection)) {
556+
if (std::shared_ptr<ConnectionRemote> c =
557+
std::dynamic_pointer_cast<ConnectionRemote>(domain.connection)) {
556558
ret = RTSetupRefMemRemoteConnection(c->name.c_str(), c->pciIndex);
557559
if (ret != RTAPI_SUCCESS) {
558560
throw RTError(ret,

lib/nodes/opal_orchestra/ddf.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* SPDX-License-Identifier: Apache-2.0
77
*/
88

9+
#include <memory>
910
extern "C" {
1011
#include <RTAPI.h>
1112
}
@@ -71,27 +72,27 @@ void BusItem::toXml(xmlNode *parent, bool withDefault) const {
7172
}
7273
}
7374

74-
DataItem *BusItem::upsertItem(std::vector<std::string> pathComponents,
75-
bool &inserted) {
75+
std::shared_ptr<DataItem>
76+
BusItem::upsertItem(std::vector<std::string> pathComponents, bool &inserted) {
7677
auto &name = pathComponents.front();
7778

7879
auto it = items.find(name);
7980
if (it == items.end()) { // No item with this name exists. Create a new one.
8081
if (pathComponents.size() == 1) { // No bus, just a signal.
81-
auto *item = new DataItem(name);
82+
auto item = std::make_shared<DataItem>(name);
8283
items.emplace(name, item);
8384
inserted = true;
8485
return item;
8586
} else {
86-
auto *bus = new BusItem(name);
87+
auto bus = std::make_shared<BusItem>(name);
8788
items.emplace(name, bus);
8889

8990
pathComponents.erase(pathComponents.begin());
9091
return bus->upsertItem(pathComponents, inserted);
9192
}
9293
} else {
9394
if (pathComponents.size() == 1) {
94-
auto *item = dynamic_cast<DataItem *>(it->second);
95+
auto item = std::dynamic_pointer_cast<DataItem>(it->second);
9596
if (!item) {
9697
throw RuntimeError("Item with name '{}' is not a data item",
9798
pathComponents.front());
@@ -100,7 +101,7 @@ DataItem *BusItem::upsertItem(std::vector<std::string> pathComponents,
100101
inserted = false;
101102
return item;
102103
} else {
103-
auto *bus = dynamic_cast<BusItem *>(it->second);
104+
auto bus = std::dynamic_pointer_cast<BusItem>(it->second);
104105
if (!bus) {
105106
throw RuntimeError("Item with name '{}' is not a bus",
106107
pathComponents.front());
@@ -112,31 +113,32 @@ DataItem *BusItem::upsertItem(std::vector<std::string> pathComponents,
112113
}
113114
}
114115

115-
DataItem *DataSet::upsertItem(const std::string &path, bool &inserted) {
116+
std::shared_ptr<DataItem> DataSet::upsertItem(const std::string &path,
117+
bool &inserted) {
116118
return upsertItem(split(path), inserted);
117119
}
118120

119-
DataItem *DataSet::upsertItem(std::vector<std::string> pathComponents,
120-
bool &inserted) {
121+
std::shared_ptr<DataItem>
122+
DataSet::upsertItem(std::vector<std::string> pathComponents, bool &inserted) {
121123
auto &name = pathComponents.front();
122124

123125
auto it = items.find(name);
124126
if (it == items.end()) { // No item with this name exists. Create a new one.
125127
if (pathComponents.size() == 1) { // No bus, just a signal.
126-
auto *item = new DataItem(name);
128+
auto item = std::make_shared<DataItem>(name);
127129
items.emplace(name, item);
128130
inserted = true;
129131
return item;
130132
} else {
131-
auto *bus = new BusItem(name);
133+
auto bus = std::make_shared<BusItem>(name);
132134
items.emplace(name, bus);
133135

134136
pathComponents.erase(pathComponents.begin());
135137
return bus->upsertItem(pathComponents, inserted);
136138
}
137139
} else {
138140
if (pathComponents.size() == 1) {
139-
auto *item = dynamic_cast<DataItem *>(it->second);
141+
auto item = std::dynamic_pointer_cast<DataItem>(it->second);
140142
if (!item) {
141143
throw RuntimeError("Item with name '{}' is not a data item",
142144
pathComponents.front());
@@ -146,7 +148,7 @@ DataItem *DataSet::upsertItem(std::vector<std::string> pathComponents,
146148
return item;
147149
} else {
148150
// Item with this name exists. Check if it is a bus.
149-
auto *bus = dynamic_cast<BusItem *>(it->second);
151+
auto bus = std::dynamic_pointer_cast<BusItem>(it->second);
150152
if (!bus) {
151153
throw RuntimeError("Item with name '{}' is not a bus",
152154
pathComponents.front());
@@ -333,7 +335,7 @@ void ConnectionDolphin::parse(json_t *json) {
333335
}
334336
}
335337

336-
Connection *Connection::fromJson(json_t *json) {
338+
std::shared_ptr<Connection> Connection::fromJson(json_t *json) {
337339
const char *type = nullptr;
338340
json_error_t err;
339341

@@ -343,16 +345,16 @@ Connection *Connection::fromJson(json_t *json) {
343345
"Failed to parse connection type");
344346
}
345347

346-
Connection *c = nullptr;
348+
std::shared_ptr<Connection> c;
347349

348350
if (strcmp(type, "local") == 0) {
349-
c = new ConnectionLocal();
351+
c = std::make_shared<ConnectionLocal>();
350352
c->parse(json);
351353
} else if (strcmp(type, "remote") == 0) {
352-
c = new ConnectionRemote();
354+
c = std::make_shared<ConnectionRemote>();
353355
c->parse(json);
354356
} else if (strcmp(type, "dolphin") == 0) {
355-
c = new ConnectionDolphin();
357+
c = std::make_shared<ConnectionDolphin>();
356358
c->parse(json);
357359
} else {
358360
throw ConfigError(json, err, "node-config-node-opal-orchestra-connection",

0 commit comments

Comments
 (0)