Skip to content

Commit

Permalink
extend ConfigContainerVolatile with addConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
matth-x committed Oct 8, 2023
1 parent cce3d81 commit edd9ff6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 40 deletions.
85 changes: 46 additions & 39 deletions src/MicroOcpp/Core/ConfigurationContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,70 @@

#include <MicroOcpp/Debug.h>

namespace MicroOcpp {
using namespace MicroOcpp;

ConfigurationContainer::~ConfigurationContainer() {

}

class ConfigurationContainerVolatile : public ConfigurationContainer {
private:
std::vector<std::shared_ptr<Configuration>> configurations;
public:
ConfigurationContainerVolatile(const char *filename, bool accessible) : ConfigurationContainer(filename, accessible) { }
ConfigurationContainerVolatile::ConfigurationContainerVolatile(const char *filename, bool accessible) : ConfigurationContainer(filename, accessible) {

bool load() override {return true;}
}

bool save() override {return true;}
bool ConfigurationContainerVolatile::load() {
return true;
}

std::shared_ptr<Configuration> createConfiguration(TConfig type, const char *key) override {
std::shared_ptr<Configuration> res = makeConfiguration(type, key);
if (!res) {
//allocation failure - OOM
MOCPP_DBG_ERR("OOM");
return nullptr;
}
configurations.push_back(res);
return res;
bool ConfigurationContainerVolatile::save() {
return true;
}

std::shared_ptr<Configuration> ConfigurationContainerVolatile::createConfiguration(TConfig type, const char *key) {
std::shared_ptr<Configuration> res = makeConfiguration(type, key);
if (!res) {
//allocation failure - OOM
MOCPP_DBG_ERR("OOM");
return nullptr;
}
configurations.push_back(res);
return res;
}

void removeConfiguration(Configuration *config) override {
for (auto entry = configurations.begin(); entry != configurations.end();) {
if (entry->get() == config) {
entry = configurations.erase(entry);
} else {
entry++;
}
void ConfigurationContainerVolatile::removeConfiguration(Configuration *config) {
for (auto entry = configurations.begin(); entry != configurations.end();) {
if (entry->get() == config) {
entry = configurations.erase(entry);
} else {
entry++;
}
}
}

size_t getConfigurationCount() override {
return configurations.size();
}
size_t ConfigurationContainerVolatile::getConfigurationCount() {
return configurations.size();
}

Configuration *getConfiguration(size_t i) override {
return configurations[i].get();
}
Configuration *ConfigurationContainerVolatile::getConfiguration(size_t i) {
return configurations[i].get();
}

std::shared_ptr<Configuration> getConfiguration(const char *key) override {
for (auto& entry : configurations) {
if (entry->getKey() && !strcmp(entry->getKey(), key)) {
return entry;
}
std::shared_ptr<Configuration> ConfigurationContainerVolatile::getConfiguration(const char *key) {
for (auto& entry : configurations) {
if (entry->getKey() && !strcmp(entry->getKey(), key)) {
return entry;
}
return nullptr;
}
};
return nullptr;
}

void ConfigurationContainerVolatile::addConfiguration(std::shared_ptr<Configuration> c) {
configurations.push_back(std::move(c));
}

namespace MicroOcpp {

std::unique_ptr<ConfigurationContainer> makeConfigurationContainerVolatile(const char *filename, bool accessible) {
return std::unique_ptr<ConfigurationContainer>(new ConfigurationContainerVolatile(filename, accessible));
std::unique_ptr<ConfigurationContainerVolatile> makeConfigurationContainerVolatile(const char *filename, bool accessible) {
return std::unique_ptr<ConfigurationContainerVolatile>(new ConfigurationContainerVolatile(filename, accessible));
}

} //end namespace MicroOcpp
21 changes: 20 additions & 1 deletion src/MicroOcpp/Core/ConfigurationContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,26 @@ class ConfigurationContainer {
virtual void loadStaticKey(Configuration& config, const char *key) { } //possible optimization: can replace internal key with passed static key
};

std::unique_ptr<ConfigurationContainer> makeConfigurationContainerVolatile(const char *filename, bool accessible);
class ConfigurationContainerVolatile : public ConfigurationContainer {
private:
std::vector<std::shared_ptr<Configuration>> configurations;
public:
ConfigurationContainerVolatile(const char *filename, bool accessible);

//ConfigurationContainer definitions
bool load() override;
bool save() override;
std::shared_ptr<Configuration> createConfiguration(TConfig type, const char *key) override;
void removeConfiguration(Configuration *config) override;
size_t getConfigurationCount() override;
Configuration *getConfiguration(size_t i) override;
std::shared_ptr<Configuration> getConfiguration(const char *key) override;

//add custom Configuration object
void addConfiguration(std::shared_ptr<Configuration> c);
};

std::unique_ptr<ConfigurationContainerVolatile> makeConfigurationContainerVolatile(const char *filename, bool accessible);

} //end namespace MicroOcpp

Expand Down

0 comments on commit edd9ff6

Please sign in to comment.