1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 21:59:55 +00:00
openmw/components/esm3/variant.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
2.8 KiB
C++
Raw Normal View History

2013-03-04 12:59:06 +00:00
#ifndef OPENMW_ESM_VARIANT_H
#define OPENMW_ESM_VARIANT_H
#include <iosfwd>
#include <string>
#include <tuple>
2013-03-04 12:59:06 +00:00
#include <variant>
namespace ESM
{
class ESMReader;
class ESMWriter;
enum VarType
{
VT_Unknown = 0,
2013-03-04 12:59:06 +00:00
VT_None,
VT_Short, // stored as a float, kinda
VT_Int,
VT_Long, // stored as a float
VT_Float,
VT_String
};
class Variant
{
VarType mType;
2023-10-24 17:25:52 +00:00
std::variant<std::monostate, int32_t, float, std::string> mData;
2013-03-04 12:59:06 +00:00
2022-09-22 18:26:05 +00:00
public:
2013-03-04 12:59:06 +00:00
enum Format
{
Format_Global,
Format_Gmst,
Format_Info,
Format_Local // local script variables in save game files
2013-03-04 12:59:06 +00:00
};
Variant()
: mType(VT_None)
, mData(std::monostate{})
{
}
2013-03-04 12:59:06 +00:00
explicit Variant(const std::string& value)
: mType(VT_String)
, mData(value)
{
}
explicit Variant(std::string&& value)
: mType(VT_String)
, mData(std::move(value))
{
}
2013-03-04 12:59:06 +00:00
2023-10-24 17:25:52 +00:00
explicit Variant(int32_t value)
: mType(VT_Long)
, mData(value)
{
}
2013-03-04 12:59:06 +00:00
explicit Variant(float value)
: mType(VT_Float)
, mData(value)
{
}
2013-03-04 12:59:06 +00:00
VarType getType() const { return mType; }
2013-03-04 12:59:06 +00:00
const std::string& getString() const;
2013-03-04 12:59:06 +00:00
///< Will throw an exception, if value can not be represented as a string.
2023-10-24 17:25:52 +00:00
int32_t getInteger() const;
2013-03-04 12:59:06 +00:00
///< Will throw an exception, if value can not be represented as an integer (implicit
/// casting of float values is permitted).
float getFloat() const;
///< Will throw an exception, if value can not be represented as a float value.
void read(ESMReader& esm, Format format);
void write(ESMWriter& esm, Format format) const;
void write(std::ostream& stream) const;
///< Write in text format.
void setType(VarType type);
void setString(const std::string& value);
///< Will throw an exception, if type is not compatible with string.
void setString(std::string&& value);
///< Will throw an exception, if type is not compatible with string.
2023-10-24 17:25:52 +00:00
void setInteger(int32_t value);
2013-03-04 12:59:06 +00:00
///< Will throw an exception, if type is not compatible with integer.
void setFloat(float value);
///< Will throw an exception, if type is not compatible with float.
friend bool operator==(const Variant& left, const Variant& right)
{
return std::tie(left.mType, left.mData) == std::tie(right.mType, right.mData);
}
friend bool operator!=(const Variant& left, const Variant& right) { return !(left == right); }
2013-03-04 12:59:06 +00:00
};
std::ostream& operator<<(std::ostream& stream, const Variant& value);
}
#endif