Skip to content

Commit 8f9875a

Browse files
committed
fable: Pass description and prototype arguments by value
This is more efficient than passing by const reference and is simpler than accepting an rvalue while providing the same value.
1 parent e629df4 commit 8f9875a

File tree

12 files changed

+41
-49
lines changed

12 files changed

+41
-49
lines changed

engine/src/stack.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ class PersistentConfable : public Confable {
9090
Conf conf_;
9191
};
9292

93-
inline auto id_prototype(std::string&& desc = "") {
93+
inline auto id_prototype(std::string desc = "") {
9494
return schema::make_prototype<std::string>(std::move(desc)).c_identifier();
9595
}
9696

97-
inline auto id_path_prototype(std::string&& desc = "") {
97+
inline auto id_path_prototype(std::string desc = "") {
9898
return schema::make_prototype<std::string>(std::move(desc))
9999
.pattern("^([a-zA-Z_][a-zA-Z0-9_]*/?)+$");
100100
}
@@ -742,7 +742,7 @@ class VehicleSchema : public fable::schema::Base<VehicleSchema> {
742742
using MakeFunc = ComponentSchema::MakeFunc;
743743
using TypeFactory = ComponentSchema::TypeFactory;
744744

745-
explicit VehicleSchema(std::string&& desc = "") : Base(std::move(desc)) {}
745+
explicit VehicleSchema(std::string desc = "") : Base(std::move(desc)) {}
746746

747747
public: // Special
748748
bool has_factory(const std::string& name) const { return components_.has_factory(name); }

fable/examples/contacts/src/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct Address : public fable::Confable {
8383
std::string postalCode;
8484

8585
Address() = default;
86-
Address(std::string&& street, std::string&& city, std::string&& state, std::string code) noexcept
86+
Address(std::string street, std::string city, std::string state, std::string code) noexcept
8787
: streetAddress(std::move(street))
8888
, city(std::move(city))
8989
, state(std::move(state))
@@ -179,7 +179,7 @@ struct Contact : public fable::Confable {
179179

180180
Contact() = default;
181181
Contact(
182-
std::string&& first, std::string&& last, bool alive, boost::optional<uint8_t> age) noexcept
182+
std::string first, std::string last, bool alive, boost::optional<uint8_t> age) noexcept
183183
: firstName(std::move(first)), lastName(std::move(last)), isAlive(alive), age(age) {}
184184

185185
Contact with_address(Address&& addr) && {

fable/include/fable/schema.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class Schema : public schema::Interface {
251251
std::string type_string() const override { return impl_->type_string(); }
252252
bool is_required() const override { return impl_->is_required(); }
253253
const std::string& description() const override { return impl_->description(); }
254-
void set_description(const std::string& s) override { return impl_->set_description(s); }
254+
void set_description(std::string s) override { return impl_->set_description(std::move(s)); }
255255
Json usage() const override { return impl_->usage(); }
256256
Json json_schema() const override { return impl_->json_schema(); }
257257
void validate(const Conf& c) const override { impl_->validate(c); }

fable/include/fable/schema/array.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ class Array : public Base<Array<T, N, P>> {
100100
using Type = std::array<T, N>;
101101
using PrototypeSchema = P;
102102

103-
Array(Type* ptr, std::string&& desc);
104-
Array(Type* ptr, const PrototypeSchema& prototype)
105-
: Base<Array<T, N, P>>(), prototype_(prototype), ptr_(ptr) {}
106-
Array(Type* ptr, const PrototypeSchema& prototype, std::string&& desc)
107-
: Base<Array<T, N, P>>(std::move(desc)), prototype_(prototype), ptr_(ptr) {}
103+
Array(Type* ptr, std::string desc);
104+
Array(Type* ptr, PrototypeSchema prototype)
105+
: Base<Array<T, N, P>>(), prototype_(std::move(prototype)), ptr_(ptr) {}
106+
Array(Type* ptr, PrototypeSchema prototype, std::string desc)
107+
: Base<Array<T, N, P>>(std::move(desc)), prototype_(std::move(prototype)), ptr_(ptr) {}
108108

109109
#if 0
110110
// This is defined in: fable/schema/magic.hpp
111-
Array(Type* ptr, std::string&& desc)
111+
Array(Type* ptr, std::string desc)
112112
: Array(ptr, make_prototype<T>(), std::move(desc)) {}
113113
#endif
114114

fable/include/fable/schema/const.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ class Const : public Base<Const<T, P>> {
3939
using Type = T;
4040
using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<P>>;
4141

42-
Const(const Type& constant, std::string desc);
43-
Const(const Type& constant, PrototypeSchema prototype, std::string desc)
42+
Const(Type constant, std::string desc);
43+
Const(Type constant, PrototypeSchema prototype, std::string desc)
4444
: Base<Const<T, P>>(prototype.type(), std::move(desc))
4545
, prototype_(std::move(prototype))
46-
, constant_(constant) {
46+
, constant_(std::move(constant)) {
4747
prototype_.reset_ptr();
4848
}
4949

@@ -98,8 +98,8 @@ class Const : public Base<Const<T, P>> {
9898
};
9999

100100
template <typename T, typename P, typename S>
101-
Const<T, P> make_const_schema(const T& constant, P&& prototype, S&& desc) {
102-
return Const<T, P>(constant, std::forward<P>(prototype), std::forward<S>(desc));
101+
Const<T, P> make_const_schema(T&& constant, P&& prototype, S&& desc) {
102+
return Const<T, P>(std::forward<T>(constant), std::forward<P>(prototype), std::forward<S>(desc));
103103
}
104104

105105
template <typename S1, typename S2>

fable/include/fable/schema/custom.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class CustomDeserializer : public schema::Interface {
6262
std::string type_string() const override { return impl_->type_string(); }
6363
bool is_required() const override { return impl_->is_required(); }
6464
const std::string& description() const override { return impl_->description(); }
65-
void set_description(const std::string& s) override { return impl_->set_description(s); }
65+
void set_description(std::string s) override { return impl_->set_description(std::move(s)); }
6666
Json usage() const override { return impl_->usage(); }
6767
Json json_schema() const override { return impl_->json_schema(); };
6868
void validate(const Conf& c) const override { impl_->validate(c); }

fable/include/fable/schema/interface.hpp

+7-11
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@
2828
#include <type_traits> // for enable_if_t<>, is_base_of<>
2929
#include <utility> // for move
3030

31-
#include <fable/conf.hpp> // for Conf
32-
#include <fable/error.hpp> // for SchemaError
33-
#include <fable/json.hpp> // for Json
31+
#include <fable/conf.hpp> // for Conf
32+
#include <fable/error.hpp> // for SchemaError
33+
#include <fable/fable_fwd.hpp> // for Confable
34+
#include <fable/json.hpp> // for Json
3435

3536
namespace fable {
36-
37-
// Forward declarations:
38-
class Confable;
39-
4037
namespace schema {
4138

4239
/**
@@ -129,7 +126,7 @@ class Interface {
129126
/**
130127
* Set human-readable description.
131128
*/
132-
virtual void set_description(const std::string& s) = 0;
129+
virtual void set_description(std::string s) = 0;
133130

134131
/**
135132
* Return a compact JSON description of the schema.
@@ -332,7 +329,7 @@ class Box : public Interface {
332329
std::string type_string() const override { return impl_->type_string(); }
333330
bool is_required() const override { return impl_->is_required(); }
334331
const std::string& description() const override { return impl_->description(); }
335-
void set_description(const std::string& s) override { return impl_->set_description(s); }
332+
void set_description(std::string s) override { return impl_->set_description(std::move(s)); }
336333
Json usage() const override { return impl_->usage(); }
337334
Json json_schema() const override { return impl_->json_schema(); };
338335
void validate(const Conf& c) const override { impl_->validate(c); }
@@ -394,8 +391,7 @@ class Base : public Interface {
394391
}
395392

396393
bool has_description() const { return !desc_.empty(); }
397-
void set_description(const std::string& s) override { desc_ = s; }
398-
void set_description(std::string&& s) { desc_ = std::move(s); }
394+
void set_description(std::string s) override { desc_ = std::move(s); }
399395
const std::string& description() const override { return desc_; }
400396
CRTP description(std::string desc) && {
401397
desc_ = std::move(desc);

fable/include/fable/schema/map.hpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323

2424
#pragma once
2525

26-
#include <limits> // for numeric_limits<>
27-
#include <map> // for map<>
28-
#include <memory> // for shared_ptr<>
29-
#include <regex> // for regex, regex_match
30-
#include <string> // for string
31-
#include <utility> // for move
32-
#include <vector> // for vector<>
33-
#include <optional> // for optional<>
26+
#include <limits> // for numeric_limits<>
27+
#include <map> // for map<>
28+
#include <memory> // for shared_ptr<>
29+
#include <optional> // for optional<>
30+
#include <regex> // for regex, regex_match
31+
#include <string> // for string
32+
#include <utility> // for move
33+
#include <vector> // for vector<>
3434

3535
#include <fable/schema/interface.hpp> // for Base<>
3636

fable/include/fable/schema/string.hpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@
3030
#include <utility> // for move
3131
#include <vector> // for vector<>
3232

33+
#include <fable/fable_fwd.hpp> // for Environment
3334
#include <fable/schema/interface.hpp> // for Base<>
3435

3536
namespace fable {
36-
37-
// Forward declarations:
38-
class Environment; // from <fable/environment.hpp>
39-
4037
namespace schema {
4138

4239
/**

fable/include/fable/schema/variant.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ class Variant : public Interface {
8282
}
8383

8484
bool has_description() const { return !desc_.empty(); }
85-
void set_description(const std::string& s) override { desc_ = s; }
86-
void set_description(std::string&& s) { desc_ = std::move(s); }
85+
void set_description(std::string s) override { desc_ = std::move(s); }
8786
const std::string& description() const override { return desc_; }
8887
Variant description(std::string desc) && {
8988
desc_ = std::move(desc);

fable/include/fable/schema/xmagic.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
#include <map> // for map<>
3939
#include <string> // for string
40-
#include <utility> // for move
40+
#include <utility> // for move, forward
4141
#include <vector> // for vector<>
4242
#include <array> // for array<>
4343

@@ -62,7 +62,7 @@ Vector<T, decltype(make_prototype<T>())> make_schema(std::vector<T>* ptr, S&& de
6262
}
6363

6464
template <typename T, size_t N, typename P>
65-
Array<T, N, P>::Array(std::array<T, N>* ptr, std::string&& desc)
65+
Array<T, N, P>::Array(std::array<T, N>* ptr, std::string desc)
6666
: Array<T, N, P>(ptr, make_prototype<T>(), std::move(desc)) {}
6767

6868
template <typename T, size_t N, typename S>
@@ -71,12 +71,12 @@ Array<T, N, decltype(make_prototype<T>())> make_schema(std::array<T, N>* ptr, S&
7171
}
7272

7373
template <typename T, typename P>
74-
Const<T, P>::Const(const T& constant, std::string desc)
74+
Const<T, P>::Const(T constant, std::string desc)
7575
: Const<T, P>(constant, make_prototype<T>(), std::move(desc)) {}
7676

7777
template <typename T, typename S>
78-
Const<T, decltype(make_prototype<T>())> make_const_schema(const T& constant, S&& desc) {
79-
return Const<T, decltype(make_prototype<T>())>(constant, std::forward<S>(desc));
78+
Const<T, decltype(make_prototype<T>())> make_const_schema(T&& constant, S&& desc) {
79+
return Const<T, decltype(make_prototype<T>())>(std::forward<T>(constant), std::forward<S>(desc));
8080
}
8181

8282
template <typename T, typename P>

plugins/noisy_sensor/src/noise_data.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class Random {
124124

125125
class DistributionFactory : public fable::schema::Factory<DistributionPtr> {
126126
public:
127-
DistributionFactory(DistributionPtr* ptr, std::string&& desc)
127+
DistributionFactory(DistributionPtr* ptr, std::string desc)
128128
: fable::schema::Factory<DistributionPtr>(ptr, std::move(desc)) {
129129
this->set_factory_key("binding");
130130
this->set_args_key("");

0 commit comments

Comments
 (0)