The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
fake_unit_ptr.cpp
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 #include "fake_unit_ptr.hpp"
16 
17 #include "fake_unit_manager.hpp"
18 #include "units/unit.hpp"
19 #include "units/ptr.hpp"
20 
21 #include <boost/swap.hpp>
22 
26 {
28 }
30 
34 }
35 
37  swap(other);
38  return *this;
39 }
40 
41 
42 /**
43  * Assignment operator, taking a unit.
44  * If already in the queue, @a this will be moved to the end of the
45  * queue (drawn last).
46  *
47  * This function is unsuitable for derived classes and MUST be overridden.
48  * Furthermore, derived classes must not explicitly call this version.
49  *
50  * The overriding function can be almost the same, except "new (this)" should
51  * be followed by the derived class instead of "fake_unit(a)".
52  */
53 /*fake_unit & fake_unit::operator=(unit const & a)
54 {
55  if ( this != &a ) {
56  fake_unit_manager * mgr = my_manager_;
57 
58  // Use the copy constructor to make sure we are coherent.
59  // (Methodology copied from unit::operator=)
60  this->~fake_unit();
61  new (this) fake_unit(a);
62  // Restore our old manager.
63  if ( mgr != nullptr )
64  place_on_fake_unit_manager(mgr);
65  }
66  return *this;
67 }*/
68 
69 /**
70  * Removes the unit from the fake manager, and resets the internal unit pointer.
71  * After this, both pointers are null.
72  */
74 {
76  unit_.reset();
77 }
78 
79 /**
80  * Resets the internal unit pointer to match the given pointer.
81  * The value of my_manager_ is preserved -- the old unit is deregistered,
82  * and the new unit is registered with the same manager.
83  */
85 {
86  if (unit_.get() != ptr.get()) {
88 
90  unit_ = ptr;
91  if (mgr)
93  }
94 }
95 
96 /**
97  * Removes @a this from the fake_units_ list if necessary.
98  */
100 {
101  try {
102  // The fake_unit class exists for this one line, which removes the
103  // fake_unit from the managers's fake_units_ dequeue in the event of an
104  // exception.
106 
107  } catch (...) {}
108 }
109 
110 /**
111  * Place @a this on @a manager's fake_units_ dequeue.
112  * This will be added at the end (drawn last, over all other units).
113  * Duplicate additions are not allowed.
114  */
116  assert(my_manager_ == nullptr); //Can only be placed on 1 fake_unit_manager
117  my_manager_=manager;
119 }
120 
121 /**
122  * Removes @a this from whatever fake_units_ list it is on (if any).
123  * @returns the number of fake_units deleted, which should be 0 or 1
124  * (any other number indicates an error).
125  */
127  int ret(0);
128  if(my_manager_ != nullptr){
130  my_manager_=nullptr;
131  }
132  return ret;
133 }
134 
void reset()
Reset the internal unit pointer, and deregister from the manager. This fake_unit_ptr is now dissassoc...
void place_on_fake_unit_manager(fake_unit_manager *d)
Place this on manager's fake_units_ dequeue.
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.
int remove_temporary_unit(internal_ptr_type)
Deregister a unit from this manager.
const unit * unit_
~fake_unit_ptr()
Removes this from the fake_units_ list if necessary.
internal_ptr unit_
Internal unit pointer.
void swap(fake_unit_ptr &o)
Pointer swap.
void place_temporary_unit(internal_ptr_type)
Register a unit with this manager.
fake_unit_ptr & operator=(fake_unit_ptr other)
Copy assignment operator using copy-and-swap idiom.
void swap(game_board &one, game_board &other)
Definition: game_board.cpp:56
Holds a temporary unit that can be drawn on the map without being placed in the unit_map.