Skip to content

Commit

Permalink
Converted config_attribute_value to use utils::variant
Browse files Browse the repository at this point in the history
All visitors that are used with apply_visitor have had their boost::static_visitor inheritance
made conditional as well.
  • Loading branch information
Vultraz committed Jan 20, 2021
1 parent c5b2298 commit e31b327
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 50 deletions.
8 changes: 6 additions & 2 deletions src/ai/composite/aspect.hpp
Expand Up @@ -381,7 +381,11 @@ class standard_aspect : public typesafe_aspect<T> {

};

class lua_aspect_visitor : public boost::static_visitor<std::string> {
class lua_aspect_visitor
#ifdef USING_BOOST_VARIANT
: public boost::static_visitor<std::string>
#endif
{
static std::string quote_string(const std::string& s);
public:
std::string operator()(bool b) const {return utils::bool_string(b);}
Expand All @@ -390,7 +394,7 @@ class lua_aspect_visitor : public boost::static_visitor<std::string> {
std::string operator()(double i) const {return std::to_string(i);}
std::string operator()(const std::string& s) const {return quote_string(s);}
std::string operator()(const t_string& s) const {return quote_string(s.str());}
std::string operator()(boost::blank) const {return "nil";}
std::string operator()(utils::monostate) const {return "nil";}
};

template<typename T>
Expand Down
5 changes: 0 additions & 5 deletions src/config.cpp
Expand Up @@ -36,11 +36,6 @@
#include <istream>
#include <locale>

#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/get.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/variant.hpp>

static lg::log_domain log_config("config");
#define ERR_CF LOG_STREAM(err, log_config)
#define DBG_CF LOG_STREAM(debug, log_config)
Expand Down
40 changes: 24 additions & 16 deletions src/config_attribute_value.cpp
Expand Up @@ -24,16 +24,14 @@
#include "log.hpp"
#include "serialization/string_utils.hpp"
#include "utils/const_clone.hpp"
#include "utils/general.hpp"

#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <istream>

#include <boost/variant/get.hpp>
#include <boost/variant/static_visitor.hpp>

static lg::log_domain log_config("config");
#define ERR_CF LOG_STREAM(err, log_config)
#define DBG_CF LOG_STREAM(debug, log_config)
Expand Down Expand Up @@ -243,9 +241,9 @@ void config_attribute_value::write_if_not_empty(const std::string& v)

bool config_attribute_value::to_bool(bool def) const
{
if(const yes_no* p = boost::get<const yes_no>(&value_))
if(const yes_no* p = utils::get_if<yes_no>(&value_))
return *p;
if(const true_false* p = boost::get<const true_false>(&value_))
if(const true_false* p = utils::get_if<true_false>(&value_))
return *p;

// No other types are ever recognized as boolean.
Expand All @@ -256,13 +254,16 @@ namespace
{
/** Visitor for converting a variant to a numeric type (T). */
template<typename T>
class attribute_numeric_visitor : public boost::static_visitor<T>
class attribute_numeric_visitor
#ifdef USING_BOOST_VARIANT
: public boost::static_visitor<T>
#endif
{
public:
// Constructor stores the default value.
attribute_numeric_visitor(T def) : def_(def) {}

T operator()(const boost::blank&) const { return def_; }
T operator()(const utils::monostate&) const { return def_; }
T operator()(bool) const { return def_; }
T operator()(int i) const { return static_cast<T>(i); }
T operator()(unsigned long long u) const { return static_cast<T>(u); }
Expand Down Expand Up @@ -306,14 +307,17 @@ double config_attribute_value::to_double(double def) const
}

/** Visitor for converting a variant to a string. */
class config_attribute_value::string_visitor : public boost::static_visitor<std::string>
class config_attribute_value::string_visitor
#ifdef USING_BOOST_VARIANT
: public boost::static_visitor<std::string>
#endif
{
const std::string default_;

public:
string_visitor(const std::string& fallback) : default_(fallback) {}

std::string operator()(const boost::blank &) const { return default_; }
std::string operator()(const utils::monostate &) const { return default_; }
std::string operator()(const yes_no & b) const { return b.str(); }
std::string operator()(const true_false & b) const { return b.str(); }
std::string operator()(int i) const { return std::to_string(i); }
Expand All @@ -330,7 +334,7 @@ std::string config_attribute_value::str(const std::string& fallback) const

t_string config_attribute_value::t_str() const
{
if(const t_string* p = boost::get<const t_string>(&value_)) {
if(const t_string* p = utils::get_if<t_string>(&value_)) {
return *p;
}

Expand All @@ -342,27 +346,30 @@ t_string config_attribute_value::t_str() const
*/
bool config_attribute_value::blank() const
{
return boost::get<const boost::blank>(&value_) != nullptr;
return utils::holds_alternative<utils::monostate>(value_);
}

/**
* Tests for an attribute that either was never set or was set to "".
*/
bool config_attribute_value::empty() const
{
if(boost::get<const boost::blank>(&value_)) {
if(blank()) {
return true;
}

if(const std::string* p = boost::get<const std::string>(&value_)) {
if(const std::string* p = utils::get_if<std::string>(&value_)) {
return p->empty();
}

return false;
}

/** Visitor handling equality checks. */
class config_attribute_value::equality_visitor : public boost::static_visitor<bool>
class config_attribute_value::equality_visitor
#ifdef USING_BOOST_VARIANT
: public boost::static_visitor<bool>
#endif
{
public:
// Most generic: not equal.
Expand Down Expand Up @@ -398,7 +405,7 @@ class config_attribute_value::equality_visitor : public boost::static_visitor<bo
*/
bool config_attribute_value::operator==(const config_attribute_value& other) const
{
return boost::apply_visitor(equality_visitor(), value_, other.value_);
return utils::visit(equality_visitor(), value_, other.value_);
}

/**
Expand All @@ -422,7 +429,8 @@ std::ostream& operator<<(std::ostream& os, const config_attribute_value& v)
{
// Simple implementation, but defined out-of-line because of the templating
// involved.
return os << v.value_;
v.apply_visitor([&os](const auto& val) { os << val; });
return os;
}

namespace utils
Expand Down
17 changes: 10 additions & 7 deletions src/config_attribute_value.hpp
Expand Up @@ -28,6 +28,8 @@
#pragma once

#include "global.hpp"
#include "tstring.hpp"
#include "utils/variant.hpp"

#include <climits>
#include <ctime>
Expand All @@ -41,12 +43,8 @@
#include <memory>

#include <boost/exception/exception.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/variant.hpp>
#include <boost/range/iterator_range.hpp>

#include "tstring.hpp"

class enum_tag;

/**
Expand Down Expand Up @@ -107,7 +105,7 @@ class config_attribute_value
// use few types (to keep the overhead low), we do have use cases for
// fractions (double) and huge numbers (up to the larger of LLONG_MAX
// and SIZE_MAX).
typedef boost::variant<boost::blank,
typedef utils::variant<utils::monostate,
true_false, yes_no,
int, unsigned long long, double,
std::string, t_string
Expand Down Expand Up @@ -238,9 +236,9 @@ class config_attribute_value
* (See the documentation for Boost.Variant.)
*/
template <typename V>
typename V::result_type apply_visitor(const V & visitor) const
auto apply_visitor(const V & visitor) const
{
return boost::apply_visitor(visitor, value_);
return utils::visit(visitor, value_);
}

private:
Expand All @@ -249,6 +247,11 @@ class config_attribute_value
static const std::string s_true, s_false;
};

#ifndef USING_BOOST_VARIANT
/** Specialize operator<< for monostate. Boost already does this, but the STL does not. */
inline std::ostream& operator<<(std::ostream& os, const std::monostate&) { return os; }
#endif

namespace utils
{
std::vector<std::string> split(const config_attribute_value& val);
Expand Down
7 changes: 5 additions & 2 deletions src/formula/callable_objects.cpp
Expand Up @@ -447,7 +447,10 @@ int unit_type_callable::do_compare(const formula_callable* callable) const
return u_.id().compare(u_callable->u_.id());
}

struct fai_variant_visitor : public boost::static_visitor<variant>
struct fai_variant_visitor
#ifdef USING_BOOST_VARIANT
: public boost::static_visitor<variant>
#endif
{
variant operator()(bool b) const { return variant(b ? 1 : 0); }
variant operator()(int i) const { return variant(i); }
Expand All @@ -458,7 +461,7 @@ struct fai_variant_visitor : public boost::static_visitor<variant>
// (or should we assume that such strings will be translatable?).
variant operator()(const std::string& s) const { return variant(s); }
variant operator()(const t_string& s) const { return variant(s.str()); }
variant operator()(boost::blank) const { return variant(); }
variant operator()(utils::monostate) const { return variant(); }
};

variant config_callable::get_value(const std::string& key) const
Expand Down
7 changes: 5 additions & 2 deletions src/scripting/lua_common.cpp
Expand Up @@ -538,12 +538,15 @@ void luaW_pushtstring(lua_State *L, const t_string& v)


namespace {
struct luaW_pushscalar_visitor : boost::static_visitor<>
struct luaW_pushscalar_visitor
#ifdef USING_BOOST_VARIANT
: boost::static_visitor<>
#endif
{
lua_State *L;
luaW_pushscalar_visitor(lua_State *l): L(l) {}

void operator()(const boost::blank&) const
void operator()(const utils::monostate&) const
{ lua_pushnil(L); }
void operator()(bool b) const
{ lua_pushboolean(L, b); }
Expand Down
2 changes: 0 additions & 2 deletions src/scripting/lua_formula_bridge.cpp
Expand Up @@ -13,8 +13,6 @@

#include "scripting/lua_formula_bridge.hpp"

#include "boost/variant/static_visitor.hpp"

#include "game_board.hpp"
#include "scripting/game_lua_kernel.hpp"
#include "scripting/lua_unit.hpp"
Expand Down
9 changes: 5 additions & 4 deletions src/serialization/parser.cpp
Expand Up @@ -46,8 +46,6 @@
#pragma warning(pop)
#endif

#include <boost/variant/static_visitor.hpp>

#include <stack>

static lg::log_domain log_config("config");
Expand Down Expand Up @@ -524,7 +522,10 @@ inline std::string escaped_string(const std::string& value)
return escaped_string(value.begin(), value.end());
}

class write_key_val_visitor : public boost::static_visitor<void>
class write_key_val_visitor
#ifdef USING_BOOST_VARIANT
: public boost::static_visitor<void>
#endif
{
public:
write_key_val_visitor(std::ostream& out, unsigned level, std::string& textdomain, const std::string& key)
Expand All @@ -547,7 +548,7 @@ class write_key_val_visitor : public boost::static_visitor<void>
// Specialized visitors for things that go in quotes:
//

void operator()(const boost::blank&) const
void operator()(const utils::monostate&) const
{
// Treat blank values as nonexistent which fits better than treating them as empty strings.
}
Expand Down
7 changes: 5 additions & 2 deletions src/units/abilities.cpp
Expand Up @@ -481,13 +481,16 @@ namespace {


template<typename T, typename TFuncFormula>
class get_ability_value_visitor : public boost::static_visitor<T>
class get_ability_value_visitor
#ifdef USING_BOOST_VARIANT
: public boost::static_visitor<T>
#endif
{
public:
// Constructor stores the default value.
get_ability_value_visitor(T def, const TFuncFormula& formula_handler) : def_(def), formula_handler_(formula_handler) {}

T operator()(const boost::blank&) const { return def_; }
T operator()(const utils::monostate&) const { return def_; }
T operator()(bool) const { return def_; }
T operator()(int i) const { return static_cast<T>(i); }
T operator()(unsigned long long u) const { return static_cast<T>(u); }
Expand Down
5 changes: 4 additions & 1 deletion src/units/filter.cpp
Expand Up @@ -194,7 +194,10 @@ struct unit_filter_attribute_literal : public unit_filter_base
F f_;
};

class contains_dollar_visitor : public boost::static_visitor<bool>
class contains_dollar_visitor
#ifdef USING_BOOST_VARIANT
: public boost::static_visitor<bool>
#endif
{
public:
contains_dollar_visitor() {}
Expand Down
8 changes: 5 additions & 3 deletions src/units/unit.hpp
Expand Up @@ -24,7 +24,6 @@
#include "utils/variant.hpp"

#include <boost/dynamic_bitset_fwd.hpp>
#include <boost/variant.hpp>

#include <bitset>
#include <optional>
Expand Down Expand Up @@ -1179,7 +1178,10 @@ class unit : public std::enable_shared_from_this<unit>
};

/** Visitor helper class to parse the upkeep value from a config. */
class upkeep_parser_visitor : public boost::static_visitor<upkeep_t>
class upkeep_parser_visitor
#ifdef USING_BOOST_VARIANT
: public boost::static_visitor<upkeep_t>
#endif
{
public:
template<typename N>
Expand All @@ -1198,7 +1200,7 @@ class unit : public std::enable_shared_from_this<unit>
throw std::invalid_argument(b.str());
}

upkeep_t operator()(boost::blank) const
upkeep_t operator()(utils::monostate) const
{
return upkeep_full();
}
Expand Down
9 changes: 5 additions & 4 deletions src/variable.cpp
Expand Up @@ -30,8 +30,6 @@
#include "units/map.hpp"
#include "team.hpp"

#include <boost/variant/static_visitor.hpp>

static lg::log_domain log_engine("engine");
#define LOG_NG LOG_STREAM(info, log_engine)
#define WRN_NG LOG_STREAM(warn, log_engine)
Expand Down Expand Up @@ -106,7 +104,7 @@ vconfig::vconfig(const config & cfg, const std::shared_ptr<const config> & cache
* If false, no copy is made, so @a cfg must be
* guaranteed to persist as long as the vconfig will.
* If in doubt, set to true; it is less efficient, but safe.
* @param[in] vars
* @param[in] vars
* See also make_safe().
*/
vconfig::vconfig(const config &cfg, bool manage_memory, const variable_set* vars)
Expand Down Expand Up @@ -330,7 +328,10 @@ bool vconfig::has_child(const std::string& key) const
}

namespace {
struct vconfig_expand_visitor : boost::static_visitor<void>
struct vconfig_expand_visitor
#ifdef USING_BOOST_VARIANT
: boost::static_visitor<void>
#endif
{
config::attribute_value &result;
const variable_set& vars;
Expand Down

0 comments on commit e31b327

Please sign in to comment.