The Battle for Wesnoth  1.15.9+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  /** Construct a fake unit pointer wrapping a normal unit pointer, marking it as a fake unit. */
38  explicit fake_unit_ptr(const internal_ptr & u);
39  /** Construct a fake unit pointer, and simultaenously register with a manager. */
40  fake_unit_ptr(const internal_ptr & u, fake_unit_manager * mgr);
41  /** Copy construct a fake unit pointer. Does not reallocate the underlying unit. */
42  fake_unit_ptr(const fake_unit_ptr & ptr);
44 
45  /** Pointer swap. */
46  void swap (fake_unit_ptr & o);
47 
48  /** Copy assignment operator using copy-and-swap idiom */
50 
51  /** Reset the internal unit pointer, and deregister from the manager. This fake_unit_ptr is now dissassociated from the manager. */
52  void reset();
53  /** 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. */
54  void reset(const internal_ptr & ptr);
55 
56  /** Dereference the internal unit pointer. */
57  internal_ptr operator->() { return unit_; }
58  /** Dereference the internal unit pointer. */
59  internal_const_ptr operator->() const { return unit_; }
60 
61  /** Get a copy of the internal unit pointer. */
62  internal_ptr get_unit_ptr() { return unit_; }
63  /** Get a copy of the internal unit pointer. */
64  internal_const_ptr get_unit_ptr() const { return unit_; }
65 
66  /** Derference the internal unit pointer. */
67  unit & operator*() { return *unit_; }
68  /** Get a raw pointer to the underlying unit. */
69  unit * get() { return unit_.get(); }
70 
71  /** Removes @a this from the fake_units_ list if necessary. */
73 
74  /** Place @a this on @a manager's fake_units_ dequeue. */
76  /** Removes @a this from whatever fake_units_ list it is on (if any). */
78 
79 private :
80  /** Internal unit pointer. */
81  internal_ptr unit_;
82  /** Raw pointer to the manager. */
84 
85 public:
86 
87  explicit operator bool() const
88  { return unit_.get() != nullptr; }
89 };
void reset()
Reset the internal unit pointer, and deregister from the manager.
This class represents a single unit of a specific type.
Definition: unit.hpp:119
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
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:25
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:26
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.
Holds a temporary unit that can be drawn on the map without being placed in the unit_map.