Skip to content

Commit 04f5b1c

Browse files
committed
runtime: Take string arguments by value where reasonable
This simplifies the code as well as providing a more efficient implementation for string instantiation.
1 parent eafa8f3 commit 04f5b1c

File tree

8 files changed

+68
-87
lines changed

8 files changed

+68
-87
lines changed

runtime/include/cloe/core/error.hpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,23 @@
1818
/**
1919
* \file cloe/core/error.hpp
2020
* \see cloe/core/error.cpp
21-
* \see cloe/core.hpp
2221
*/
2322

2423
#pragma once
2524

26-
#include <stdexcept> // for exception
27-
#include <string> // for string
25+
#include <stdexcept> // for exception
26+
#include <string> // for string
27+
#include <string_view> // for string
2828

29-
#include <cloe/core/logger.hpp> // for fmt::format
29+
#include <fmt/format.h> // for fmt::format
3030

3131
namespace cloe {
3232

33+
class Error;
34+
3335
class Error : public std::exception {
3436
public:
35-
explicit Error(const std::string& what) : err_(what) {}
36-
explicit Error(const char* what) : err_(what) {}
37+
Error(std::string_view what) : err_(what.data()) {}
3738

3839
template <typename... Args>
3940
Error(std::string_view format, Args&&... args)
@@ -45,7 +46,7 @@ class Error : public std::exception {
4546

4647
bool has_explanation() const { return !explanation_.empty(); }
4748

48-
void set_explanation(const std::string& explanation);
49+
void set_explanation(std::string explanation);
4950

5051
template <typename... Args>
5152
void set_explanation(std::string_view format, Args&&... args) {
@@ -54,8 +55,8 @@ class Error : public std::exception {
5455

5556
const std::string& explanation() const { return explanation_; }
5657

57-
Error explanation(const std::string& explanation) && {
58-
set_explanation(explanation);
58+
Error explanation(std::string explanation) && {
59+
set_explanation(std::move(explanation));
5960
return std::move(*this);
6061
}
6162

runtime/include/cloe/entity.hpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ namespace cloe {
3434
*/
3535
class InvalidNameError : public Error {
3636
public:
37-
explicit InvalidNameError(const std::string& name)
38-
: Error("name is invalid: " + name), name_(name) {}
37+
explicit InvalidNameError(std::string name)
38+
: Error("name is invalid: " + name), name_(std::move(name)) {}
3939
virtual ~InvalidNameError() noexcept = default;
4040

4141
const std::string& name() const { return name_; }
@@ -49,22 +49,22 @@ class InvalidNameError : public Error {
4949
*/
5050
class Entity {
5151
public:
52-
explicit Entity(const std::string& name) {
53-
set_name(name);
52+
explicit Entity(std::string name) {
53+
set_name(std::move(name));
5454
set_description("");
5555
}
5656

57-
Entity(const std::string& name, const std::string& desc) {
58-
set_name(name);
59-
set_description(desc);
57+
Entity(std::string name, std::string desc) {
58+
set_name(std::move(name));
59+
set_description(std::move(desc));
6060
}
6161

6262
virtual ~Entity() noexcept = default;
6363

6464
/**
6565
* Return the name of the Entity.
6666
*/
67-
std::string name() const { return name_; }
67+
const std::string& name() const { return name_; }
6868

6969
/**
7070
* Set the name of the Entity.
@@ -80,16 +80,19 @@ class Entity {
8080
* start
8181
* _/strange_but_0k
8282
*/
83-
void set_name(const std::string& name);
83+
void set_name(std::string name);
8484

8585
/**
8686
* Return the optional description of the Entity.
8787
*
8888
* If there is no description, the empty string is returned.
8989
*/
90-
std::string description() const { return desc_; }
90+
const std::string& description() const { return desc_; }
9191

92-
void set_description(const std::string& desc) { desc_ = desc; }
92+
/**
93+
* Set the free-form description of the entity.
94+
*/
95+
void set_description(std::string desc) { desc_ = std::move(desc); }
9396

9497
protected:
9598
/**

runtime/include/cloe/model.hpp

+20-31
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,14 @@
4848

4949
#pragma once
5050

51-
#include <fable/confable.hpp> // for Confable
51+
#include <fable/confable.hpp> // for Confable
5252

53-
#include <cloe/core.hpp> // for Duration, Error
54-
#include <cloe/entity.hpp> // for Entity
53+
#include <cloe/cloe_fwd.hpp> // for Sync, Registrar
54+
#include <cloe/core.hpp> // for Duration, Error
55+
#include <cloe/entity.hpp> // for Entity
5556

5657
namespace cloe {
5758

58-
// Forward declaration:
59-
class Sync; // from cloe/sync.hpp
60-
class Registrar; // from cloe/registrar.hpp
61-
6259
/**
6360
* ModelError indicates that an error in a model has occurred.
6461
*/
@@ -67,16 +64,14 @@ class ModelError : public Error {
6764
using Error::Error;
6865
virtual ~ModelError() noexcept = default;
6966

70-
const std::string& explanation() const { return Error::explanation(); }
71-
72-
ModelError explanation(const std::string& explanation) && {
73-
this->set_explanation(explanation);
67+
ModelError explanation(std::string explanation) && {
68+
this->set_explanation(std::move(explanation));
7469
return std::move(*this);
7570
}
7671

7772
template <typename... Args>
78-
ModelError explanation(std::string_view format, const Args&... args) && {
79-
this->set_explanation(fmt::format(fmt::runtime(format), args...));
73+
ModelError explanation(std::string_view format, Args&&... args) && {
74+
this->set_explanation(fmt::format(fmt::runtime(format), std::forward<Args>(args)...));
8075
return std::move(*this);
8176
}
8277
};
@@ -92,16 +87,14 @@ class ModelAbort : public ModelError {
9287
using ModelError::ModelError;
9388
virtual ~ModelAbort() noexcept = default;
9489

95-
const std::string& explanation() const { return Error::explanation(); }
96-
97-
ModelError explanation(const std::string& explanation) && {
98-
this->set_explanation(explanation);
90+
ModelAbort explanation(std::string explanation) && {
91+
this->set_explanation(std::move(explanation));
9992
return std::move(*this);
10093
}
10194

10295
template <typename... Args>
103-
ModelAbort explanation(std::string_view format, const Args&... args) && {
104-
this->set_explanation(fmt::format(fmt::runtime(format), args...));
96+
ModelAbort explanation(std::string_view format, Args&&... args) && {
97+
this->set_explanation(fmt::format(fmt::runtime(format), std::forward<Args>(args)...));
10598
return std::move(*this);
10699
}
107100
};
@@ -115,16 +108,14 @@ class ModelReset : public ModelError {
115108
using ModelError::ModelError;
116109
virtual ~ModelReset() noexcept = default;
117110

118-
const std::string& explanation() const { return Error::explanation(); }
119-
120-
ModelError explanation(const std::string& explanation) && {
121-
this->set_explanation(explanation);
111+
ModelReset explanation(std::string explanation) && {
112+
this->set_explanation(std::move(explanation));
122113
return std::move(*this);
123114
}
124115

125116
template <typename... Args>
126-
ModelReset explanation(std::string_view format, const Args&... args) && {
127-
this->set_explanation(fmt::format(fmt::runtime(format), args...));
117+
ModelReset explanation(std::string_view format, Args&&... args) && {
118+
this->set_explanation(fmt::format(fmt::runtime(format), std::forward(args)...));
128119
return std::move(*this);
129120
}
130121
};
@@ -138,16 +129,14 @@ class ModelStop : public ModelError {
138129
using ModelError::ModelError;
139130
virtual ~ModelStop() noexcept = default;
140131

141-
const std::string& explanation() const { return Error::explanation(); }
142-
143-
ModelError explanation(const std::string& explanation) && {
144-
this->set_explanation(explanation);
132+
ModelStop explanation(std::string explanation) && {
133+
this->set_explanation(std::move(explanation));
145134
return std::move(*this);
146135
}
147136

148137
template <typename... Args>
149-
ModelStop explanation(std::string_view format, const Args&... args) && {
150-
this->set_explanation(fmt::format(fmt::runtime(format), args...));
138+
ModelStop explanation(std::string_view format, Args&&... args) && {
139+
this->set_explanation(fmt::format(fmt::runtime(format), std::forward<Args>(args)...));
151140
return std::move(*this);
152141
}
153142
};

runtime/include/cloe/trigger.hpp

+18-28
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ struct InlineSchema {
9090
* Construct an implicit inline schema if enabled is true, i.e., one where
9191
* only the name itself is sufficient.
9292
*
93-
* If possible, it is recommended to use InlineSchema(std::string&&) instead.
93+
* If possible, it is recommended to use InlineSchema(std::string) instead.
9494
* This constructor is primarily useful when you want to explicitly disable
9595
* an inline schema.
9696
*/
@@ -99,14 +99,14 @@ struct InlineSchema {
9999
/**
100100
* Construct an implicit inline schema with the given description.
101101
*/
102-
explicit InlineSchema(std::string&& desc) : required_(false), desc_(std::move(desc)) {}
102+
explicit InlineSchema(std::string desc) : required_(false), desc_(std::move(desc)) {}
103103

104104
/**
105105
* Construct an inline schema that takes a particular primitive type.
106106
*
107107
* The type may not be null, object, or array.
108108
*/
109-
InlineSchema(std::string&& desc, fable::JsonType type, bool required = true);
109+
InlineSchema(std::string desc, fable::JsonType type, bool required = true);
110110

111111
/**
112112
* Construct an inline schema that takes a string with the given format.
@@ -131,7 +131,7 @@ struct InlineSchema {
131131
* ambiguity but should be used sparingly (for now, as usage is often
132132
* directly read from JSON).
133133
*/
134-
InlineSchema(std::string&& desc, std::string&& format, bool required = true)
134+
InlineSchema(std::string desc, std::string format, bool required = true)
135135
: type_(fable::JsonType::string)
136136
, required_(required)
137137
, usage_(std::move(format))
@@ -221,52 +221,42 @@ struct TriggerSchema {
221221
* "name": "stop"
222222
* }
223223
*/
224-
TriggerSchema(const std::string& name, const std::string& desc)
225-
: name_(name), schema_(std::string(desc)), inline_(true) {}
224+
TriggerSchema(std::string name, std::string desc)
225+
: name_(std::move(name)), schema_(std::move(desc)), inline_(true) {}
226226

227227
/**
228228
* Construct a TriggerSchema that describes a trigger with parameters
229229
* but no inline format.
230230
*/
231-
TriggerSchema(const std::string& name, const std::string& desc,
232-
fable::schema::PropertyList<> props)
233-
: name_(name), schema_(std::string(desc), props), inline_(false) {}
231+
TriggerSchema(std::string name, std::string desc, fable::schema::PropertyList<> props)
232+
: name_(std::move(name)), schema_(std::move(desc), std::move(props)), inline_(false) {}
234233

235234
/**
236235
* Construct a TriggerSchema that describes a trigger with the given Schema
237236
* but no inline format.
238237
*/
239-
TriggerSchema(const std::string& name, const std::string& desc, Schema&& s)
240-
: name_(name), schema_(std::move(s)), inline_(false) {
241-
schema_.set_description(desc);
238+
TriggerSchema(std::string name, std::string desc, Schema s)
239+
: name_(std::move(name)), schema_(std::move(s)), inline_(false) {
240+
schema_.set_description(std::move(desc));
242241
}
243242

244243
/**
245244
* Construct a TriggerSchema that describes a trigger with parameters
246245
* and a specified inline format.
247246
*/
248-
TriggerSchema(const std::string& name, const std::string& desc, InlineSchema&& usage,
247+
TriggerSchema(std::string name, std::string desc, InlineSchema usage,
249248
fable::schema::PropertyList<> props)
250-
: name_(name), schema_(std::string(desc), props), inline_(std::move(usage)) {}
251-
252-
/**
253-
* Construct a TriggerSchema that describes a trigger with the given Schema
254-
* and a specified inline format.
255-
*/
256-
TriggerSchema(const std::string& name, const std::string& desc, InlineSchema&& usage,
257-
Schema&& init)
258-
: name_(name), schema_(std::move(init)), inline_(std::move(usage)) {
259-
schema_.set_description(desc);
260-
}
249+
: name_(std::move(name))
250+
, schema_(std::move(desc), std::move(props))
251+
, inline_(std::move(usage)) {}
261252

262253
/**
263254
* Construct a TriggerSchema that describes a trigger with the given Schema
264255
* and a specified inline format.
265256
*/
266-
TriggerSchema(const std::string& name, const std::string& desc, InlineSchema&& usage,
267-
const Schema& init)
268-
: name_(name), schema_(init), inline_(std::move(usage)) {
269-
schema_.set_description(desc);
257+
TriggerSchema(std::string name, std::string desc, InlineSchema usage, Schema init)
258+
: name_(std::move(name)), schema_(std::move(init)), inline_(std::move(usage)) {
259+
schema_.set_description(std::move(desc));
270260
}
271261

272262
const std::string& name() const { return name_; }

runtime/include/cloe/utility/tcp_transceiver_config.hpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ struct TcpTransceiverConfiguration : public Confable {
8181
*/
8282
struct TcpTransceiverFullConfiguration : public TcpTransceiverConfiguration {
8383
TcpTransceiverFullConfiguration() = default;
84-
TcpTransceiverFullConfiguration(const std::string& host, uint16_t port)
85-
: host(host), port(port) {}
86-
TcpTransceiverFullConfiguration(std::string&& host, uint16_t port)
84+
TcpTransceiverFullConfiguration(std::string host, uint16_t port)
8785
: host(std::move(host)), port(port) {}
8886

8987
/**

runtime/src/cloe/core/error.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828

2929
namespace cloe {
3030

31-
void Error::set_explanation(const std::string& s) {
31+
void Error::set_explanation(std::string s) {
3232
// Writing long explanations is a PITA, so we do some preprocessing on the
3333
// string s to make it nicer:
3434
//
3535
// If s starts with a newline, then the following string of spaces is
3636
// interpreted as the amount to be removed following every newline.
37-
explanation_ = s;
37+
explanation_ = std::move(s);
3838
if (explanation_.empty() || explanation_[0] != '\n') {
3939
return;
4040
}

runtime/src/cloe/entity.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
namespace cloe {
2828

2929
static std::regex VALID_NAME_REGEX{"^[a-zA-Z_][a-zA-Z0-9_]*(/[a-zA-Z_][a-zA-Z0-9_]*)*$"};
30-
void Entity::set_name(const std::string& name) {
30+
void Entity::set_name(std::string name) {
3131
if (!std::regex_match(name, VALID_NAME_REGEX)) {
3232
throw InvalidNameError(name);
3333
}
34-
name_ = name;
34+
name_ = std::move(name);
3535
}
3636

3737
} // namespace cloe

runtime/src/cloe/trigger.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
namespace cloe {
3131

32-
InlineSchema::InlineSchema(std::string&& desc, JsonType type, bool required)
32+
InlineSchema::InlineSchema(std::string desc, JsonType type, bool required)
3333
: type_(type)
3434
, required_(required)
3535
, usage_("<" + fable::to_string(type) + ">")

0 commit comments

Comments
 (0)