1 #ifndef _Predicates_h_ 2 #define _Predicates_h_ 3 4 5 #include "../util/Export.h" 6 7 #include <memory> 8 9 10 class UniverseObject; 11 class Building; 12 class Fleet; 13 class Planet; 14 class Ship; 15 class System; 16 class Field; 17 class Fighter; 18 19 20 FO_COMMON_API extern const int ALL_EMPIRES; 21 22 /** the base class for UniverseObject visitor classes. These visitors have Visit() overloads for each type in the UniversObject-based 23 class herarchy. Calling Visit() returns the \a obj parameter, if some predicate is true of that object. Each UniverseObject 24 subclass needs to have an Accept(const UniverseObjectVisitor& visitor) method that consists only of "visitor->Visit(this)". Because 25 of the specific-type overloads, passing a UniverseObjectVisitor into the Accept() method of a UniverseObject will cause the 26 UniverseObjectVisitor's appropriate Visit() method to be called. Since the specific type of the \a obj parameter is known within 27 each Visit() method, \a obj can be accessed by type, without using a dynamic_cast. Note that is is therefore safe to static_cast a 28 UniversObject pointer that is returned from a UniverseObjectVisitor subclass that only returns a nonzero for one specific 29 UniverseObject subclass (e.g. StationaryFleetVisitor<Planet>). The default behavior of all Visit() methods besides 30 Visit(UniverseObject*) is to return the result of a call to Visit(UniverseObject*). This means that UniverseObjectVisitor 31 subclasses can override Visit(UniverseObject*) only, and calls to all Visit() overloads will work. The default return value for 32 Visit(UniverseObject*) is 0, so overridding any \a one Visit() method besides this one will ensure that only UniverseObjects 33 of a single subclass are recognized by the visitor. */ 34 struct FO_COMMON_API UniverseObjectVisitor { 35 virtual std::shared_ptr<UniverseObject> Visit(std::shared_ptr<UniverseObject> obj) const; 36 37 virtual std::shared_ptr<UniverseObject> Visit(std::shared_ptr<Building> obj) const; 38 39 virtual std::shared_ptr<UniverseObject> Visit(std::shared_ptr<Fleet> obj) const; 40 41 virtual std::shared_ptr<UniverseObject> Visit(std::shared_ptr<Planet> obj) const; 42 43 virtual std::shared_ptr<UniverseObject> Visit(std::shared_ptr<Ship> obj) const; 44 45 virtual std::shared_ptr<UniverseObject> Visit(std::shared_ptr<System> obj) const; 46 47 virtual std::shared_ptr<UniverseObject> Visit(std::shared_ptr<Field> obj) const; 48 49 virtual std::shared_ptr<UniverseObject> Visit(std::shared_ptr<Fighter> obj) const; 50 51 virtual ~UniverseObjectVisitor(); 52 }; 53 54 /** returns obj iff \a obj is a Fleet belonging to the given empire object that is parked at a System, not under orders to move. 55 If the given empire is -1, all orderd moving fleets will be returned. Note that it is preferable to use this functor on System 56 searches, rather than Universe ones. */ 57 struct FO_COMMON_API StationaryFleetVisitor : UniverseObjectVisitor 58 { 59 StationaryFleetVisitor(int empire = ALL_EMPIRES); 60 61 virtual ~StationaryFleetVisitor(); 62 63 std::shared_ptr<UniverseObject> Visit(std::shared_ptr<Fleet> obj) const override; 64 65 const int empire_id; 66 }; 67 68 /** returns obj iff \a obj is a Fleet belonging to the given empire, and that is under orders to move, but is not yet moving. 69 If the given empire is -1, all stationary fleets will be returned. Note that it is preferable to use this functor on System 70 searches, rather than Universe ones. */ 71 struct FO_COMMON_API OrderedMovingFleetVisitor : UniverseObjectVisitor 72 { 73 OrderedMovingFleetVisitor(int empire = ALL_EMPIRES); 74 75 virtual ~OrderedMovingFleetVisitor(); 76 77 std::shared_ptr<UniverseObject> Visit(std::shared_ptr<Fleet> obj) const override; 78 79 const int empire_id; 80 }; 81 82 /** returns obj iff \a obj is a moving Fleet belonging to the given empire, and that is moving between systems. 83 If the given empire is -1, all moving fleets will be returned. */ 84 struct FO_COMMON_API MovingFleetVisitor : UniverseObjectVisitor 85 { 86 MovingFleetVisitor(int empire = ALL_EMPIRES); 87 88 virtual ~MovingFleetVisitor(); 89 90 std::shared_ptr<UniverseObject> Visit(std::shared_ptr<Fleet> obj) const override; 91 92 const int empire_id; 93 }; 94 95 /** returns obj iff \a obj is owned by the empire with id \a empire, and \a obj is of type T. */ 96 struct FO_COMMON_API OwnedVisitor : UniverseObjectVisitor 97 { 98 OwnedVisitor(int empire = ALL_EMPIRES); 99 100 virtual ~OwnedVisitor(); 101 102 std::shared_ptr<UniverseObject> Visit(std::shared_ptr<UniverseObject> obj) const override; 103 104 const int empire_id; 105 }; 106 107 struct FO_COMMON_API HostileVisitor : UniverseObjectVisitor 108 { 109 HostileVisitor(int viewing_empire, int owning_empire = ALL_EMPIRES); 110 111 virtual ~HostileVisitor(); 112 113 std::shared_ptr<UniverseObject> Visit(std::shared_ptr<UniverseObject> obj) const override; 114 115 const int viewing_empire_id; 116 const int owning_empire_id; 117 }; 118 119 #endif // _Predicates_h_ 120