dingusppc/machines/machineproperties.h

209 lines
5.5 KiB
C
Raw Normal View History

/*
DingusPPC - The Experimental PowerPC Macintosh emulator
2022-07-19 21:45:41 +00:00
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-09-15 22:46:38 +00:00
#include <loguru.hpp>
2020-08-22 18:05:08 +00:00
#include <cinttypes>
2022-02-16 23:50:37 +00:00
#include <limits>
2020-08-22 18:05:08 +00:00
#include <map>
#include <memory>
2022-02-16 23:50:37 +00:00
#include <string>
2020-10-09 13:58:47 +00:00
#include <vector>
2020-08-22 18:05:08 +00:00
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;
/** Property types. */
enum PropType : int {
PROP_TYPE_UNKNOWN = 0,
PROP_TYPE_STRING = 1,
PROP_TYPE_INTEGER = 2,
2022-07-19 21:45:41 +00:00
PROP_TYPE_BINARY = 3, // binary aka on/off switch
};
2020-10-09 13:58:47 +00:00
/** Check types for property values. */
enum CheckType : int {
CHECK_TYPE_NONE = 0,
CHECK_TYPE_RANGE = 1,
CHECK_TYPE_LIST = 2,
2020-10-09 13:58:47 +00:00
};
/** Abstract base class for properties. */
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;
2020-10-09 13:58:47 +00:00
/* Clone method for copying derived property objects. */
virtual BasicProperty* clone() const = 0;
virtual string get_string() {
return this->val;
2020-09-20 21:25:29 +00:00
}
virtual void set_string(string str) {
this->val = str;
2020-08-22 18:05:08 +00:00
}
virtual PropType get_type() {
return this->type;
}
protected:
PropType type;
string val;
};
2020-10-09 13:58:47 +00:00
/** Property class that holds a string value. */
class StrProperty : public BasicProperty {
public:
StrProperty(string str)
: BasicProperty(PROP_TYPE_STRING, str)
{
this->check_type = CHECK_TYPE_NONE;
this->vec.clear();
}
/* construct a string property with a list of valid values. */
StrProperty(string str, vector<string> vec)
: BasicProperty(PROP_TYPE_STRING, str)
{
this->check_type = CHECK_TYPE_LIST;
this->vec = vec;
}
BasicProperty* clone() const { return new StrProperty(*this); }
/* override BasicProperty::set_string() and perform checks */
void set_string(string str);
string get_valid_values_as_str();
protected:
bool check_val(string str);
private:
CheckType check_type;
vector<string> vec;
2020-09-20 21:25:29 +00:00
};
2020-10-09 13:58:47 +00:00
/** Property class that holds an integer value. */
class IntProperty : public BasicProperty {
2020-09-20 21:25:29 +00:00
public:
2020-10-09 13:58:47 +00:00
/* construct an integer property without value check. */
IntProperty(uint32_t val)
: BasicProperty(PROP_TYPE_INTEGER, to_string(val))
{
2020-10-09 13:58:47 +00:00
this->int_val = val;
this->min = std::numeric_limits<uint32_t>::min();
this->max = std::numeric_limits<uint32_t>::max();
this->check_type = CHECK_TYPE_NONE;
this->vec.clear();
2020-09-20 21:25:29 +00:00
}
2020-10-09 13:58:47 +00:00
/* construct an integer property with a predefined range. */
IntProperty(uint32_t val, uint32_t min, uint32_t max)
: BasicProperty(PROP_TYPE_INTEGER, to_string(val))
{
2020-10-09 13:58:47 +00:00
this->int_val = val;
this->min = min;
this->max = max;
this->check_type = CHECK_TYPE_RANGE;
this->vec.clear();
}
2020-10-09 13:58:47 +00:00
/* construct an integer property with a list of valid values. */
IntProperty(uint32_t val, vector<uint32_t> vec)
: BasicProperty(PROP_TYPE_INTEGER, to_string(val))
{
this->int_val = val;
this->min = std::numeric_limits<uint32_t>::min();
this->max = std::numeric_limits<uint32_t>::max();
this->check_type = CHECK_TYPE_LIST;
2020-10-09 13:58:47 +00:00
this->vec = vec;
2020-09-20 21:25:29 +00:00
}
BasicProperty* clone() const { return new IntProperty(*this); }
uint32_t get_int();
2020-09-20 21:25:29 +00:00
string get_valid_values_as_str();
2020-10-09 13:58:47 +00:00
protected:
bool check_val(uint32_t val);
2020-10-09 13:58:47 +00:00
2020-09-20 21:25:29 +00:00
private:
2020-10-09 13:58:47 +00:00
uint32_t int_val;
CheckType check_type;
uint32_t min;
uint32_t max;
vector<uint32_t> vec;
2020-08-22 18:05:08 +00:00
};
2022-07-19 21:45:41 +00:00
/** Property class that holds a binary value. */
class BinProperty : public BasicProperty {
public:
BinProperty(int val)
: BasicProperty(PROP_TYPE_BINARY, (val & 1) ? "on" : "off")
{
this->bin_val = val & 1;
};
BasicProperty* clone() const { return new BinProperty(*this); }
// override BasicProperty::set_string() and perform checks
void set_string(string str);
string get_valid_values_as_str() {
return string("on, off, ON, OFF");
};
int get_val() { return this->bin_val; };
private:
int bin_val;
};
2020-10-09 13:58:47 +00:00
/** Special map type for specifying machine presets. */
typedef map<string, BasicProperty*> PropMap;
2020-10-09 13:58:47 +00:00
/** Global map that holds settings for the running machine. */
extern map<string, unique_ptr<BasicProperty>> gMachineSettings;
2020-10-09 13:58:47 +00:00
/** Conveniency macros to hide complex casts. */
#define GET_STR_PROP(name) \
dynamic_cast<StrProperty*>(gMachineSettings.at(name).get())->get_string()
#define GET_INT_PROP(name) \
dynamic_cast<IntProperty*>(gMachineSettings.at(name).get())->get_int()
2022-07-19 21:45:41 +00:00
#define GET_BIN_PROP(name) \
dynamic_cast<BinProperty*>(gMachineSettings.at(name).get())->get_val()
2020-09-20 21:25:29 +00:00
#endif /* MACHINE_PROPERTIES_H */