INET Framework for OMNeT++/OMNEST
inet::TagSet Class Reference

This class maintains a set of tags. More...

#include <TagSet.h>

Inheritance diagram for inet::TagSet:

Public Member Functions

 TagSet ()
 
 TagSet (const TagSet &other)
 
 TagSet (TagSet &&other)
 
 ~TagSet ()
 
TagSetoperator= (const TagSet &other)
 
TagSetoperator= (TagSet &&other)
 
int getNumTags () const
 Returns the number of tags. More...
 
cObject * getTag (int index) const
 Returns the tag at the given index. More...
 
void clearTags ()
 Clears the set of tags. More...
 
void copyTags (const TagSet &other)
 Copies the set of tags from the other set. More...
 
template<typename T >
const TfindTag () const
 Returns the tag for the provided type, or returns nullptr if no such tag is present. More...
 
template<typename T >
TfindTagForUpdate ()
 Returns the tag of the provided type for update, or returns nullptr if no such tag is present. More...
 
template<typename T >
const TgetTag () const
 Returns the tag for the provided type, or throws an exception if no such tag is present. More...
 
template<typename T >
TgetTagForUpdate ()
 Returns the tag of the provided type for update, or throws an exception if no such tag is present. More...
 
template<typename T >
TaddTag ()
 Returns a newly added tag for the provided type, or throws an exception if such a tag is already present. More...
 
template<typename T >
TaddTagIfAbsent ()
 Returns a newly added tag for the provided type if absent, or returns the tag that is already present. More...
 
template<typename T >
TremoveTag ()
 Removes the tag for the provided type, or throws an exception if no such tag is present. More...
 
template<typename T >
TremoveTagIfPresent ()
 Removes the tag for the provided type if present, or returns nullptr if no such tag is present. More...
 

Protected Member Functions

void ensureAllocated ()
 
void addTag (cObject *tag)
 
cObject * removeTag (int index)
 
int getTagIndex (const std::type_info &typeInfo) const
 
template<typename T >
int getTagIndex () const
 

Protected Attributes

std::vector< cObject * > * tags
 

Detailed Description

This class maintains a set of tags.

Tags are usually small data strcutures that hold some relevant information. Tags are identified by their type, which means that this class supports adding the same tag type only once. Added tags are exclusively owned by this class and they get deleted with it.

Constructor & Destructor Documentation

◆ TagSet() [1/3]

inet::TagSet::TagSet ( )
28  :
29  tags(nullptr)
30 {
31 }

◆ TagSet() [2/3]

inet::TagSet::TagSet ( const TagSet other)
33  :
34  tags(nullptr)
35 {
36  operator=(other);
37 }

◆ TagSet() [3/3]

inet::TagSet::TagSet ( TagSet &&  other)
39  :
40  tags(other.tags)
41 {
42  other.tags = nullptr;
43 }

◆ ~TagSet()

inet::TagSet::~TagSet ( )
46 {
47  clearTags();
48 }

Member Function Documentation

◆ addTag() [1/2]

template<typename T >
T * inet::TagSet::addTag
inline

Returns a newly added tag for the provided type, or throws an exception if such a tag is already present.

167 {
169  int index = getTagIndex<T>();
170  if (index != -1)
171  throw cRuntimeError("Tag '%s' is present", opp_typename(typeid(T)));
172  T *tag = new T();
173  addTag(tag);
174  return tag;
175 }

Referenced by addTagIfAbsent(), and operator=().

◆ addTag() [2/2]

void inet::TagSet::addTag ( cObject *  tag)
protected
84 {
86  tags->push_back(tag);
87  if (tag->isOwnedObject())
88  take(static_cast<cOwnedObject *>(tag));
89 }

Referenced by inet::NetworkInterface::addProtocolData().

◆ addTagIfAbsent()

template<typename T >
T * inet::TagSet::addTagIfAbsent
inline

Returns a newly added tag for the provided type if absent, or returns the tag that is already present.

179 {
181  T *tag = findTagForUpdate<T>();
182  if (tag == nullptr)
183  addTag(tag = new T());
184  return tag;
185 }

Referenced by inet::NetworkInterface::addProtocolDataIfAbsent().

◆ clearTags()

void inet::TagSet::clearTags ( )

Clears the set of tags.

105 {
106 #ifdef INET_WITH_SELFDOC
107  selfDoc(__FUNCTION__, "");
109 #endif // INET_WITH_SELFDOC
110  if (tags != nullptr) {
111  int numTags = tags->size();
112  for (int index = 0; index < numTags; index++) {
113  cObject *tag = (*tags)[index];
114  if (tag->isOwnedObject())
115  drop(static_cast<cOwnedObject *>(tag));
116  delete tag;
117  }
118  delete tags;
119  tags = nullptr;
120  }
121 }

Referenced by inet::NetworkInterface::clearProtocolDataSet(), copyTags(), operator=(), inet::NetworkInterface::resetInterface(), and ~TagSet().

◆ copyTags()

void inet::TagSet::copyTags ( const TagSet other)

Copies the set of tags from the other set.

124 {
125  clearTags();
126  if (source.tags != nullptr) {
127  int numTags = source.tags->size();
128  tags = new std::vector<cObject *>(numTags);
129  for (int index = 0; index < numTags; index++) {
130  cObject *tag = (*source.tags)[index]->dup();
131  if (tag->isOwnedObject())
132  take(static_cast<cOwnedObject *>(tag));
133  (*tags)[index] = tag;
134  }
135  }
136 }

◆ ensureAllocated()

void inet::TagSet::ensureAllocated ( )
protected
76 {
77  if (tags == nullptr) {
78  tags = new std::vector<cObject *>();
79  tags->reserve(16);
80  }
81 }

Referenced by addTag(), and operator=().

◆ findTag()

template<typename T >
const T * inet::TagSet::findTag
inline

Returns the tag for the provided type, or returns nullptr if no such tag is present.

135 {
137  int index = getTagIndex<T>();
138  return index == -1 ? nullptr : static_cast<T *>((*tags)[index]);
139 }

Referenced by inet::NetworkInterface::findProtocolData().

◆ findTagForUpdate()

template<typename T >
T * inet::TagSet::findTagForUpdate
inline

Returns the tag of the provided type for update, or returns nullptr if no such tag is present.

143 {
145  return const_cast<T *>(findTag<T>());
146 }

Referenced by inet::NetworkInterface::findProtocolDataForUpdate().

◆ getNumTags()

int inet::TagSet::getNumTags ( ) const
inline

Returns the number of tags.

118 {
119  return tags == nullptr ? 0 : tags->size();
120 }

Referenced by inet::NetworkInterface::clearProtocolDataSet(), inet::NetworkInterface::getNumProtocolData(), and inet::NetworkInterface::str().

◆ getTag() [1/2]

template<typename T >
const T * inet::TagSet::getTag
inline

Returns the tag for the provided type, or throws an exception if no such tag is present.

150 {
152  int index = getTagIndex<T>();
153  if (index == -1)
154  throw cRuntimeError("Tag '%s' is absent", opp_typename(typeid(T)));
155  return static_cast<T *>((*tags)[index]);
156 }

◆ getTag() [2/2]

cObject * inet::TagSet::getTag ( int  index) const
inline

Returns the tag at the given index.

The index must be in the range [0, getNumTags()).

123 {
124  return tags->at(index);
125 }

Referenced by inet::NetworkInterface::clearProtocolDataSet(), inet::NetworkInterface::getProtocolData(), and inet::NetworkInterface::str().

◆ getTagForUpdate()

template<typename T >
T * inet::TagSet::getTagForUpdate
inline

Returns the tag of the provided type for update, or throws an exception if no such tag is present.

160 {
162  return const_cast<T *>(getTag<T>());
163 }

Referenced by inet::NetworkInterface::getProtocolDataForUpdate().

◆ getTagIndex() [1/2]

template<typename T >
int inet::TagSet::getTagIndex
inlineprotected
129 {
130  return getTagIndex(typeid(T));
131 }

◆ getTagIndex() [2/2]

int inet::TagSet::getTagIndex ( const std::type_info &  typeInfo) const
protected
139 {
140  if (tags == nullptr)
141  return -1;
142  else {
143  int numTags = tags->size();
144  for (int index = 0; index < numTags; index++) {
145  cObject *tag = (*tags)[index];
146  if (typeInfo == typeid(*tag))
147  return index;
148  }
149  return -1;
150  }
151 }

◆ operator=() [1/2]

TagSet & inet::TagSet::operator= ( const TagSet other)
51 {
52  if (this != &other) {
53  clearTags();
54  if (other.tags == nullptr)
55  tags = nullptr;
56  else {
58  for (auto tag : *other.tags)
59  addTag(tag->dup());
60  }
61  }
62  return *this;
63 }

Referenced by TagSet().

◆ operator=() [2/2]

TagSet & inet::TagSet::operator= ( TagSet &&  other)
66 {
67  if (this != &other) {
68  clearTags();
69  tags = other.tags;
70  other.tags = nullptr;
71  }
72  return *this;
73 }

◆ removeTag() [1/2]

template<typename T >
T * inet::TagSet::removeTag
inline

Removes the tag for the provided type, or throws an exception if no such tag is present.

189 {
191  int index = getTagIndex<T>();
192  if (index == -1)
193  throw cRuntimeError("Tag '%s' is absent", opp_typename(typeid(T)));
194  return static_cast<T *>(removeTag(index));
195 }

Referenced by removeTagIfPresent().

◆ removeTag() [2/2]

cObject * inet::TagSet::removeTag ( int  index)
protected
92 {
93  cObject *tag = (*tags)[index];
94  if (tag->isOwnedObject())
95  drop(static_cast<cOwnedObject *>(tag));
96  tags->erase(tags->begin() + index);
97  if (tags->size() == 0) {
98  delete tags;
99  tags = nullptr;
100  }
101  return tag;
102 }

Referenced by inet::NetworkInterface::removeProtocolData().

◆ removeTagIfPresent()

template<typename T >
T * inet::TagSet::removeTagIfPresent
inline

Removes the tag for the provided type if present, or returns nullptr if no such tag is present.

199 {
201  int index = getTagIndex<T>();
202  return index == -1 ? nullptr : static_cast<T *>(removeTag(index));
203 }

Referenced by inet::NetworkInterface::removeProtocolDataIfPresent().

Member Data Documentation

◆ tags

std::vector<cObject *>* inet::TagSet::tags
protected

The documentation for this class was generated from the following files:
inet::units::units::T
compose< Wb, pow< m, -2 > > T
Definition: Units.h:951
inet::TagSet::getTagIndex
int getTagIndex() const
Definition: TagSet.h:128
inet::TagSet::ensureAllocated
void ensureAllocated()
Definition: TagSet.cc:75
SELFDOC_FUNCTION_T
#define SELFDOC_FUNCTION_T
Definition: TagSet.h:20
inet::TagSet::tags
std::vector< cObject * > * tags
Definition: TagSet.h:32
inet::TagSet::removeTag
T * removeTag()
Removes the tag for the provided type, or throws an exception if no such tag is present.
Definition: TagSet.h:188
SelfDocTempOff
#define SelfDocTempOff
Definition: SelfDoc.h:50
inet::TagSet::addTag
T * addTag()
Returns a newly added tag for the provided type, or throws an exception if such a tag is already pres...
Definition: TagSet.h:166
inet::TagSet::operator=
TagSet & operator=(const TagSet &other)
Definition: TagSet.cc:50
inet::TagSet::clearTags
void clearTags()
Clears the set of tags.
Definition: TagSet.cc:104