2023-10-05 20:18:11 +02:00
|
|
|
/*
|
2023-10-15 15:50:24 +02:00
|
|
|
* VariantIdentifier.h, part of VCMI engine
|
2023-10-05 20:18:11 +02:00
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "IdentifierBase.h"
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2023-11-06 18:27:16 +02:00
|
|
|
/// This class represents field that may contain value of multiple different identifier types
|
2023-10-14 17:13:59 +02:00
|
|
|
template<typename... Types>
|
2023-11-02 20:01:20 +02:00
|
|
|
class VariantIdentifier
|
2023-10-05 20:18:11 +02:00
|
|
|
{
|
2023-11-08 17:35:17 +02:00
|
|
|
using Type = std::variant<Types...>;
|
|
|
|
Type value;
|
2023-10-05 20:18:11 +02:00
|
|
|
|
2023-11-08 17:35:17 +02:00
|
|
|
public:
|
2023-10-15 15:50:24 +02:00
|
|
|
VariantIdentifier()
|
2023-10-14 17:13:59 +02:00
|
|
|
{}
|
2023-10-05 20:18:11 +02:00
|
|
|
|
|
|
|
template<typename IdentifierType>
|
2023-10-15 15:50:24 +02:00
|
|
|
VariantIdentifier(const IdentifierType & identifier)
|
2023-10-14 17:13:59 +02:00
|
|
|
: value(identifier)
|
|
|
|
{}
|
|
|
|
|
|
|
|
int32_t getNum() const
|
2023-10-05 20:18:11 +02:00
|
|
|
{
|
2023-10-15 15:50:24 +02:00
|
|
|
int32_t result;
|
2023-10-14 17:13:59 +02:00
|
|
|
|
|
|
|
std::visit([&result] (const auto& v) { result = v.getNum(); }, value);
|
|
|
|
|
2023-10-15 15:50:24 +02:00
|
|
|
return result;
|
2023-10-05 20:18:11 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 17:13:59 +02:00
|
|
|
std::string toString() const
|
|
|
|
{
|
2023-10-15 15:50:24 +02:00
|
|
|
std::string result;
|
2023-10-14 17:13:59 +02:00
|
|
|
|
|
|
|
std::visit([&result] (const auto& v) { result = v.encode(v.getNum()); }, value);
|
|
|
|
|
2023-10-15 15:50:24 +02:00
|
|
|
return result;
|
2023-10-14 17:13:59 +02:00
|
|
|
}
|
2023-10-05 20:18:11 +02:00
|
|
|
|
|
|
|
template<typename IdentifierType>
|
|
|
|
IdentifierType as() const
|
|
|
|
{
|
2023-10-14 17:13:59 +02:00
|
|
|
auto * result = std::get_if<IdentifierType>(&value);
|
|
|
|
assert(result);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
return *result;
|
|
|
|
else
|
|
|
|
return IdentifierType();
|
2023-10-05 20:18:11 +02:00
|
|
|
}
|
|
|
|
|
2024-01-20 20:34:51 +02:00
|
|
|
template <typename Handler> void serialize(Handler &h)
|
2023-10-05 20:18:11 +02:00
|
|
|
{
|
2023-10-14 17:13:59 +02:00
|
|
|
h & value;
|
2023-10-05 20:18:11 +02:00
|
|
|
}
|
|
|
|
|
2023-10-15 15:50:24 +02:00
|
|
|
bool operator == (const VariantIdentifier & other) const
|
2023-10-14 17:13:59 +02:00
|
|
|
{
|
|
|
|
return value == other.value;
|
|
|
|
}
|
2023-10-15 15:50:24 +02:00
|
|
|
bool operator != (const VariantIdentifier & other) const
|
2023-10-14 17:13:59 +02:00
|
|
|
{
|
|
|
|
return value != other.value;
|
|
|
|
}
|
2023-10-15 15:50:24 +02:00
|
|
|
bool operator < (const VariantIdentifier & other) const
|
2023-10-14 17:13:59 +02:00
|
|
|
{
|
|
|
|
return value < other.value;
|
|
|
|
}
|
2023-10-05 20:18:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|