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

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

#include <SharingTagSet.h>

Inheritance diagram for inet::SharingTagSet:

Public Member Functions

Constructors and operators
 SharingTagSet ()
 
 SharingTagSet (const SharingTagSet &other)
 
 SharingTagSet (SharingTagSet &&other)
 
SharingTagSetoperator= (const SharingTagSet &other)
 
SharingTagSetoperator= (SharingTagSet &&other)
 
virtual void parsimPack (cCommBuffer *buffer) const override
 
virtual void parsimUnpack (cCommBuffer *buffer) override
 
Type independent functions
int getNumTags () const
 Returns the number of tags. More...
 
const Ptr< const TagBase > & getTag (int index) const
 Returns the shared tag at the given index. More...
 
const Ptr< TagBasegetTagForUpdate (int index)
 Returns the exclusively owned tag at the given index for update. More...
 
void clearTags ()
 Clears the set of tags. More...
 
void copyTags (const SharingTagSet &other)
 Copies the set of tags from the other set. More...
 
Type dependent functions
template<typename T >
const Ptr< const TfindTag () const
 Returns the shared tag of the provided type, or returns nullptr if no such tag is present. More...
 
template<typename T >
const Ptr< TfindTagForUpdate ()
 Returns the exclusively owned tag of the provided type for update, or returns nullptr if no such tag is present. More...
 
template<typename T >
const Ptr< const TgetTag () const
 Returns the shared tag of the provided type, or throws an exception if no such tag is present. More...
 
template<typename T >
const Ptr< TgetTagForUpdate ()
 Returns the exclusively owned tag of the provided type for update, or throws an exception if no such tag is present. More...
 
template<typename T >
const Ptr< TaddTag ()
 Returns a newly added exclusively owned tag of the provided type for update, or throws an exception if such a tag is already present. More...
 
template<typename T >
const Ptr< TaddTagIfAbsent ()
 Returns a newly added exclusively owned tag of the provided type if absent, or returns the exclusively owned tag that is already present for update. More...
 
template<typename T >
const Ptr< TremoveTag ()
 Removes the tag of the provided type and returns it for update, or throws an exception if no such tag is present. More...
 
template<typename T >
const Ptr< TremoveTagIfPresent ()
 Removes the tag of the provided type if present and returns it for update, or returns nullptr if no such tag is present. More...
 

Protected Member Functions

void setTag (int index, const Ptr< const TagBase > &tag)
 
void addTag (const Ptr< const TagBase > &tag)
 
const Ptr< TagBaseremoveTag (int index)
 
int getTagIndex (const std::type_info &typeInfo) const
 
template<typename T >
int getTagIndex () const
 
void ensureTagsVectorAllocated ()
 
void prepareTagsVectorForUpdate ()
 

Protected Attributes

Ptr< SharedVector< Ptr< const TagBase > > > tags
 Both the vector and the individual tags can be shared among different instances of this class. More...
 

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. Tags are shared between other instances of this class. Tags can be changed with a copy-on-write mechanism.

Constructor & Destructor Documentation

◆ SharingTagSet() [1/3]

inet::SharingTagSet::SharingTagSet ( )
inline
56 : tags(nullptr) {}

◆ SharingTagSet() [2/3]

inet::SharingTagSet::SharingTagSet ( const SharingTagSet other)
inline
57 : tags(other.tags) {}

◆ SharingTagSet() [3/3]

inet::SharingTagSet::SharingTagSet ( SharingTagSet &&  other)
inline
58 : tags(other.tags) { other.tags = nullptr; }

Member Function Documentation

◆ addTag() [1/2]

template<typename T >
const Ptr< T > inet::SharingTagSet::addTag
inline

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

240 {
242  int index = getTagIndex<T>();
243  if (index != -1)
244  throw cRuntimeError("Tag '%s' is present", opp_typename(typeid(T)));
245  const Ptr<T>& tag = makeShared<T>();
246  addTag(tag);
247  return tag;
248 }

Referenced by addTagIfAbsent(), and parsimUnpack().

◆ addTag() [2/2]

void inet::SharingTagSet::addTag ( const Ptr< const TagBase > &  tag)
protected
29 {
32  tags->push_back(tag);
33 }

◆ addTagIfAbsent()

template<typename T >
const Ptr< T > inet::SharingTagSet::addTagIfAbsent
inline

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

252 {
254  const Ptr<T>& tag = findTagForUpdate<T>();
255  if (tag != nullptr)
256  return tag;
257  else {
258  const Ptr<T>& tag = makeShared<T>();
259  addTag(tag);
260  return tag;
261  }
262 }

◆ clearTags()

void inet::SharingTagSet::clearTags ( )
inline

Clears the set of tags.

184 {
185 #ifdef INET_WITH_SELFDOC
186  selfDoc(__FUNCTION__, "");
187 #endif // INET_WITH_SELFDOC
188  tags = nullptr;
189 }

Referenced by parsimUnpack().

◆ copyTags()

void inet::SharingTagSet::copyTags ( const SharingTagSet other)
inline

Copies the set of tags from the other set.

192 {
193  operator=(source);
194 }

◆ ensureTagsVectorAllocated()

void inet::SharingTagSet::ensureTagsVectorAllocated ( )
protected
62 {
63  if (tags == nullptr) {
64  tags = makeShared<SharedVector<Ptr<const TagBase>>>();
65  tags->reserve(16);
66  }
67 }

Referenced by addTag().

◆ findTag()

template<typename T >
const Ptr< const T > inet::SharingTagSet::findTag
inline

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

204 {
206  int index = getTagIndex<T>();
207  return index == -1 ? nullptr : staticPtrCast<const T>(getTag(index));
208 }

Referenced by inet::SctpNatServer::handleMessage(), inet::SctpPeer::handleMessage(), inet::SctpNatPeer::handleMessage(), inet::SctpServer::makeReceiveRequest(), inet::SctpPeer::socketDataNotificationArrived(), and inet::SctpNatPeer::socketDataNotificationArrived().

◆ findTagForUpdate()

template<typename T >
const Ptr< T > inet::SharingTagSet::findTagForUpdate
inline

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

212 {
214  int index = getTagIndex<T>();
215  return index == -1 ? nullptr : staticPtrCast<T>(getTagForUpdate(index));
216 }

◆ getNumTags()

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

Returns the number of tags.

160 {
161  return tags == nullptr ? 0 : tags->size();
162 }

Referenced by inet::Packet::forEachChild(), and parsimPack().

◆ getTag() [1/2]

template<typename T >
const Ptr< const T > inet::SharingTagSet::getTag
inline

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

220 {
222  int index = getTagIndex<T>();
223  if (index == -1)
224  throw cRuntimeError("Tag '%s' is absent", opp_typename(typeid(T)));
225  return staticPtrCast<const T>(getTag(index));
226 }

Referenced by findTag(), and getTagForUpdate().

◆ getTag() [2/2]

const Ptr< const TagBase > & inet::SharingTagSet::getTag ( int  index) const
inline

Returns the shared tag at the given index.

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

170 {
171  return (*tags)[index];
172 }

Referenced by inet::Packet::forEachChild().

◆ getTagForUpdate() [1/2]

template<typename T >
const Ptr< T > inet::SharingTagSet::getTagForUpdate
inline

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

230 {
232  int index = getTagIndex<T>();
233  if (index == -1)
234  throw cRuntimeError("Tag '%s' is absent", opp_typename(typeid(T)));
235  return staticPtrCast<T>(getTagForUpdate(index));
236 }

Referenced by findTagForUpdate(), and removeTag().

◆ getTagForUpdate() [2/2]

const Ptr< TagBase > inet::SharingTagSet::getTagForUpdate ( int  index)
inline

Returns the exclusively owned tag at the given index for update.

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

175 {
176  const Ptr<const TagBase>& tag = getTag(index);
178  if (tag.use_count() != 1)
179  setTag(index, tag->dupShared());
180  return constPtrCast<TagBase>(getTag(index));
181 }

◆ getTagIndex() [1/2]

template<typename T >
int inet::SharingTagSet::getTagIndex
inlineprotected
198 {
199  return getTagIndex(typeid(T));
200 }

◆ getTagIndex() [2/2]

int inet::SharingTagSet::getTagIndex ( const std::type_info &  typeInfo) const
protected
46 {
47  if (tags == nullptr)
48  return -1;
49  else {
50  int numTags = tags->size();
51  for (int index = 0; index < numTags; index++) {
52  const Ptr<const TagBase>& tag = (*tags)[index];
53  const TagBase *tagObject = tag.get();
54  if (typeInfo == typeid(*tagObject))
55  return index;
56  }
57  return -1;
58  }
59 }

◆ operator=() [1/2]

SharingTagSet & inet::SharingTagSet::operator= ( const SharingTagSet other)
inline
144 {
145  if (this != &other)
146  tags = other.tags;
147  return *this;
148 }

Referenced by copyTags().

◆ operator=() [2/2]

SharingTagSet & inet::SharingTagSet::operator= ( SharingTagSet &&  other)
inline
151 {
152  if (this != &other) {
153  tags = other.tags;
154  other.tags = nullptr;
155  }
156  return *this;
157 }

◆ parsimPack()

void inet::SharingTagSet::parsimPack ( cCommBuffer *  buffer) const
overridevirtual
79 {
80  buffer->pack(getNumTags());
81  if (tags != nullptr)
82  for (auto tag : *tags)
83  buffer->packObject(const_cast<TagBase *>(tag.get()));
84 }

Referenced by inet::Packet::parsimPack().

◆ parsimUnpack()

void inet::SharingTagSet::parsimUnpack ( cCommBuffer *  buffer)
overridevirtual
87 {
88  clearTags();
89  int size;
90  buffer->unpack(size);
91  for (int i = 0; i < size; i++) {
92  auto tag = check_and_cast<TagBase *>(buffer->unpackObject());
93  addTag(tag->shared_from_this());
94  }
95 }

Referenced by inet::Packet::parsimUnpack().

◆ prepareTagsVectorForUpdate()

void inet::SharingTagSet::prepareTagsVectorForUpdate ( )
protected
70 {
71  if (tags.use_count() != 1) {
72  const auto& newTags = makeShared<SharedVector<Ptr<const TagBase>>>();
73  newTags->insert(newTags->begin(), tags->begin(), tags->end());
74  tags = newTags;
75  }
76 }

Referenced by addTag(), getTagForUpdate(), and removeTag().

◆ removeTag() [1/2]

template<typename T >
const Ptr< T > inet::SharingTagSet::removeTag
inline

Removes the tag of the provided type and returns it for update, or throws an exception if no such tag is present.

266 {
268  int index = getTagIndex<T>();
269  if (index == -1)
270  throw cRuntimeError("Tag '%s' is absent", opp_typename(typeid(T)));
271  return staticPtrCast<T>(removeTag(index));
272 }

Referenced by removeTagIfPresent().

◆ removeTag() [2/2]

const Ptr< TagBase > inet::SharingTagSet::removeTag ( int  index)
protected
36 {
38  const Ptr<TagBase> tag = getTagForUpdate(index);
39  tags->erase(tags->begin() + index);
40  if (tags->size() == 0)
41  tags = nullptr;
42  return constPtrCast<TagBase>(tag);
43 }

◆ removeTagIfPresent()

template<typename T >
const Ptr< T > inet::SharingTagSet::removeTagIfPresent
inline

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

276 {
278  int index = getTagIndex<T>();
279  return index == -1 ? nullptr : staticPtrCast<T>(removeTag(index));
280 }

◆ setTag()

void inet::SharingTagSet::setTag ( int  index,
const Ptr< const TagBase > &  tag 
)
inlineprotected
165 {
166  (*tags)[index] = tag;
167 }

Referenced by getTagForUpdate().

Member Data Documentation

◆ tags

Ptr<SharedVector<Ptr<const TagBase> > > inet::SharingTagSet::tags
protected

Both the vector and the individual tags can be shared among different instances of this class.

Referenced by addTag(), clearTags(), ensureTagsVectorAllocated(), getNumTags(), getTag(), getTagIndex(), operator=(), parsimPack(), prepareTagsVectorForUpdate(), and removeTag().


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::SharingTagSet::operator=
SharingTagSet & operator=(const SharingTagSet &other)
Definition: SharingTagSet.h:143
inet::SharingTagSet::getTagForUpdate
const Ptr< T > getTagForUpdate()
Returns the exclusively owned tag of the provided type for update, or throws an exception if no such ...
Definition: SharingTagSet.h:229
inet::SharingTagSet::prepareTagsVectorForUpdate
void prepareTagsVectorForUpdate()
Definition: SharingTagSet.cc:69
inet::SharingTagSet::getTagIndex
int getTagIndex() const
Definition: SharingTagSet.h:197
inet::SharingTagSet::setTag
void setTag(int index, const Ptr< const TagBase > &tag)
Definition: SharingTagSet.h:164
inet::SharingTagSet::removeTag
const Ptr< T > removeTag()
Removes the tag of the provided type and returns it for update, or throws an exception if no such tag...
Definition: SharingTagSet.h:265
inet::SharingTagSet::ensureTagsVectorAllocated
void ensureTagsVectorAllocated()
Definition: SharingTagSet.cc:61
inet::SharingTagSet::getTag
const Ptr< const T > getTag() const
Returns the shared tag of the provided type, or throws an exception if no such tag is present.
Definition: SharingTagSet.h:219
inet::SharingTagSet::tags
Ptr< SharedVector< Ptr< const TagBase > > > tags
Both the vector and the individual tags can be shared among different instances of this class.
Definition: SharingTagSet.h:40
inet::SharingTagSet::addTag
const Ptr< T > addTag()
Returns a newly added exclusively owned tag of the provided type for update, or throws an exception i...
Definition: SharingTagSet.h:239
inet::SharingTagSet::clearTags
void clearTags()
Clears the set of tags.
Definition: SharingTagSet.h:183
inet::SharingTagSet::getNumTags
int getNumTags() const
Returns the number of tags.
Definition: SharingTagSet.h:159
SELFDOC_FUNCTION_T
#define SELFDOC_FUNCTION_T
Definition: SharingTagSet.h:24