The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
fake_unit_ptr.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef INCL_FAKE_UNIT_HPP_
16 #define INCL_FAKE_UNIT_HPP_
17 
18 #include "units/ptr.hpp"
19 
20 class fake_unit_manager;
21 
22 /** Holds a temporary unit that can be drawn on the map without
23  being placed in the unit_map.
24  Temporary units can overlap units.
25  They are drawn after the normal units, and so draw over them.
26  Adding the same unit twice isn't allowed.
27  The fake_unit privately holds a referenced counted point to its underlying unit,
28  when it goes out of scope it removes the entry from the fake_units list.
29  The intent is to provide exception safety when the code
30  creating the temp unit is unexpectedly forced out of scope.
31  */
33 public:
36 
37  fake_unit_ptr();
38  explicit fake_unit_ptr(const internal_ptr & u); //!< Construct a fake unit pointer wrapping a normal unit pointer, marking it as a fake unit.
39  fake_unit_ptr(const internal_ptr & u, fake_unit_manager * mgr); //!< Construct a fake unit pointer, and simultaenously register with a manager.
40  fake_unit_ptr(const fake_unit_ptr & ptr); //!< Copy construct a fake unit pointer. Does not reallocate the underlying unit.
41 
42  void swap (fake_unit_ptr & o); //!< Pointer swap.
43 
44  fake_unit_ptr & operator=(fake_unit_ptr other); //!< Copy assignment operator using copy-and-swap idiom
45 
46  void reset(); //!< Reset the internal unit pointer, and deregister from the manager. This fake_unit_ptr is now dissassociated from the manager.
47  void reset(const internal_ptr & ptr); //!< Reset the internal unit pointer and point to a new unit. The old unit is deregistered, and the new unit is registered with the old manager, if there was one.
48 
49  internal_ptr operator->() { return unit_; } //!< Dereference the internal unit pointer.
50  internal_const_ptr operator->() const { return unit_; } //!< Dereference the internal unit pointer.
51 
52  internal_ptr get_unit_ptr() { return unit_; } //!< Get a copy of the internal unit pointer.
53  internal_const_ptr get_unit_ptr() const { return unit_; } //!< Get a copy of the internal unit pointer.
54 
55  unit & operator*() { return *unit_; } //!< Derference the internal unit pointer.
56  unit * get() { return unit_.get(); } //!< Get a raw pointer to the underlying unit.
57 
58  /// Removes @a this from the fake_units_ list if necessary.
60 
61  /// Place @a this on @a manager's fake_units_ dequeue.
63  /// Removes @a this from whatever fake_units_ list it is on (if any).
65 
66 private :
67  internal_ptr unit_; //!< Internal unit pointer.
68  fake_unit_manager * my_manager_; //!< Raw pointer to the manager.
69 
70 public:
71 
72  explicit operator bool() const
73  { return unit_.get() != nullptr; }
74 };
75 
76 #endif
void reset()
Reset the internal unit pointer, and deregister from the manager. This fake_unit_ptr is now dissassoc...
Definition: unit.hpp:95
internal_ptr get_unit_ptr()
Get a copy of the internal unit pointer.
void place_on_fake_unit_manager(fake_unit_manager *d)
Place this on manager's fake_units_ dequeue.
internal_const_ptr get_unit_ptr() const
Get a copy of the internal unit pointer.
internal_ptr operator->()
Dereference the internal unit pointer.
Manages a list of fake units for the display object.
int remove_from_fake_unit_manager()
Removes this from whatever fake_units_ list it is on (if any).
fake_unit_manager * my_manager_
Raw pointer to the manager.
#define d
~fake_unit_ptr()
Removes this from the fake_units_ list if necessary.
unit & operator*()
Derference the internal unit pointer.
internal_ptr unit_
Internal unit pointer.
unit_const_ptr internal_const_ptr
internal_const_ptr operator->() const
Dereference the internal unit pointer.
void swap(fake_unit_ptr &o)
Pointer swap.
unit_ptr internal_ptr
fake_unit_ptr & operator=(fake_unit_ptr other)
Copy assignment operator using copy-and-swap idiom.
Holds a temporary unit that can be drawn on the map without being placed in the unit_map.