ATLAS Offline Software
|
Helper classes to manage shared objects in a scope. More...
#include <ObjContainer.h>
Public Member Functions | |
ObjContainer (unsigned int min_size=0) | |
~ObjContainer () noexcept(false) | |
bool | isValid (ObjRef ref) const |
std::pair< short, bool > | search (T_Obj *obj) const |
unsigned short | size () const |
Return the current number of slots on the container. More... | |
Protected Member Functions | |
ObjRef | registerObj (T_Obj &obj) |
Manage an external object. More... | |
ObjRef | registerObj (T_Obj *obj) |
manage a new object. More... | |
ObjRef | share (ObjRef ref) |
Increase the share count of a given object. More... | |
void | drop (ObjRef &ref) |
Decrease the share count of the referenced object and eventually destroy it. More... | |
const T_Obj * | get (ObjRef ref) const |
Get a pointer to a managed object. More... | |
T_Obj * | get (ObjRef ref) |
Get a pointer to a managed object. More... | |
T_Obj * | release (ObjRef ref) |
Remove ownership over the given object from the container. More... | |
bool | isOwned (ObjRef ref) const |
Return true if this container owns the object. More... | |
bool | isShared (ObjRef ref) const |
Return true if the object is referred to by more than one ObjPtr. More... | |
bool | isExtern (ObjRef ref) const |
Return true if the object is external i.e. More... | |
short | count (ObjRef ref) const |
ObjRef | find (T_Obj *obj) const |
Search for an object in the container. More... | |
void | ensureValidity (ObjRef ref) const |
Will throw an exception if the reference is not valid. More... | |
void | ensureExists (ObjRef ref) const |
Will throw an exception if the referenced object does not exist. More... | |
void | checkCapacity () |
Will throw an exception if the maximum capacity of the container is exceeded. More... | |
ObjRef | registerObj (T_Obj *obj, short initial_count) |
Register an object with a given state (external, released, shared) More... | |
Protected Attributes | |
std::vector< std::pair< T_Obj *, short > > | m_objs |
The storage for the object pointers. More... | |
unsigned short | m_freeIdx =std::numeric_limits<unsigned short>::max() |
Static Protected Attributes | |
constexpr static short | s_externalObj = -2 |
"share count" of external objects More... | |
constexpr static short | s_releasedObj = -1 |
"share count" of released objects More... | |
Friends | |
template<class T_Obj_ > | |
class | ObjPtr |
Helper classes to manage shared objects in a scope.
Container for managed objects.
Implements smart pointer which allow mixing external i.e. unmanaged resources, resources which are shared or released ( out of the scope.). The pointer to all objects are stored in a central container which tags external and released resources and performs reference counting for shared resources. Non external or released resources are freed if the container goes out scope.
The template cloneObj may have to be specialised if the copy constructor of the container object base class does not properly clone the managed objects.
Objects can be either external (unmanaged), released (ownership changed to external), or shared. The methods of this class are mostly not meant to be used directly but rather through ObjPtr. Managed objects are referred to by ObjPtr and references (ObjRef) to the objects can be passed functions. While ObjPtr guarantee that objects stay alive, the references ObjRef do not provide such guarantees.
For example:
* using Cont = ObjContainer<...>; * using Ptr = ObjPtr<...>; * using Ref = ObjRef<...>; * ... inner(Cont &cont, Ref ref) { * Ptr b_ptr(cont,ref); * ... * } * ... outer() { * Cont cont; * Ptr a_ptr(cont, ...); * inner(cont, a_ptr.index()); * ... * } *
Lifetime guarantee of object referred to by a_ptr in outer, provided because the instance a_ptr will stay alive while inner is called, thus ref will point to a valid object in the container.
This will not work:
* ... inner(Cont &cont, Ref ref) { * Ptr b_ptr(cont,ref); * ... * } * ... outer() { * Cont cont; * Ref dangling_ref; * { * Ptr a_ptr(cont, ...); * dangling_ref=a_ptr.index(); // BAD: dangling_ref has wider scope than a_ptr * } * // object referred to by a_ptr / dangling_ref got deleted * // thus dangling_ref now refers to an invalid object. * inner(cont, dangling_ref); * ... * } *
Definition at line 30 of file ObjContainer.h.
|
inline |
Definition at line 215 of file ObjContainer.h.
|
inlinenoexcept |
Definition at line 217 of file ObjContainer.h.
|
inlineprotected |
Will throw an exception if the maximum capacity of the container is exceeded.
Definition at line 412 of file ObjContainer.h.
|
inlineprotected |
Definition at line 374 of file ObjContainer.h.
|
inlineprotected |
Decrease the share count of the referenced object and eventually destroy it.
Will destroy "shared" objects if the share count drops to zero, but external or released objects will not be touched.
Definition at line 289 of file ObjContainer.h.
|
inlineprotected |
Will throw an exception if the referenced object does not exist.
Definition at line 405 of file ObjContainer.h.
|
inlineprotected |
|
inlineprotected |
Search for an object in the container.
Definition at line 382 of file ObjContainer.h.
|
inlineprotected |
Get a pointer to a managed object.
ref | reference to the object. |
if | the reference is invalid or the object does not exist. |
Definition at line 328 of file ObjContainer.h.
|
inlineprotected |
Get a pointer to a managed object.
ref | reference to the object. |
if | the reference is invalid or the object does not exist. |
Definition at line 319 of file ObjContainer.h.
|
inlineprotected |
Return true if the object is external i.e.
not owned by this container.
Definition at line 370 of file ObjContainer.h.
|
inlineprotected |
Return true if this container owns the object.
Definition at line 358 of file ObjContainer.h.
|
inlineprotected |
Return true if the object is referred to by more than one ObjPtr.
Definition at line 364 of file ObjContainer.h.
|
inline |
|
inlineprotected |
Manage an external object.
obj | to an external object. The ownership is not taken by the container. |
Definition at line 247 of file ObjContainer.h.
|
inlineprotected |
manage a new object.
obj | the object to be managed. This passes ownership to the container. |
Definition at line 255 of file ObjContainer.h.
|
inlineprotected |
Register an object with a given state (external, released, shared)
Definition at line 420 of file ObjContainer.h.
|
inlineprotected |
Remove ownership over the given object from the container.
ref | reference to the object. |
will | throw an exception if the reference is invalid. Will remove ownership over the referenced object from the container. The object is still accessible like an external object. Thus, it is up to the user to ensure that released objects remain alive at least as long as the object container. If the object is an external object or was released already a clone is created. |
Definition at line 341 of file ObjContainer.h.
|
inline |
|
inlineprotected |
Increase the share count of a given object.
ref | a reference to the object that is to be shared. |
Definition at line 262 of file ObjContainer.h.
|
inline |
Return the current number of slots on the container.
Not each slot may point to an object.
Definition at line 241 of file ObjContainer.h.
Definition at line 213 of file ObjContainer.h.
|
protected |
Definition at line 466 of file ObjContainer.h.
|
protected |
The storage for the object pointers.
Definition at line 465 of file ObjContainer.h.
|
staticconstexprprotected |
"share count" of external objects
Definition at line 462 of file ObjContainer.h.
|
staticconstexprprotected |
"share count" of released objects
Definition at line 463 of file ObjContainer.h.