DABC (Data Acquisition Backbone Core)  2.9.9
ReferencesVector.cxx
Go to the documentation of this file.
1 // $Id: ReferencesVector.cxx 4476 2020-04-15 14:12:38Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #include "dabc/ReferencesVector.h"
17 
18 #include <cstring>
19 
20 #include "dabc/Object.h"
21 
23  fVector(new refs_vector())
24 {
25  if (fVector->capacity() < 8) fVector->reserve(8);
26 }
27 
29 {
30  Clear();
31  delete fVector;
32  fVector = 0;
33 }
34 
36 {
37  if ((fVector->size() == 0) || (fVector->size() < fVector->capacity())) return;
38 
39  unsigned new_capacity = fVector->capacity()*2;
40 
41  refs_vector* vect = new refs_vector();
42  vect->reserve(new_capacity);
43 
44  // make shift, which does not require any locking
45  for (unsigned n=0;n<fVector->size();n++) {
46  vect->push_back(dabc::Reference());
47  vect->back() << fVector->at(n);
48  }
49 
50  fVector->clear();
51  delete fVector;
52  fVector = vect;
53 }
54 
55 
57 {
58  if (ref.GetObject()==0) return false;
59 
60  ExpandVector();
61 
62  fVector->push_back(Reference());
63 
64  // by such action no any locking is required -
65  fVector->back() << ref;
66 
67  return true;
68 }
69 
70 
71 bool dabc::ReferencesVector::AddAt(Reference& ref, unsigned pos) throw()
72 {
73  if (ref.GetObject()==0) return false;
74 
75  ExpandVector();
76 
77  fVector->push_back(Reference());
78 
79  if (pos >= fVector->size()) {
80  fVector->back() << ref;
81  } else {
82  for (unsigned n=fVector->size()-1; n>pos; n--)
83  fVector->at(n) << fVector->at(n-1);
84  fVector->at(pos) << ref;
85  }
86 
87  return true;
88 }
89 
90 
92 {
93  if ((obj==0) || (GetSize()==0)) return false;
94 
95  unsigned n = GetSize();
96  while (n-->0)
97  if (fVector->at(n).GetObject()==obj) RemoveAt(n);
98 
99  return true;
100 }
101 
102 
103 void dabc::ReferencesVector::RemoveAt(unsigned n) throw()
104 {
105  if (n>=fVector->size()) return;
106 
107  Reference ref;
108  ref << fVector->at(n);
109  ref.Release();
110 
111  for (unsigned indx = n; indx < fVector->size()-1;indx++)
112  fVector->at(indx) << fVector->at(indx+1);
113 
114  fVector->pop_back();
115 }
116 
118 {
119  dabc::Reference ref;
120 
121  ExtractRef(n, ref);
122 
123  return ref;
124 }
125 
126 
128 {
129  if (n>=GetSize()) return false;
130 
131  // by this action reference will be removed from the vector
132  ref << fVector->at(n);
133  RemoveAt(n);
134  return true;
135 }
136 
138 {
139  for (unsigned n=0;n<GetSize();n++)
140  if (fVector->at(n).GetObject() == obj)
141  return ExtractRef(n, ref);
142  return false;
143 }
144 
145 
146 
148 {
149  dabc::Reference ref;
150 
151  if (GetSize()>0) {
152  ref << fVector->back();
153  fVector->pop_back();
154  }
155 
156  return ref;
157 }
158 
159 
160 bool dabc::ReferencesVector::Clear(bool isowner) throw()
161 {
162  for (unsigned n=0;n<fVector->size();n++) {
163  dabc::Reference ref;
164  ref << fVector->at(n);
165  if (isowner) ref.Destroy();
166  else ref.Release();
167  }
168 
169  fVector->clear();
170 
171  return true;
172 }
173 
174 dabc::Object* dabc::ReferencesVector::FindObject(const char* name, int len) const
175 {
176  for (unsigned n=0; n<fVector->size(); n++) {
177  dabc::Object* obj = fVector->at(n).GetObject();
178  if (obj && obj->IsName(name, len)) return obj;
179  }
180  return 0;
181 }
182 
184 {
185  if (ptr==0) return false;
186  for (unsigned n=0; n<fVector->size(); n++)
187  if (fVector->at(n).GetObject() == ptr) return true;
188 
189  return false;
190 }
Base class for most of the DABC classes.
Definition: Object.h:116
bool IsName(const char *str) const
Checks if object name is same as provided string, thread safe
Definition: Object.h:298
Reference on the arbitrary object
Definition: Reference.h:73
void Release()
Releases reference on the object.
Definition: Reference.cxx:138
void Destroy()
Release reference and starts destroyment of referenced object.
Definition: Reference.cxx:148
Object * FindObject(const char *name, int len=-1) const
Simple search of object by name, no any subfolder structures.
virtual ~ReferencesVector()
Destructor.
bool HasObject(Object *ptr)
Return true if vector has pointer on the object.
void RemoveAt(unsigned n)
Remove reference on specified object
ReferencesVector()
Constructor.
Reference TakeLast()
Remove last reference from vector.
Reference TakeRef(unsigned n)
Remove reference from vector and return it to the user.
std::vector< Reference > refs_vector
refs_vector * fVector
vector with references
bool Clear(bool asowner=false)
Clear all references, if owner specified objects will be destroyed.
bool Add(Reference &ref)
Add reference to the vector.
bool Remove(Object *obj)
Remove reference on specified object
bool AddAt(Reference &ref, unsigned pos)
Add reference to the vector at specified position.
bool ExtractRef(unsigned n, Reference &ref)
Remove reference at specified position.