mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-23 22:37:55 +02:00
vcmi: use std::optional
This commit is contained in:
@@ -162,7 +162,7 @@ namespace ERMConverter
|
||||
|
||||
Variable operator()(const TVarExpNotMacro & val) const
|
||||
{
|
||||
if(val.val.is_initialized())
|
||||
if(val.val.has_value())
|
||||
return Variable(val.varsym, val.val.get());
|
||||
else
|
||||
return Variable(val.varsym, 0);
|
||||
@@ -396,7 +396,7 @@ namespace ERMConverter
|
||||
|
||||
std::vector<ParamIO> optionParams;
|
||||
|
||||
if(trig.params.is_initialized())
|
||||
if(trig.params.has_value())
|
||||
{
|
||||
for(auto & p : trig.params.get())
|
||||
optionParams.push_back(std::visit(BodyOption(), p));
|
||||
@@ -576,7 +576,7 @@ namespace ERMConverter
|
||||
{
|
||||
case 'S':
|
||||
{
|
||||
if(option.params.is_initialized())
|
||||
if(option.params.has_value())
|
||||
{
|
||||
for(auto & p : option.params.get())
|
||||
{
|
||||
@@ -743,7 +743,7 @@ namespace ERMConverter
|
||||
|
||||
std::vector<ParamIO> optionParams;
|
||||
|
||||
if(trig.params.is_initialized())
|
||||
if(trig.params.has_value())
|
||||
{
|
||||
for(auto & p : trig.params.get())
|
||||
optionParams.push_back(std::visit(BodyOption(), p));
|
||||
@@ -765,7 +765,7 @@ namespace ERMConverter
|
||||
break;
|
||||
case 'H': //checking if string is empty
|
||||
{
|
||||
if(!trig.params.is_initialized() || trig.params.get().size() != 1)
|
||||
if(!trig.params.has_value() || trig.params.get().size() != 1)
|
||||
throw EScriptExecError("VR:H option takes exactly 1 parameter!");
|
||||
|
||||
std::string opt = std::visit(VR_H(), trig.params.get()[0]);
|
||||
@@ -776,7 +776,7 @@ namespace ERMConverter
|
||||
break;
|
||||
case 'U':
|
||||
{
|
||||
if(!trig.params.is_initialized() || trig.params.get().size() != 1)
|
||||
if(!trig.params.has_value() || trig.params.get().size() != 1)
|
||||
throw EScriptExecError("VR:H/U need 1 parameter!");
|
||||
|
||||
std::string opt = std::visit(VR_S(), trig.params.get()[0]);
|
||||
@@ -787,7 +787,7 @@ namespace ERMConverter
|
||||
break;
|
||||
case 'M': //string operations
|
||||
{
|
||||
if(!trig.params.is_initialized() || trig.params.get().size() < 2)
|
||||
if(!trig.params.has_value() || trig.params.get().size() < 2)
|
||||
throw EScriptExecError("VR:M needs at least 2 parameters!");
|
||||
|
||||
std::string opt = std::visit(VR_X(), trig.params.get()[0]);
|
||||
@@ -820,7 +820,7 @@ namespace ERMConverter
|
||||
break;
|
||||
case 'X': //bit xor
|
||||
{
|
||||
if(!trig.params.is_initialized() || trig.params.get().size() != 1)
|
||||
if(!trig.params.has_value() || trig.params.get().size() != 1)
|
||||
throw EScriptExecError("VR:X option takes exactly 1 parameter!");
|
||||
|
||||
std::string opt = std::visit(VR_X(), trig.params.get()[0]);
|
||||
@@ -837,7 +837,7 @@ namespace ERMConverter
|
||||
break;
|
||||
case 'S': //setting variable
|
||||
{
|
||||
if(!trig.params.is_initialized() || trig.params.get().size() != 1)
|
||||
if(!trig.params.has_value() || trig.params.get().size() != 1)
|
||||
throw EScriptExecError("VR:S option takes exactly 1 parameter!");
|
||||
|
||||
std::string opt = std::visit(VR_S(), trig.params.get()[0]);
|
||||
@@ -855,7 +855,7 @@ namespace ERMConverter
|
||||
break;
|
||||
case 'V': //convert string to value
|
||||
{
|
||||
if(!trig.params.is_initialized() || trig.params.get().size() != 1)
|
||||
if(!trig.params.has_value() || trig.params.get().size() != 1)
|
||||
throw EScriptExecError("VR:V option takes exactly 1 parameter!");
|
||||
|
||||
std::string opt = std::visit(VR_X(), trig.params.get()[0]);
|
||||
@@ -879,9 +879,9 @@ namespace ERMConverter
|
||||
{}
|
||||
|
||||
template <typename Visitor>
|
||||
void performBody(const boost::optional<ERM::Tbody> & body, const Visitor & visitor) const
|
||||
void performBody(const std::optional<ERM::Tbody> & body, const Visitor & visitor) const
|
||||
{
|
||||
if(body.is_initialized())
|
||||
if(body.has_value())
|
||||
{
|
||||
const ERM::Tbody & bo = body.get();
|
||||
for(int g=0; g<bo.size(); ++g)
|
||||
@@ -891,14 +891,14 @@ namespace ERMConverter
|
||||
}
|
||||
}
|
||||
|
||||
void convert(const std::string & name, const boost::optional<Tidentifier> & identifier, const boost::optional<Tbody> & body) const
|
||||
void convert(const std::string & name, const std::optional<Tidentifier> & identifier, const std::optional<Tbody> & body) const
|
||||
{
|
||||
if(name == "VR")
|
||||
{
|
||||
if(!identifier.is_initialized())
|
||||
if(!identifier.has_value())
|
||||
throw EScriptExecError("VR receiver requires arguments");
|
||||
|
||||
ERM::Tidentifier tid = identifier.get();
|
||||
ERM::Tidentifier tid = identifier.value();
|
||||
if(tid.size() != 1)
|
||||
throw EScriptExecError("VR receiver takes exactly 1 argument");
|
||||
|
||||
@@ -906,10 +906,10 @@ namespace ERMConverter
|
||||
}
|
||||
else if(name == "re")
|
||||
{
|
||||
if(!identifier.is_initialized())
|
||||
if(!identifier.has_value())
|
||||
throw EScriptExecError("re receiver requires arguments");
|
||||
|
||||
ERM::Tidentifier tid = identifier.get();
|
||||
ERM::Tidentifier tid = identifier.value();
|
||||
|
||||
auto argc = tid.size();
|
||||
|
||||
@@ -935,15 +935,15 @@ namespace ERMConverter
|
||||
throw EScriptExecError("re receiver requires arguments");
|
||||
}
|
||||
}
|
||||
else if(name == "FU" && !identifier.is_initialized())
|
||||
else if(name == "FU" && !identifier.has_value())
|
||||
{
|
||||
performBody(body, FU(out)); //assume FU:E
|
||||
}
|
||||
else if(name == "MC")
|
||||
{
|
||||
if(identifier.is_initialized())
|
||||
if(identifier.has_value())
|
||||
{
|
||||
ERM::Tidentifier tid = identifier.get();
|
||||
ERM::Tidentifier tid = identifier.value();
|
||||
if(tid.size() != 1)
|
||||
throw EScriptExecError("MC receiver takes no more than 1 argument");
|
||||
|
||||
@@ -958,9 +958,9 @@ namespace ERMConverter
|
||||
{
|
||||
std::vector<std::string> identifiers;
|
||||
|
||||
if(identifier.is_initialized())
|
||||
if(identifier.has_value())
|
||||
{
|
||||
for(const auto & id : identifier.get())
|
||||
for(const auto & id : identifier.value())
|
||||
{
|
||||
Variable v = std::visit(LVL1IexpToVar(), id);
|
||||
|
||||
@@ -979,7 +979,7 @@ namespace ERMConverter
|
||||
params += *iter;
|
||||
}
|
||||
|
||||
if(body.is_initialized())
|
||||
if(body.has_value())
|
||||
{
|
||||
const ERM::Tbody & bo = body.get();
|
||||
if(bo.size() == 1)
|
||||
@@ -1038,7 +1038,7 @@ namespace ERMConverter
|
||||
|
||||
put(lhs);
|
||||
|
||||
if(cond.rhs.is_initialized())
|
||||
if(cond.rhs.has_value())
|
||||
{
|
||||
switch (op)
|
||||
{
|
||||
@@ -1061,7 +1061,7 @@ namespace ERMConverter
|
||||
put("if ");
|
||||
put(lhs);
|
||||
|
||||
if(cond.rhs.is_initialized())
|
||||
if(cond.rhs.has_value())
|
||||
{
|
||||
switch (cond.ctype)
|
||||
{
|
||||
@@ -1079,14 +1079,14 @@ namespace ERMConverter
|
||||
putLine(" then ");
|
||||
}
|
||||
|
||||
void convertReceiverOrInstruction(const boost::optional<Tcondition> & condition,
|
||||
void convertReceiverOrInstruction(const std::optional<Tcondition> & condition,
|
||||
const std::string & name,
|
||||
const boost::optional<Tidentifier> & identifier,
|
||||
const boost::optional<Tbody> & body) const
|
||||
const std::optional<Tidentifier> & identifier,
|
||||
const std::optional<Tbody> & body) const
|
||||
{
|
||||
if(name=="if")
|
||||
{
|
||||
if(condition.is_initialized())
|
||||
if(condition.has_value())
|
||||
convertCondition(condition.get());
|
||||
else
|
||||
putLine("if true then");
|
||||
@@ -1101,7 +1101,7 @@ namespace ERMConverter
|
||||
}
|
||||
else
|
||||
{
|
||||
if(condition.is_initialized())
|
||||
if(condition.has_value())
|
||||
{
|
||||
convertCondition(condition.get());
|
||||
convert(name, identifier, body);
|
||||
@@ -1126,7 +1126,7 @@ namespace ERMConverter
|
||||
|
||||
void operator()(const Tinstruction & trig) const
|
||||
{
|
||||
convertReceiverOrInstruction(trig.condition, trig.name, trig.identifier, boost::make_optional(trig.body));
|
||||
convertReceiverOrInstruction(trig.condition, trig.name, trig.identifier, std::make_optional(trig.body));
|
||||
}
|
||||
|
||||
void operator()(const Treceiver & trig) const
|
||||
@@ -1277,10 +1277,10 @@ namespace ERMConverter
|
||||
|
||||
out << "ERM:addTrigger({" << std::endl;
|
||||
|
||||
if(!trig.identifier.is_initialized())
|
||||
if(!trig.identifier.has_value())
|
||||
throw EInterpreterError("Function must have identifier");
|
||||
|
||||
ERM::Tidentifier tid = trig.identifier.get();
|
||||
ERM::Tidentifier tid = trig.identifier.value();
|
||||
|
||||
if(tid.empty())
|
||||
throw EInterpreterError("Function must have identifier");
|
||||
@@ -1328,9 +1328,9 @@ namespace ERMConverter
|
||||
|
||||
std::vector<std::string> identifiers;
|
||||
|
||||
if(trig.identifier.is_initialized())
|
||||
if(trig.identifier.has_value())
|
||||
{
|
||||
for(const auto & id : trig.identifier.get())
|
||||
for(const auto & id : trig.identifier.value())
|
||||
{
|
||||
Variable v = std::visit(LVL1IexpToVar(), id);
|
||||
|
||||
|
||||
@@ -216,7 +216,7 @@ BOOST_FUSION_ADAPT_STRUCT(
|
||||
|
||||
BOOST_FUSION_ADAPT_STRUCT(
|
||||
ERM::TVarExpNotMacro,
|
||||
(boost::optional<char>, questionMark)
|
||||
(std::optional<char>, questionMark)
|
||||
(std::string, varsym)
|
||||
(ERM::TVarExpNotMacro::Tval, val)
|
||||
)
|
||||
@@ -248,14 +248,14 @@ BOOST_FUSION_ADAPT_STRUCT(
|
||||
BOOST_FUSION_ADAPT_STRUCT(
|
||||
ERM::TNormalBodyOption,
|
||||
(char, optionCode)
|
||||
(boost::optional<ERM::TNormalBodyOptionList>, params)
|
||||
(std::optional<ERM::TNormalBodyOptionList>, params)
|
||||
)
|
||||
|
||||
BOOST_FUSION_ADAPT_STRUCT(
|
||||
ERM::Ttrigger,
|
||||
(ERM::TCmdName, name)
|
||||
(boost::optional<ERM::Tidentifier>, identifier)
|
||||
(boost::optional<ERM::Tcondition>, condition)
|
||||
(std::optional<ERM::Tidentifier>, identifier)
|
||||
(std::optional<ERM::Tcondition>, condition)
|
||||
)
|
||||
|
||||
BOOST_FUSION_ADAPT_STRUCT(
|
||||
@@ -293,24 +293,24 @@ BOOST_FUSION_ADAPT_STRUCT(
|
||||
BOOST_FUSION_ADAPT_STRUCT(
|
||||
ERM::Tinstruction,
|
||||
(ERM::TCmdName, name)
|
||||
(boost::optional<ERM::Tidentifier>, identifier)
|
||||
(boost::optional<ERM::Tcondition>, condition)
|
||||
(std::optional<ERM::Tidentifier>, identifier)
|
||||
(std::optional<ERM::Tcondition>, condition)
|
||||
(ERM::Tbody, body)
|
||||
)
|
||||
|
||||
BOOST_FUSION_ADAPT_STRUCT(
|
||||
ERM::Treceiver,
|
||||
(ERM::TCmdName, name)
|
||||
(boost::optional<ERM::Tidentifier>, identifier)
|
||||
(boost::optional<ERM::Tcondition>, condition)
|
||||
(boost::optional<ERM::Tbody>, body)
|
||||
(std::optional<ERM::Tidentifier>, identifier)
|
||||
(std::optional<ERM::Tcondition>, condition)
|
||||
(std::optional<ERM::Tbody>, body)
|
||||
)
|
||||
|
||||
BOOST_FUSION_ADAPT_STRUCT(
|
||||
ERM::TPostTrigger,
|
||||
(ERM::TCmdName, name)
|
||||
(boost::optional<ERM::Tidentifier>, identifier)
|
||||
(boost::optional<ERM::Tcondition>, condition)
|
||||
(std::optional<ERM::Tidentifier>, identifier)
|
||||
(std::optional<ERM::Tcondition>, condition)
|
||||
)
|
||||
|
||||
//BOOST_FUSION_ADAPT_STRUCT(
|
||||
|
||||
@@ -74,8 +74,8 @@ namespace ERM
|
||||
|
||||
struct TVarExpNotMacro
|
||||
{
|
||||
typedef boost::optional<int> Tval;
|
||||
boost::optional<char> questionMark;
|
||||
typedef std::optional<int> Tval;
|
||||
std::optional<char> questionMark;
|
||||
std::string varsym;
|
||||
Tval val;
|
||||
};
|
||||
@@ -134,7 +134,7 @@ namespace ERM
|
||||
struct TNormalBodyOption
|
||||
{
|
||||
char optionCode;
|
||||
boost::optional<TNormalBodyOptionList> params;
|
||||
std::optional<TNormalBodyOptionList> params;
|
||||
};
|
||||
typedef std::variant<TVRLogic, TVRArithmetic, TNormalBodyOption> TBodyOption;
|
||||
|
||||
@@ -148,12 +148,7 @@ namespace ERM
|
||||
};
|
||||
|
||||
struct Tcondition;
|
||||
typedef
|
||||
boost::optional<
|
||||
boost::recursive_wrapper<Tcondition>
|
||||
>
|
||||
TconditionNode;
|
||||
|
||||
typedef std::optional<boost::recursive_wrapper<Tcondition>> TconditionNode;
|
||||
|
||||
struct Tcondition
|
||||
{
|
||||
@@ -170,8 +165,8 @@ namespace ERM
|
||||
{
|
||||
bool pre; //if false it's !$ post-trigger, elsewise it's !# (pre)trigger
|
||||
TCmdName name;
|
||||
boost::optional<Tidentifier> identifier;
|
||||
boost::optional<Tcondition> condition;
|
||||
std::optional<Tidentifier> identifier;
|
||||
std::optional<Tcondition> condition;
|
||||
};
|
||||
|
||||
struct Ttrigger : TTriggerBase
|
||||
@@ -202,17 +197,17 @@ namespace ERM
|
||||
struct Tinstruction
|
||||
{
|
||||
TCmdName name;
|
||||
boost::optional<Tidentifier> identifier;
|
||||
boost::optional<Tcondition> condition;
|
||||
std::optional<Tidentifier> identifier;
|
||||
std::optional<Tcondition> condition;
|
||||
Tbody body;
|
||||
};
|
||||
|
||||
struct Treceiver
|
||||
{
|
||||
TCmdName name;
|
||||
boost::optional<Tidentifier> identifier;
|
||||
boost::optional<Tcondition> condition;
|
||||
boost::optional<Tbody> body;
|
||||
std::optional<Tidentifier> identifier;
|
||||
std::optional<Tcondition> condition;
|
||||
std::optional<Tbody> body;
|
||||
};
|
||||
|
||||
struct Tcommand
|
||||
|
||||
@@ -53,10 +53,10 @@ public:
|
||||
void push(const JsonNode & value);
|
||||
|
||||
template<typename T>
|
||||
void push(const boost::optional<T> & value)
|
||||
void push(const std::optional<T> & value)
|
||||
{
|
||||
if(value.is_initialized())
|
||||
push(value.get());
|
||||
if(value.has_value())
|
||||
push(value.value());
|
||||
else
|
||||
pushNil();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user