|
ATLAS Offline Software
|
Go to the documentation of this file.
5 #ifndef TRKEXTOOLS_OBJCONTAINER_H
6 #define TRKEXTOOLS_OBJCONTAINER_H
48 std::unique_ptr<T_Obj>
62 template <
class T_Obj_>
64 template <
class T_Obj_>
100 return obj.idx() !=
idx();
114 template <
class T_Obj_>
119 throw std::logic_error(
"No object container.");
123 std::stringstream
msg;
124 msg <<
"Maximum object container capacity exceeded (" << max_objs <<
"). Bail out.";
125 throw std::runtime_error(
msg.str());
129 std::stringstream
msg;
130 msg <<
"Maximum number of shares exceeded (" << max_shares <<
"). Bail out.";
131 throw std::runtime_error(
msg.str());
135 std::stringstream
msg;
136 msg <<
"Invalid object reference. Index " <<
idx <<
" not in allowed range [0.." << n_objs <<
"). Bail out.";
137 throw std::logic_error(
msg.str());
141 std::stringstream
msg;
142 msg <<
"Object " <<
idx <<
" already deleted. Bail out.";
143 throw std::logic_error(
msg.str());
147 std::stringstream
msg;
148 msg <<
"Object " <<
ptr <<
" already registered. Bail out.";
149 throw std::logic_error(
msg.str());
153 std::stringstream
msg;
154 msg <<
"Managed object " <<
ptr <<
" still has " <<
cnt <<
" references, but the container is now being destructed.";
155 throw std::logic_error(
msg.str());
209 template <
class T_Obj>
212 template <
class T_Obj_>
218 for (std::pair<T_Obj *, short> &elm :
m_objs) {
220 this->throwObjectStillAlive(elm.first,
static_cast<size_t>(elm.second));
222 if (elm.first && elm.second>0) {
223 this->throwObjectStillAlive(elm.first,
static_cast<size_t>(elm.second));
235 return std::make_pair(
ref ?
m_objs[
ref.idx()].second :
short{},
ref.isValid() );
241 unsigned short size()
const {
return static_cast<unsigned short>(
m_objs.size()); }
271 case 0:
if (!
m_objs[
ref.idx()].first) {this->throwObjectAlreadyDeleted(
ref.idx());}
297 case 0: this->throwObjectAlreadyDeleted(
ref.idx());
344 case 0: this->throwObjectAlreadyDeleted(
ref.idx());
383 for (
typename std::vector< std::pair<T_Obj *, short> >::const_reverse_iterator
387 if (iter->first ==
obj) {
407 if (
m_objs[
ref.idx() ].second == 0) { this->throwObjectAlreadyDeleted(
ref.idx()); }
414 this->throwMaximumCapacitiyExceeded(
m_objs.size());
436 this->throwObjectExistsAlready(
obj);
441 for (
typename std::vector< std::pair<T_Obj *, short> >::const_reverse_iterator
445 if (iter->second==0) {
452 m_objs.push_back(std::make_pair(
obj, initial_count));
465 std::vector< std::pair<T_Obj *, short> >
m_objs;
471 template <
class T_Obj>
529 if (
obj.m_container) {
544 if (
obj.m_container) {
589 if (!
m_ref)
return nullptr;
598 const T_Obj *
get()
const {
599 if (!
m_ref)
return nullptr;
600 else return std::as_const(*m_container).get(
m_ref);
611 if (!
m_ref)
return nullptr;
623 return std::unique_ptr<T_Obj>(
release());
ObjPtr(const ObjPtr &obj_ptr)
bool isOwned() const
Return true if the object is managed and owned by the container.
bool operator!=(const ObjPtr &obj) const
Test whether two pointer point to different objects.
T_Obj * release()
Release the object this pointer points to from the container.
bool operator==(const ObjRef &obj) const
constexpr static short s_externalObj
"share count" of external objects
Object container base class.
void drop(ObjRef &ref)
Decrease the share count of the referenced object and eventually destroy it.
T_Obj * get(ObjRef ref)
Get a pointer to a managed object.
ObjRef & operator=(ObjRef &&ref)
static void throwNoContainer()
const T_Obj * get(ObjRef ref) const
Get a pointer to a managed object.
static void throwObjectStillAlive(const void *ptr, size_t cnt)
static void throwMaximumNumberOfSharesExceeded(size_t max_shares)
ObjPtr(ObjPtr &&obj_ptr)=default
T_Obj * cloneObj(const T_Obj *obj)
Template to be specialised if object cloning is not handled by the copy operator of the container obj...
constexpr static short s_releasedObj
"share count" of released objects
std::unique_ptr< T_Obj > uniqueClone(const T_Obj *obj)
bool isOwned(ObjRef ref) const
Return true if this container owns the object.
ObjPtr(ObjContainer< T_Obj > &container, ObjRef ref=ObjRef())
Share the referred object managed by the given container.
void ensureValidity(ObjRef ref) const
Will throw an exception if the reference is not valid.
T_Obj * release(ObjRef ref)
Remove ownership over the given object from the container.
void checkCapacity()
Will throw an exception if the maximum capacity of the container is exceeded.
ObjRef & operator=(const ObjRef &ref)=default
bool isShared(ObjRef ref) const
Return true if the object is referred to by more than one ObjPtr.
unsigned short idx() const
ObjRef registerObj(T_Obj &obj)
Manage an external object.
bool isValid(ObjRef ref) const
bool isShared() const
Return true if the object is managed by the container and if there are more than one instances of Obj...
static void throwObjectAlreadyDeleted(size_t idx)
bool isValid() const
Return true if this class refers to an object which used to exist in the container.
ObjPtr(ObjContainer< T_Obj > &container, const T_Obj &obj)
Store an external object in the container.
ObjContainer< T_Obj > * m_container
pointer to the conainer
static void throwMaximumCapacitiyExceeded(size_t max_objs)
unsigned short size() const
Return the current number of slots on the container.
std::vector< std::pair< T_Obj *, short > > m_objs
The storage for the object pointers.
ObjRef registerObj(T_Obj *obj, short initial_count)
Register an object with a given state (external, released, shared)
bool isExtern() const
Return true if the object is an external object i.e.
std::pair< short, bool > search(T_Obj *obj) const
const T_Obj * get() const
Get a const pointer to a valid manged object.
ObjPtr(ObjContainer< T_Obj > &container, std::unique_ptr< T_Obj > obj)
Pass a unique_ptr to the container The Unique_ptr becomes an Owned object i.e has count 1.
ObjRef find(T_Obj *obj) const
Search for an object in the container.
ObjRef registerObj(T_Obj *obj)
manage a new object.
void shareAndSet(ObjRef ref)
Helper method to increase the share count of the referenced object and point to that object.
Pointer to objects managed by ObjContainer.
static void throwObjectExistsAlready(const void *ptr)
bool operator==(const ObjPtr &obj) const
Test whether two pointer point to the same object.
ObjPtr & operator=(const ObjPtr &obj)
ObjRef(unsigned short idx)
Helper class to refer to objects in the container ObjContainer.
ObjPtr & operator=(ObjRef ref)
T_Obj * get()
Get a pointer to a valid manged object.
const boost::regex ref(r_ef)
std::unique_ptr< T_Obj > to_unique()
Release the object this pointer points to from the container.
~ObjPtr() noexcept(false)
short count(ObjRef ref) const
static void warnShareDropAfterRelease(size_t idx)
void ensureExists(ObjRef ref) const
Will throw an exception if the referenced object does not exist.
bool isExtern(ObjRef ref) const
Return true if the object is external i.e.
ObjRef(const ObjRef &ref)=default
~ObjContainer() noexcept(false)
ObjPtr & operator=(ObjPtr &&obj)
static void throwInvalidObject(size_t idx, size_t n_objs)
Define macros for attributes used to control the static checker.
bool operator!=(const ObjRef &obj) const
setBGCode setTAP setLVL2ErrorBits bool
ObjRef share(ObjRef ref)
Increase the share count of a given object.
ObjRef m_ref
a valid reference to an object stored in the container or an invalid reference.
ObjContainer(unsigned int min_size=0)
static constexpr unsigned short invalid()
ObjRef index() const
Get a light-weight reference to the object pointed to by this pointer The returned light-weight refer...
Helper classes to manage shared objects in a scope.