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 5 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
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;
36 changes: 36 additions & 0 deletions src/celengine/property.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "property.h"


namespace celestia
{
namespace engine
{

#define PROPERTY_UPDATE(ValueType) \
auto v = m_config->find(m_name); \
m_has_value = v->getType() == Value:: ValueType ## Type; \
if (m_has_value) \
m_value = v->get ## ValueType (); \

template<>
void Property<double>::update()
{
PROPERTY_UPDATE(Number);
}

template<>
void Property<std::string>::update()
{
PROPERTY_UPDATE(String);
}

template<>
void Property<bool>::update()
{
PROPERTY_UPDATE(Boolean);
}

#undef PROPERTY_UPDATE

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

#include <memory>
#include "configuration.h"


namespace celestia
{
namespace engine
{

class IProperty
{
public:
virtual void update() = 0;
friend class Config;
};

template<typename T>
class Property : IProperty
{
public:
Property() = default;

Property(std::shared_ptr<Config> config, std::string name, T def) :
m_config(std::move(config)),
m_name(std::move(name)),
m_default(std::move(def))
{
m_config->addProperty(this);
};

~Property()
{
m_config->removeProperty(this);
};

// Getters and setters
inline Property& set(T value)
{
m_value = value;
};

Property& operator() (T value)
{
return set(value);
};

Property& operator=(T value)
{
return set(value);
};

T get() const
{
return m_has_value ? m_value : m_default;
};

T operator()() const
{
return get();
};

// Used by Config to propagate changes
void update() override;

private:
std::shared_ptr<Config> m_config { nullptr };
std::string m_name;
T m_value {};
T m_default {};
bool m_has_value { false };
};

using NumericProperty = Property<double>;
using StringProperty = Property<std::string>;
using BoolProperty = Property<bool>;

}
} // namespace;
81 changes: 81 additions & 0 deletions src/celengine/testprops.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "property.h"
#include <iostream>
#include <memory>

using namespace celestia::engine;
using namespace std;

class myconfig : IConfigUpdater
{
public:
myconfig(const std::shared_ptr<Config> &cfg) : IConfigUpdater(cfg) {}
void read();
};

void myconfig::read()
{
beginUpdate();
auto *v1 = new Value(10.18);
set("Distance", v1);
auto *v2 = new Value(std::string("foobar"));
set("Name", v2);
auto *v3 = new Value(true);
set("Visible", v3);
endUpdate();
}

class Foo
{
Property<double> m_p1;
NumericProperty m_distance;
StringProperty m_name, m_type;
BoolProperty m_visible;
shared_ptr<Config> m_cfg;

public:
Foo(const shared_ptr<Config> &cfg):
m_cfg (cfg),
m_p1 (Property<double>(cfg, "P1", 10.5)),
m_distance (NumericProperty(cfg, "distance", 55.1)),
m_name (StringProperty(cfg, "name", "baz")),
m_type (StringProperty(cfg, "type", "baz")),
m_visible (BoolProperty(cfg, "visible", false))
{
}
double p1()
{
return m_p1.get();
}
double distance()
{
return m_distance();
}
string name()
{
return m_name();
}
string type()
{
return m_type();
}
bool visible()
{
return m_visible();
}
};

int main()
{
auto c = make_shared<Config>();
myconfig my(c);
my.read();
c->dump();
Foo f(c);
cout << f.p1() << ' ' << f.distance() << ' ' << f.name() << ' ' << f.type() << ' ' << f.visible() << '\n';
auto *v = c->find("Name");
if (v->getType() == Value::StringType)
cout << v->getString() << '\n';
else
cout << "dunno\n";
c->dump();
}
Loading