Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Dynamic configuration framework #385

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/celengine/astro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <utility>
#include <ctime>
#include <celmath/mathlib.h>
#include <config.h>
#include "astro.h"
#include "univcoord.h"
#include <celutil/util.h>
Expand Down
97 changes: 97 additions & 0 deletions src/celengine/configuration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "configuration.h"
#include "property.h"
#include <iostream>
#include <algorithm>


namespace celestia
{
namespace engine
{

#ifdef DEBUG
void Config::dump() const
{
for (const auto &m : m_values)
{
std::cout << m.first << ' ';
auto d = m.second;
std::cout << d->getType() << ' ';
switch (d->getType())
{
case Value::NullType:
std::cout << "null";
break;
case Value::NumberType:
std::cout << d->getNumber();
break;
case Value::StringType:
std::cout << d->getString();
break;
case Value::BooleanType:
std::cout << d->getBoolean();
break;
default:
std::cout << "not supported yet";
break;
}
std::cout << '\n';
}
}
#endif

void Config::addProperty(IProperty *p)
{
if (std::find(m_props.begin(), m_props.end(), p) == m_props.end())
m_props.push_back(p);
p->update();
}

void Config::removeProperty(IProperty *p)
{
auto pos = std::find(m_props.begin(), m_props.end(), p);
if (pos != m_props.end())
m_props.erase(pos);
}

const Value* Config::find(const std::string& name) const
{
static Value v;

auto it = m_values.find(name);
if (it != m_values.end())
return it->second;

return &v;
}

void Config::beginUpdate()
{
m_update = true;
}

void Config::set(const std::string& name, const Value* value)
{
m_values[name] = value;
}

void Config::endUpdate()
{
m_update = false;
onUpdate();
}

void Config::onUpdate()
{
for (auto *p : m_props)
p->update();
}

Config::~Config()
{
for (const auto &p : m_values)
delete p.second;
}

}
} // namespace;
78 changes: 78 additions & 0 deletions src/celengine/configuration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#pragma once

#include <string>
#include <memory>
#include <vector>
#include <map>
#include "value.h"
#include <celutil/util.h>


namespace celestia
{
namespace engine
{
class IConfigUpdater;
class IConfigWriter;
class IProperty;

class Config
{
public:
Config() = default;
Config(const Config&) = delete;
Config(Config&&) = delete;
~Config();

Config& operator=(const Config&) = delete;
Config& operator=(Config&&) = delete;

void addProperty(IProperty*);
void removeProperty(IProperty*);
const Value* find(const std::string& name) const;

#ifdef DEBUG
void dump() const;
#else
void dump() const {};
#endif

private:
struct Cmp
{
bool operator()(const std::string &a, const std::string &b) const
{
return compareIgnoringCase(a, b) < 0;
}
};

void onUpdate();
void beginUpdate();
void set(const std::string& name, const Value *value);
void endUpdate();

std::vector<IProperty*> m_props;
std::map<std::string, const Value*, Cmp> m_values;

bool m_update { false };

friend class IConfigUpdater;
friend class IConfigWriter;
friend class IProperty;
};


// Proxy class to invoke private Config's methods from derived classes
class IConfigUpdater
{
std::shared_ptr<Config> m_cfg;
public:
IConfigUpdater(const std::shared_ptr<Config> &cfg) : m_cfg(cfg) {}
inline void beginUpdate() { m_cfg->beginUpdate(); }
inline void set(const std::string &name, Value *value) { m_cfg->set(name, value); }
inline void endUpdate() { m_cfg->endUpdate(); }
};


}
} // namespace;
Loading