Skip to content

Commit

Permalink
Convert uses of boost functional to standard library variants
Browse files Browse the repository at this point in the history
This commit converts the following function calls:

* boost::bind                          -> std::bind
* boost::function and boost::functionN -> std::function
* boost::ref and boost::cref           -> std::ref and std::cref
* boost::bad_function_call             -> std::bad_function_call

In the process, it was discovered that std::bind has trouble with overloaded
functions. There were two such cases in the code:

* gui2::twindow had an ancient unused overload to draw(). The overload was removed.
* gui2::trepeating_button was binding tdispatcher::fire. This case was converted
  to a lambda.
  • Loading branch information
Vultraz committed Apr 3, 2016
1 parent 1df91e0 commit 0ca4e6c
Show file tree
Hide file tree
Showing 179 changed files with 913 additions and 922 deletions.
19 changes: 9 additions & 10 deletions src/ai/composite/ai.cpp
Expand Up @@ -29,8 +29,7 @@
#include "actions/attack.hpp"
#include "log.hpp"

#include <boost/bind.hpp>
#include "utils/boost_function_guarded.hpp"
#include "utils/functional.hpp"

namespace ai {

Expand Down Expand Up @@ -70,17 +69,17 @@ void ai_composite::on_create()
e_ptr->set_ai_context(this);
}

boost::function2<void, std::vector<engine_ptr>&, const config&> factory_engines =
boost::bind(&ai::ai_composite::create_engine,*this,_1,_2);
std::function<void(std::vector<engine_ptr>&, const config&)> factory_engines =
std::bind(&ai::ai_composite::create_engine,*this,_1,_2);

boost::function2<void, std::vector<goal_ptr>&, const config&> factory_goals =
boost::bind(&ai::ai_composite::create_goal,*this,_1,_2);
std::function<void(std::vector<goal_ptr>&, const config&)> factory_goals =
std::bind(&ai::ai_composite::create_goal,*this,_1,_2);

boost::function2<void, std::vector<stage_ptr>&, const config&> factory_stages =
boost::bind(&ai::ai_composite::create_stage,*this,_1,_2);
std::function<void(std::vector<stage_ptr>&, const config&)> factory_stages =
std::bind(&ai::ai_composite::create_stage,*this,_1,_2);

boost::function3<void, std::map<std::string,aspect_ptr>&, const config&, std::string> factory_aspects =
boost::bind(&ai::ai_composite::replace_aspect,*this,_1,_2,_3);
std::function<void(std::map<std::string,aspect_ptr>&, const config&, std::string)> factory_aspects =
std::bind(&ai::ai_composite::replace_aspect,*this,_1,_2,_3);

register_vector_property(property_handlers(),"engine",get_engines(), factory_engines);
register_vector_property(property_handlers(),"goal",get_goals(), factory_goals);
Expand Down
14 changes: 7 additions & 7 deletions src/ai/composite/aspect.hpp
Expand Up @@ -28,7 +28,7 @@
#include "log.hpp"
#include "util.hpp"

#include <boost/bind.hpp>
#include "utils/functional.hpp"
#include <boost/pointer_cast.hpp>

#ifdef _MSC_VER
Expand Down Expand Up @@ -78,8 +78,8 @@ class aspect : public readonly_context_proxy, public events::observer, public co
{
invalidate();
}


virtual bool active() const;

virtual std::string get_name() const
Expand All @@ -96,7 +96,7 @@ class aspect : public readonly_context_proxy, public events::observer, public co
protected:
std::string time_of_day_;
std::string turns_;

mutable bool valid_;
mutable bool valid_variant_;
mutable bool valid_lua_;
Expand Down Expand Up @@ -283,8 +283,8 @@ class composite_aspect : public typesafe_aspect<T> {
}
}

boost::function2<void, typename aspect_type<T>::typesafe_ptr_vector&, const config&> factory_facets =
boost::bind(&ai::composite_aspect<T>::create_facet,*this,_1,_2);
std::function<void(typename aspect_type<T>::typesafe_ptr_vector&, const config&)> factory_facets =
std::bind(&ai::composite_aspect<T>::create_facet,*this,_1,_2);

register_facets_property(this->property_handlers(),"facet",facets_,default_, factory_facets);

Expand Down Expand Up @@ -398,7 +398,7 @@ class standard_aspect : public typesafe_aspect<T> {
}

};

class lua_aspect_visitor : public boost::static_visitor<std::string> {
static std::string quote_string(const std::string& s);
public:
Expand Down
29 changes: 14 additions & 15 deletions src/ai/composite/property_handler.hpp
Expand Up @@ -21,8 +21,7 @@
#ifndef AI_COMPOSITE_PROPERTY_HANDLER_HPP_INCLUDED
#define AI_COMPOSITE_PROPERTY_HANDLER_HPP_INCLUDED

#include "utils/boost_function_guarded.hpp"

#include "utils/functional.hpp"
#include "config.hpp"
#include "ai/composite/component.hpp"

Expand Down Expand Up @@ -77,7 +76,7 @@ class vector_property_handler : public base_property_handler {
typedef boost::shared_ptr<T> t_ptr;
typedef std::vector< boost::shared_ptr<T> > t_ptr_vector;

vector_property_handler(const std::string &property, t_ptr_vector &values, boost::function2<void, t_ptr_vector&, const config&> &construction_factory)
vector_property_handler(const std::string &property, t_ptr_vector &values, std::function<void(t_ptr_vector&, const config&)> &construction_factory)
: factory_(construction_factory), property_(property), values_(values){}


Expand Down Expand Up @@ -162,7 +161,7 @@ class vector_property_handler : public base_property_handler {
return (j>0);
}

boost::function2<void, t_ptr_vector&, const config&> factory_;
std::function<void(t_ptr_vector&, const config&)> factory_;
const std::string property_;
t_ptr_vector &values_;

Expand All @@ -175,13 +174,13 @@ class facets_property_handler : public vector_property_handler<T> {
typedef typename vector_property_handler<T>::t_ptr t_ptr;
typedef typename vector_property_handler<T>::t_ptr_vector t_ptr_vector;
public:
facets_property_handler(const std::string &property, t_ptr_vector &values, t_ptr& def, boost::function2<void, t_ptr_vector&, const config&> &construction_factory)

facets_property_handler(const std::string &property, t_ptr_vector &values, t_ptr& def, std::function<void(t_ptr_vector&, const config&)> &construction_factory)
: vector_property_handler<T>(property, values, construction_factory)
, default_(def)
{
}

component* handle_get(const path_element &child)
{
// special case - 'get the default facet'
Expand All @@ -190,7 +189,7 @@ class facets_property_handler : public vector_property_handler<T> {
}
return vector_property_handler<T>::handle_get(child);
}

bool handle_change(const path_element &child, config cfg)
{
// special case - 'replace the default facet'
Expand All @@ -202,14 +201,14 @@ class facets_property_handler : public vector_property_handler<T> {
}
return vector_property_handler<T>::handle_change(child, cfg);
}

std::vector<component*> handle_get_children()
{
std::vector<component*> children = vector_property_handler<T>::handle_get_children();
children.push_back(default_.get());
return children;
}

private:
t_ptr& default_;
};
Expand All @@ -222,7 +221,7 @@ class aspect_property_handler : public base_property_handler {
typedef boost::shared_ptr<T> t_ptr;
typedef std::map< std::string, t_ptr > aspect_map;

aspect_property_handler(const std::string &property, aspect_map &aspects, boost::function3<void, aspect_map&, const config&, std::string> &construction_factory)
aspect_property_handler(const std::string &property, aspect_map &aspects, std::function<void(aspect_map&, const config&, std::string)> &construction_factory)
: property_(property), aspects_(aspects), factory_(construction_factory)
{
}
Expand Down Expand Up @@ -282,28 +281,28 @@ class aspect_property_handler : public base_property_handler {

const std::string &property_;
aspect_map &aspects_;
boost::function3<void, aspect_map&, const config&, std::string> factory_;
std::function<void(aspect_map&, const config&, std::string)> factory_;

};



template<typename X>
static void register_vector_property(std::map<std::string,property_handler_ptr> &property_handlers, const std::string &property, std::vector< boost::shared_ptr<X> > &values, boost::function2<void, std::vector< boost::shared_ptr<X> >&, const config&> construction_factory)
static void register_vector_property(std::map<std::string,property_handler_ptr> &property_handlers, const std::string &property, std::vector< boost::shared_ptr<X> > &values, std::function<void(std::vector< boost::shared_ptr<X> >&, const config&)> construction_factory)
{
property_handler_ptr handler_ptr = property_handler_ptr(new vector_property_handler<X>(property,values,construction_factory));
property_handlers.insert(std::make_pair(property,handler_ptr));
}

template<typename X>
static void register_facets_property(std::map<std::string,property_handler_ptr> &property_handlers, const std::string &property, std::vector< boost::shared_ptr<X> > &values, boost::shared_ptr<X>& def, boost::function2<void, std::vector< boost::shared_ptr<X> >&, const config&> construction_factory)
static void register_facets_property(std::map<std::string,property_handler_ptr> &property_handlers, const std::string &property, std::vector< boost::shared_ptr<X> > &values, boost::shared_ptr<X>& def, std::function<void(std::vector< boost::shared_ptr<X> >&, const config&)> construction_factory)
{
property_handler_ptr handler_ptr = property_handler_ptr(new facets_property_handler<X>(property,values,def,construction_factory));
property_handlers.insert(std::make_pair(property,handler_ptr));
}

template<typename X>
static void register_aspect_property(std::map<std::string,property_handler_ptr> &property_handlers, const std::string &property, std::map< std::string, boost::shared_ptr<X> > &aspects, boost::function3<void, std::map< std::string, boost::shared_ptr<X> >&, const config&, std::string> construction_factory)
static void register_aspect_property(std::map<std::string,property_handler_ptr> &property_handlers, const std::string &property, std::map< std::string, boost::shared_ptr<X> > &aspects, std::function<void(std::map< std::string, boost::shared_ptr<X> >&, const config&, std::string)> construction_factory)
{
property_handler_ptr handler_ptr = property_handler_ptr(new aspect_property_handler<X>(property,aspects,construction_factory));
property_handlers.insert(std::make_pair(property,handler_ptr));
Expand Down
8 changes: 4 additions & 4 deletions src/ai/default/recruitment.cpp
Expand Up @@ -1803,10 +1803,10 @@ recruitment_aspect::recruitment_aspect(readonly_context &context, const config &
for (config lim : parsed_cfg.child_range("limit")) {
create_limit(limits_, lim);
}
boost::function2<void, std::vector<boost::shared_ptr<recruit_job> >&, const config&> factory_jobs =
boost::bind(&recruitment_aspect::create_job,*this,_1,_2);
boost::function2<void, std::vector<boost::shared_ptr<recruit_limit> >&, const config&> factory_limits =
boost::bind(&recruitment_aspect::create_limit,*this,_1,_2);
std::function<void(std::vector<boost::shared_ptr<recruit_job> >&, const config&)> factory_jobs =
std::bind(&recruitment_aspect::create_job,*this,_1,_2);
std::function<void(std::vector<boost::shared_ptr<recruit_limit> >&, const config&)> factory_limits =
std::bind(&recruitment_aspect::create_limit,*this,_1,_2);
register_vector_property(property_handlers(), "recruit", jobs_, factory_jobs);
register_vector_property(property_handlers(), "limit", limits_, factory_limits);
}
Expand Down
6 changes: 3 additions & 3 deletions src/ai/default/stage_rca.cpp
Expand Up @@ -27,7 +27,7 @@
#include "ai/gamestate_observer.hpp"
#include "log.hpp"

#include <boost/bind.hpp>
#include "utils/functional.hpp"

namespace ai {

Expand All @@ -52,8 +52,8 @@ void candidate_action_evaluation_loop::on_create()
engine::parse_candidate_action_from_config(*this,cfg_element,back_inserter(candidate_actions_));
}

boost::function2<void, std::vector<candidate_action_ptr>&, const config&> factory_candidate_actions =
boost::bind(&candidate_action_evaluation_loop::create_candidate_action,*this,_1,_2);
std::function<void(std::vector<candidate_action_ptr>&, const config&)> factory_candidate_actions =
std::bind(&candidate_action_evaluation_loop::create_candidate_action,*this,_1,_2);

register_vector_property(property_handlers(),"candidate_action",candidate_actions_, factory_candidate_actions);

Expand Down
2 changes: 1 addition & 1 deletion src/campaign_server/campaign_server.cpp
Expand Up @@ -896,7 +896,7 @@ int main(int argc, char**argv)
} catch(network::error& e) {
std::cerr << "Aborted with network error: " << e.message << '\n';
return 3;
} catch(boost::bad_function_call& /*e*/) {
} catch(std::bad_function_call& /*e*/) {
std::cerr << "Bad request handler function call\n";
return 4;
}
Expand Down
6 changes: 3 additions & 3 deletions src/campaign_server/campaign_server.hpp
Expand Up @@ -19,7 +19,7 @@
#include "network.hpp"
#include "server/input_stream.hpp"

#include "utils/boost_function_guarded.hpp"
#include "utils/functional.hpp"
#include <boost/scoped_ptr.hpp>
#include <boost/unordered_map.hpp>

Expand Down Expand Up @@ -78,7 +78,7 @@ class server : private boost::noncopyable
{}
};

typedef boost::function<void (server*, const request& req)> request_handler;
typedef std::function<void (server*, const request& req)> request_handler;
typedef std::map<std::string, request_handler> request_handlers_table;

config cfg_;
Expand Down Expand Up @@ -161,7 +161,7 @@ class server : private boost::noncopyable
* @param cmd The request command, corresponding to the name of the [tag}
* with the request body (e.g. "handle_request_terms").
* @param func The request function. This should be a class method passed
* as a @a boost::bind function object that takes a single
* as a @a std::bind function object that takes a single
* parameter of type @a request.
*/
void register_handler(const std::string& cmd, const request_handler& func);
Expand Down
4 changes: 2 additions & 2 deletions src/config.cpp
Expand Up @@ -31,7 +31,7 @@
#include <deque>
#include <istream>

#include <boost/bind.hpp>
#include "utils/functional.hpp"
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/get.hpp>
#include <boost/variant/static_visitor.hpp>
Expand Down Expand Up @@ -421,7 +421,7 @@ bool config::attribute_value::equals(const std::string &str) const
return *this == v;
// if c["a"] = "1" then this solution would have resulted in c["a"] == "1" beeing false
// because a["a"] is '1' and not '"1"'.
// return boost::apply_visitor(boost::bind( equality_visitor(), _1, boost::cref(str) ), value_);
// return boost::apply_visitor(std::bind( equality_visitor(), _1, boost::cref(str) ), value_);
// that's why we don't use it.
}

Expand Down
6 changes: 3 additions & 3 deletions src/construct_dialog.cpp
Expand Up @@ -29,7 +29,7 @@
#include "sdl/utils.hpp"
#include "sdl/rect.hpp"

#include <boost/bind.hpp>
#include "utils/functional.hpp"

static lg::log_domain log_display("display");
#define ERR_DP LOG_STREAM(err, log_display)
Expand Down Expand Up @@ -294,7 +294,7 @@ int dialog::show()
));

plugins_context pc("Dialog");
pc.set_callback("set_result", boost::bind(&dialog::set_result, this, boost::bind(get_int, _1, "result", CLOSE_DIALOG)), false);
pc.set_callback("set_result", std::bind(&dialog::set_result, this, std::bind(get_int, _1, "result", CLOSE_DIALOG)), false);

while (pm->any_running() && result() == CONTINUE_DIALOG) {
pc.play_slice();
Expand Down Expand Up @@ -330,7 +330,7 @@ int dialog::show()
));

plugins_context pc("Dialog");
pc.set_callback("set_result", boost::bind(&dialog::set_result, this, boost::bind(get_int, _1, "result", CLOSE_DIALOG)), false);
pc.set_callback("set_result", std::bind(&dialog::set_result, this, std::bind(get_int, _1, "result", CLOSE_DIALOG)), false);

dialog_process_info dp_info;
do
Expand Down
4 changes: 2 additions & 2 deletions src/display.cpp
Expand Up @@ -2676,14 +2676,14 @@ void display::redraw_everything()
int ticks3 = SDL_GetTicks();
LOG_DP << "invalidate and draw: " << (ticks3 - ticks2) << " and " << (ticks2 - ticks1) << "\n";

for (boost::function<void(display&)> f : redraw_observers_) {
for (std::function<void(display&)> f : redraw_observers_) {
f(*this);
}

complete_redraw_event_.notify_observers();
}

void display::add_redraw_observer(boost::function<void(display&)> f)
void display::add_redraw_observer(std::function<void(display&)> f)
{
redraw_observers_.push_back(f);
}
Expand Down
6 changes: 3 additions & 3 deletions src/display.hpp
Expand Up @@ -65,7 +65,7 @@ namespace wb {

#include "overlay.hpp"

#include "utils/boost_function_guarded.hpp"
#include "utils/functional.hpp"
#include <boost/weak_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <deque>
Expand Down Expand Up @@ -376,7 +376,7 @@ class display : public filter_context, public video2::draw_layering
void redraw_everything();

/** Adds a redraw observer, a function object to be called when redraw_everything is used */
void add_redraw_observer(boost::function<void(display&)> f);
void add_redraw_observer(std::function<void(display&)> f);

/** Clear the redraw observers */
void clear_redraw_observers();
Expand Down Expand Up @@ -1148,7 +1148,7 @@ class display : public filter_context, public video2::draw_layering

surface map_screenshot_surf_;

std::vector<boost::function<void(display&)> > redraw_observers_;
std::vector<std::function<void(display&)> > redraw_observers_;

/** Debug flag - overlay x,y coords on tiles */
bool draw_coordinates_;
Expand Down
6 changes: 3 additions & 3 deletions src/editor/controller/editor_controller.cpp
Expand Up @@ -53,7 +53,7 @@

#include "halo.hpp"

#include <boost/bind.hpp>
#include "utils/functional.hpp"

namespace {
static std::vector<std::string> saved_windows_;
Expand All @@ -64,7 +64,7 @@ namespace editor {
editor_controller::editor_controller(const config &game_config, CVideo& video)
: controller_base(game_config, video)
, mouse_handler_base()
, quit_confirmation(boost::bind(&editor_controller::quit_confirm, this))
, quit_confirmation(std::bind(&editor_controller::quit_confirm, this))
, active_menu_(editor::MAP)
, reports_(new reports())
, gui_(new editor_display(editor::get_dummy_display_context(), video, *reports_, controller_base::get_theme(game_config, "editor"), config()))
Expand Down Expand Up @@ -97,7 +97,7 @@ void editor_controller::init_gui()
{
gui_->change_display_context(&context_manager_->get_map_context());
gui_->set_grid(preferences::grid());
gui_->add_redraw_observer(boost::bind(&editor_controller::display_redraw_callback, this, _1));
gui_->add_redraw_observer(std::bind(&editor_controller::display_redraw_callback, this, _1));
floating_label_manager_.reset(new font::floating_label_context());
gui().set_draw_coordinates(preferences::editor::draw_hex_coordinates());
gui().set_draw_terrain_codes(preferences::editor::draw_terrain_codes());
Expand Down
1 change: 0 additions & 1 deletion src/game_events/manager.hpp
Expand Up @@ -18,7 +18,6 @@
#include "game_events/handlers.hpp"
#include "game_events/wmi_container.hpp"

#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
Expand Down

0 comments on commit 0ca4e6c

Please sign in to comment.