Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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
59#include <vector>
60
61
62namespace lib {
63
80 template<typename ELM>
82 : public std::vector<P<ELM>>
83 {
84 using _Vec = std::vector<P<ELM>>;
85 using Iter = _Vec::iterator;
86 using CIter = _Vec::const_iterator;
87
88 public:
90 {
91 try { clear(); }
92 catch(...) {/*ignored*/}
93 }
94
95 void
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
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
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
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
215
216 static bool
217 is_attached_to (Registry const& someRegistry)
218 {
219 return getRegistry.points_to (someRegistry);
220 }
221
222 protected:
224 };
225
228 template<typename TAR>
230
231
232
233} // namespace lib
234#endif
Helper mixin template for implementing a type intended to participate in automatic element tracking.
static RegistryLink getRegistry
storage for the functor to link an AutoRegistered entity to the corresponding registration service
static void deactivateRegistryLink()
lib::ElementTracker< TAR > Registry
static PTarget create()
factory for creating smart-ptr managed TAR instances, automatically registered with the element-track...
lib::OptionalRef< Registry > RegistryLink
void detach()
detach this element from the element-tracking registry.
static void setRegistryInstance(Registry &registry_to_use)
static bool is_attached_to(Registry const &someRegistry)
Registry for tracking object instances.
static void unlink_it(P< ELM > &elm)
bool isRegistered(ELM const &asset) const
_Vec::const_iterator CIter
void append(P< ELM > const &asset)
std::vector< P< ELM > > _Vec
void remove(ELM const &asset)
Optional or switchable link to an existing object.
bool points_to(T const &target) const
void link_to(T &target)
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition p.hpp:77
Implementation namespace for support and library code.
The asset subsystem of the Steam-Layer.
disable_if< can_IterForEach< Container >, FUN > for_each(Container const &coll, FUN doIt)
operate on all elements of a STL container.
a checked, switchable reference.
Customised refcounting smart pointer.
Perform operations "for each element" of a collection.