The Battle for Wesnoth  1.15.1+dev
fake_unit_ptr.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
17 #include "units/ptr.hpp"
18 
19 class fake_unit_manager;
20 
21 /** Holds a temporary unit that can be drawn on the map without
22  being placed in the unit_map.
23  Temporary units can overlap units.
24  They are drawn after the normal units, and so draw over them.
25  Adding the same unit twice isn't allowed.
26  The fake_unit privately holds a referenced counted point to its underlying unit,
27  when it goes out of scope it removes the entry from the fake_units list.
28  The intent is to provide exception safety when the code
29  creating the temp unit is unexpectedly forced out of scope.
30  */
32 public:
35 
36  fake_unit_ptr();
37  explicit fake_unit_ptr(const internal_ptr & u); //!< Construct a fake unit pointer wrapping a normal unit pointer, marking it as a fake unit.
38  fake_unit_ptr(const internal_ptr & u, fake_unit_manager * mgr); //!< Construct a fake unit pointer, and simultaenously register with a manager.
39  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 };
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
void reset()
Reset the internal unit pointer, and deregister from the manager. This fake_unit_ptr is now dissassoc...
This class represents a single unit of a specific type.
Definition: unit.hpp:99
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&#39;s fake_units_ dequeue.
internal_const_ptr operator->() const
Dereference 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
internal_const_ptr get_unit_ptr() const
Get a copy of the internal unit pointer.
~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
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.
boost::intrusive_ptr< unit > unit_ptr
Definition: ptr.hpp:29
Holds a temporary unit that can be drawn on the map without being placed in the unit_map.