FEDEM Model Database  R8.0
Source code of the FEDEM model database
FFaReferenceList.H
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2023 SAP SE
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 // This file is part of FEDEM - https://openfedem.org
7 
13 #ifndef FFA_REFERENCE_LIST_H
14 #define FFA_REFERENCE_LIST_H
15 
16 #include <list>
17 
19 
20 
34 {
39 
40 protected:
42  FFaReferenceListBase() : myOwner(NULL), myContext(NULL), autoSizing(true) {}
44  virtual ~FFaReferenceListBase() { this->clear(); }
45 
46 public:
48  void setAutoSizing(bool onOrOff) { autoSizing = onOrOff; }
50  bool isAutoSizing() const { return autoSizing; }
51 
53  void setContextName(const char* name) { myContext = name; }
55  const char* getContextName() const { return myContext ? myContext : ""; }
56 
58  bool erase(int index);
60  void clear();
61 
63  bool shuffle(size_t newFirst);
65  void reverse() { myRefs.reverse(); }
67  void removePtr (const FFaFieldContainer* ptr) { this->removePtr(ptr,true); }
69  void zeroOutPtr(const FFaFieldContainer* ptr) { this->zeroOutPtr(ptr,true); }
71  bool hasPtr(const FFaFieldContainer* ptr, int* index = NULL) const;
72 
74  void getBasePtrs(std::vector<FFaFieldContainer*>& toFill) const;
76  FFaFieldContainer* getBasePtr(int idx) const;
77 
79  size_t size() const { return myRefs.size(); }
81  bool empty() const { return myRefs.empty(); }
82 
84  void write(std::ostream& os) const;
86  void read(std::istream& is);
87 
88 protected:
90  using FFaReferenceList = std::list<FFaReferenceBase*>;
91 
93 
97  virtual FFaReferenceBase* createNewReference() const = 0;
98 
100  FFaReferenceList::const_iterator findPtr(const FFaFieldContainer* ptr) const;
102  FFaReferenceList::iterator findPtr(const FFaFieldContainer* ptr);
103 
106  FFaReferenceBase* getRefBase(int idx) const;
107 
110 
112  void copy(const FFaReferenceListBase* other, bool unresolve = false);
113 
114 private:
116  friend class FFaFieldContainer;
118  friend class FFaReferenceBase;
119 
124 
127 
129  void resolve(FFaSearcher& findCB);
131  void unresolve();
133  void updateAssemblyRef(int from, int to, size_t ind = 0);
135  void updateAssemblyRef(const IntVec& from, const IntVec& to);
136 
138  void removePtr (const FFaFieldContainer* ptr, bool notifyContainer);
140  void zeroOutPtr(const FFaFieldContainer* ptr, bool notifyContainer);
141 
143  const char* myContext;
144 
148 };
149 
150 
192 template<class Type>
194 {
195 public:
197  FFaReferenceList<Type>& operator=(const std::vector<Type*>& ptrs)
198  { this->setPtrs(ptrs); return *this; }
199 
201  void push_front(Type* ptr, bool insertNullToo = false)
202  { if (ptr || insertNullToo) myRefs.push_front(this->makeNewRef(ptr)); }
203 
205  void push_back(Type* ptr, bool insertNullToo = false)
206  { if (ptr || insertNullToo) myRefs.push_back(this->makeNewRef(ptr)); }
207 
211  void insertPtrAfter(Type* ptr, Type* objInList = NULL)
212  {
213  std::list<FFaReferenceBase*>::iterator it = myRefs.end();
214  if (objInList)
215  it = this->findPtr(static_cast<const FFaFieldContainer*>(objInList));
216 
217  if (it != myRefs.end())
218  myRefs.insert(++it,this->makeNewRef(ptr));
219  else
220  this->push_back(ptr);
221  }
222 
226  void insert(Type* ptr, size_t idx = 0)
227  {
228  size_t i = 0;
229  std::list<FFaReferenceBase*>::iterator it;
230  for (it = myRefs.begin(); it != myRefs.end(); i++, it++)
231  if (i == idx)
232  {
233  myRefs.insert(it,this->makeNewRef(ptr));
234  return;
235  }
236 
237  this->push_back(ptr);
238  }
239 
241  void getPtrs(std::vector<Type*>& toFill, bool includeNullToo = false) const
242  {
243  toFill.clear();
244  for (FFaReferenceBase* ref : myRefs)
245  if (includeNullToo || !ref->isNull())
246  toFill.push_back(static_cast<FFaReference<Type>*>(ref)->getPointer());
247  }
248 
250  void setPtrs(const std::vector<Type*>& ptrs)
251  {
252  this->clear();
253  for (Type* ptr : ptrs)
254  myRefs.push_back(this->makeNewRef(ptr));
255  }
256 
258  void setRefs(const IntVec& IDs)
259  {
260  this->clear();
261  for (int id : IDs)
262  myRefs.push_back(this->makeNewRef(NULL,id));
263  }
264 
276  {
277  FFaReferenceBase* refp = this->getRefBase(idx);
278  if (refp) return *static_cast<FFaReference<Type>*>(refp);
279 
280  std::cerr <<"FFaReference::operator[]: Index "<< idx
281  <<" out of range [0,"<< this->size() <<"]"<< std::endl;
282  static FFaReference<Type> dummy;
283  return dummy;
284  }
285 
287  const FFaReference<Type>& operator[](int idx) const
288  {
289  FFaReferenceBase* refp = this->getRefBase(idx);
290  if (refp) return *static_cast<const FFaReference<Type>*>(refp);
291 
292  std::cerr <<"FFaReference::operator[]: Index "<< idx
293  <<" out of range [0,"<< this->size() <<"]"<< std::endl;
294 
295  static FFaReference<Type> dummy;
296  return dummy;
297  }
298 
301  void setPtr(Type* ptr, int idx)
302  {
303  int i = 0;
304  std::list<FFaReferenceBase*>::iterator it;
305  for (it = myRefs.begin(); it != myRefs.end(); i++, it++)
306  if (i == idx)
307  {
308  *static_cast<FFaReference<Type>*>(*it) = ptr;
309  return;
310  }
311 
312  while (i++ < idx)
313  myRefs.push_back(this->makeNewRef(NULL));
314 
315  myRefs.push_back(this->makeNewRef(ptr));
316  }
317 
320  Type* getPtr(int idx) const
321  {
322  FFaReferenceBase* refp = this->getRefBase(idx);
323  if (refp) return static_cast<FFaReference<Type>*>(refp)->getPointer();
324 
325  return NULL;
326  }
327 
329  Type* getFirstPtr() const
330  {
331  if (myRefs.empty()) return NULL;
332 
333  return static_cast<FFaReference<Type>*>(myRefs.front())->getPointer();
334  }
335 
337  Type* getLastPtr() const
338  {
339  if (myRefs.empty()) return NULL;
340 
341  return static_cast<FFaReference<Type>*>(myRefs.back())->getPointer();
342  }
343 
344 private:
346  FFaReference<Type>* makeNewRef(Type* ptr, int ID = 0)
347  {
349  this->setOwnerOnRef(sr);
350  sr->setPointer(ptr);
351  if (ID) sr->setRef(ID,Type::getClassTypeID());
352  return sr;
353  }
354 
356  { return static_cast<FFaReferenceBase*>(new FFaReference<Type>()); }
357 };
358 
359 
360 // Specialized instantiations of FFaField methods.
361 
363 using FFaRefListPtr = FFaReferenceListBase*;
364 
365 template<> inline
367 {
368  myData = myDefaultValue = NULL;
369 }
370 
371 template<> inline bool
373 {
374  return false;
375 }
376 
377 template<> inline bool
379 {
380  return myData ? !myData->empty() : false;
381 }
382 
383 template<> inline void
384 FFaField<FFaRefListPtr>::write(std::ostream& os) const
385 {
386  if (myData) myData->write(os);
387 }
388 
389 template<> inline void
390 FFaField<FFaRefListPtr>::read(std::istream& is, FFaRefListPtr& v) const
391 {
392  if (v) v->read(is);
393 }
394 
396 #endif
std::vector< int > IntVec
Convenience type alias.
Definition: FFaField.H:25
Classes representing guarded pointers.
FFaDynCB4< FFaFieldContainer *&, int, int, const IntVec & > FFaSearcher
Callback searching for FFaFieldContainer objects when resolving references.
Definition: FFaReference.H:23
Base class for the FEDEM model database structure.
Definition: FFaFieldContainer.H:39
FFaField()
Empty default constructor.
Definition: FFaField.H:42
bool isPrintable() const override
Returns true, if this field should be printed.
Definition: FFaField.H:94
void read(std::istream &is) override
Reads the field value from an input stream.
Definition: FFaField.H:117
bool isDataField() const override
Returns true, if this is a numerical data field.
Definition: FFaField.H:93
void write(std::ostream &os) const override
Writes the field name and value to an output stream.
Definition: FFaField.H:116
Base class for the template class FFaReference for guarded pointers.
Definition: FFaReference.H:31
void setOwnerReferenceList(FFaReferenceListBase *)
Sets the FFaReferenceListBase that this reference is a member of.
Definition: FFaReference.C:599
bool setRef(FFaFieldContainer *ptr)
Sets the pointer value from a FFaFieldContainer pointer directly.
Definition: FFaReference.C:78
The base class for FFaReferenceList.
Definition: FFaReferenceList.H:34
void resolve(FFaSearcher &findCB)
Used after reading to translate IDs to actual pointers.
Definition: FFaReferenceList.C:207
const char * myContext
Typically, this will be the variable name.
Definition: FFaReferenceList.H:143
FFaReferenceList::const_iterator findPtr(const FFaFieldContainer *ptr) const
Returns a const_iterator matching a FFaFieldContainer pointer.
Definition: FFaReferenceList.C:84
FFaReferenceListBase & operator=(const FFaReferenceListBase &)=delete
Remove implicitly defined assignment operator,.
void setOwnerOnRef(FFaReferenceBase *pt)
Sets this list as the owner of the reference pt.
Definition: FFaReferenceList.H:109
FFaReferenceBase * getRefBase(int idx) const
Returns the pointer at index idx.
Definition: FFaReferenceList.C:128
void setOwnerFieldContainer(FFaFieldContainer *owner)
Sets the FFaFieldContainer that this reference list is a member of.
Definition: FFaReferenceList.H:121
FFaFieldContainer * getOwnerFieldContainer() const
Returns the FFaFieldContainer this reference list is a member of.
Definition: FFaReferenceList.H:123
void insertRefLast(FFaReferenceBase *ref)
Appends a reference to the end of myRefs.
Definition: FFaReferenceList.C:163
void updateAssemblyRef(int from, int to, size_t ind=0)
Updates the assembly reference when moving to another assembly.
Definition: FFaReferenceList.C:226
bool isAutoSizing() const
Returns the value of the autoSizing flag.
Definition: FFaReferenceList.H:50
void setContextName(const char *name)
Assigns the context name myContext for this reference.
Definition: FFaReferenceList.H:53
void setAutoSizing(bool onOrOff)
Sets the autoSizing flag.
Definition: FFaReferenceList.H:48
void write(std::ostream &os) const
Writes the reference list to specified output stream.
Definition: FFaReferenceList.C:240
bool shuffle(size_t newFirst)
Reorders the elements in the list.
Definition: FFaReferenceList.C:23
FFaReferenceListBase()
The default constructor initializes the members.
Definition: FFaReferenceList.H:42
void eraseReferenceIfNeeded(FFaReferenceBase *ref)
Removes a reference from the list.
Definition: FFaReferenceList.C:295
void clear()
Clears the reference list.
Definition: FFaReferenceList.C:187
void unresolve()
Does the opposite of resolve.
Definition: FFaReferenceList.C:219
FFaFieldContainer * getBasePtr(int idx) const
Returns the pointer to the idx'th member in the list.
Definition: FFaReferenceList.C:138
void getBasePtrs(std::vector< FFaFieldContainer * > &toFill) const
Fills provided vector with the non-NULL pointers in the list.
Definition: FFaReferenceList.C:198
void zeroOutPtr(const FFaFieldContainer *ptr)
Sets all instances of ptr in this list to zero.
Definition: FFaReferenceList.H:69
virtual ~FFaReferenceListBase()
The destructor clears the list.
Definition: FFaReferenceList.H:44
void read(std::istream &is)
Reads the reference list from specified input stream.
Definition: FFaReferenceList.C:255
bool erase(int index)
Erases entry index from the list, decrementing its size by one.
Definition: FFaReferenceList.C:172
virtual FFaReferenceBase * createNewReference() const =0
Virtual method for creating a FFaReference of correct type.
bool empty() const
Returns true if myRefs is empty (zero length).
Definition: FFaReferenceList.H:81
void copy(const FFaReferenceListBase *other, bool unresolve=false)
Copy method used by copy constructors, etc.
Definition: FFaReferenceList.C:145
void removePtr(const FFaFieldContainer *ptr)
Removes all instances of ptr from this list.
Definition: FFaReferenceList.H:67
FFaReferenceList myRefs
The actual list of references.
Definition: FFaReferenceList.H:92
FFaReferenceListBase(const FFaReferenceListBase &)=delete
Remove implicitly defined copy constructor.
size_t size() const
Returns the length of myRefs,.
Definition: FFaReferenceList.H:79
bool autoSizing
Definition: FFaReferenceList.H:147
bool hasPtr(const FFaFieldContainer *ptr, int *index=NULL) const
Returns true if ptr is present in the list.
Definition: FFaReferenceList.C:113
void reverse()
Inverts the element order in the list.
Definition: FFaReferenceList.H:65
const char * getContextName() const
Returns the context name myContext used by this reference.
Definition: FFaReferenceList.H:55
FFaFieldContainer * myOwner
Points to the owner object.
Definition: FFaReferenceList.H:142
A container of FFaReference objects (guarded pointers).
Definition: FFaReferenceList.H:194
void insertPtrAfter(Type *ptr, Type *objInList=NULL)
Insert ptr after objInList in the list.
Definition: FFaReferenceList.H:211
void push_back(Type *ptr, bool insertNullToo=false)
Inserts ptr at the end of the list.
Definition: FFaReferenceList.H:205
FFaReference< Type > & operator[](int idx)
Indexing operator (for update).
Definition: FFaReferenceList.H:275
const FFaReference< Type > & operator[](int idx) const
Indexing operator (for read only).
Definition: FFaReferenceList.H:287
void push_front(Type *ptr, bool insertNullToo=false)
Inserts ptr in the front of the list.
Definition: FFaReferenceList.H:201
void setPtrs(const std::vector< Type * > &ptrs)
Replaces the current contents with the provided vector of pointers.
Definition: FFaReferenceList.H:250
void setPtr(Type *ptr, int idx)
Assigns ptr at position idx.
Definition: FFaReferenceList.H:301
Type * getFirstPtr() const
Returns the first pointer, or NULL if myRefs is empty.
Definition: FFaReferenceList.H:329
FFaReferenceBase * createNewReference() const final
Virtual method for creating a FFaReference of correct type.
Definition: FFaReferenceList.H:355
Type * getLastPtr() const
Returns the last pointer, or NULL if myRefs is empty.
Definition: FFaReferenceList.H:337
void setRefs(const IntVec &IDs)
Replaces the current contents with the provided new references.
Definition: FFaReferenceList.H:258
FFaReferenceList< Type > & operator=(const std::vector< Type * > &ptrs)
Overloaded assignment operator.
Definition: FFaReferenceList.H:197
FFaReference< Type > * makeNewRef(Type *ptr, int ID=0)
Convenience method to create and set up a new reference properly.
Definition: FFaReferenceList.H:346
Type * getPtr(int idx) const
Returns the pointer at position idx.
Definition: FFaReferenceList.H:320
void getPtrs(std::vector< Type * > &toFill, bool includeNullToo=false) const
Fills the provided vector with the pointers contained in the list.
Definition: FFaReferenceList.H:241
void insert(Type *ptr, size_t idx=0)
Inserts ptr at position idx in the list.
Definition: FFaReferenceList.H:226
A "guarded pointer" like class.
Definition: FFaReference.H:221
bool setPointer(Type *ptr)
Assigns ptr to this reference.
Definition: FFaReference.H:227