Skip to content

Commit

Permalink
Allow ctor to init
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisTM committed Jun 6, 2024
1 parent dd352cd commit a3b479c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 45 deletions.
16 changes: 3 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ ctest
### Basic usage

```cpp
cracon::File config;
bool success = config.init("config.json", "defaults.json");
if (!success) {
// The file couldn't be opened/read/written/created
}
auto config = cracon::File("config.json", "defaults.json");
// Or config.init("config.json", "defaults.json")

int int_value = config.get("/int", 1);
auto some_string = config.get<std::string>("/oh/hi", "mark");
Expand Down Expand Up @@ -98,16 +95,9 @@ class Car {
};

int main() {
cracon::SharedFile config;
bool success = config.init("config.json", "defaults.json");
if (!success) {
fprintf(stderr, "We failed to open, read or write the configuration files.");
exit(EXIT_FAILURE);
}

auto config = cracon::SharedFile("config.json", "defaults.json");
Car car = Car(config.get_group("car"));
car.speed.set(1000);

config.write();
}
```
Expand Down
9 changes: 2 additions & 7 deletions examples/basic_usage.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
#include <cracon/cracon.hpp>

int main() {
cracon::File config;
bool success = config.init("config.json", "defaults.json");
if (!success) {
fprintf(stderr, "We failed to open, read or write the configuration files.");
exit(EXIT_FAILURE);
}
cracon::File config = cracon::File("config.json", "defaults.json");

int int_value = config.get("/int", 1);
auto some_string = config.get<std::string>("/oh/hi", "mark");
auto a_vector = config.get<std::vector<float>>("/vector", {1., 2., 3.});

int_value = config.set("/int", 42);
if(config.should_write()) { // Only write it if there is a change
if (config.should_write()) { // Only write it if there is a change
config.write();
}
}
7 changes: 1 addition & 6 deletions examples/group_param_usage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ class Car {

int main() {
// The SharedFile allows to use the Group and Param features
cracon::SharedFile config;
bool success = config.init("config.json", "defaults.json");
if (!success) {
fprintf(stderr, "We failed to open, read or write the configuration files.");
exit(EXIT_FAILURE);
}
cracon::SharedFile config = cracon::SharedFile("config.json", "defaults.json");
Car car = Car(config.get_group("car"));
car.speed.set(1000);

Expand Down
21 changes: 13 additions & 8 deletions include/cracon/cracon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
namespace cracon {
class File {
public:
File() {}
File(std::string const &filename_config, std::string const &filename_default);
/**
* @brief Sets the configuration filenames and parses them if it exists.
*
* Create the files otherwise. Returns false if it failed to be created/read.
*
* @param filename_filename The json file to to read or create
* @param default_filename The default configuration file to create
* @param filename_config The json file to to read or create
* @param filename_default The default configuration file to create
* @return true File loaded or created
* @return false File couldn't be read or created
*/
bool init(std::string const &filename_filename,
std::string const &default_filename);
bool init(std::string const &filename_config,
std::string const &filename_default);
/**
* @brief True if we the local representation differs from the files.
*/
Expand Down Expand Up @@ -136,14 +138,17 @@ class File {
* classes.
*
*/
class SharedFile : public File {
class SharedFile {
public:
SharedFile(){};
SharedFile(std::string const &filename_config,
std::string const &filename_default);
/**
* @brief Sets the configuration filenames and parses them if it exists. See
* `File::init`
*/
bool init(std::string const &filename_filename,
std::string const &default_filename);
bool init(std::string const &filename_config,
std::string const &filename_default);

/**
* Live parameter directly writing to/from the file.
Expand Down Expand Up @@ -222,7 +227,7 @@ class SharedFile : public File {
*/
void update() {
assert(config_ != nullptr);
(void) config_->set<Type>(accessor_, data_);
(void)config_->set<Type>(accessor_, data_);
}

/**
Expand Down
28 changes: 17 additions & 11 deletions src/cracon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include "nlohmann/json.hpp"

namespace cracon {

File::File(std::string const &filename_config,
std::string const &filename_default) {
init(filename_config, filename_default);
}

bool File::write() {
std::unique_lock lock(mutex_);
if (should_write_config_) {
Expand Down Expand Up @@ -48,23 +54,23 @@ bool File::init(std::string const &filename_config,
std::unique_lock lock(mutex_);
filename_config_ = filename_config;
filename_default_ = filename_default;
try {
config_ = nlohmann::json::object();
std::ifstream file(filename_config);
if (file.good()) {
config_ = nlohmann::json::parse(file);
if (config_.is_null()) {
config_ = nlohmann::json::object();
}
config_ = nlohmann::json::object();
std::ifstream file(filename_config);
if (file.good()) {
config_ = nlohmann::json::parse(file);
if (config_.is_null()) {
config_ = nlohmann::json::object();
}
} catch (std::exception const &ex) {
fprintf(stderr, "[cracon] [ERROR] %s\n", ex.what());
return false;
}
}
return write();
}

SharedFile::SharedFile(std::string const &filename_config,
std::string const &filename_default) {
init(filename_config, filename_default);
}

bool SharedFile::init(std::string const &filename_config,
std::string const &filename_default) {
return file_->init(filename_config, filename_default);
Expand Down

0 comments on commit a3b479c

Please sign in to comment.