66 * SPDX-License-Identifier: Apache-2.0
77 */
88
9+ #include < memory>
910extern " 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