Author: tschmitz
Date: Wed Aug 3 23:58:59 2011
New Revision: 50575
URL: http://svn.gna.org/viewcvs/wesnoth?rev=50575&view=rev
Log:
Reduced complexity by eliminating inheritance of visitor from visitor_base.
Renamed visitor_base to enable_visit_all.
Added visitor::visit_all_actions() to fill the resulting void.
Modified:
trunk/src/whiteboard/highlight_visitor.cpp
trunk/src/whiteboard/highlight_visitor.hpp
trunk/src/whiteboard/manager.cpp
trunk/src/whiteboard/mapbuilder_visitor.cpp
trunk/src/whiteboard/mapbuilder_visitor.hpp
trunk/src/whiteboard/validate_visitor.hpp
trunk/src/whiteboard/visitor.hpp
Modified: trunk/src/whiteboard/highlight_visitor.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/highlight_visitor.cpp?rev=50575&r1=50574&r2=50575&view=diff
==============================================================================
--- trunk/src/whiteboard/highlight_visitor.cpp (original)
+++ trunk/src/whiteboard/highlight_visitor.cpp Wed Aug 3 23:58:59 2011
@@ -379,7 +379,7 @@
//assert(side_actions_->team_index() ==
resources::screen->viewing_team());
//Find the main highlight -- see visit(), below.
- visitor_base<highlight_visitor>::reverse_visit_all();
+ reverse_visit_all();
}
//Used only by find_main_highlight()
@@ -399,7 +399,7 @@
assert(owner_unit_);
assert(secondary_highlights_.empty());
mode_ = FIND_SECONDARY_HIGHLIGHTS;
- visitor::visit_all(); //< Standard visitation procedure.
+ visit_all_actions(); //< protected fcn from visitor
}
} // end namespace wb
Modified: trunk/src/whiteboard/highlight_visitor.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/highlight_visitor.hpp?rev=50575&r1=50574&r2=50575&view=diff
==============================================================================
--- trunk/src/whiteboard/highlight_visitor.hpp (original)
+++ trunk/src/whiteboard/highlight_visitor.hpp Wed Aug 3 23:58:59 2011
@@ -39,9 +39,9 @@
*/
class highlight_visitor
: private visitor
- , private visitor_base<highlight_visitor>
+ , private enable_visit_all<highlight_visitor>
{
- friend class visitor_base<highlight_visitor>;
+ friend class enable_visit_all<highlight_visitor>;
public:
highlight_visitor(const unit_map& unit_map, side_actions_ptr
side_actions);
@@ -71,10 +71,8 @@
virtual void visit_recall(recall_ptr recall);
virtual void visit_suppose_dead(suppose_dead_ptr sup_d);
- //"Inherited" from visitor_base
+ //"Inherited" from enable_visit_all
bool visit(size_t team_index, team&, side_actions&,
side_actions::iterator);
- using visitor::pre_visit_team;
- using visitor::post_visit_team;
void unhighlight();
Modified: trunk/src/whiteboard/manager.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/manager.cpp?rev=50575&r1=50574&r2=50575&view=diff
==============================================================================
--- trunk/src/whiteboard/manager.cpp (original)
+++ trunk/src/whiteboard/manager.cpp Wed Aug 3 23:58:59 2011
@@ -402,9 +402,9 @@
{
//Only used by manager::draw_hex()
struct draw_visitor
- : private visitor_base<draw_visitor>
- {
- friend class visitor_base<draw_visitor>;
+ : private enable_visit_all<draw_visitor>
+ {
+ friend class enable_visit_all<draw_visitor>;
public:
draw_visitor(map_location const& hex, side_actions::numbers_t&
numbers)
@@ -415,7 +415,7 @@
void operator()() {visit_all();}
private:
- //"Inherited" from visitor_base
+ //"Inherited" from enable_visit_all
bool visit(size_t /*team_index*/, team&, side_actions&,
side_actions::iterator itor)
{ (*itor)->draw_hex(hex_); return true; }
//using default pre_visit_team()
Modified: trunk/src/whiteboard/mapbuilder_visitor.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/mapbuilder_visitor.cpp?rev=50575&r1=50574&r2=50575&view=diff
==============================================================================
--- trunk/src/whiteboard/mapbuilder_visitor.cpp (original)
+++ trunk/src/whiteboard/mapbuilder_visitor.cpp Wed Aug 3 23:58:59 2011
@@ -67,7 +67,7 @@
reset_moves();
mode_ = BUILD_PLANNED_MAP;
- visit_all(); //< Inherited from visitor_base
+ visit_all();
}
bool mapbuilder_visitor::visit(size_t, team&, side_actions&,
side_actions::iterator itor)
Modified: trunk/src/whiteboard/mapbuilder_visitor.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/mapbuilder_visitor.hpp?rev=50575&r1=50574&r2=50575&view=diff
==============================================================================
--- trunk/src/whiteboard/mapbuilder_visitor.hpp (original)
+++ trunk/src/whiteboard/mapbuilder_visitor.hpp Wed Aug 3 23:58:59 2011
@@ -37,9 +37,9 @@
*/
class mapbuilder_visitor
: protected visitor
- , private visitor_base<mapbuilder_visitor>
+ , private enable_visit_all<mapbuilder_visitor>
{
- friend class visitor_base<mapbuilder_visitor>;
+ friend class enable_visit_all<mapbuilder_visitor>;
public:
mapbuilder_visitor(unit_map& unit_map);
@@ -59,9 +59,10 @@
virtual void visit_recall(recall_ptr recall);
virtual void visit_suppose_dead(suppose_dead_ptr sup_d);
- //"Inherited" from visitor_base
+ //"Inherited" from enable_visit_all
bool visit(size_t team_index, team&, side_actions&,
side_actions::iterator);
- using visitor::pre_visit_team;
+ bool pre_visit_team(size_t team_index, team& t, side_actions& sa)
+ {return
enable_visit_all<mapbuilder_visitor>::pre_visit_team(team_index,t,sa);}
bool post_visit_team(size_t team_index, team&, side_actions&)
{return team_index != viewer_team();} //< Stop after visiting
viewer_team
@@ -70,7 +71,7 @@
void reset_moves();
private:
- void visit_all() {visitor_base<mapbuilder_visitor>::visit_all();}
+ void visit_all() {enable_visit_all<mapbuilder_visitor>::visit_all();}
void restore_normal_map();
Modified: trunk/src/whiteboard/validate_visitor.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/validate_visitor.hpp?rev=50575&r1=50574&r2=50575&view=diff
==============================================================================
--- trunk/src/whiteboard/validate_visitor.hpp (original)
+++ trunk/src/whiteboard/validate_visitor.hpp Wed Aug 3 23:58:59 2011
@@ -36,9 +36,9 @@
*/
class validate_visitor
: private mapbuilder_visitor
- , private visitor_base<validate_visitor>
+ , private enable_visit_all<validate_visitor>
{
- friend class visitor_base<validate_visitor>;
+ friend class enable_visit_all<validate_visitor>;
public:
explicit validate_visitor(unit_map& unit_map);
@@ -59,12 +59,12 @@
VALIDITY evaluate_move_validity(move_ptr);
bool no_previous_invalids(side_actions::iterator const&);
- //"Inherited" from visitor_base
+ //"Inherited" from enable_visit_all
bool visit(size_t team_index, team& t, side_actions& sa,
side_actions::iterator itor)
{ arg_itor_=itor; return
visitor::visit(team_index,t,sa,itor); }
using mapbuilder_visitor::pre_visit_team;
- using visitor_base<validate_visitor>::post_visit_team;
- using visitor_base<validate_visitor>::visit_all;
+ using enable_visit_all<validate_visitor>::post_visit_team;
+ using enable_visit_all<validate_visitor>::visit_all;
side_actions& viewer_actions_;
Modified: trunk/src/whiteboard/visitor.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/visitor.hpp?rev=50575&r1=50574&r2=50575&view=diff
==============================================================================
--- trunk/src/whiteboard/visitor.hpp (original)
+++ trunk/src/whiteboard/visitor.hpp Wed Aug 3 23:58:59 2011
@@ -15,6 +15,16 @@
/**
* @file
+ * Two classes are presented in this file: enable_visit_all and visitor.
+ * enable_visit_all is a class template that provides the all-too-common
iteration
+ * code that iterates over every planned action from every team, starting
with the
+ * current-turn team. Using "static polymorphism," derived classes can
customize
+ * the iteration by overriding some functions.
+ * visitor is an abstract interface that simply provides derived classes with
the
+ * mechanism for the double dispatch used in the Visitor Pattern:
+ * action.accept(visitor) calls visitor.visit_***(action)
+ * Derived classes will usually derive from both of these.
+ * Example usage is seen is highlight_visitor, mapbuilder_visitor,
validate_visitor.
*/
#ifndef WB_VISITOR_HPP_
@@ -34,18 +44,17 @@
{
/**
- * visitor_base: A base class template, using the so-called CRTP
+ * enable_visit_all: A base class template, using the so-called CRTP. This is
the
+ * "static polymorphism" part.
* If you want visit_all() and reverse_visit_all() in your class,
- * you should derive from this class.
+ * you should derive from this class template.
* Derived classes can "override" the following:
- * visit, pre_visit_team, post_visit_team
- * Derived classes should declare visitor_base<Derived> as a friend, or
+ * visit (required), pre_visit_team, post_visit_team
+ * Derived classes should declare enable_visit_all<Derived> as a friend, or
* else make the overridden functions public.
- * I recommend making the inheritance private or protected.
- * See class visitor for an example.
*/
template<typename Derived>
-class visitor_base
+class enable_visit_all
{
public:
void visit_all() {visit_all_helper<false>();}
@@ -106,40 +115,58 @@
};
/**
+ * visitor: This is the "dynamic polymorphism" part.
* Abstract base class for all the visitors (cf GoF Visitor Design Pattern)
- * the whiteboard uses.
+ * the whiteboard uses.
+ * visitor does not inherit from enable_visit_all because it tends to make it
more
+ * "difficult" for derived classes to inherit from both visitor and
enable_visit_all.
*/
class visitor
: private boost::noncopyable
- , private visitor_base<visitor>
{
- friend class visitor_base<visitor>;
-
friend class move;
friend class attack;
friend class recruit;
friend class recall;
friend class suppose_dead;
-public:
- using visitor_base<visitor>::visit_all;
- using visitor_base<visitor>::reverse_visit_all;
-
protected:
visitor() {}
virtual ~visitor() {} //Not intended for polymorphic deletion
- //"Inherited" from visitor_base
+ /**
+ * This function is available for derived classes so they can inherit
from enable_visit_all
+ * without having to override visit(); i.e., the below implementation
can be used as a
+ * default.
+ */
bool visit(size_t /*team_index*/, team&, side_actions&,
side_actions::iterator itor)
{ (*itor)->accept(*this); return true; }
- using visitor_base<visitor>::pre_visit_team;
- using visitor_base<visitor>::post_visit_team;
+
+ //Utility fcn for derived classes that don't need to customize the
iteration.
+ void visit_all_actions()
{visitor_helper::visit_all_actions_helper(this);}
virtual void visit_move(move_ptr move) = 0;
virtual void visit_attack(attack_ptr attack) = 0;
virtual void visit_recruit(recruit_ptr recruit) = 0;
virtual void visit_recall(recall_ptr recall) = 0;
virtual void visit_suppose_dead(suppose_dead_ptr sup_d) = 0;
+
+private:
+ struct visitor_helper
+ : enable_visit_all<visitor_helper>
+ {
+ bool visit(size_t /*team_index*/, team&, side_actions&,
side_actions::iterator itor)
+ { (*itor)->accept(*v_); return true; }
+
+ static void visit_all_actions_helper(visitor* v)
+ {
+ static visitor_helper vh;
+ vh.v_ = v;
+ vh.visit_all();
+ }
+
+ visitor* v_;
+ };
};
}
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits