The Battle for Wesnoth  1.19.5+dev
fake_unit_ptr.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by Chris Beck <render787@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
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  /** Construct a fake unit pointer wrapping a normal unit pointer, marking it as a fake unit. */
39  explicit fake_unit_ptr(const internal_ptr & u);
40  /** Construct a fake unit pointer, and simultaenously register with a manager. */
42  /** Copy construct a fake unit pointer. Does not reallocate the underlying unit. */
43  fake_unit_ptr(const fake_unit_ptr & ptr);
45 
46  /** Pointer swap. */
47  void swap (fake_unit_ptr & o);
48 
49  /** Copy assignment operator using copy-and-swap idiom */
51 
52  /** Reset the internal unit pointer, and deregister from the manager. This fake_unit_ptr is now dissassociated from the manager. */
53  void reset();
54  /** 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. */
55  void reset(const internal_ptr & ptr);
56 
57  /** Dereference the internal unit pointer. */
59  /** Dereference the internal unit pointer. */
60  internal_const_ptr operator->() const { return unit_; }
61 
62  /** Get a copy of the internal unit pointer. */
64  /** Get a copy of the internal unit pointer. */
66 
67  /** Derference the internal unit pointer. */
68  unit & operator*() { return *unit_; }
69  /** Get a raw pointer to the underlying unit. */
70  unit * get() { return unit_.get(); }
71 
72  /** Removes @a this from the fake_units_ list if necessary. */
74 
75  /** Place @a this on @a manager's fake_units_ dequeue. */
77  /** Removes @a this from whatever fake_units_ list it is on (if any). */
79 
80 private :
81  /** Internal unit pointer. */
83  /** Raw pointer to the manager. */
85 
86 public:
87 
88  explicit operator bool() const
89  { return unit_.get() != nullptr; }
90 };
Manages a list of fake units for the display object.
Holds a temporary unit that can be drawn on the map without being placed in the unit_map.
unit & operator*()
Derference the internal unit pointer.
internal_ptr get_unit_ptr()
Get a copy of the internal unit pointer.
void reset()
Reset the internal unit pointer, and deregister from the manager.
fake_unit_manager * my_manager_
Raw pointer to the manager.
internal_const_ptr operator->() const
Dereference the internal unit pointer.
unit_ptr internal_ptr
~fake_unit_ptr()
Removes this from the fake_units_ list if necessary.
unit_const_ptr internal_const_ptr
fake_unit_ptr & operator=(fake_unit_ptr other)
Copy assignment operator using copy-and-swap idiom.
internal_const_ptr get_unit_ptr() const
Get a copy of the internal unit pointer.
unit * get()
Get a raw pointer to the underlying unit.
void swap(fake_unit_ptr &o)
Pointer swap.
internal_ptr operator->()
Dereference the internal unit pointer.
int remove_from_fake_unit_manager()
Removes this from whatever fake_units_ list it is on (if any).
internal_ptr unit_
Internal unit pointer.
void place_on_fake_unit_manager(fake_unit_manager *d)
Place this on manager's fake_units_ dequeue.
This class represents a single unit of a specific type.
Definition: unit.hpp:133
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
#define d