Lumiera  0.pre.03
»edit your freedom«
element-tracker.hpp
Go to the documentation of this file.
1 /*
2  ELEMENT-TRACKER.hpp - registry for tracking instances automatically
3 
4  Copyright (C)
5  2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
52 #ifndef LIB_ELEMENT_TRACKER_H
53 #define LIB_ELEMENT_TRACKER_H
54 
55 #include "lib/p.hpp"
56 #include "lib/optional-ref.hpp"
57 #include "lib/util-foreach.hpp"
58 #include "lib/ref-array-impl.hpp"
59 
60 
61 
62 namespace lib {
63 
80  template<typename ELM>
82  : public lib::RefArrayVector<P<ELM>>
83  {
84  using _Vec = std::vector<P<ELM>>;
85  using Iter = typename _Vec::iterator;
86  using CIter = typename _Vec::const_iterator;
87 
88  public:
90  {
91  try { clear(); }
92  catch(...) {/*ignored*/}
93  }
94 
95  void
96  clear ()
97  {
98  _Vec toKill;
99  toKill.reserve(_Vec::size());
100  toKill.swap(*this); // prevent quadratic detach()
101  ASSERT (0 == _Vec::size());
102  util::for_each (toKill, unlink_it);
103  }
104 
105  void
106  append (P<ELM> const& asset)
107  {
108  REQUIRE (asset, "Attempt to track a NIL element");
109  remove (*asset);
110  this->push_back (asset);
111  }
112 
113  void
114  remove (ELM const& asset)
115  {
116  for (Iter i = _Vec::begin();
117  i != _Vec::end() ; ++i )
118  if (asset == **i) // _Vec contains smart-ptrs
119  { // ELM is required to define '=='
120  this->erase (i);
121  return;
122  }
123  }
124 
125  bool
126  isRegistered (ELM const& asset) const
127  {
128  for (CIter i = _Vec::begin();
129  i != _Vec::end() ; ++i )
130  if (asset == **i)
131  return true;
132 
133  return false;
134  }
135 
136  private:
137  static void
138  unlink_it (P<ELM>& elm)
139  {
140  REQUIRE (elm);
141  try { elm->detach(); }
142  catch(...)
143  {
144  WARN (common,"problems while clearing ElementTracker, ignored.");
145  }
146  }
147  };
148 
149 
150 
162  template<typename TAR>
164  {
165  public:
168 
173  void
175  {
176  if (!getRegistry) return;
177  TAR& element = static_cast<TAR&> (*this);
178 
179  getRegistry().remove(element);
180  ENSURE (!getRegistry().isRegistered(element));
181  }
182 
183 
184  typedef P<TAR> PTarget;
185 
190  static PTarget
192  {
193  REQUIRE (getRegistry);
194 
195  PTarget newElement (new TAR());
196  getRegistry().append (newElement);
197 
198  ENSURE (newElement);
199  ENSURE (getRegistry().isRegistered(*newElement));
200  return newElement;
201  }
202 
203 
204  static void
205  setRegistryInstance (Registry& registry_to_use)
206  {
207  getRegistry.link_to (registry_to_use);
208  }
209 
210  static void
211  deactivateRegistryLink()
212  {
213  getRegistry.clear();
214  }
215 
216  static bool
217  is_attached_to (Registry const& someRegistry)
218  {
219  return getRegistry.points_to (someRegistry);
220  }
221 
222  protected:
223  static RegistryLink getRegistry;
224  };
225 
228  template<typename TAR>
230 
231 
232 
233 } // namespace lib
234 #endif
Optional or switchable link to an existing object.
a checked, switchable reference.
Customised refcounting smart pointer.
Registry for tracking object instances.
Implementation namespace for support and library code.
Helper mixin template for implementing a type intended to participate in automatic element tracking...
static PTarget create()
factory for creating smart-ptr managed TAR instances, automatically registered with the element-track...
void detach()
detach this element from the element-tracking registry.
Some (library-) implementations of the RefArray interface.
The asset subsystem of the Steam-Layer.
Definition: wrapperptr.hpp:35
Perform operations "for each element" of a collection.
static RegistryLink getRegistry
storage for the functor to link an AutoRegistered entity to the corresponding registration service ...
This variation of the wrapper actually is a vector, but can act as a RefArray.