Skip to content

Commit d3a4e54

Browse files
committed
fix(node-opal-orchestra): Rework Item::upsertItem using std::string_view
Signed-off-by: Steffen Vogel <[email protected]>
1 parent b5f85c9 commit d3a4e54

File tree

2 files changed

+24
-57
lines changed
  • include/villas/nodes/opal_orchestra
  • lib/nodes/opal_orchestra

2 files changed

+24
-57
lines changed

include/villas/nodes/opal_orchestra/ddf.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ class BusItem : public Item {
5353

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

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

5958
void toXml(xmlNode *parent, bool withDefault) const override;
6059
};
@@ -66,9 +65,7 @@ class DataSet {
6665

6766
DataSet(const std::string &name) : items(), name(name) {}
6867

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);
68+
std::shared_ptr<DataItem> upsertItem(std::string_view path, bool &inserted);
7269

7370
void toXml(xmlNode *parent, bool withDefault) const;
7471
};

lib/nodes/opal_orchestra/ddf.cpp

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ extern "C" {
2020
using namespace villas::node;
2121
using namespace villas::node::orchestra;
2222

23-
static std::vector<std::string> split(const std::string &path,
24-
char delimiter = '/') {
25-
std::vector<std::string> components;
26-
size_t start = 0, end = 0;
27-
while ((end = path.find(delimiter, start)) != std::string::npos) {
28-
if (end != start) {
29-
components.push_back(path.substr(start, end - start));
30-
}
31-
start = end + 1;
32-
}
33-
if (start < path.size()) {
34-
components.push_back(path.substr(start));
35-
}
36-
return components;
37-
}
38-
3923
void DataItem::toXml(xmlNode *parent, bool withDefault) const {
4024
xmlNode *item = xmlNewChild(parent, nullptr, BAD_CAST "item", nullptr);
4125
xmlNewProp(item, BAD_CAST "name", BAD_CAST name.c_str());
@@ -72,13 +56,15 @@ void BusItem::toXml(xmlNode *parent, bool withDefault) const {
7256
}
7357
}
7458

75-
std::shared_ptr<DataItem>
76-
BusItem::upsertItem(std::vector<std::string> pathComponents, bool &inserted) {
77-
auto &name = pathComponents.front();
59+
std::shared_ptr<DataItem> BusItem::upsertItem(std::string_view path,
60+
bool &inserted) {
61+
auto separator = path.find('/');
62+
auto isSignal = separator == std::string::npos; // No bus, just a signal.
63+
auto name = std::string(path.substr(0, separator));
7864

7965
auto it = items.find(name);
8066
if (it == items.end()) { // No item with this name exists. Create a new one.
81-
if (pathComponents.size() == 1) { // No bus, just a signal.
67+
if (isSignal) {
8268
auto item = std::make_shared<DataItem>(name);
8369
items.emplace(name, item);
8470
inserted = true;
@@ -87,75 +73,59 @@ BusItem::upsertItem(std::vector<std::string> pathComponents, bool &inserted) {
8773
auto bus = std::make_shared<BusItem>(name);
8874
items.emplace(name, bus);
8975

90-
pathComponents.erase(pathComponents.begin());
91-
return bus->upsertItem(pathComponents, inserted);
76+
return bus->upsertItem(path.substr(separator + 1), inserted);
9277
}
9378
} else {
94-
if (pathComponents.size() == 1) {
79+
if (isSignal) {
9580
auto item = std::dynamic_pointer_cast<DataItem>(it->second);
9681
if (!item) {
97-
throw RuntimeError("Item with name '{}' is not a data item",
98-
pathComponents.front());
82+
throw RuntimeError("Item with name '{}' is not a data item", name);
9983
}
10084

10185
inserted = false;
10286
return item;
10387
} else {
10488
auto bus = std::dynamic_pointer_cast<BusItem>(it->second);
10589
if (!bus) {
106-
throw RuntimeError("Item with name '{}' is not a bus",
107-
pathComponents.front());
90+
throw RuntimeError("Item with name '{}' is not a bus", name);
10891
}
10992

110-
pathComponents.erase(pathComponents.begin());
111-
return bus->upsertItem(pathComponents, inserted);
93+
return bus->upsertItem(path.substr(separator + 1), inserted);
11294
}
11395
}
11496
}
11597

116-
std::shared_ptr<DataItem> DataSet::upsertItem(const std::string &path,
98+
std::shared_ptr<DataItem> DataSet::upsertItem(std::string_view path,
11799
bool &inserted) {
118-
return upsertItem(split(path), inserted);
119-
}
120-
121-
std::shared_ptr<DataItem>
122-
DataSet::upsertItem(std::vector<std::string> pathComponents, bool &inserted) {
123-
auto &name = pathComponents.front();
124-
100+
auto separator = path.find('/');
101+
auto name = std::string(path.substr(0, separator));
125102
auto it = items.find(name);
126103
if (it == items.end()) { // No item with this name exists. Create a new one.
127-
if (pathComponents.size() == 1) { // No bus, just a signal.
104+
if (separator == std::string_view::npos) { // No bus, just a signal.
128105
auto item = std::make_shared<DataItem>(name);
129-
items.emplace(name, item);
106+
items.emplace(std::move(name), item);
130107
inserted = true;
131108
return item;
132109
} else {
133110
auto bus = std::make_shared<BusItem>(name);
134-
items.emplace(name, bus);
135-
136-
pathComponents.erase(pathComponents.begin());
137-
return bus->upsertItem(pathComponents, inserted);
111+
items.emplace(std::move(name), bus);
112+
return bus->upsertItem(path.substr(separator + 1), inserted);
138113
}
139114
} else {
140-
if (pathComponents.size() == 1) {
115+
if (separator == std::string_view::npos) {
141116
auto item = std::dynamic_pointer_cast<DataItem>(it->second);
142117
if (!item) {
143-
throw RuntimeError("Item with name '{}' is not a data item",
144-
pathComponents.front());
118+
throw RuntimeError("Item with name '{}' is not a data item", name);
145119
}
146-
147120
inserted = false;
148121
return item;
149122
} else {
150123
// Item with this name exists. Check if it is a bus.
151124
auto bus = std::dynamic_pointer_cast<BusItem>(it->second);
152125
if (!bus) {
153-
throw RuntimeError("Item with name '{}' is not a bus",
154-
pathComponents.front());
126+
throw RuntimeError("Item with name '{}' is not a bus", name);
155127
}
156-
157-
pathComponents.erase(pathComponents.begin());
158-
return bus->upsertItem(pathComponents, inserted);
128+
return bus->upsertItem(path.substr(separator + 1), inserted);
159129
}
160130
}
161131
}

0 commit comments

Comments
 (0)