hyprlang/src/common.cpp

156 lines
5 KiB
C++
Raw Normal View History

#include "public.hpp"
2023-12-29 19:35:23 +01:00
#include "config.hpp"
#include <string.h>
using namespace Hyprlang;
void CParseResult::setError(const std::string& err) {
error = true;
errorStdString = err;
errorString = errorStdString.c_str();
}
void CParseResult::setError(const char* err) {
error = true;
errorStdString = err;
errorString = errorStdString.c_str();
}
CConfigValue::~CConfigValue() {
2023-12-29 19:35:23 +01:00
if (m_eType == CONFIGDATATYPE_CUSTOM)
reinterpret_cast<CConfigCustomValueType*>(m_pData)->dtor(&reinterpret_cast<CConfigCustomValueType*>(m_pData)->data);
if (m_pData)
free(m_pData);
}
CConfigValue::CConfigValue(const int64_t value) {
m_pData = calloc(1, sizeof(int64_t));
*reinterpret_cast<int64_t*>(m_pData) = value;
m_eType = CONFIGDATATYPE_INT;
}
CConfigValue::CConfigValue(const float value) {
m_pData = calloc(1, sizeof(float));
*reinterpret_cast<float*>(m_pData) = value;
m_eType = CONFIGDATATYPE_FLOAT;
}
2023-12-28 21:04:51 +01:00
CConfigValue::CConfigValue(const SVector2D value) {
m_pData = calloc(1, sizeof(SVector2D));
*reinterpret_cast<SVector2D*>(m_pData) = value;
m_eType = CONFIGDATATYPE_VEC2;
}
CConfigValue::CConfigValue(const char* value) {
m_pData = calloc(1, strlen(value) + 1);
strncpy((char*)m_pData, value, strlen(value));
m_eType = CONFIGDATATYPE_STR;
}
2023-12-29 19:35:23 +01:00
CConfigValue::CConfigValue(CConfigCustomValueType&& value) {
m_pData = calloc(1, sizeof(CConfigCustomValueType));
new (m_pData) CConfigCustomValueType(value);
m_eType = CONFIGDATATYPE_CUSTOM;
}
CConfigValue::CConfigValue() {
;
}
void* CConfigValue::dataPtr() const {
return m_pData;
}
2023-12-31 14:15:03 +01:00
void* const* CConfigValue::getDataStaticPtr() const {
return &m_pData;
}
2023-12-29 19:35:23 +01:00
CConfigCustomValueType::CConfigCustomValueType(PCONFIGCUSTOMVALUEHANDLERFUNC handler_, PCONFIGCUSTOMVALUEDESTRUCTOR dtor_, const char* def) {
handler = handler_;
dtor = dtor_;
defaultVal = def;
}
CConfigCustomValueType::~CConfigCustomValueType() {
dtor(&data);
}
void CConfigValue::defaultFrom(SConfigDefaultValue& ref) {
m_eType = (CConfigValue::eDataType)ref.type;
switch (m_eType) {
case CONFIGDATATYPE_FLOAT: {
if (!m_pData)
m_pData = calloc(1, sizeof(float));
*reinterpret_cast<float*>(m_pData) = std::any_cast<float>(ref.data);
break;
}
2023-12-29 19:35:23 +01:00
case CONFIGDATATYPE_INT: {
if (!m_pData)
m_pData = calloc(1, sizeof(int64_t));
*reinterpret_cast<int64_t*>(m_pData) = std::any_cast<int64_t>(ref.data);
break;
}
2023-12-29 19:35:23 +01:00
case CONFIGDATATYPE_STR: {
2023-12-31 16:51:50 +01:00
if (m_pData)
2023-12-29 19:35:23 +01:00
free(m_pData);
std::string str = std::any_cast<std::string>(ref.data);
m_pData = calloc(1, str.length() + 1);
strncpy((char*)m_pData, str.c_str(), str.length());
2023-12-28 21:04:51 +01:00
break;
}
2023-12-29 19:35:23 +01:00
case CONFIGDATATYPE_VEC2: {
if (!m_pData)
m_pData = calloc(1, sizeof(SVector2D));
*reinterpret_cast<SVector2D*>(m_pData) = std::any_cast<SVector2D>(ref.data);
break;
}
2023-12-29 19:35:23 +01:00
case CONFIGDATATYPE_CUSTOM: {
if (!m_pData)
m_pData = calloc(1, sizeof(CConfigCustomValueType));
CConfigCustomValueType* type = reinterpret_cast<CConfigCustomValueType*>(m_pData);
type->handler = ref.handler;
type->dtor = ref.dtor;
type->handler(std::any_cast<std::string>(ref.data).c_str(), &type->data);
break;
}
default: {
throw "bad defaultFrom type";
}
}
}
2023-12-29 19:35:23 +01:00
void CConfigValue::setFrom(std::any value) {
switch (m_eType) {
case CONFIGDATATYPE_FLOAT: {
*reinterpret_cast<float*>(m_pData) = std::any_cast<float>(value);
break;
}
2023-12-29 19:35:23 +01:00
case CONFIGDATATYPE_INT: {
*reinterpret_cast<int64_t*>(m_pData) = std::any_cast<int64_t>(value);
break;
}
2023-12-29 19:35:23 +01:00
case CONFIGDATATYPE_STR: {
2023-12-31 16:51:50 +01:00
if (m_pData)
free(m_pData);
2023-12-29 19:35:23 +01:00
std::string str = std::any_cast<std::string>(value);
m_pData = calloc(1, str.length() + 1);
strncpy((char*)m_pData, str.c_str(), str.length());
2023-12-28 21:04:51 +01:00
break;
}
2023-12-29 19:35:23 +01:00
case CONFIGDATATYPE_VEC2: {
*reinterpret_cast<SVector2D*>(m_pData) = std::any_cast<SVector2D>(value);
break;
}
2023-12-29 19:35:23 +01:00
// case CONFIGDATATYPE_CUSTOM: {
// CConfigCustomValueType* type = reinterpret_cast<CConfigCustomValueType*>(m_pData);
// type->handler = ref.handler;
// type->dtor = ref.dtor;
// type->handler(std::any_cast<std::string>(ref.data).c_str(), &type->data);
// break;
// }
default: {
throw "bad defaultFrom type";
}
}
2023-12-29 19:35:23 +01:00
}