2023-12-28 20:38:01 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifndef HYPRLANG_HPP
|
|
|
|
#define HYPRLANG_HPP
|
|
|
|
|
2023-12-29 18:01:04 +01:00
|
|
|
#include <any>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
2023-12-28 20:38:01 +01:00
|
|
|
|
2023-12-29 18:01:04 +01:00
|
|
|
class CConfigImpl;
|
2023-12-29 19:35:23 +01:00
|
|
|
struct SConfigDefaultValue;
|
|
|
|
struct SSpecialCategory;
|
2023-12-28 20:38:01 +01:00
|
|
|
|
2023-12-29 18:01:04 +01:00
|
|
|
namespace Hyprlang {
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
A very simple vector type
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
struct SVector2D {
|
|
|
|
float x = 0, y = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
bool operator==(const SVector2D& rhs) const {
|
|
|
|
return x == rhs.x && y == rhs.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& os, const SVector2D& rhs) {
|
|
|
|
return os << "[" << rhs.x << ", " << rhs.y << "]";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CParseResult {
|
|
|
|
public:
|
|
|
|
bool error = false;
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Get this ParseResult's error string.
|
|
|
|
Pointer valid until the error string is changed or this
|
|
|
|
object gets destroyed.
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
const char* getError() const {
|
|
|
|
return errorString;
|
|
|
|
}
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Set an error contained by this ParseResult.
|
|
|
|
Creates a copy of the string, does not take ownership.
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
void setError(const char* err);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setError(const std::string& err);
|
|
|
|
|
|
|
|
std::string errorStdString = "";
|
|
|
|
const char* errorString = nullptr;
|
|
|
|
|
|
|
|
friend class CConfig;
|
|
|
|
};
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Generic struct for options for the config parser
|
|
|
|
*/
|
2023-12-30 13:30:46 +01:00
|
|
|
struct SConfigOptions {
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Don't throw errors on missing values.
|
|
|
|
*/
|
2023-12-30 13:30:46 +01:00
|
|
|
bool verifyOnly = false;
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Return all errors instead of just the first
|
|
|
|
*/
|
2023-12-30 13:30:46 +01:00
|
|
|
bool throwAllErrors = false;
|
2023-12-31 01:59:37 +01:00
|
|
|
|
|
|
|
/*!
|
2023-12-31 14:15:03 +01:00
|
|
|
\since 0.2.0
|
2023-12-31 01:59:37 +01:00
|
|
|
Don't throw on a missing config file. Carry on as if nothing happened.
|
|
|
|
*/
|
|
|
|
bool allowMissingConfig = false;
|
2023-12-30 13:30:46 +01:00
|
|
|
};
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Generic struct for options for handlers
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
struct SHandlerOptions {
|
|
|
|
bool allowFlags = false;
|
|
|
|
};
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Generic struct for options for special categories
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
struct SSpecialCategoryOptions {
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
a key is the name of a value that will be the identifier of a special category
|
|
|
|
can be left null for no key, aka a generic one
|
|
|
|
keys are always strings. Default key value is "0"
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
const char* key = nullptr;
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
don't pop up an error if the config value is missing
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
bool ignoreMissing = false;
|
|
|
|
};
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
typedefs
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
typedef CParseResult (*PCONFIGHANDLERFUNC)(const char* COMMAND, const char* VALUE);
|
2023-12-29 19:35:23 +01:00
|
|
|
typedef CParseResult (*PCONFIGCUSTOMVALUEHANDLERFUNC)(const char* VALUE, void** data);
|
|
|
|
typedef void (*PCONFIGCUSTOMVALUEDESTRUCTOR)(void** data);
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Container for a custom config value type
|
|
|
|
When creating, pass your handler.
|
|
|
|
Handler will receive a void** that points to a void* that you can set to your own
|
|
|
|
thing. Pass a dtor to free whatever you allocated when the custom value type is being released.
|
|
|
|
data may always be pointing to a nullptr.
|
2023-12-29 19:35:23 +01:00
|
|
|
*/
|
|
|
|
class CConfigCustomValueType {
|
|
|
|
public:
|
|
|
|
CConfigCustomValueType(PCONFIGCUSTOMVALUEHANDLERFUNC handler_, PCONFIGCUSTOMVALUEDESTRUCTOR dtor_, const char* defaultValue);
|
|
|
|
~CConfigCustomValueType();
|
|
|
|
|
|
|
|
private:
|
|
|
|
PCONFIGCUSTOMVALUEHANDLERFUNC handler = nullptr;
|
|
|
|
PCONFIGCUSTOMVALUEDESTRUCTOR dtor = nullptr;
|
|
|
|
void* data = nullptr;
|
|
|
|
std::string defaultVal = "";
|
|
|
|
|
|
|
|
friend class CConfigValue;
|
|
|
|
friend class CConfig;
|
|
|
|
};
|
2023-12-29 18:01:04 +01:00
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Container for a config value
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
class CConfigValue {
|
|
|
|
public:
|
|
|
|
CConfigValue();
|
|
|
|
CConfigValue(const int64_t value);
|
|
|
|
CConfigValue(const float value);
|
|
|
|
CConfigValue(const char* value);
|
|
|
|
CConfigValue(const SVector2D value);
|
2023-12-29 19:35:23 +01:00
|
|
|
CConfigValue(CConfigCustomValueType&& value);
|
|
|
|
CConfigValue(const CConfigValue&) = delete;
|
|
|
|
CConfigValue(CConfigValue&&) = delete;
|
|
|
|
CConfigValue(const CConfigValue&&) = delete;
|
|
|
|
CConfigValue(CConfigValue&) = delete;
|
2023-12-29 18:01:04 +01:00
|
|
|
~CConfigValue();
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
2023-12-31 14:15:03 +01:00
|
|
|
Return a pointer to the data. Prefer getDataStaticPtr()
|
2023-12-30 14:31:48 +01:00
|
|
|
*/
|
|
|
|
void* dataPtr() const;
|
|
|
|
|
2023-12-31 14:15:03 +01:00
|
|
|
/*!
|
|
|
|
\since 0.2.0
|
|
|
|
Return a static pointer to the m_pData.
|
|
|
|
As long as this configValue is alive, this pointer is valid.
|
|
|
|
CConfigValues are alive as long as the owning CConfig is alive.
|
|
|
|
*/
|
|
|
|
void* const* getDataStaticPtr() const;
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Get the contained value as an std::any.
|
|
|
|
For strings, this is a const char*.
|
|
|
|
For custom data types, this is a CConfigCustomValueType*.
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
std::any getValue() const {
|
|
|
|
switch (m_eType) {
|
|
|
|
case CONFIGDATATYPE_EMPTY: throw;
|
|
|
|
case CONFIGDATATYPE_INT: return std::any(*reinterpret_cast<int64_t*>(m_pData));
|
|
|
|
case CONFIGDATATYPE_FLOAT: return std::any(*reinterpret_cast<float*>(m_pData));
|
|
|
|
case CONFIGDATATYPE_STR: return std::any(reinterpret_cast<const char*>(m_pData));
|
|
|
|
case CONFIGDATATYPE_VEC2: return std::any(*reinterpret_cast<SVector2D*>(m_pData));
|
2023-12-29 19:35:23 +01:00
|
|
|
case CONFIGDATATYPE_CUSTOM: return std::any(reinterpret_cast<CConfigCustomValueType*>(m_pData)->data);
|
2023-12-29 18:01:04 +01:00
|
|
|
default: throw;
|
|
|
|
}
|
|
|
|
return {}; // unreachable
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-12-29 19:35:23 +01:00
|
|
|
// remember to also edit config.hpp if editing
|
2023-12-29 18:01:04 +01:00
|
|
|
enum eDataType {
|
|
|
|
CONFIGDATATYPE_EMPTY,
|
|
|
|
CONFIGDATATYPE_INT,
|
|
|
|
CONFIGDATATYPE_FLOAT,
|
|
|
|
CONFIGDATATYPE_STR,
|
|
|
|
CONFIGDATATYPE_VEC2,
|
2023-12-29 19:35:23 +01:00
|
|
|
CONFIGDATATYPE_CUSTOM,
|
2023-12-29 18:01:04 +01:00
|
|
|
};
|
|
|
|
eDataType m_eType = eDataType::CONFIGDATATYPE_EMPTY;
|
|
|
|
void* m_pData = nullptr;
|
2023-12-29 19:35:23 +01:00
|
|
|
void defaultFrom(SConfigDefaultValue& ref);
|
|
|
|
void setFrom(std::any value);
|
2023-12-29 18:01:04 +01:00
|
|
|
|
|
|
|
friend class CConfig;
|
|
|
|
};
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Base class for a config file
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
class CConfig {
|
|
|
|
public:
|
2023-12-30 13:30:46 +01:00
|
|
|
CConfig(const char* configPath, const SConfigOptions& options);
|
2023-12-29 18:01:04 +01:00
|
|
|
~CConfig();
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Add a config value, for example myCategory:myValue.
|
|
|
|
This has to be done before commence()
|
|
|
|
Value provided becomes default.
|
|
|
|
*/
|
2023-12-29 19:35:23 +01:00
|
|
|
void addConfigValue(const char* name, const CConfigValue& value);
|
2023-12-29 18:01:04 +01:00
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Register a handler. Can be called anytime, though not recommended
|
|
|
|
to do this dynamically .
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
void registerHandler(PCONFIGHANDLERFUNC func, const char* name, SHandlerOptions options);
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Commence the config state. Config becomes immutable, as in
|
|
|
|
no new values may be added or removed. Required for parsing.
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
void commence();
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Add a special category. Can be done dynamically.
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
void addSpecialCategory(const char* name, SSpecialCategoryOptions options);
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Add a config value to a special category.
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
void addSpecialConfigValue(const char* cat, const char* name, const CConfigValue value);
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Parse the config. Refresh the values.
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
CParseResult parse();
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Same as parse(), but parse a specific file, without any refreshing.
|
|
|
|
recommended to use for stuff like source = path.conf
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
CParseResult parseFile(std::string file);
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Parse a single "line", dynamically.
|
|
|
|
Values set by this are temporary and will be overwritten
|
|
|
|
by default / config on the next parse()
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
CParseResult parseDynamic(const char* line);
|
|
|
|
CParseResult parseDynamic(const char* command, const char* value);
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Get a config's value ptr. These are static.
|
|
|
|
nullptr on fail
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
CConfigValue* getConfigValuePtr(const char* name);
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Get a special category's config value ptr. These are only static for static (key-less)
|
2023-12-29 18:01:04 +01:00
|
|
|
categories, unless a new variable is added via addSpecialConfigValue.
|
|
|
|
key can be nullptr for static categories. Cannot be nullptr for id-based categories.
|
2023-12-30 14:31:48 +01:00
|
|
|
nullptr on fail.
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
CConfigValue* getSpecialConfigValuePtr(const char* category, const char* name, const char* key = nullptr);
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Get a config value's stored value. Empty on fail
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
std::any getConfigValue(const char* name) {
|
|
|
|
CConfigValue* val = getConfigValuePtr(name);
|
|
|
|
if (!val)
|
|
|
|
return {};
|
|
|
|
return val->getValue();
|
|
|
|
}
|
|
|
|
|
2023-12-30 14:31:48 +01:00
|
|
|
/*!
|
|
|
|
Get a special config value's stored value. Empty on fail.
|
|
|
|
*/
|
2023-12-29 18:01:04 +01:00
|
|
|
std::any getSpecialConfigValue(const char* category, const char* name, const char* key = nullptr) {
|
|
|
|
CConfigValue* val = getSpecialConfigValuePtr(category, name, key);
|
|
|
|
if (!val)
|
|
|
|
return {};
|
|
|
|
return val->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_bCommenced = false;
|
|
|
|
|
|
|
|
CConfigImpl* impl;
|
|
|
|
|
|
|
|
CParseResult parseLine(std::string line, bool dynamic = false);
|
|
|
|
CParseResult configSetValueSafe(const std::string& command, const std::string& value);
|
|
|
|
CParseResult parseVariable(const std::string& lhs, const std::string& rhs, bool dynamic = false);
|
|
|
|
void clearState();
|
2023-12-29 19:35:23 +01:00
|
|
|
void applyDefaultsToCat(SSpecialCategory& cat);
|
2023-12-29 18:01:04 +01:00
|
|
|
};
|
|
|
|
};
|
2023-12-28 20:38:01 +01:00
|
|
|
#endif
|