dingusppc/machines/machineproperties.h

124 lines
2.8 KiB
C
Raw Normal View History

2020-08-22 18:05:08 +00:00
#include "endianswap.h"
2020-09-20 21:25:29 +00:00
#include <thirdparty/loguru/loguru.hpp>
2020-08-22 18:05:08 +00:00
#include <cinttypes>
2020-08-26 03:07:02 +00:00
#include <string>
2020-08-22 18:05:08 +00:00
#include <map>
#include <memory>
2020-08-22 18:05:08 +00:00
#include <iostream>
#include <utility>
2020-08-26 03:07:02 +00:00
#ifndef MACHINE_PROPERTIES_H
#define MACHINE_PROPERTIES_H
2020-08-22 18:05:08 +00:00
using namespace std;
2020-08-26 03:07:02 +00:00
#define ILLEGAL_DEVICE_VALUE 0x168A523B
/** Property types. */
enum PropType : int {
PROP_TYPE_UNKNOWN = 0,
PROP_TYPE_STRING = 1,
PROP_TYPE_INTEGER = 2,
};
class BasicProperty {
2020-08-22 18:05:08 +00:00
public:
BasicProperty(PropType type, string val) {
this->type = type;
set_string(val);
2020-08-22 18:05:08 +00:00
}
virtual ~BasicProperty() = default;
virtual BasicProperty* clone() const = 0;
2020-09-20 21:25:29 +00:00
string get_string() {
return this->val;
2020-09-20 21:25:29 +00:00
}
void set_string(string str) {
this->val = str;
2020-08-22 18:05:08 +00:00
}
PropType get_type() {
return this->type;
}
protected:
PropType type;
string val;
};
class StrProperty : public BasicProperty {
public:
StrProperty(string str)
: BasicProperty(PROP_TYPE_STRING, str) {}
BasicProperty* clone() const { return new StrProperty(*this); }
2020-08-22 18:05:08 +00:00
uint32_t IntRep() {
try {
2020-09-20 21:25:29 +00:00
return strtoul(get_string().c_str(), 0, 0);
2020-08-22 18:05:08 +00:00
} catch (string bad_string) {
cerr << "Could not convert string " << bad_string << "to an ineteger!" << endl;
2020-08-26 03:07:02 +00:00
return ILLEGAL_DEVICE_VALUE;
2020-08-22 18:05:08 +00:00
}
}
2020-09-20 21:25:29 +00:00
};
class IntProperty : public BasicProperty {
2020-09-20 21:25:29 +00:00
public:
IntProperty(string str)
: BasicProperty(PROP_TYPE_INTEGER, str)
{
this->int_val = 0;
this->min = std::numeric_limits<uint32_t>::min();
this->max = std::numeric_limits<uint32_t>::max();
2020-09-20 21:25:29 +00:00
}
IntProperty(string str, uint32_t min, uint32_t max)
: BasicProperty(PROP_TYPE_INTEGER, str)
{
this->int_val = 0;
this->min = min;
this->max = max;
this->int_val = this->get_int();
2020-09-20 21:25:29 +00:00
}
BasicProperty* clone() const { return new IntProperty(*this); }
2020-09-20 21:25:29 +00:00
uint32_t get_int() {
try {
uint32_t result = strtoul(this->get_string().c_str(), 0, 0);
/* perform range check */
if (result < this->min || result > this->max) {
LOG_F(ERROR, "Value %d out of range!", result);
} else {
this->int_val = result;
}
2020-09-20 21:25:29 +00:00
} catch (string bad_string) {
LOG_F(ERROR, "Could not convert string %s to an integer!",
bad_string.c_str());
}
return this->int_val;
2020-09-20 21:25:29 +00:00
}
private:
uint32_t int_val;
uint32_t min;
uint32_t max;
2020-08-22 18:05:08 +00:00
};
typedef map<string, BasicProperty*> PropMap;
extern map<string, unique_ptr<BasicProperty>> gMachineSettings;
#define GET_INT_PROP(name) \
dynamic_cast<IntProperty*>(gMachineSettings.at(name).get())->get_int()
2020-09-20 21:25:29 +00:00
#endif /* MACHINE_PROPERTIES_H */