Initial Commit
This commit is contained in:
63
options.h
Normal file
63
options.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#ifndef OPTIONS_H
|
||||
#define OPTIONS_H
|
||||
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <ostream>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace OptionLib {
|
||||
class __OptionBase {
|
||||
public:
|
||||
const std::string key;
|
||||
|
||||
__OptionBase(const std::string &key);
|
||||
virtual ~__OptionBase() = default;
|
||||
|
||||
virtual void setFromString(const std::string& value) = 0;
|
||||
|
||||
virtual void store(std::ostream& stream) = 0;
|
||||
};
|
||||
|
||||
|
||||
class OptionsBase {
|
||||
private:
|
||||
std::vector<__OptionBase*> options;
|
||||
std::string comment = "";
|
||||
|
||||
public:
|
||||
void registerOption(__OptionBase* opt);
|
||||
const std::vector<__OptionBase*>& getOptions() const;
|
||||
void store(const std::string& filename, std::string comment="");
|
||||
void read(const std::string& filename);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class Option : public __OptionBase{
|
||||
private:
|
||||
T value;
|
||||
public:
|
||||
Option(OptionsBase * parent, std::string name) : __OptionBase(name) {
|
||||
parent->registerOption(this);
|
||||
}
|
||||
Option(OptionsBase * parent, std::string name, T defaultValue) : __OptionBase(name) {
|
||||
parent->registerOption(this);
|
||||
set(defaultValue);
|
||||
}
|
||||
|
||||
const T& get() const {return value;}
|
||||
void set(const T& v) {value = v;}
|
||||
|
||||
void setFromString(const std::string& str) override {
|
||||
std::istringstream iss(str);
|
||||
iss >> value;
|
||||
}
|
||||
|
||||
void store(std::ostream& stream) {
|
||||
stream << value;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user