FEDEM Model Database  R8.0
Source code of the FEDEM model database
FFaReference.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_H
14 #define FFA_REFERENCE_H
15 
18 
19 class FFaFieldContainer;
21 
23 using FFaSearcher = FFaDynCB4<FFaFieldContainer*&,int,int,const IntVec&>;
24 
25 
31 {
36 
37 public:
41  virtual ~FFaReferenceBase();
42 
43  // Assigning
44 
46  bool setRef(FFaFieldContainer* ptr);
48  void setRef(int objId, int typeId);
50  void setRef(int objId, int typeId, const IntVec& assemblyIDs);
52  bool setPointerToNull() { return this->setRef(NULL); }
53 
55  void copy(const FFaReferenceBase& aReference, bool unresolve = false);
56 
57  // Access
58 
60  FFaFieldContainer* getRef() const;
62  bool isResolved() const { return IAmResolved; }
64  bool isNull() const { return IAmResolved && IAmBound ? !myPtr : true; }
65 
67  void setContextName(const char* name) { myContextName = name; }
69  const char* getContextName() const;
70 
71  // Type info access
72 
74  virtual int getRefClassTypeID() const { return -1; }
75 
77  const char* getRefTypeName() const;
79  int getRefTypeID() const;
82  int getRefID() const;
85  bool getRefAssemblyID(IntVec& assID) const;
86 
87  // IO
88 
90  void setPrintIfZero(bool doPrint) { printIfZero = doPrint; }
92  bool printMeIfZero() const { return printIfZero; }
93 
95  void write(std::ostream& os) const;
97  void read(std::istream& is);
98 
99 protected:
101  void resolve(FFaSearcher& findCB);
103  void unresolve();
105  void updateAssemblyRef(int from, int to, size_t ind = 0);
107  void updateAssemblyRef(const IntVec& from, const IntVec& to);
109  bool isEqual(const FFaReferenceBase* p) const;
110 
112  void bind();
114  void unbind();
116  void clearResolveRef();
117 
118 private:
120  friend class FFaFieldContainer;
122  friend class FFaReferenceListBase;
123 
125  void zeroOut();
128 
139 
141  void setRefID(int id);
143  void setRefTypeID(int id);
145  void setRefAssemblyID(const IntVec& assemblyID);
146 
148  using UnResolvedID = std::vector<int>;
149 
153  bool IAmBound;
156 
158  union {
161  };
162 
164  union {
167  };
168 
169  const char* myContextName;
170  bool printIfZero;
171 };
172 
173 
220 template<class Type> class FFaReference : public FFaReferenceBase
221 {
222 public:
223 
224  // Assigning
225 
227  bool setPointer(Type* ptr) { return this->setRef(ptr); }
229  FFaReference<Type>& operator=(Type* ptr) { this->setRef(ptr); return *this; }
230 
231  // Access
232 
234  Type* getPointer() const { return (Type*)this->getRef(); }
236  operator Type*() const { return this->getPointer(); }
238  Type* operator->() const { return this->getPointer(); }
240  Type& operator*() const { return *this->getPointer(); }
241 
243  bool operator==(const FFaReference<Type>& p) const { return this->isEqual(&p); }
245  bool operator!=(const FFaReference<Type>& p) const { return !this->isEqual(&p); }
246 
248  int getRefClassTypeID() const final { return Type::getClassTypeID(); }
249 };
250 
251 
252 // Specialized instantiations of FFaField methods.
253 // The pointer value in fields of reference pointers is not supposed to change.
254 
256 using FFaRefPtr = FFaReferenceBase*;
257 
258 
259 template<>
261 {
262  myData = myDefaultValue = NULL;
263 }
264 
265 template<>
266 inline bool FFaField<FFaRefPtr>::isDataField() const
267 {
268  return false;
269 }
270 
271 template<>
272 inline bool FFaField<FFaRefPtr>::isPrintable() const
273 {
274  return myData ? (myData->isNull() ? myData->printMeIfZero() : true) : false;
275 }
276 
277 template<>
278 inline void FFaField<FFaRefPtr>::write(std::ostream& os) const
279 {
280  if (myData) myData->write(os);
281 }
282 
283 template<>
284 inline void FFaField<FFaRefPtr>::read(std::istream& is, FFaRefPtr& v) const
285 {
286  if (v) v->read(is);
287 }
288 
290 #endif
Macros for definition of dynamic callback objects.
Template class for field representation in the FEDEM model database.
std::vector< int > IntVec
Convenience type alias.
Definition: FFaField.H:25
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 copy(const FFaReferenceBase &aReference, bool unresolve=false)
A copy method mostly used by FFaFieldContainer::copy().
Definition: FFaReference.C:486
FFaReferenceBase()
The default constructor initializes the members.
Definition: FFaReference.C:59
void write(std::ostream &os) const
Writes the reference to specified output stream.
Definition: FFaReference.C:339
FFaFieldContainer * getRef() const
Returns the FFaFieldContainer pointer contained.
Definition: FFaReference.C:123
bool isResolved() const
Returns whether this reference has been resolved or not.
Definition: FFaReference.H:62
void unbind()
Tells the referred object that it is not being referred anymore.
Definition: FFaReference.C:466
const char * getRefTypeName() const
Returns the type name of the referred object.
Definition: FFaReference.C:159
bool IAmResolved
false if we have an UnResolvedID object pointed to by myUnresolvedRef
Definition: FFaReference.H:151
void setOwnerReferenceList(FFaReferenceListBase *)
Sets the FFaReferenceListBase that this reference is a member of.
Definition: FFaReference.C:599
bool isNull() const
Returns whether this reference points to an actual object.
Definition: FFaReference.H:64
bool IAmBound
true if myPtr is pointing to a container, and the container knows it
Definition: FFaReference.H:153
void setRefAssemblyID(const IntVec &assemblyID)
Convenience method used when reading from file.
Definition: FFaReference.C:547
bool isEqual(const FFaReferenceBase *p) const
Checks for equality between two references.
Definition: FFaReference.C:615
void setRefID(int id)
Convenience method used when reading from file.
Definition: FFaReference.C:519
const char * myContextName
Typically, this will be the variable name.
Definition: FFaReference.H:169
void zeroOut()
Only used by the FFaReferenceListBase to clean a ToRef.
Definition: FFaReference.C:562
UnResolvedID * myUnresolvedRef
Id of referred object before resolving.
Definition: FFaReference.H:166
void bind()
Tells the object this reference is pointing at that it is referred.
Definition: FFaReference.C:452
FFaReferenceBase & operator=(const FFaReferenceBase &)=delete
Remove implicitly defined assignment operator.
bool setPointerToNull()
Clears the reference.
Definition: FFaReference.H:52
bool printIfZero
If true, print the reference also when NULL.
Definition: FFaReference.H:170
void setPrintIfZero(bool doPrint)
Flags whether this reference should be printed even if NULL.
Definition: FFaReference.H:90
void updateAssemblyRef(int from, int to, size_t ind=0)
Updates the assembly reference when moving to another assembly.
Definition: FFaReference.C:289
bool setRef(FFaFieldContainer *ptr)
Sets the pointer value from a FFaFieldContainer pointer directly.
Definition: FFaReference.C:78
int getRefTypeID() const
Returns the type id of the referred object.
Definition: FFaReference.C:168
FFaFieldContainer * myPtr
Points to the referred object when resolved.
Definition: FFaReference.H:165
void setRefTypeID(int id)
Convenience method used when reading from file.
Definition: FFaReference.C:533
FFaFieldContainer * getOwnerFieldContainer() const
Returns the FFaFieldContainer this reference is a member of.
Definition: FFaReference.C:588
FFaReferenceListBase * getOwnerReferenceList() const
Returns the FFaReferenceListBase this reference is a member of.
Definition: FFaReference.C:606
void clearResolveRef()
Deletes the variable used to hold temporary ID information.
Definition: FFaReference.C:475
bool getRefAssemblyID(IntVec &assID) const
Returns the assembly id array of the referred object.
Definition: FFaReference.C:190
virtual int getRefClassTypeID() const
Returns the type ID that this reference is supposed to refer to.
Definition: FFaReference.H:74
bool printMeIfZero() const
Returns if this reference should be printed even if NULL.
Definition: FFaReference.H:92
void resolve(FFaSearcher &findCB)
Finds the object that has matching Type and ID as read from file.
Definition: FFaReference.C:208
std::vector< int > UnResolvedID
Type of unique identification of the unresolved references.
Definition: FFaReference.H:148
void zeroOutOrRemoveFromList()
Only used by FFaFieldContainer to notify that it is obsolete.
Definition: FFaReference.C:572
void setOwnerFieldContainer(FFaFieldContainer *)
Sets the FFaFieldContainer that this reference is a member of.
Definition: FFaReference.C:581
virtual ~FFaReferenceBase()
The destructor clears the cross references.
Definition: FFaReference.C:67
FFaReferenceBase(const FFaReferenceBase &)=delete
Remove implicitly defined copy constructor.
const char * getContextName() const
Returns the context name used by this reference.
Definition: FFaReference.C:148
void read(std::istream &is)
Reads the reference from specified input stream.
Definition: FFaReference.C:360
void setContextName(const char *name)
Assigns the context name for this reference.
Definition: FFaReference.H:67
void unresolve()
Does the opposite of resolve.
Definition: FFaReference.C:268
bool IAmInAList
true if this reference is maintained by a FFaReferenceList
Definition: FFaReference.H:155
FFaReferenceListBase * myOwnerList
Points to the owner reference list.
Definition: FFaReference.H:160
FFaFieldContainer * myOwnerObj
Points to the owner object.
Definition: FFaReference.H:159
int getRefID() const
Returns the id number of the referred object.
Definition: FFaReference.C:179
The base class for FFaReferenceList.
Definition: FFaReferenceList.H:34
A "guarded pointer" like class.
Definition: FFaReference.H:221
FFaReference< Type > & operator=(Type *ptr)
Overloaded assignment operator.
Definition: FFaReference.H:229
int getRefClassTypeID() const final
Returns the class type ID this reference is instantiated over.
Definition: FFaReference.H:248
bool operator==(const FFaReference< Type > &p) const
Equality operator.
Definition: FFaReference.H:243
bool operator!=(const FFaReference< Type > &p) const
Inequality operator.
Definition: FFaReference.H:245
Type * getPointer() const
Returns the type-casted pointer this reference points to.
Definition: FFaReference.H:234
bool setPointer(Type *ptr)
Assigns ptr to this reference.
Definition: FFaReference.H:227
Type & operator*() const
Dereference operator.
Definition: FFaReference.H:240
Type * operator->() const
Pointer operator.
Definition: FFaReference.H:238