Skip to content

Commit

Permalink
AI: converted code to use utils::variant wrapper
Browse files Browse the repository at this point in the history
lua_aspect_visitor was left unconditionally boost-compatible since it's still used by
config_attribute_value::apply_visitor, and c_a_v still uses boost::variant behind the scenes.
  • Loading branch information
Vultraz committed Jan 19, 2021
1 parent 693d5ba commit f41e904
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 84 deletions.
26 changes: 15 additions & 11 deletions src/ai/composite/value_translator.hpp
Expand Up @@ -20,13 +20,13 @@

#include "ai/composite/engine.hpp"
#include "ai/composite/stage.hpp"

#include "ai/lua/aspect_advancements.hpp"
#include "ai/manager.hpp"
#include "terrain/filter.hpp"
#include "lexical_cast.hpp"
#include "serialization/string_utils.hpp"
#include "resources.hpp"
#include "ai/lua/aspect_advancements.hpp"
#include "serialization/string_utils.hpp"
#include "terrain/filter.hpp"
#include "utils/variant.hpp"

namespace ai {

Expand Down Expand Up @@ -112,7 +112,11 @@ class config_value_translator<bool> {
}
};

class leader_aspects_visitor : public boost::static_visitor<std::string> {
class leader_aspects_visitor
#ifdef USING_BOOST_VARIANT
: public boost::static_visitor<std::string>
#endif
{
public:
std::string operator()(const bool b) const {
if (b) {
Expand All @@ -125,28 +129,28 @@ class leader_aspects_visitor : public boost::static_visitor<std::string> {
};

template<>
class config_value_translator< boost::variant<bool, std::vector<std::string>>> {
class config_value_translator<utils::variant<bool, std::vector<std::string>>> {
public:

static boost::variant<bool, std::vector<std::string>> cfg_to_value(const config &cfg)
static utils::variant<bool, std::vector<std::string>> cfg_to_value(const config &cfg)
{
if (cfg["value"].to_bool(true) == cfg["value"].to_bool(false)) {
return cfg["value"].to_bool();
}
return utils::split(cfg["value"]);
}

static void cfg_to_value(const config &cfg, boost::variant<bool, std::vector<std::string>> &value)
static void cfg_to_value(const config &cfg, utils::variant<bool, std::vector<std::string>> &value)
{
value = cfg_to_value(cfg);
}

static void value_to_cfg(const boost::variant<bool, std::vector<std::string>> &value, config &cfg)
static void value_to_cfg(const utils::variant<bool, std::vector<std::string>> &value, config &cfg)
{
cfg["value"] = boost::apply_visitor(leader_aspects_visitor(), value);
cfg["value"] = utils::visit(leader_aspects_visitor(), value);
}

static config value_to_cfg(const boost::variant<bool, std::vector<std::string>> &value)
static config value_to_cfg(const utils::variant<bool, std::vector<std::string>> &value)
{
config cfg;
value_to_cfg(value,cfg);
Expand Down
20 changes: 10 additions & 10 deletions src/ai/contexts.cpp
Expand Up @@ -666,12 +666,12 @@ config readonly_context_impl::get_leader_goal() const
return config();
}

boost::variant<bool, std::vector<std::string>> readonly_context_impl::get_leader_ignores_keep() const
utils::variant<bool, std::vector<std::string>> readonly_context_impl::get_leader_ignores_keep() const
{
if (leader_ignores_keep_) {
return leader_ignores_keep_->get();
}
return boost::variant<bool, std::vector<std::string>>();
return {};
}

double readonly_context_impl::get_leader_value() const
Expand All @@ -682,20 +682,20 @@ double readonly_context_impl::get_leader_value() const
return 0;
}

boost::variant<bool, std::vector<std::string>> readonly_context_impl::get_passive_leader() const
utils::variant<bool, std::vector<std::string>> readonly_context_impl::get_passive_leader() const
{
if (passive_leader_) {
return passive_leader_->get();
}
return boost::variant<bool, std::vector<std::string>>();
return {};
}

boost::variant<bool, std::vector<std::string>> readonly_context_impl::get_passive_leader_shares_keep() const
utils::variant<bool, std::vector<std::string>> readonly_context_impl::get_passive_leader_shares_keep() const
{
if (passive_leader_shares_keep_) {
return passive_leader_shares_keep_->get();
}
return boost::variant<bool, std::vector<std::string>>();
return {};
}

const moves_map& readonly_context_impl::get_possible_moves() const
Expand Down Expand Up @@ -1198,12 +1198,12 @@ bool readonly_context_impl::is_active(const std::string &time_of_day, const std:
return true;
}

bool readonly_context_impl::applies_to_leader(const boost::variant<bool, std::vector<std::string>> &aspect_value, const std::string &id) const
bool readonly_context_impl::applies_to_leader(const utils::variant<bool, std::vector<std::string>> &aspect_value, const std::string &id) const
{
if (aspect_value.which() == 0) {
return boost::get<bool>(aspect_value);
if (utils::variant_index(aspect_value) == 0) {
return utils::get<bool>(aspect_value);
} else {
std::vector<std::string> aspect_ids = boost::get<std::vector<std::string>>(aspect_value);
std::vector<std::string> aspect_ids = utils::get<std::vector<std::string>>(aspect_value);
for(std::vector<std::string>::const_iterator aspect_id = aspect_ids.begin(); aspect_id != aspect_ids.end() ; ++aspect_id ) {
if(*aspect_id == id) {
return true;
Expand Down
27 changes: 14 additions & 13 deletions src/ai/contexts.hpp
Expand Up @@ -27,6 +27,7 @@
#include "generic_event.hpp" // for observer
#include "units/ptr.hpp" // for unit_ptr
#include "map/location.hpp" // for map_location
#include "utils/variant.hpp"

#include <map> // for map, map<>::value_compare
#include <set> // for set
Expand Down Expand Up @@ -241,13 +242,13 @@ class readonly_context : public virtual side_context {

virtual config get_leader_goal() const = 0;

virtual boost::variant<bool, std::vector<std::string>> get_leader_ignores_keep() const = 0;
virtual utils::variant<bool, std::vector<std::string>> get_leader_ignores_keep() const = 0;

virtual double get_leader_value() const = 0;

virtual boost::variant<bool, std::vector<std::string>> get_passive_leader() const = 0;
virtual utils::variant<bool, std::vector<std::string>> get_passive_leader() const = 0;

virtual boost::variant<bool, std::vector<std::string>> get_passive_leader_shares_keep() const = 0;
virtual utils::variant<bool, std::vector<std::string>> get_passive_leader_shares_keep() const = 0;

virtual const moves_map& get_possible_moves() const = 0;

Expand Down Expand Up @@ -647,7 +648,7 @@ class readonly_context_proxy : public virtual readonly_context, public virtual s
return target_->get_leader_goal();
}

virtual boost::variant<bool, std::vector<std::string>> get_leader_ignores_keep() const override
virtual utils::variant<bool, std::vector<std::string>> get_leader_ignores_keep() const override
{
return target_->get_leader_ignores_keep();
}
Expand All @@ -657,12 +658,12 @@ class readonly_context_proxy : public virtual readonly_context, public virtual s
return target_->get_leader_value();
}

virtual boost::variant<bool, std::vector<std::string>> get_passive_leader() const override
virtual utils::variant<bool, std::vector<std::string>> get_passive_leader() const override
{
return target_->get_passive_leader();
}

virtual boost::variant<bool, std::vector<std::string>> get_passive_leader_shares_keep() const override
virtual utils::variant<bool, std::vector<std::string>> get_passive_leader_shares_keep() const override
{
return target_->get_passive_leader_shares_keep();
}
Expand Down Expand Up @@ -1157,13 +1158,13 @@ class readonly_context_impl : public virtual side_context_proxy, public readonly

virtual config get_leader_goal() const override;

virtual boost::variant<bool, std::vector<std::string>> get_leader_ignores_keep() const override;
virtual utils::variant<bool, std::vector<std::string>> get_leader_ignores_keep() const override;

virtual double get_leader_value() const override;

virtual boost::variant<bool, std::vector<std::string>> get_passive_leader() const override;
virtual utils::variant<bool, std::vector<std::string>> get_passive_leader() const override;

virtual boost::variant<bool, std::vector<std::string>> get_passive_leader_shares_keep() const override;
virtual utils::variant<bool, std::vector<std::string>> get_passive_leader_shares_keep() const override;

virtual const moves_map& get_possible_moves() const override;

Expand Down Expand Up @@ -1249,7 +1250,7 @@ class readonly_context_impl : public virtual side_context_proxy, public readonly
template<typename T>
void add_known_aspect(const std::string &name, typesafe_aspect_ptr<T>& where);

bool applies_to_leader(const boost::variant<bool, std::vector<std::string>> &aspect_value, const std::string &id) const;
bool applies_to_leader(const utils::variant<bool, std::vector<std::string>> &aspect_value, const std::string &id) const;

const config cfg_;

Expand All @@ -1276,16 +1277,16 @@ class readonly_context_impl : public virtual side_context_proxy, public readonly
mutable keeps_cache keeps_;
typesafe_aspect_ptr<double> leader_aggression_;
typesafe_aspect_ptr<config> leader_goal_;
typesafe_aspect_ptr<boost::variant<bool, std::vector<std::string>>> leader_ignores_keep_;
typesafe_aspect_ptr<utils::variant<bool, std::vector<std::string>>> leader_ignores_keep_;
typesafe_aspect_ptr<double> leader_value_;
mutable bool move_maps_enemy_valid_;
mutable bool move_maps_valid_;
mutable bool dst_src_valid_lua_;
mutable bool dst_src_enemy_valid_lua_;
mutable bool src_dst_valid_lua_;
mutable bool src_dst_enemy_valid_lua_;
typesafe_aspect_ptr<boost::variant<bool, std::vector<std::string>>> passive_leader_;
typesafe_aspect_ptr<boost::variant<bool, std::vector<std::string>>> passive_leader_shares_keep_;
typesafe_aspect_ptr<utils::variant<bool, std::vector<std::string>>> passive_leader_;
typesafe_aspect_ptr<utils::variant<bool, std::vector<std::string>>> passive_leader_shares_keep_;
mutable moves_map possible_moves_;
typesafe_aspect_ptr<double> recruitment_diversity_;
typesafe_aspect_ptr<config> recruitment_instructions_;
Expand Down
25 changes: 13 additions & 12 deletions src/ai/formula/ai.cpp
Expand Up @@ -51,6 +51,7 @@
#include "ai/formula/function_table.hpp" // for ai_function_symbol_table
#include "ai/game_info.hpp" // for move_result_ptr, move_map, etc
#include "ai/formula/candidates.hpp" // for base_candidate_action, etc
#include "utils/variant.hpp"

#include <cassert> // for assert
#include <ctime> // for time
Expand Down Expand Up @@ -306,11 +307,11 @@ variant formula_ai::get_value(const std::string& key) const

} else if(key == "leader_ignores_keep")
{
boost::variant<bool, std::vector<std::string>> leader_ignores_keep = get_leader_ignores_keep();
if (leader_ignores_keep.which() == 0) {
return variant(boost::get<bool>(leader_ignores_keep));
auto leader_ignores_keep = get_leader_ignores_keep();
if (utils::variant_index(leader_ignores_keep) == 0) {
return variant(utils::get<bool>(leader_ignores_keep));
} else {
std::vector<std::string> &strlist = boost::get<std::vector<std::string>>(leader_ignores_keep);
std::vector<std::string> &strlist = utils::get<std::vector<std::string>>(leader_ignores_keep);
std::vector<variant> vars;
for(const std::string &i : strlist) {
vars.emplace_back(i);
Expand All @@ -324,11 +325,11 @@ variant formula_ai::get_value(const std::string& key) const

} else if(key == "passive_leader")
{
boost::variant<bool, std::vector<std::string>> passive_leader = get_passive_leader();
if (passive_leader.which() == 0) {
return variant(boost::get<bool>(passive_leader));
auto passive_leader = get_passive_leader();
if (utils::variant_index(passive_leader) == 0) {
return variant(utils::get<bool>(passive_leader));
} else {
std::vector<std::string> &strlist = boost::get<std::vector<std::string>>(passive_leader);
std::vector<std::string> &strlist = utils::get<std::vector<std::string>>(passive_leader);
std::vector<variant> vars;
for(const std::string &i : strlist) {
vars.emplace_back(i);
Expand All @@ -338,11 +339,11 @@ variant formula_ai::get_value(const std::string& key) const

} else if(key == "passive_leader_shares_keep")
{
boost::variant<bool, std::vector<std::string>> passive_leader_shares_keep = get_passive_leader_shares_keep();
if (passive_leader_shares_keep.which() == 0) {
return variant(boost::get<bool>(passive_leader_shares_keep));
auto passive_leader_shares_keep = get_passive_leader_shares_keep();
if (utils::variant_index(passive_leader_shares_keep) == 0) {
return variant(utils::get<bool>(passive_leader_shares_keep));
} else {
std::vector<std::string> &strlist = boost::get<std::vector<std::string>>(passive_leader_shares_keep);
std::vector<std::string> &strlist = utils::get<std::vector<std::string>>(passive_leader_shares_keep);
std::vector<variant> vars;
for(const std::string &i : strlist) {
vars.emplace_back(i);
Expand Down
24 changes: 12 additions & 12 deletions src/ai/lua/core.cpp
Expand Up @@ -436,11 +436,11 @@ static int cfun_ai_get_leader_goal(lua_State *L)
static int cfun_ai_get_leader_ignores_keep(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("leader_ignores_keep");
boost::variant<bool, std::vector<std::string>> leader_ignores_keep = get_readonly_context(L).get_leader_ignores_keep();
if (leader_ignores_keep.which() == 0) {
lua_pushboolean(L, boost::get<bool>(leader_ignores_keep));
auto leader_ignores_keep = get_readonly_context(L).get_leader_ignores_keep();
if (utils::variant_index(leader_ignores_keep) == 0) {
lua_pushboolean(L, utils::get<bool>(leader_ignores_keep));
} else {
std::vector<std::string> strlist = boost::get<std::vector<std::string>>(leader_ignores_keep);
std::vector<std::string> strlist = utils::get<std::vector<std::string>>(leader_ignores_keep);
lua_createtable(L, strlist.size(), 0);
for(const std::string& str : strlist) {
lua_pushlstring(L, str.c_str(), str.size());
Expand All @@ -461,11 +461,11 @@ static int cfun_ai_get_leader_value(lua_State *L)
static int cfun_ai_get_passive_leader(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("passive_leader");
boost::variant<bool, std::vector<std::string>> passive_leader = get_readonly_context(L).get_passive_leader();
if (passive_leader.which() == 0) {
lua_pushboolean(L, boost::get<bool>(passive_leader));
auto passive_leader = get_readonly_context(L).get_passive_leader();
if (utils::variant_index(passive_leader) == 0) {
lua_pushboolean(L, utils::get<bool>(passive_leader));
} else {
std::vector<std::string> strlist = boost::get<std::vector<std::string>>(passive_leader);
std::vector<std::string> strlist = utils::get<std::vector<std::string>>(passive_leader);
lua_createtable(L, strlist.size(), 0);
for(const std::string& str : strlist) {
lua_pushlstring(L, str.c_str(), str.size());
Expand All @@ -478,11 +478,11 @@ static int cfun_ai_get_passive_leader(lua_State *L)
static int cfun_ai_get_passive_leader_shares_keep(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("passive_leader_shares_keep");
boost::variant<bool, std::vector<std::string>> passive_leader_shares_keep = get_readonly_context(L).get_passive_leader_shares_keep();
if (passive_leader_shares_keep.which() == 0) {
lua_pushboolean(L, boost::get<bool>(passive_leader_shares_keep));
auto passive_leader_shares_keep = get_readonly_context(L).get_passive_leader_shares_keep();
if (utils::variant_index(passive_leader_shares_keep) == 0) {
lua_pushboolean(L, utils::get<bool>(passive_leader_shares_keep));
} else {
std::vector<std::string> strlist = boost::get<std::vector<std::string>>(passive_leader_shares_keep);
std::vector<std::string> strlist = utils::get<std::vector<std::string>>(passive_leader_shares_keep);
lua_createtable(L, strlist.size(), 0);
for(const std::string& str : strlist) {
lua_pushlstring(L, str.c_str(), str.size());
Expand Down
15 changes: 7 additions & 8 deletions src/ai/lua/lua_object.hpp
Expand Up @@ -30,7 +30,6 @@
#include "ai/default/contexts.hpp"
#include "ai/lua/aspect_advancements.hpp"

#include <boost/variant/get.hpp>
#include <iterator>
#include <string>
#include <vector>
Expand Down Expand Up @@ -117,13 +116,13 @@ inline std::shared_ptr<std::string> lua_object<std::string>::to_type(lua_State *
}

template <>
inline void lua_object<boost::variant<bool, std::vector<std::string>>>::from_type(lua_State *L, std::shared_ptr<boost::variant<bool, std::vector<std::string>>> value)
inline void lua_object<utils::variant<bool, std::vector<std::string>>>::from_type(lua_State *L, std::shared_ptr<utils::variant<bool, std::vector<std::string>>> value)
{
if(value) {
if (value->which() == 0) {
lua_pushboolean(L, boost::get<bool>(*value));
if (utils::variant_index(*value) == 0) {
lua_pushboolean(L, utils::get<bool>(*value));
} else {
std::vector<std::string> strlist = boost::get<std::vector<std::string>>(*value);
std::vector<std::string> strlist = utils::get<std::vector<std::string>>(*value);
lua_createtable(L, strlist.size(), 0);
for(const std::string& str : strlist) {
lua_pushlstring(L, str.c_str(), str.size());
Expand All @@ -134,10 +133,10 @@ inline void lua_object<boost::variant<bool, std::vector<std::string>>>::from_typ
}

template <>
inline std::shared_ptr< boost::variant<bool, std::vector<std::string>> > lua_object< boost::variant<bool, std::vector<std::string>> >::to_type(lua_State *L, int n)
inline std::shared_ptr< utils::variant<bool, std::vector<std::string>> > lua_object< utils::variant<bool, std::vector<std::string>> >::to_type(lua_State *L, int n)
{
if (lua_isboolean(L, n)) {
return std::make_shared<boost::variant<bool, std::vector<std::string>>>(luaW_toboolean(L, n));
return std::make_shared<utils::variant<bool, std::vector<std::string>>>(luaW_toboolean(L, n));
} else {
std::shared_ptr<std::vector<std::string>> v(new std::vector<std::string>());
int l = lua_rawlen(L, n);
Expand All @@ -150,7 +149,7 @@ inline std::shared_ptr< boost::variant<bool, std::vector<std::string>> > lua_obj
v->push_back(s);
}

return std::make_shared<boost::variant<bool, std::vector<std::string>>>(*v);
return std::make_shared<utils::variant<bool, std::vector<std::string>>>(*v);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/ai/manager.cpp
Expand Up @@ -229,9 +229,9 @@ const std::string holder::get_ai_overview()
get_ai_ref();
}
// These assignments are necessary because the code will otherwise not compile on some platforms with an lvalue/rvalue mismatch error
boost::variant<bool, std::vector<std::string>> lik = this->ai_->get_leader_ignores_keep();
boost::variant<bool, std::vector<std::string>> pl = this->ai_->get_passive_leader();
boost::variant<bool, std::vector<std::string>> plsk = this->ai_->get_passive_leader_shares_keep();
auto lik = this->ai_->get_leader_ignores_keep();
auto pl = this->ai_->get_passive_leader();
auto plsk = this->ai_->get_passive_leader_shares_keep();
// In order to display booleans as yes/no rather than 1/0 or true/false
config cfg;
cfg["simple_targeting"] = this->ai_->get_simple_targeting();
Expand All @@ -242,10 +242,10 @@ const std::string holder::get_ai_overview()
s << "caution: " << this->ai_->get_caution() << std::endl;
s << "grouping: " << this->ai_->get_grouping() << std::endl;
s << "leader_aggression: " << this->ai_->get_leader_aggression() << std::endl;
s << "leader_ignores_keep: " << boost::apply_visitor(leader_aspects_visitor(), lik) << std::endl;
s << "leader_ignores_keep: " << utils::visit(leader_aspects_visitor(), lik) << std::endl;
s << "leader_value: " << this->ai_->get_leader_value() << std::endl;
s << "passive_leader: " << boost::apply_visitor(leader_aspects_visitor(), pl) << std::endl;
s << "passive_leader_shares_keep: " << boost::apply_visitor(leader_aspects_visitor(), plsk) << std::endl;
s << "passive_leader: " << utils::visit(leader_aspects_visitor(), pl) << std::endl;
s << "passive_leader_shares_keep: " << utils::visit(leader_aspects_visitor(), plsk) << std::endl;
s << "recruitment_diversity: " << this->ai_->get_recruitment_diversity() << std::endl;
s << "recruitment_instructions: " << std::endl << "----config begin----" << std::endl;
s << this->ai_->get_recruitment_instructions() << "-----config end-----" << std::endl;
Expand Down

0 comments on commit f41e904

Please sign in to comment.