Skip to content

Commit 7184411

Browse files
committed
engine: Replace boost::filesystem with std::filesystem
1 parent aec4bce commit 7184411

File tree

7 files changed

+75
-70
lines changed

7 files changed

+75
-70
lines changed

engine/src/simulation.cpp

+14-15
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,13 @@
7575

7676
#include "simulation.hpp"
7777

78-
#include <cstdint> // for uint64_t
79-
#include <fstream> // for ofstream
80-
#include <future> // for future<>, async
81-
#include <sstream> // for stringstream
82-
#include <string> // for string
83-
#include <thread> // for sleep_for
84-
85-
#include <boost/filesystem.hpp> // for is_directory, is_regular_file, ...
78+
#include <cstdint> // for uint64_t
79+
#include <filesystem> // for filesystem::path
80+
#include <fstream> // for ofstream
81+
#include <future> // for future<>, async
82+
#include <sstream> // for stringstream
83+
#include <string> // for string
84+
#include <thread> // for sleep_for
8685

8786
#include <cloe/controller.hpp> // for Controller
8887
#include <cloe/core/abort.hpp> // for AsyncAbort
@@ -1608,7 +1607,7 @@ size_t Simulation::write_output(const SimulationResult& r) const {
16081607
return;
16091608
}
16101609

1611-
boost::filesystem::path filepath = r.get_output_filepath(*filename);
1610+
std::filesystem::path filepath = r.get_output_filepath(*filename);
16121611
if (write_output_file(filepath, output)) {
16131612
files_written++;
16141613
}
@@ -1624,7 +1623,7 @@ size_t Simulation::write_output(const SimulationResult& r) const {
16241623
return files_written;
16251624
}
16261625

1627-
bool Simulation::write_output_file(const boost::filesystem::path& filepath,
1626+
bool Simulation::write_output_file(const std::filesystem::path& filepath,
16281627
const cloe::Json& j) const {
16291628
if (!is_writable(filepath)) {
16301629
return false;
@@ -1641,24 +1640,24 @@ bool Simulation::write_output_file(const boost::filesystem::path& filepath,
16411640
return true;
16421641
}
16431642

1644-
bool Simulation::is_writable(const boost::filesystem::path& filepath) const {
1643+
bool Simulation::is_writable(const std::filesystem::path& filepath) const {
16451644
// Make sure we're not clobbering anything if we shouldn't.
16461645
auto native = filepath.native();
1647-
if (boost::filesystem::exists(filepath)) {
1646+
if (std::filesystem::exists(filepath)) {
16481647
if (!config_.engine.output_clobber_files) {
16491648
logger()->error("Will not clobber file: {}", native);
16501649
return false;
16511650
}
1652-
if (!boost::filesystem::is_regular_file(filepath)) {
1651+
if (!std::filesystem::is_regular_file(filepath)) {
16531652
logger()->error("Cannot clobber non-regular file: {}", native);
16541653
return false;
16551654
}
16561655
}
16571656

16581657
// Make sure the directory exists.
16591658
auto dirpath = filepath.parent_path();
1660-
if (!boost::filesystem::is_directory(dirpath)) {
1661-
bool ok = boost::filesystem::create_directories(dirpath);
1659+
if (!std::filesystem::is_directory(dirpath)) {
1660+
bool ok = std::filesystem::create_directories(dirpath);
16621661
if (!ok) {
16631662
logger()->error("Error creating leading directories: {}", dirpath.native());
16641663
return false;

engine/src/simulation.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct SimulationResult {
4949
cloe::Json signals; // dump of all signals in DataBroker right before the simulation started
5050
std::vector<std::string>
5151
signals_autocompletion; // pseudo lua file used for vscode autocompletion
52-
std::optional<boost::filesystem::path> output_dir;
52+
std::optional<std::filesystem::path> output_dir;
5353

5454
public:
5555
/**
@@ -65,8 +65,8 @@ struct SimulationResult {
6565
* and output path are set automatically. Thus, if they are empty, then
6666
* that is because the user explicitly set them so.
6767
*/
68-
boost::filesystem::path get_output_filepath(const boost::filesystem::path& filename) const {
69-
boost::filesystem::path filepath;
68+
std::filesystem::path get_output_filepath(const std::filesystem::path& filename) const {
69+
std::filesystem::path filepath;
7070
if (filename.is_absolute()) {
7171
filepath = filename;
7272
} else if (output_dir) {
@@ -136,12 +136,12 @@ class Simulation {
136136
/**
137137
* Write the given JSON output into the file. Return true if successful.
138138
*/
139-
bool write_output_file(const boost::filesystem::path& filepath, const cloe::Json& j) const;
139+
bool write_output_file(const std::filesystem::path& filepath, const cloe::Json& j) const;
140140

141141
/**
142142
* Check if the given filepath may be opened, respecting clobber options.
143143
*/
144-
bool is_writable(const boost::filesystem::path& filepath) const;
144+
bool is_writable(const std::filesystem::path& filepath) const;
145145

146146
/**
147147
* Set whether simulation progress should be reported.

engine/src/stack.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323

2424
#include "stack.hpp"
2525

26-
#include <algorithm> // for transform, swap
27-
#include <map> // for map<>
28-
#include <memory> // for shared_ptr<>
29-
#include <set> // for set<>
30-
#include <string> // for string
26+
#include <algorithm> // for transform, swap
27+
#include <filesystem> // for filesystem
28+
#include <map> // for map<>
29+
#include <memory> // for shared_ptr<>
30+
#include <set> // for set<>
31+
#include <string> // for string
3132

32-
#include <boost/algorithm/string/predicate.hpp> // for starts_with
33-
#include <boost/filesystem.hpp> // for path
34-
namespace fs = boost::filesystem;
33+
namespace fs = std::filesystem;
3534

3635
#include <cloe/utility/std_extensions.hpp> // for join_vector
37-
#include <fable/utility.hpp> // for indent_string
36+
#include <fable/utility.hpp> // for indent_string
37+
#include <fable/utility/string.hpp> // for starts_with
3838

3939
namespace cloe {
4040

@@ -87,7 +87,7 @@ void LoggingConf::apply() const {
8787

8888
std::string PluginConf::canonical() const {
8989
// Handle builtins specially, these are in a URI form.
90-
if (boost::starts_with(plugin_path.string(), "builtin://")) {
90+
if (fable::starts_with(plugin_path.string(), "builtin://")) {
9191
return plugin_path.string();
9292
}
9393

engine/src/stack.hpp

+21-23
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@
2323

2424
#pragma once
2525

26-
#include <map> // for map<>
27-
#include <memory> // for shared_ptr<>
28-
#include <set> // for set<>
29-
#include <string> // for string
30-
#include <utility> // for move
31-
#include <vector> // for vector<>
32-
#include <optional> // for optional<>
33-
34-
#include <boost/filesystem/path.hpp> // for path
35-
#include <fable/schema/boost_optional.hpp> // for Optional<>
36-
#include <fable/schema/boost_path.hpp> // for Path
26+
#include <filesystem> // for filesystem::path
27+
#include <map> // for map<>
28+
#include <memory> // for shared_ptr<>
29+
#include <optional> // for optional<>
30+
#include <set> // for set<>
31+
#include <string> // for string
32+
#include <utility> // for move
33+
#include <vector> // for vector<>
34+
3735
#include <fable/schema/custom.hpp> // for CustomDeserializer
3836
#include <fable/schema/factory.hpp> // for Factory
3937

@@ -83,7 +81,7 @@ inline auto id_path_prototype(std::string desc = "") {
8381
* IncludeConf is a relative or absolute filepath that should be included in
8482
* the stack configuration.
8583
*/
86-
using IncludeConf = boost::filesystem::path;
84+
using IncludeConf = std::filesystem::path;
8785
using IncludeSchema = decltype(schema::make_schema(static_cast<IncludeConf*>(nullptr), ""));
8886
using IncludesSchema = schema::Vector<IncludeConf, IncludeSchema>;
8987

@@ -202,7 +200,7 @@ struct ServerConf : public Confable {
202200
*/
203201
struct PluginConf : public PersistentConfable {
204202
/** Filesystem path to file or directory. */
205-
boost::filesystem::path plugin_path{};
203+
std::filesystem::path plugin_path{};
206204

207205
/** Name to give plugin if path is to a single file. */
208206
std::optional<std::string> plugin_name{};
@@ -309,14 +307,14 @@ struct EngineConf : public Confable {
309307
bool triggers_ignore_source{false};
310308

311309
// Output:
312-
std::optional<boost::filesystem::path> registry_path{CLOE_DATA_HOME "/registry"};
313-
std::optional<boost::filesystem::path> output_path{"${CLOE_SIMULATION_UUID}"};
314-
std::optional<boost::filesystem::path> output_file_config{"config.json"};
315-
std::optional<boost::filesystem::path> output_file_result{"result.json"};
316-
std::optional<boost::filesystem::path> output_file_triggers{"triggers.json"};
317-
std::optional<boost::filesystem::path> output_file_signals{"signals.json"};
318-
std::optional<boost::filesystem::path> output_file_signals_autocompletion;
319-
std::optional<boost::filesystem::path> output_file_data_stream;
310+
std::optional<std::filesystem::path> registry_path{CLOE_DATA_HOME "/registry"};
311+
std::optional<std::filesystem::path> output_path{"${CLOE_SIMULATION_UUID}"};
312+
std::optional<std::filesystem::path> output_file_config{"config.json"};
313+
std::optional<std::filesystem::path> output_file_result{"result.json"};
314+
std::optional<std::filesystem::path> output_file_triggers{"triggers.json"};
315+
std::optional<std::filesystem::path> output_file_signals{"signals.json"};
316+
std::optional<std::filesystem::path> output_file_signals_autocompletion;
317+
std::optional<std::filesystem::path> output_file_data_stream;
320318
bool output_clobber_files{true};
321319

322320
/**
@@ -390,8 +388,8 @@ struct EngineConf : public Confable {
390388
CONFABLE_SCHEMA(EngineConf) {
391389
// clang-format off
392390
using namespace schema; // NOLINT(build/namespaces)
393-
auto dir_proto = []() { return make_prototype<boost::filesystem::path>().not_file(); };
394-
auto file_proto = []() { return make_prototype<boost::filesystem::path>().not_dir().resolve(false); };
391+
auto dir_proto = []() { return make_prototype<std::filesystem::path>().not_file(); };
392+
auto file_proto = []() { return make_prototype<std::filesystem::path>().not_dir().resolve(false); };
395393
return Struct{
396394
{"ignore", make_schema(&ignore_sections, "JSON pointers to sections that should be ignored").extend(true)},
397395
{"security", Struct{

engine/src/stack_factory.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
#include <string> // for string
2828
#include <vector> // for vector<>
2929

30-
#include <boost/filesystem.hpp> // for path
31-
3230
#include <cloe/utility/std_extensions.hpp> // for split_string
3331
#include <cloe/utility/xdg.hpp> // for merge_config
3432
#include <fable/environment.hpp> // for Environment
@@ -49,7 +47,16 @@ Conf read_conf(const StackOptions& opt, const std::string& filepath) {
4947
// Prepare environment with extra variables:
5048
fable::Environment env(*opt.environment);
5149
if (!filepath.empty() && filepath != "-") {
52-
std::string dirpath = boost::filesystem::canonical(filepath).parent_path().native();
50+
// We use weakly_canonical() because otherwise
51+
// we get an error when calling cloe-engine like:
52+
//
53+
// cloe-engine run <(cat file.json)
54+
std::string dirpath;
55+
if (std::filesystem::is_other(filepath)) {
56+
dirpath = std::filesystem::path(filepath).parent_path().native();
57+
} else {
58+
dirpath = std::filesystem::weakly_canonical(filepath).parent_path().native();
59+
}
5360
env.set("THIS_STACKFILE_FILE", filepath);
5461
env.set("THIS_STACKFILE_DIR", dirpath);
5562
}
@@ -106,7 +113,7 @@ Stack new_stack(const StackOptions& opt) {
106113

107114
// Interpolate known variables, if requested.
108115
if (opt.interpolate_vars) {
109-
auto interpolate_path = [&opt](std::optional<boost::filesystem::path>& p) {
116+
auto interpolate_path = [&opt](std::optional<std::filesystem::path>& p) {
110117
p = fable::interpolate_vars(p->native(), opt.environment.get());
111118
};
112119
interpolate_path(s.engine.registry_path);
@@ -150,7 +157,7 @@ Stack new_stack(const StackOptions& opt) {
150157

151158
// Merge system configurations:
152159
if (!opt.no_system_confs) {
153-
auto mergefn = [&](const boost::filesystem::path& file) -> bool {
160+
auto mergefn = [&](const std::filesystem::path& file) -> bool {
154161
s.logger()->info("Include conf {}", file.native());
155162
merge_stack(opt, s, file.native());
156163
return true;

engine/src/utility/command.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ CommandResult CommandExecuter::run_and_release(const cloe::Command& cmd) const {
4848
}
4949
try {
5050
if (!cmd.is_sync()) {
51-
r.child = bp::child(cmd.executable(), cmd.args());
51+
r.child = bp::child(cmd.executable().native(), cmd.args());
5252

5353
if (cmd.is_detach()) {
5454
r.child->detach();
@@ -59,7 +59,7 @@ CommandResult CommandExecuter::run_and_release(const cloe::Command& cmd) const {
5959

6060
// The syntax `(bp::std_out & bp::std_err) > is` is valid and works, but
6161
// was only found by rummaging through the source code. Ridiculous.
62-
r.child = bp::child(cmd.executable(), cmd.args(), (bp::std_out & bp::std_err) > is);
62+
r.child = bp::child(cmd.executable().native(), cmd.args(), (bp::std_out & bp::std_err) > is);
6363

6464
std::string line;
6565
// After finished running output the rest of the lines.

engine/src/utility/command.hpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <optional> // for optional<>
2828
#include <string> // for string
2929
#include <system_error> // for system_error
30+
#include <utility> // for move
3031
#include <vector> // for vector<>
3132

3233
#include <boost/process/child.hpp> // for child
@@ -48,12 +49,12 @@ struct CommandResult {
4849
class CommandExecuter {
4950
public:
5051
explicit CommandExecuter(cloe::Logger logger, bool enabled = true)
51-
: logger_(logger), enabled_(enabled) {}
52+
: logger_(std::move(logger)), enabled_(enabled) {}
5253

53-
bool is_enabled() const { return enabled_; }
54+
[[nodiscard]] bool is_enabled() const { return enabled_; }
5455
void set_enabled(bool v) { enabled_ = v; }
5556

56-
CommandResult run_and_release(const cloe::Command&) const;
57+
CommandResult run_and_release(const cloe::Command&) const; // NOLINT
5758

5859
void run(const cloe::Command&);
5960

@@ -65,7 +66,7 @@ class CommandExecuter {
6566

6667
std::vector<CommandResult> release_all();
6768

68-
cloe::Logger logger() const { return logger_; }
69+
[[nodiscard]] cloe::Logger logger() const { return logger_; }
6970

7071
private:
7172
std::vector<CommandResult> handles_;
@@ -77,16 +78,16 @@ namespace actions {
7778

7879
class Command : public cloe::Action {
7980
public:
80-
Command(const std::string& name, const cloe::Command& cmd, CommandExecuter* exec)
81-
: Action(name), command_(cmd), executer_(exec) {
81+
Command(const std::string& name, cloe::Command cmd, CommandExecuter* exec)
82+
: Action(name), command_(std::move(cmd)), executer_(exec) {
8283
assert(executer_ != nullptr);
8384
}
8485

8586
cloe::ActionPtr clone() const override {
8687
return std::make_unique<Command>(name(), command_, executer_);
8788
}
8889

89-
cloe::CallbackResult operator()(const cloe::Sync&, cloe::TriggerRegistrar&) override;
90+
cloe::CallbackResult operator()(const cloe::Sync& sync, cloe::TriggerRegistrar& registrar) override;
9091

9192
protected:
9293
void to_json(cloe::Json& j) const override;
@@ -103,9 +104,9 @@ class CommandFactory : public cloe::ActionFactory {
103104
: cloe::ActionFactory("command", "run a system command"), executer_(exec) {
104105
assert(executer_ != nullptr);
105106
}
106-
cloe::TriggerSchema schema() const override;
107-
cloe::ActionPtr make(const cloe::Conf& c) const override;
108-
cloe::ActionPtr make(const std::string& s) const override;
107+
[[nodiscard]] cloe::TriggerSchema schema() const override;
108+
[[nodiscard]] cloe::ActionPtr make(const cloe::Conf& c) const override;
109+
[[nodiscard]] cloe::ActionPtr make(const std::string& s) const override;
109110

110111
private:
111112
CommandExecuter* executer_{nullptr};

0 commit comments

Comments
 (0)