6
6
* SPDX-License-Identifier: Apache-2.0
7
7
*/
8
8
9
+ #include < memory>
9
10
extern " C" {
10
11
#include < RTAPI.h>
11
12
}
@@ -71,27 +72,27 @@ void BusItem::toXml(xmlNode *parent, bool withDefault) const {
71
72
}
72
73
}
73
74
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) {
76
77
auto &name = pathComponents.front ();
77
78
78
79
auto it = items.find (name);
79
80
if (it == items.end ()) { // No item with this name exists. Create a new one.
80
81
if (pathComponents.size () == 1 ) { // No bus, just a signal.
81
- auto * item = new DataItem (name);
82
+ auto item = std::make_shared< DataItem> (name);
82
83
items.emplace (name, item);
83
84
inserted = true ;
84
85
return item;
85
86
} else {
86
- auto * bus = new BusItem (name);
87
+ auto bus = std::make_shared< BusItem> (name);
87
88
items.emplace (name, bus);
88
89
89
90
pathComponents.erase (pathComponents.begin ());
90
91
return bus->upsertItem (pathComponents, inserted);
91
92
}
92
93
} else {
93
94
if (pathComponents.size () == 1 ) {
94
- auto * item = dynamic_cast <DataItem * >(it->second );
95
+ auto item = std::dynamic_pointer_cast <DataItem>(it->second );
95
96
if (!item) {
96
97
throw RuntimeError (" Item with name '{}' is not a data item" ,
97
98
pathComponents.front ());
@@ -100,7 +101,7 @@ DataItem *BusItem::upsertItem(std::vector<std::string> pathComponents,
100
101
inserted = false ;
101
102
return item;
102
103
} else {
103
- auto * bus = dynamic_cast <BusItem * >(it->second );
104
+ auto bus = std::dynamic_pointer_cast <BusItem>(it->second );
104
105
if (!bus) {
105
106
throw RuntimeError (" Item with name '{}' is not a bus" ,
106
107
pathComponents.front ());
@@ -112,31 +113,32 @@ DataItem *BusItem::upsertItem(std::vector<std::string> pathComponents,
112
113
}
113
114
}
114
115
115
- DataItem *DataSet::upsertItem (const std::string &path, bool &inserted) {
116
+ std::shared_ptr<DataItem> DataSet::upsertItem (const std::string &path,
117
+ bool &inserted) {
116
118
return upsertItem (split (path), inserted);
117
119
}
118
120
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) {
121
123
auto &name = pathComponents.front ();
122
124
123
125
auto it = items.find (name);
124
126
if (it == items.end ()) { // No item with this name exists. Create a new one.
125
127
if (pathComponents.size () == 1 ) { // No bus, just a signal.
126
- auto * item = new DataItem (name);
128
+ auto item = std::make_shared< DataItem> (name);
127
129
items.emplace (name, item);
128
130
inserted = true ;
129
131
return item;
130
132
} else {
131
- auto * bus = new BusItem (name);
133
+ auto bus = std::make_shared< BusItem> (name);
132
134
items.emplace (name, bus);
133
135
134
136
pathComponents.erase (pathComponents.begin ());
135
137
return bus->upsertItem (pathComponents, inserted);
136
138
}
137
139
} else {
138
140
if (pathComponents.size () == 1 ) {
139
- auto * item = dynamic_cast <DataItem * >(it->second );
141
+ auto item = std::dynamic_pointer_cast <DataItem>(it->second );
140
142
if (!item) {
141
143
throw RuntimeError (" Item with name '{}' is not a data item" ,
142
144
pathComponents.front ());
@@ -146,7 +148,7 @@ DataItem *DataSet::upsertItem(std::vector<std::string> pathComponents,
146
148
return item;
147
149
} else {
148
150
// 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 );
150
152
if (!bus) {
151
153
throw RuntimeError (" Item with name '{}' is not a bus" ,
152
154
pathComponents.front ());
@@ -333,7 +335,7 @@ void ConnectionDolphin::parse(json_t *json) {
333
335
}
334
336
}
335
337
336
- Connection * Connection::fromJson (json_t *json) {
338
+ std::shared_ptr< Connection> Connection::fromJson (json_t *json) {
337
339
const char *type = nullptr ;
338
340
json_error_t err;
339
341
@@ -343,16 +345,16 @@ Connection *Connection::fromJson(json_t *json) {
343
345
" Failed to parse connection type" );
344
346
}
345
347
346
- Connection *c = nullptr ;
348
+ std::shared_ptr< Connection> c ;
347
349
348
350
if (strcmp (type, " local" ) == 0 ) {
349
- c = new ConnectionLocal ();
351
+ c = std::make_shared< ConnectionLocal> ();
350
352
c->parse (json);
351
353
} else if (strcmp (type, " remote" ) == 0 ) {
352
- c = new ConnectionRemote ();
354
+ c = std::make_shared< ConnectionRemote> ();
353
355
c->parse (json);
354
356
} else if (strcmp (type, " dolphin" ) == 0 ) {
355
- c = new ConnectionDolphin ();
357
+ c = std::make_shared< ConnectionDolphin> ();
356
358
c->parse (json);
357
359
} else {
358
360
throw ConfigError (json, err, " node-config-node-opal-orchestra-connection" ,
0 commit comments