mirror of
https://github.com/mariiaan/minipp.git
synced 2026-05-14 10:11:17 +02:00
Add easy API
This commit is contained in:
@@ -77,78 +77,94 @@ namespace minipp
|
|||||||
public:
|
public:
|
||||||
class StringValue : public Value
|
class StringValue : public Value
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
using BaseType = std::string;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_value;
|
BaseType m_value;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StringValue() = default;
|
StringValue() = default;
|
||||||
StringValue(const std::string& str) : m_value(str) {};
|
StringValue(const BaseType& str) : m_value(str) {};
|
||||||
EResult Parse(const std::string& str) noexcept override;
|
EResult Parse(const std::string& str) noexcept override;
|
||||||
EResult ToString(std::string& destination) const noexcept override;
|
EResult ToString(std::string& destination) const noexcept override;
|
||||||
const std::string& GetValue() const noexcept { return m_value; }
|
const BaseType& GetValue() const noexcept { return m_value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class IntValue : public Value
|
class IntValue : public Value
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
using BaseType = int64_t;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int64_t m_value = 0;
|
BaseType m_value = 0;
|
||||||
EIntStyle m_style = EIntStyle::Decimal;
|
EIntStyle m_style = EIntStyle::Decimal;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
IntValue() = default;
|
IntValue() = default;
|
||||||
IntValue(int64_t value) : m_value(value) {};
|
IntValue(BaseType value) : m_value(value) {};
|
||||||
EResult Parse(const std::string& str) noexcept override;
|
EResult Parse(const std::string& str) noexcept override;
|
||||||
EResult ToString(std::string& destination) const noexcept override;
|
EResult ToString(std::string& destination) const noexcept override;
|
||||||
int64_t GetValue() const noexcept { return m_value; }
|
BaseType GetValue() const noexcept { return m_value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class BooleanValue : public Value
|
class BooleanValue : public Value
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
using BaseType = bool;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_value = false;
|
BaseType m_value = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BooleanValue() = default;
|
BooleanValue() = default;
|
||||||
BooleanValue(bool value) : m_value(value) {};
|
BooleanValue(BaseType value) : m_value(value) {};
|
||||||
EResult Parse(const std::string& str) noexcept override;
|
EResult Parse(const std::string& str) noexcept override;
|
||||||
EResult ToString(std::string& destination) const noexcept override;
|
EResult ToString(std::string& destination) const noexcept override;
|
||||||
bool GetValue() const noexcept { return m_value; }
|
BaseType GetValue() const noexcept { return m_value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class FloatValue : public Value
|
class FloatValue : public Value
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
using BaseType = double;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
double m_value = 0.0f;
|
BaseType m_value = 0.0f;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FloatValue() = default;
|
FloatValue() = default;
|
||||||
FloatValue(double value) : m_value(value) {};
|
FloatValue(BaseType value) : m_value(value) {};
|
||||||
EResult Parse(const std::string& str) noexcept override;
|
EResult Parse(const std::string& str) noexcept override;
|
||||||
EResult ToString(std::string& destination) const noexcept override;
|
EResult ToString(std::string& destination) const noexcept override;
|
||||||
double GetValue() const noexcept { return m_value; }
|
BaseType GetValue() const noexcept { return m_value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class ArrayValue : public Value
|
class ArrayValue : public Value
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
using BaseType = std::vector<Value*>;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::unique_ptr<Value>> m_values;
|
BaseType m_values;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ArrayValue() = default;
|
ArrayValue() = default;
|
||||||
ArrayValue(const ArrayValue&) = delete;
|
ArrayValue(const ArrayValue&) = delete;
|
||||||
ArrayValue& operator=(const ArrayValue&) = delete;
|
ArrayValue& operator=(const ArrayValue&) = delete;
|
||||||
|
virtual ~ArrayValue();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EResult Parse(const std::string& str) noexcept override;
|
EResult Parse(const std::string& str) noexcept override;
|
||||||
EResult ToString(std::string& destination) const noexcept override;
|
EResult ToString(std::string& destination) const noexcept override;
|
||||||
std::vector<std::unique_ptr<Value>>& GetValues() noexcept { return m_values; }
|
BaseType& GetValue() noexcept { return m_values; }
|
||||||
const std::vector<std::unique_ptr<Value>>& GetValues() const noexcept { return m_values; }
|
const BaseType& GetValue() const noexcept { return m_values; }
|
||||||
|
|
||||||
Value* operator[](size_t index) noexcept
|
Value* operator[](size_t index) noexcept
|
||||||
{
|
{
|
||||||
if (index >= m_values.size())
|
if (index >= m_values.size())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
return m_values[index].get();
|
return m_values[index];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -222,6 +238,18 @@ namespace minipp
|
|||||||
return EResult::Success;
|
return EResult::Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename ValueDataType>
|
||||||
|
typename ValueDataType::BaseType GetValueOrDefault(const std::string& key,
|
||||||
|
const typename ValueDataType::BaseType& defaultValue = typename ValueDataType::BaseType{})
|
||||||
|
{
|
||||||
|
static_assert(std::is_base_of<Value, ValueDataType>::value, "ValueDataType must be a subclass of Value");
|
||||||
|
|
||||||
|
ValueDataType* value = nullptr;
|
||||||
|
if (GetValue(key, &value) != EResult::Success)
|
||||||
|
return defaultValue;
|
||||||
|
return value->GetValue();
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EResult GetSubSection(const std::string& key, Section** destination) const noexcept;
|
EResult GetSubSection(const std::string& key, Section** destination) const noexcept;
|
||||||
EResult SetSubSection(const std::string& name, std::unique_ptr<Section> value, bool allowOverwrite = false) noexcept;
|
EResult SetSubSection(const std::string& name, std::unique_ptr<Section> value, bool allowOverwrite = false) noexcept;
|
||||||
@@ -434,6 +462,7 @@ minipp::EResult minipp::MiniPPFile::Values::IntValue::ToString(std::string& dest
|
|||||||
{
|
{
|
||||||
std::bitset<64> bs(m_value);
|
std::bitset<64> bs(m_value);
|
||||||
destination = bs.to_string() + "b";
|
destination = bs.to_string() + "b";
|
||||||
|
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < destination.size(); ++i) // cut of leading zeros
|
for (i = 0; i < destination.size(); ++i) // cut of leading zeros
|
||||||
if (destination[i] == '1')
|
if (destination[i] == '1')
|
||||||
@@ -485,6 +514,12 @@ minipp::EResult minipp::MiniPPFile::Values::FloatValue::ToString(std::string& de
|
|||||||
return EResult::Success;
|
return EResult::Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
minipp::MiniPPFile::Values::ArrayValue::~ArrayValue()
|
||||||
|
{
|
||||||
|
for (auto& val : m_values)
|
||||||
|
delete val;
|
||||||
|
}
|
||||||
|
|
||||||
minipp::EResult minipp::MiniPPFile::Values::ArrayValue::Parse(const std::string& str) noexcept
|
minipp::EResult minipp::MiniPPFile::Values::ArrayValue::Parse(const std::string& str) noexcept
|
||||||
{
|
{
|
||||||
std::string nValue = str;
|
std::string nValue = str;
|
||||||
@@ -581,7 +616,7 @@ minipp::EResult minipp::MiniPPFile::Values::ArrayValue::Parse(const std::string&
|
|||||||
else if (typeid(*parsed).hash_code() != lastTypeIdHash)
|
else if (typeid(*parsed).hash_code() != lastTypeIdHash)
|
||||||
return EResult::ArrayDataTypeInconsistency;
|
return EResult::ArrayDataTypeInconsistency;
|
||||||
|
|
||||||
m_values.push_back(std::move(parsed));
|
m_values.push_back(parsed.release());
|
||||||
}
|
}
|
||||||
|
|
||||||
return EResult::Success;
|
return EResult::Success;
|
||||||
|
|||||||
@@ -42,5 +42,6 @@ int main()
|
|||||||
|
|
||||||
result = file.Write("test_out.mini");
|
result = file.Write("test_out.mini");
|
||||||
|
|
||||||
|
int64_t test = root.GetValueOrDefault<MiniPPFile::Values::IntValue>("game.year", 1999);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user