INET Framework for OMNeT++/OMNEST
inet::Chunk Class Referenceabstract

This class represents a piece of data that is usually part of a packet or some other data such as a protocol buffer. More...

#include <Chunk.h>

Inheritance diagram for inet::Chunk:
inet::IPrintableObject inet::IRegionTaggedObject inet::BitCountChunk inet::BitsChunk inet::ByteCountChunk inet::BytesChunk inet::cPacketChunk inet::EmptyChunk inet::EncryptedChunk inet::FieldsChunk inet::PacketDissector::ProtocolDataUnit inet::SequenceChunk inet::SliceChunk

Classes

class  BackwardIterator
 Position and index are measured from the end. More...
 
class  ForwardIterator
 Position and index are measured from the beginning. More...
 
class  Iterator
 

Public Types

enum  ChunkType {
  CT_EMPTY, CT_BITCOUNT, CT_BITS, CT_BYTECOUNT,
  CT_BYTES, CT_SLICE, CT_ENCRYPTED, CT_CPACKET,
  CT_SEQUENCE, CT_FIELDS
}
 This enum is used to avoid std::dynamic_cast and dynamicPtrCast. More...
 
enum  PeekFlag {
  PF_ALLOW_NULLPTR = (1 << 0), PF_ALLOW_EMPTY = (1 << 1), PF_ALLOW_INCOMPLETE = (1 << 2), PF_ALLOW_INCORRECT = (1 << 3),
  PF_ALLOW_IMPROPERLY_REPRESENTED = (1 << 4), PF_ALLOW_SERIALIZATION = (1 << 5), PF_ALLOW_ALL = -1
}
 This enum specifies bitmasks for the flags argument of various peek functions. More...
 
- Public Types inherited from inet::IPrintableObject
enum  PrintLevel {
  PRINT_LEVEL_TRACE, PRINT_LEVEL_DEBUG, PRINT_LEVEL_DETAIL, PRINT_LEVEL_INFO,
  PRINT_LEVEL_COMPLETE = INT_MIN
}
 
enum  PrintFlag { PRINT_FLAG_FORMATTED = (1 << 0), PRINT_FLAG_MULTILINE = (1 << 1) }
 

Public Member Functions

Constructors, destructors and duplication related functions
 Chunk ()
 
 Chunk (const Chunk &other)
 
virtual const Ptr< ChunkdupShared () const
 Returns a mutable copy of this chunk in a shared pointer. More...
 
virtual void forEachChild (cVisitor *v) override
 
virtual void parsimPack (cCommBuffer *buffer) const override
 
virtual void parsimUnpack (cCommBuffer *buffer) override
 
Mutability related functions
virtual bool isMutable () const
 
virtual bool isImmutable () const
 
virtual void markImmutable ()
 
void markMutableIfExclusivelyOwned ()
 
Completeness related functions
virtual bool isComplete () const
 
virtual bool isIncomplete () const
 
virtual void markIncomplete ()
 
Correctness related functions
virtual bool isCorrect () const
 
virtual bool isIncorrect () const
 
virtual void markIncorrect ()
 
Proper representation related functions
virtual bool isProperlyRepresented () const
 
virtual bool isImproperlyRepresented () const
 
virtual void markImproperlyRepresented ()
 
Iteration related functions
virtual void moveIterator (Iterator &iterator, b length) const
 
virtual void seekIterator (Iterator &iterator, b position) const
 
Inserting data related functions
virtual bool canInsertAtFront (const Ptr< const Chunk > &chunk) const
 Returns true if this chunk is capable of representing the result. More...
 
virtual bool canInsertAtBack (const Ptr< const Chunk > &chunk) const
 Returns true if this chunk is capable of representing the result. More...
 
virtual bool canInsertAt (const Ptr< const Chunk > &chunk, b offset) const
 Returns true if this chunk is capable of representing the result. More...
 
void insertAtFront (const Ptr< const Chunk > &chunk)
 Inserts the provided chunk at the beginning of this chunk. More...
 
void insertAtBack (const Ptr< const Chunk > &chunk)
 Inserts the provided chunk at the end of this chunk. More...
 
void insertAt (const Ptr< const Chunk > &chunk, b offset)
 Inserts the provided chunk into this chunk. More...
 
Removing data related functions
virtual bool canRemoveAtFront (b length) const
 Returns true if this chunk is capable of representing the result. More...
 
virtual bool canRemoveAtBack (b length) const
 Returns true if this chunk is capable of representing the result. More...
 
virtual bool canRemoveAt (b offset, b length) const
 Returns true if this chunk is capable of representing the result. More...
 
void removeAtFront (b length)
 Removes the requested part from the beginning of this chunk. More...
 
void removeAtBack (b length)
 Removes the requested part from the end of this chunk. More...
 
void removeAt (b offset, b length)
 Removes the requested part from this chunk. More...
 
Chunk querying related functions
int getChunkId () const
 Returns the sequentially assigned id. More...
 
virtual ChunkType getChunkType () const =0
 Returns the type of this chunk as an enum member. More...
 
virtual b getChunkLength () const =0
 Returns the length of data represented by this chunk. More...
 
virtual bool containsSameData (const Chunk &other) const
 Returns true if the two chunks represent the same binary data. More...
 
virtual bool isEmpty () const
 Returns true if this chunk contains no data. More...
 
const Ptr< Chunksimplify () const
 Returns the simplified representation of this chunk eliminating all potential redundancies. More...
 
const Ptr< Chunkpeek (const Iterator &iterator, b length=unspecifiedLength, int flags=0) const
 Returns the designated part of the data represented by this chunk in its default representation. More...
 
template<typename T >
bool has (const Iterator &iterator, b length=unspecifiedLength) const
 Returns whether if the designated part of the data is available in the requested representation. More...
 
template<typename T >
const Ptr< Tpeek (const Iterator &iterator, b length=unspecifiedLength, int flags=0) const
 Returns the designated part of the data represented by this chunk in the requested representation. More...
 
Region tagging functions
virtual SharingRegionTagSetgetRegionTags () override
 Returns all region tags. More...
 
int getNumTags () const
 Returns the number of chunk tags. More...
 
const Ptr< const TagBasegetTag (int index) const
 Returns the chunk tag at the given index. More...
 
void clearTags (b offset=b(0), b length=b(-1))
 Clears the set of chunk tags in the given region. More...
 
void copyTags (const Chunk &source, b sourceOffset=b(0), b offset=b(0), b length=b(-1))
 Copies the set of chunk tags from the source region to the provided region. More...
 
template<typename T >
const Ptr< const TfindTag (b offset=b(0), b length=b(-1)) const
 Returns the chunk tag for the provided type and range, or returns nullptr if no such chunk tag is found. More...
 
template<typename T >
const Ptr< const TgetTag (b offset=b(0), b length=b(-1)) const
 Returns the chunk tag for the provided type and range, or throws an exception if no such chunk tag is found. More...
 
template<typename T >
void mapAllTags (b offset, b length, std::function< void(b, b, const Ptr< const T > &)> f) const
 Maps all tags in the provided range to to the function. More...
 
template<typename T >
void mapAllTagsForUpdate (b offset, b length, std::function< void(b, b, const Ptr< T > &)> f)
 Maps all tags in the provided range to to the function. More...
 
template<typename T >
std::vector< SharingRegionTagSet::RegionTag< const T > > getAllTags (b offset=b(0), b length=b(-1)) const
 Returns all chunk tags for the provided type and range in a detached vector of region tags. More...
 
template<typename T >
std::vector< SharingRegionTagSet::RegionTag< T > > getAllTagsForUpdate (b offset=b(0), b length=b(-1))
 Returns all chunk tags for the provided type and range in a detached vector of region tags. More...
 
template<typename T >
const Ptr< TaddTag (b offset=b(0), b length=b(-1))
 Returns a newly added chunk tag for the provided type and range, or throws an exception if such a chunk tag is already present. More...
 
template<typename T >
const Ptr< TaddTagIfAbsent (b offset=b(0), b length=b(-1))
 Returns a newly added chunk tag for the provided type and range if absent, or returns the chunk tag that is already present. More...
 
template<typename T >
std::vector< SharingRegionTagSet::RegionTag< T > > addTagsWhereAbsent (b offset=b(0), b length=b(-1))
 Returns the newly added chunk tags for the provided type and range where the tag is absent. More...
 
template<typename T >
const Ptr< TremoveTag (b offset, b length)
 Removes the chunk tag for the provided type and range, or throws an exception if no such chunk tag is found. More...
 
template<typename T >
const Ptr< TremoveTagIfPresent (b offset, b length)
 Removes the chunk tag for the provided type and range if present, or returns nullptr if no such chunk tag is found. More...
 
template<typename T >
std::vector< SharingRegionTagSet::RegionTag< T > > removeTagsWherePresent (b offset, b length)
 Removes and returns all chunk tags for the provided type and range. More...
 
Utility functions
virtual std::ostream & printToStream (std::ostream &stream, int level, int evFlags=0) const override
 Prints an optinally formatted human readable string representation of the data present in this chunk to the given stream. More...
 
virtual std::ostream & printFieldsToStream (std::ostream &stream, int level, int evFlags=0) const
 Prints an optinally formatted human readable string representation of the feilds of this chunk to the given stream. More...
 
virtual std::string str () const override
 Returns a human readable string representation of the data present in this chunk. More...
 
- Public Member Functions inherited from inet::IPrintableObject
virtual ~IPrintableObject ()
 
virtual std::string printToString () const
 
virtual std::string printToString (int level, int evFlags=0) const
 
virtual std::string getInfoStringRepresentation (int evFlags=0) const
 
virtual std::string getDetailStringRepresentation (int evFlags=0) const
 
virtual std::string getDebugStringRepresentation (int evFlags=0) const
 
virtual std::string getTraceStringRepresentation (int evFlags=0) const
 
virtual std::string getCompleteStringRepresentation (int evFlags=0) const
 

Static Public Member Functions

Chunk serialization related functions
static void serialize (MemoryOutputStream &stream, const Ptr< const Chunk > &chunk, b offset=b(0), b length=b(-1))
 Serializes a chunk into the given stream. More...
 
static const Ptr< Chunkdeserialize (MemoryInputStream &stream, const std::type_info &typeInfo)
 Deserializes a chunk from the given stream. More...
 

Static Public Attributes

static const b unspecifiedLength = b(-std::numeric_limits<int64_t>::max())
 
static bool enableImplicitChunkSerialization = false
 Peeking some part into a chunk that requires automatic serialization will throw an exception when implicit chunk serialization is disabled. More...
 
static int nextId = 0
 

Protected Types

enum  ChunkFlag { CF_IMMUTABLE = (1 << 0), CF_INCOMPLETE = (1 << 1), CF_INCORRECT = (1 << 2), CF_IMPROPERLY_REPRESENTED = (1 << 3) }
 This enum specifies bitmasks for the flags field of Chunk. More...
 

Protected Member Functions

Class descriptor functions
int getBinDumpNumLines () const
 
int getHexDumpNumLines () const
 
const char * getBinDumpLine (int index)
 
const char * getHexDumpLine (int index)
 
const SharingRegionTagSet::RegionTag< TagBase > & _getTag (int index) const
 
Self checking functions
void checkMutable () const
 

Protected Attributes

int id
 The id is automatically assigned sequentially during construction. More...
 
int flags
 The boolean chunk flags are merged into a single integer. More...
 
SharingRegionTagSet regionTags
 The set of region tags attached to the data represented by this chunk. More...
 

Friends

class SliceChunk
 
class EncryptedChunk
 
class SequenceChunk
 
class ChunkDescriptor
 

Customizable functions

typedef bool(* PeekPredicate) (const Ptr< Chunk > &)
 
const typedef Ptr< Chunk >(* PeekConverter )(const Ptr< Chunk > &chunk, const Chunk::Iterator &iterator, b length, int flags)
 
virtual void handleChange ()
 
virtual void doInsertAtFront (const Ptr< const Chunk > &chunk)
 
virtual void doInsertAtBack (const Ptr< const Chunk > &chunk)
 
virtual void doInsertAt (const Ptr< const Chunk > &chunk, b offset)
 
virtual void doRemoveAtFront (b length)
 
virtual void doRemoveAtBack (b length)
 
virtual void doRemoveAt (b offset, b length)
 
virtual const Ptr< ChunkpeekUnchecked (PeekPredicate predicate, PeekConverter converter, const Iterator &iterator, b length, int flags) const =0
 
template<typename T >
const Ptr< TpeekConverted (const Iterator &iterator, b length, int flags) const
 
template<typename T >
const Ptr< TcheckPeekResult (const Ptr< T > &chunk, int flags) const
 
static const Ptr< ChunkconvertChunk (const std::type_info &typeInfo, const Ptr< Chunk > &chunk, b offset, b length, int flags)
 Creates a new chunk of the given type that represents the designated part of the provided chunk. More...
 

Detailed Description

This class represents a piece of data that is usually part of a packet or some other data such as a protocol buffer.

The chunk interface is designed to be very flexible in terms of alternative representations. For example, when the actual bytes in the data is irrelevant, a chunk can be as simple as an integer specifying its length. Contrary, sometimes it might be necessary to represent the data bytes as is. For example, when one is using an external program to send or receive the data. But most often it's better to have a representation that is easy to inspect and understand during debugging. Fortunately, this representation can be easily generated using the omnetpp message compiler. In any case, chunks can always be converted to and from a sequence of bytes using the corresponding serializer.

Chunks can represent data in different ways:

Chunks are initially:

  • mutable, then may become immutable, but never the other way around. There is one exception to this rule, when the chunk is exclusively owned. That is when it has exactly one reference. Immutable chunks cannot be changed anymore because they might be used to efficiently share data.
  • complete, then may become incomplete, but never the other way around. Incomplete chunks are not totally filled in. For example, when a chunk is created by deserializing it from insufficient amount of data.
  • correct, then may become incorrect, but never the other way around. Incorrect chunks contain bit errors. For example, when a packet is sent through a lossy channel causing bit errors in it.
  • properly represented, then may become improperly represented, but never the other way around. Improperly represented chunks misrepresent their data. For example, when a chunk is created by deserializing data but the field based representation does not fully support all possible data.

Chunks can be safely shared using Ptr. In fact, the reason for having immutable chunks is to allow for efficient sharing using shared pointers. For example, when a Packet data structure is duplicated the copy can share immutable chunks with the original.

In general, chunks support the following operations:

  • insert to the beginning or end
  • remove from the beginning or end
  • query length and peek an arbitrary part
  • serialize to and deserialize from a sequence of bytes
  • copy to a new mutable chunk
  • convert to a human readable string

The chunk API supports polymorphism, so it's possible to create chunk class hierarchies with proper automatic deserialization. Chunks can be nested into each other similarly how SliceChunk and SequenceChunk does it. You can find examples for these in the tests folder of INET.

Communication protocol headers often contain optional parts. There are many ways to represent optional fields with chunks. For example, the IP header contains an optional part that is a list of IP specific options. Assuming the packet already contains a SequenceChunk, the IP header can be represented in various ways. Here are a few examples:

Communication protocol headers also often contain CRC or checksum fields. In network simulations, most of the time it's unnecessary to compute the correct CRC. One can safely assume the correct CRC is present, unless the packet has to be serialized. As for the representation, a FieldsChunk subclass should contain a CRC field and possibly a CRC mode field. Please note that computing, inserting and verifying the actual CRC is not the task of the chunk serializer but rather the task of the communication protocol. You can find a few examples how to implement CRC insertion and verification in the TCP/IP protocol stack.

Implementing lossy communication channels is somewhat more complicated but also more flexible with new chunk API. Whenever a packet passes through a lossy channel there are a number of options to represent errors.

  • the simplest way is marking the whole packet erroneous via setBitError()
  • a more detailed way would be iterating through the chunks in the packet and marking the erroneous chunks via markIncorrect(). Note that the chunks marked this way has to be duplicated before, and they have to be replaced in the packet to avoid accidentally propagating the same error to multiple receivers.
  • the most detailed way is asking for the BytesChunk (or BitsChunk) of the packet and actually changing the erroneous bytes (bits) before the packet arrives at the receiver. Fortunately, the receiver doesn't have to worry about receiving a packet containing bytes instead of the original chunks, because the chunk API provides the requested chunk types either way.

Representation duality is a very important aspect of the chunk API. It means that a protocol doesn't have to worry about the actual representation of the packet's data while it's processing. For example, when the IP protocol peeks the packet for the IpHeader, there are a few possiblities. The requested class is either already there to be returned, or if the packet contains the raw bytes, then it's going to be deserialized automatically and transparently.

In fact, the chunk API supports turning any kind of chunk into any other kind of chunk using serialization/deserialization. This represents the fact that the protocol data is really a sequence of bits and any other representation is just a view on top of that. Of course, this could lead to all sorts of surprising things such as accidentally processing a Foo protocol header as a Bar protocol header. This can happen due bit errors (e.g. incorrect length field) or programming errors. To avoid hard to debug errors this conversion is disabled by default.

General rules for peeking into a chunk: 1) Peeking never returns a) a SliceChunk containing

  • a BitsChunk
  • a BitCountChunk
  • a BytesChunk
  • a ByteCountChunk
  • another SliceChunk
  • the whole of another chunk
  • a SequenceChunk with superfluous elements b) a SequenceChunk containing
  • connecting BitsChunks
  • connecting BitCountChunks
  • connecting BytesChunks
  • connecting ByteCountChunks
  • connecting SliceChunks of the same chunk
  • another SequenceChunk
  • only one chunk 2) Peeking (various special cases) a) an empty part of the chunk returns nullptr if allowed b) the whole of the chunk returns the chunk c) any part that is directly represented by another chunk returns that chunk 3) Peeking without providing a return type for a a) BitsChunk always returns a BitsChunk containing the bits of the requested part b) BitCountChunk always returns a BitCountChunk containing the requested length c) BytesChunk always returns a BytesChunk containing the bytes of the requested part d) ByteCountChunk always returns a ByteCountChunk containing the requested length e) SliceChunk always returns a SliceChunk containing the requested slice of the chunk that is used by the original SliceChunk f) SequenceChunk may return
  • an element chunk
  • a SliceChunk of an element chunk
  • a SliceChunk using the original SequenceChunk
  • a new SequenceChunk using the elements of the original SequenceChunk g) any other chunk returns a SliceChunk 4) Peeking with providing a return type always returns a chunk of the requested type (or a subtype thereof) a) Peeking with a BitCountChunk return type for any chunk returns a BitCountChunk containing the requested bit length b) Peeking with a BitsChunk return type for any chunk returns a BitsChunk containing a part of the serialized bits of the original chunk c) Peeking with a ByteCountChunk return type for any chunk returns a ByteCountChunk containing the requested byte length d) Peeking with a BytesChunk return type for any chunk returns a BytesChunk containing a part of the serialized bytes of the original chunk e) Peeking with a SliceChunk return type for any chunk returns a SliceChunk containing the requested slice of the original chunk f) Peeking with a SequenceChunk return type is an error g) Peeking with a any other return type for any chunk returns a chunk of the requested type containing data deserialized from the bytes that were serialized from the original chunk 5) Peeking never returns a chunk that is one of the following unless it's explicitly allowed by passing the corresponding peek flag a) a nullptr b) an incomplete chunk c) an incorrect chunk d) an improperly represented chunk e) a chunk converted from Foo to Bar

General rules for inserting a chunk into another: a) Inserting a BitsChunk into a BitsChunk merges them b) Inserting a BitCountChunk into a BitCountChunk merges them c) Inserting a BytesChunk into a BytesChunk merges them d) Inserting a ByteCountChunk into a ByteCountChunk merges them e) Inserting a connecting SliceChunk into a SliceChunk merges them

Member Typedef Documentation

◆ PeekPredicate

typedef bool(* inet::Chunk::PeekPredicate) (const Ptr< Chunk > &)
protected

Member Enumeration Documentation

◆ ChunkFlag

enum inet::Chunk::ChunkFlag
protected

This enum specifies bitmasks for the flags field of Chunk.

Enumerator
CF_IMMUTABLE 
CF_INCOMPLETE 
CF_INCORRECT 
CF_IMPROPERLY_REPRESENTED 
250  {
251  CF_IMMUTABLE = (1 << 0),
252  CF_INCOMPLETE = (1 << 1),
253  CF_INCORRECT = (1 << 2),
254  CF_IMPROPERLY_REPRESENTED = (1 << 3),
255  };

◆ ChunkType

This enum is used to avoid std::dynamic_cast and dynamicPtrCast.

Enumerator
CT_EMPTY 
CT_BITCOUNT 
CT_BITS 
CT_BYTECOUNT 
CT_BYTES 
CT_SLICE 
CT_ENCRYPTED 
CT_CPACKET 
CT_SEQUENCE 
CT_FIELDS 
261  {
262  CT_EMPTY,
263  CT_BITCOUNT,
264  CT_BITS,
265  CT_BYTECOUNT,
266  CT_BYTES,
267  CT_SLICE,
268  CT_ENCRYPTED,
269  CT_CPACKET,
270  CT_SEQUENCE,
271  CT_FIELDS
272  };

◆ PeekFlag

This enum specifies bitmasks for the flags argument of various peek functions.

Enumerator
PF_ALLOW_NULLPTR 
PF_ALLOW_EMPTY 
PF_ALLOW_INCOMPLETE 
PF_ALLOW_INCORRECT 
PF_ALLOW_IMPROPERLY_REPRESENTED 
PF_ALLOW_SERIALIZATION 
PF_ALLOW_ALL 
277  {
278  PF_ALLOW_NULLPTR = (1 << 0),
279  PF_ALLOW_EMPTY = (1 << 1),
280  PF_ALLOW_INCOMPLETE = (1 << 2),
281  PF_ALLOW_INCORRECT = (1 << 3),
283  PF_ALLOW_SERIALIZATION = (1 << 5),
284  PF_ALLOW_ALL = -1
285  };

Constructor & Destructor Documentation

◆ Chunk() [1/2]

inet::Chunk::Chunk ( )
18  :
19  id(nextId++),
20  flags(0)
21 {
22 }

◆ Chunk() [2/2]

inet::Chunk::Chunk ( const Chunk other)
24  :
25  cObject(other),
26  SharedBase<Chunk>(other),
27  id(nextId++),
28  flags(other.flags & ~CF_IMMUTABLE),
29  regionTags(other.regionTags)
30 {
31 }

Member Function Documentation

◆ _getTag()

const SharingRegionTagSet::RegionTag<TagBase>& inet::Chunk::_getTag ( int  index) const
inlineprotected
365 { return regionTags.getRegionTag(index); }

◆ addTag()

template<typename T >
const Ptr<T> inet::Chunk::addTag ( b  offset = b(0),
b  length = b(-1) 
)
inline

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

770  {
771  checkMutable();
772  return regionTags.addTag<T>(offset, length == b(-1) ? getChunkLength() - offset : length);
773  }

Referenced by inet::EthernetCsmaMac::frameReceptionComplete().

◆ addTagIfAbsent()

template<typename T >
const Ptr<T> inet::Chunk::addTagIfAbsent ( b  offset = b(0),
b  length = b(-1) 
)
inline

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

778  {
779  checkMutable();
780  return regionTags.addTagIfAbsent<T>(offset, length == b(-1) ? getChunkLength() - offset : length);
781  }

Referenced by inet::physicallayer::EthernetPhy::handleMessage(), inet::Ipv6::handleMessage(), and inet::Gptp::receiveSignal().

◆ addTagsWhereAbsent()

template<typename T >
std::vector<SharingRegionTagSet::RegionTag<T> > inet::Chunk::addTagsWhereAbsent ( b  offset = b(0),
b  length = b(-1) 
)
inline

Returns the newly added chunk tags for the provided type and range where the tag is absent.

786  {
787  checkMutable();
788  return regionTags.addTagsWhereAbsent<T>(offset, length == b(-1) ? getChunkLength() - offset : length);
789  }

◆ canInsertAt()

virtual bool inet::Chunk::canInsertAt ( const Ptr< const Chunk > &  chunk,
b  offset 
) const
inlinevirtual

Returns true if this chunk is capable of representing the result.

Reimplemented in inet::BytesChunk, inet::BitsChunk, inet::BitCountChunk, and inet::ByteCountChunk.

502 { return false; }

◆ canInsertAtBack()

virtual bool inet::Chunk::canInsertAtBack ( const Ptr< const Chunk > &  chunk) const
inlinevirtual

Returns true if this chunk is capable of representing the result.

Reimplemented in inet::SliceChunk, inet::SequenceChunk, inet::BytesChunk, inet::BitsChunk, inet::BitCountChunk, and inet::ByteCountChunk.

497 { return false; }

◆ canInsertAtFront()

virtual bool inet::Chunk::canInsertAtFront ( const Ptr< const Chunk > &  chunk) const
inlinevirtual

Returns true if this chunk is capable of representing the result.

Reimplemented in inet::SliceChunk, inet::SequenceChunk, inet::BytesChunk, inet::BitsChunk, inet::BitCountChunk, and inet::ByteCountChunk.

492 { return false; }

◆ canRemoveAt()

virtual bool inet::Chunk::canRemoveAt ( b  offset,
b  length 
) const
inlinevirtual

Returns true if this chunk is capable of representing the result.

Reimplemented in inet::BytesChunk, inet::BitsChunk, inet::BitCountChunk, and inet::ByteCountChunk.

555 { return false; }

◆ canRemoveAtBack()

virtual bool inet::Chunk::canRemoveAtBack ( b  length) const
inlinevirtual

Returns true if this chunk is capable of representing the result.

Reimplemented in inet::SequenceChunk, inet::SliceChunk, inet::BytesChunk, inet::BitsChunk, inet::BitCountChunk, and inet::ByteCountChunk.

550 { return false; }

◆ canRemoveAtFront()

virtual bool inet::Chunk::canRemoveAtFront ( b  length) const
inlinevirtual

Returns true if this chunk is capable of representing the result.

Reimplemented in inet::SequenceChunk, inet::SliceChunk, inet::BytesChunk, inet::BitsChunk, inet::BitCountChunk, and inet::ByteCountChunk.

545 { return false; }

◆ checkMutable()

void inet::Chunk::checkMutable ( ) const
inlineprotected
370 { CHUNK_CHECK_USAGE(isMutable(), "chunk is immutable"); }

Referenced by handleChange().

◆ checkPeekResult()

template<typename T >
const Ptr<T> inet::Chunk::checkPeekResult ( const Ptr< T > &  chunk,
int  flags 
) const
inlineprotected
408  {
409  if (chunk == nullptr) {
410  if (!(flags & PF_ALLOW_NULLPTR))
411  throw cRuntimeError("Returning an empty chunk (nullptr) is not allowed according to the flags: %x", flags);
412  }
413  else if (chunk->getChunkType() == CT_EMPTY) {
414  if (!(flags & PF_ALLOW_EMPTY))
415  throw cRuntimeError("Returning an empty chunk (EmptyChunk) is not allowed according to the flags: %x", flags);
416  }
417  else {
418  if (chunk->isIncomplete() && !(flags & PF_ALLOW_INCOMPLETE))
419  throw cRuntimeError("Returning an incomplete chunk is not allowed according to the flags: %x", flags);
420  if (chunk->isIncorrect() && !(flags & PF_ALLOW_INCORRECT))
421  throw cRuntimeError("Returning an incorrect chunk is not allowed according to the flags: %x", flags);
422  if (chunk->isImproperlyRepresented() && !(flags & PF_ALLOW_IMPROPERLY_REPRESENTED))
423  throw cRuntimeError("Returning an improperly represented chunk is not allowed according to the flags: %x", flags);
424  }
425  return chunk;
426  }

◆ clearTags()

void inet::Chunk::clearTags ( b  offset = b(0),
b  length = b(-1) 
)
inline

Clears the set of chunk tags in the given region.

710  {
711  checkMutable();
712  regionTags.clearTags(offset, length == b(-1) ? getChunkLength() - offset : length);
713  }

◆ containsSameData()

virtual bool inet::Chunk::containsSameData ( const Chunk other) const
inlinevirtual

Returns true if the two chunks represent the same binary data.

The two chunks are compared without serialization, and the method is allowed to return false if the result cannot be determined correctly. Tags and other metadata are ignored in the comparison.

Reimplemented in inet::SliceChunk, inet::EncryptedChunk, inet::BytesChunk, inet::SequenceChunk, inet::BitsChunk, inet::BitCountChunk, inet::ByteCountChunk, inet::FieldsChunk, and inet::cPacketChunk.

619  {
620  return getChunkType() == other.getChunkType() && getChunkLength() == other.getChunkLength();
621  }

Referenced by inet::cPacketChunk::containsSameData(), inet::FieldsChunk::containsSameData(), inet::BitCountChunk::containsSameData(), inet::ByteCountChunk::containsSameData(), inet::BitsChunk::containsSameData(), inet::SequenceChunk::containsSameData(), inet::BytesChunk::containsSameData(), inet::EncryptedChunk::containsSameData(), and inet::SliceChunk::containsSameData().

◆ convertChunk()

const Ptr< Chunk > inet::Chunk::convertChunk ( const std::type_info &  typeInfo,
const Ptr< Chunk > &  chunk,
b  offset,
b  length,
int  flags 
)
staticprotected

Creates a new chunk of the given type that represents the designated part of the provided chunk.

The designated part starts at the provided offset and has the provided length. This function isn't a constructor to allow creating instances of message compiler generated field based chunk classes.

118 {
119  auto chunkType = chunk->getChunkType();
120  if (!enableImplicitChunkSerialization && !(flags & PF_ALLOW_SERIALIZATION) && chunkType != CT_BITS && chunkType != CT_BYTES) {
121  auto chunkObject = chunk.get();
122  throw cRuntimeError("Implicit data reinterpretation via chunk serialization/deserialization (%s -> %s) is disabled to prevent unexpected behavior due to reinterpretation and unpredictable performance degradation due to overhead (you may consider changing the Chunk::enableImplicitChunkSerialization flag or passing the PF_ALLOW_SERIALIZATION flag)", opp_typename(typeid(*chunkObject)), opp_typename(typeInfo));
123  }
124  MemoryOutputStream outputStream;
125  serialize(outputStream, chunk, offset, length < b(0) ? std::min(-length, chunk->getChunkLength() - offset) : length);
126  MemoryInputStream inputStream(outputStream.getData());
127  const auto& result = deserialize(inputStream, typeInfo);
128  result->regionTags.copyTags(chunk->regionTags, offset, b(0), result->getChunkLength());
129  return result;
130 }

Referenced by inet::FieldsChunk::convertChunk().

◆ copyTags()

void inet::Chunk::copyTags ( const Chunk source,
b  sourceOffset = b(0),
b  offset = b(0),
b  length = b(-1) 
)
inline

Copies the set of chunk tags from the source region to the provided region.

718  {
719  checkMutable();
720  regionTags.copyTags(source.regionTags, sourceOffset, offset, length == b(-1) ? getChunkLength() - offset : length);
721  }

◆ deserialize()

const Ptr< Chunk > inet::Chunk::deserialize ( MemoryInputStream stream,
const std::type_info &  typeInfo 
)
static

Deserializes a chunk from the given stream.

The returned chunk will be an instance of the provided type. The chunk represents the bytes in the stream starting from the current stream position up to the length required by the deserializer of the chunk.

193 {
194  auto serializer = ChunkSerializerRegistry::globalRegistry.getSerializer(typeInfo);
195 #if CHUNK_CHECK_IMPLEMENTATION_ENABLED
196  auto startPosition = B(stream.getPosition());
197 #endif
198  auto chunk = serializer->deserialize(stream, typeInfo);
199 #if CHUNK_CHECK_IMPLEMENTATION_ENABLED
200  auto endPosition = B(stream.getPosition());
201  CHUNK_CHECK_IMPLEMENTATION(chunk->getChunkLength() == endPosition - startPosition);
202 #endif
203  if (stream.isReadBeyondEnd())
204  chunk->markIncomplete();
205  return chunk;
206 }

Referenced by convertChunk().

◆ doInsertAt()

virtual void inet::Chunk::doInsertAt ( const Ptr< const Chunk > &  chunk,
b  offset 
)
inlineprotectedvirtual

Reimplemented in inet::BitCountChunk, inet::BitsChunk, inet::ByteCountChunk, and inet::BytesChunk.

379 { throw cRuntimeError("Invalid operation"); }

◆ doInsertAtBack()

virtual void inet::Chunk::doInsertAtBack ( const Ptr< const Chunk > &  chunk)
inlineprotectedvirtual

Reimplemented in inet::SequenceChunk, inet::SliceChunk, inet::BitCountChunk, inet::BitsChunk, inet::ByteCountChunk, and inet::BytesChunk.

378 { throw cRuntimeError("Invalid operation"); }

◆ doInsertAtFront()

virtual void inet::Chunk::doInsertAtFront ( const Ptr< const Chunk > &  chunk)
inlineprotectedvirtual

Reimplemented in inet::SequenceChunk, inet::SliceChunk, inet::BitCountChunk, inet::BitsChunk, inet::ByteCountChunk, and inet::BytesChunk.

377 { throw cRuntimeError("Invalid operation"); }

◆ doRemoveAt()

virtual void inet::Chunk::doRemoveAt ( b  offset,
b  length 
)
inlineprotectedvirtual

Reimplemented in inet::BitCountChunk, inet::BitsChunk, inet::ByteCountChunk, and inet::BytesChunk.

382 { throw cRuntimeError("Invalid operation"); }

◆ doRemoveAtBack()

virtual void inet::Chunk::doRemoveAtBack ( b  length)
inlineprotectedvirtual

Reimplemented in inet::SequenceChunk, inet::SliceChunk, inet::BitCountChunk, inet::BitsChunk, inet::ByteCountChunk, and inet::BytesChunk.

381 { throw cRuntimeError("Invalid operation"); }

◆ doRemoveAtFront()

virtual void inet::Chunk::doRemoveAtFront ( b  length)
inlineprotectedvirtual

Reimplemented in inet::SequenceChunk, inet::SliceChunk, inet::BitCountChunk, inet::BitsChunk, inet::ByteCountChunk, and inet::BytesChunk.

380 { throw cRuntimeError("Invalid operation"); }

◆ dupShared()

◆ findTag()

template<typename T >
const Ptr<const T> inet::Chunk::findTag ( b  offset = b(0),
b  length = b(-1) 
) const
inline

Returns the chunk tag for the provided type and range, or returns nullptr if no such chunk tag is found.

726  {
727  return regionTags.findTag<T>(offset, length == b(-1) ? getChunkLength() - offset : length);
728  }

◆ forEachChild()

void inet::Chunk::forEachChild ( cVisitor *  v)
overridevirtual

Reimplemented in inet::SequenceChunk, inet::SliceChunk, and inet::EncryptedChunk.

34 {
35  for (int i = 0; i < regionTags.getNumTags(); i++)
36  v->visit(const_cast<TagBase *>(regionTags.getTag(i).get()));
37 }

Referenced by inet::EncryptedChunk::forEachChild(), inet::SliceChunk::forEachChild(), and inet::SequenceChunk::forEachChild().

◆ getAllTags()

template<typename T >
std::vector<SharingRegionTagSet::RegionTag<const T> > inet::Chunk::getAllTags ( b  offset = b(0),
b  length = b(-1) 
) const
inline

Returns all chunk tags for the provided type and range in a detached vector of region tags.

755  {
756  return regionTags.getAllTags<const T>(offset, length == b(-1) ? getChunkLength() - offset : length);
757  }

◆ getAllTagsForUpdate()

template<typename T >
std::vector<SharingRegionTagSet::RegionTag<T> > inet::Chunk::getAllTagsForUpdate ( b  offset = b(0),
b  length = b(-1) 
)
inline

Returns all chunk tags for the provided type and range in a detached vector of region tags.

762  {
763  checkMutable();
764  return regionTags.getAllTagsForUpdate<T>(offset, length == b(-1) ? getChunkLength() - offset : length);
765  }

◆ getBinDumpLine()

const char * inet::Chunk::getBinDumpLine ( int  index)
protected
71 {
72  asStringValue = "";
73  try {
74  int offset = index * 32;
75  int length = std::min(32, (int)b(getChunkLength()).get() - offset);
76  MemoryOutputStream outputStream;
77  serialize(outputStream, shared_from_this(), b(offset), b(length));
78  std::vector<bool> bits;
79  outputStream.copyData(bits);
80  for (int i = 0; i < length; i++) {
81  if (i != 0 && i % 4 == 0)
82  asStringValue += " ";
83  asStringValue += (bits[i] ? "1" : "0");
84  }
85  }
86  catch (cRuntimeError& e) {
87  asStringValue = e.what();
88  }
89  return asStringValue.c_str();
90 }

◆ getBinDumpNumLines()

int inet::Chunk::getBinDumpNumLines ( ) const
protected
59 {
60  return (b(getChunkLength()).get() + 31) / 32;
61 }

◆ getChunkId()

int inet::Chunk::getChunkId ( ) const
inline

Returns the sequentially assigned id.

600 { return id; }

◆ getChunkLength()

◆ getChunkType()

virtual ChunkType inet::Chunk::getChunkType ( ) const
pure virtual

Returns the type of this chunk as an enum member.

This can be used to avoid expensive std::dynamic_cast and dynamicPtrCast operators.

Implemented in inet::SequenceChunk, inet::SliceChunk, inet::EncryptedChunk, inet::BytesChunk, inet::BitsChunk, inet::BitCountChunk, inet::ByteCountChunk, inet::FieldsChunk, inet::cPacketChunk, inet::EmptyChunk, and inet::PacketDissector::ProtocolDataUnit.

Referenced by containsSameData().

◆ getHexDumpLine()

const char * inet::Chunk::getHexDumpLine ( int  index)
protected
93 {
94  asStringValue = "";
95  try {
96  int offset = index * 8 * 16;
97  int length = std::min(8 * 16, (int)b(getChunkLength()).get() - offset);
98  MemoryOutputStream outputStream;
99  serialize(outputStream, shared_from_this(), b(offset), b(length));
100  ASSERT(outputStream.getLength() == b(length));
101  std::vector<uint8_t> bytes;
102  outputStream.copyData(bytes);
103  char tmp[3] = " ";
104  for (size_t i = 0; i < bytes.size(); i++) {
105  if (i != 0)
106  asStringValue += " ";
107  sprintf(tmp, "%02X", bytes[i]);
108  asStringValue += tmp;
109  }
110  }
111  catch (cRuntimeError& e) {
112  asStringValue = e.what();
113  }
114  return asStringValue.c_str();
115 }

◆ getHexDumpNumLines()

int inet::Chunk::getHexDumpNumLines ( ) const
protected
64 {
65  return ((b(getChunkLength()).get() + 7) / 8 + 15) / 16;
66 }

◆ getNumTags()

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

Returns the number of chunk tags.

696  {
697  return regionTags.getNumTags();
698  }

◆ getRegionTags()

virtual SharingRegionTagSet& inet::Chunk::getRegionTags ( )
inlineoverridevirtual

Returns all region tags.

Implements inet::IRegionTaggedObject.

691 { return regionTags; }

◆ getTag() [1/2]

template<typename T >
const Ptr<const T> inet::Chunk::getTag ( b  offset = b(0),
b  length = b(-1) 
) const
inline

Returns the chunk tag for the provided type and range, or throws an exception if no such chunk tag is found.

733  {
734  return regionTags.getTag<T>(offset, length == b(-1) ? getChunkLength() - offset : length);
735  }

◆ getTag() [2/2]

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

Returns the chunk tag at the given index.

703  {
704  return regionTags.getTag(index);
705  }

◆ handleChange()

◆ has()

template<typename T >
bool inet::Chunk::has ( const Iterator iterator,
b  length = unspecifiedLength 
) const
inline

Returns whether if the designated part of the data is available in the requested representation.

651  {
652  if (length >= b(0) && getChunkLength() < iterator.getPosition() + length)
653  return false;
654  else {
655  const auto& chunk = peek<T>(iterator, length, PF_ALLOW_NULLPTR | PF_ALLOW_INCOMPLETE);
656  return chunk != nullptr && chunk->isComplete();
657  }
658  }

◆ insertAt()

void inet::Chunk::insertAt ( const Ptr< const Chunk > &  chunk,
b  offset 
)
inline

Inserts the provided chunk into this chunk.

529  {
530  auto chunkLength = getChunkLength();
531  CHUNK_CHECK_USAGE(b(0) <= offset && offset <= chunkLength, "offset is invalid");
533  handleChange();
534  regionTags.moveTags(offset, chunkLength - offset, chunk->getChunkLength());
535  regionTags.copyTags(chunk->regionTags, b(0), offset, chunk->getChunkLength());
536  doInsertAt(chunk, offset);
537  }

◆ insertAtBack()

void inet::Chunk::insertAtBack ( const Ptr< const Chunk > &  chunk)
inline

Inserts the provided chunk at the end of this chunk.

519  {
521  handleChange();
522  regionTags.copyTags(chunk->regionTags, b(0), getChunkLength(), chunk->getChunkLength());
523  doInsertAtBack(chunk);
524  }

Referenced by inet::ieee80211::Defragmentation::defragmentFrames().

◆ insertAtFront()

void inet::Chunk::insertAtFront ( const Ptr< const Chunk > &  chunk)
inline

Inserts the provided chunk at the beginning of this chunk.

507  {
509  handleChange();
510  auto length = chunk->getChunkLength();
511  regionTags.moveTags(length);
512  regionTags.copyTags(chunk->regionTags, b(0), b(0), length);
513  doInsertAtFront(chunk);
514  }

Referenced by inet::tcp::TcpLwipSendQueue::createSegmentWithBytes().

◆ isComplete()

virtual bool inet::Chunk::isComplete ( ) const
inlinevirtual

◆ isCorrect()

◆ isEmpty()

virtual bool inet::Chunk::isEmpty ( ) const
inlinevirtual

Returns true if this chunk contains no data.

Reimplemented in inet::SequenceChunk.

626 { return getChunkLength() == b(0); }

◆ isImmutable()

virtual bool inet::Chunk::isImmutable ( ) const
inlinevirtual

◆ isImproperlyRepresented()

◆ isIncomplete()

virtual bool inet::Chunk::isIncomplete ( ) const
inlinevirtual

◆ isIncorrect()

virtual bool inet::Chunk::isIncorrect ( ) const
inlinevirtual

◆ isMutable()

virtual bool inet::Chunk::isMutable ( ) const
inlinevirtual

◆ isProperlyRepresented()

virtual bool inet::Chunk::isProperlyRepresented ( ) const
inlinevirtual

◆ mapAllTags()

template<typename T >
void inet::Chunk::mapAllTags ( b  offset,
b  length,
std::function< void(b, b, const Ptr< const T > &)>  f 
) const
inline

Maps all tags in the provided range to to the function.

740  {
741  return regionTags.mapAllTags<const T>(offset, length == b(-1) ? getChunkLength() - offset : length, f);
742  }

◆ mapAllTagsForUpdate()

template<typename T >
void inet::Chunk::mapAllTagsForUpdate ( b  offset,
b  length,
std::function< void(b, b, const Ptr< T > &)>  f 
)
inline

Maps all tags in the provided range to to the function.

747  {
748  checkMutable();
749  return regionTags.mapAllTagsForUpdate<T>(offset, length == b(-1) ? getChunkLength() - offset : length, f);
750  }

◆ markImmutable()

virtual void inet::Chunk::markImmutable ( )
inlinevirtual
449 { flags |= CF_IMMUTABLE; }

Referenced by inet::EmptyChunk::EmptyChunk().

◆ markImproperlyRepresented()

virtual void inet::Chunk::markImproperlyRepresented ( )
inlinevirtual

◆ markIncomplete()

virtual void inet::Chunk::markIncomplete ( )
inlinevirtual

◆ markIncorrect()

virtual void inet::Chunk::markIncorrect ( )
inlinevirtual

◆ markMutableIfExclusivelyOwned()

void inet::Chunk::markMutableIfExclusivelyOwned ( )
inline
450  {
451  // NOTE: one for the external reference and one for the temporary return value of shared_from_this()
452  CHUNK_CHECK_USAGE(shared_from_this().use_count() == 2, "chunk is not exclusively owned");
453  flags &= ~CF_IMMUTABLE;
454  }

◆ moveIterator()

void inet::Chunk::moveIterator ( Iterator iterator,
b  length 
) const
virtual

Reimplemented in inet::SequenceChunk.

133 {
134  auto position = iterator.getPosition() + length;
135  iterator.setPosition(position);
136  iterator.setIndex(position == b(0) ? 0 : -1);
137 }

◆ parsimPack()

◆ parsimUnpack()

◆ peek() [1/2]

const Ptr< Chunk > inet::Chunk::peek ( const Iterator iterator,
b  length = unspecifiedLength,
int  flags = 0 
) const

Returns the designated part of the data represented by this chunk in its default representation.

If the length is unspecified, then the length of the result is chosen according to the internal representation. If the length is negative, then the result won't be longer than the absolute length. The result is mutable iff the designated part is directly represented in this chunk by a mutable chunk, otherwise the result is immutable.

146 {
147  const auto& chunk = peekUnchecked(nullptr, nullptr, iterator, length, flags);
148  return checkPeekResult<Chunk>(chunk, flags);
149 }

◆ peek() [2/2]

template<typename T >
const Ptr<T> inet::Chunk::peek ( const Iterator iterator,
b  length = unspecifiedLength,
int  flags = 0 
) const
inline

Returns the designated part of the data represented by this chunk in the requested representation.

If the length is unspecified, then the length of the result is chosen according to the internal representation. If the length is negative, then the result won't be longer than the absolute length. The result is mutable iff the designated part is directly represented in this chunk by a mutable chunk, otherwise the result is immutable.

669  {
670  if (std::is_same<T, Chunk>::value)
671  // NOTE: this code used to be a simple peek template specialization for the Chunk type but
672  // explicit specialization in non-namespace scope does not compile in GCC:
673  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85282
674  // specialization for the case where any returned type is acceptable.
675  return staticPtrCast<T>(peek(iterator, length, flags));
676  else {
677  CHUNK_CHECK_USAGE((flags & PF_ALLOW_EMPTY) == 0, "peeking with a specific chunk type with PF_ALLOW_EMPTY is an invalid operation");
678  const auto& predicate = [] (const Ptr<Chunk>& chunk) -> bool { return chunk == nullptr || dynamicPtrCast<T>(chunk); };
679  const auto& converter = [] (const Ptr<Chunk>& chunk, const Iterator& iterator, b length, int flags) -> const Ptr<Chunk> { return chunk->peekConverted<T>(iterator, length, flags); };
680  const auto& chunk = peekUnchecked(predicate, converter, iterator, length, flags);
681  return checkPeekResult<T>(staticPtrCast<T>(chunk), flags);
682  }
683  }

◆ peekConverted()

template<typename T >
const Ptr<T> inet::Chunk::peekConverted ( const Iterator iterator,
b  length,
int  flags 
) const
inlineprotected
399  {
400  CHUNK_CHECK_USAGE(iterator.isForward() || length >= b(0), "chunk conversion using backward iterator with undefined length is invalid");
401  auto offset = iterator.isForward() ? iterator.getPosition() : getChunkLength() - iterator.getPosition() - length;
402  const auto& chunk = T::convertChunk(typeid(T), const_cast<Chunk *>(this)->shared_from_this(), offset, length, flags);
403  chunk->markImmutable();
404  return staticPtrCast<T>(chunk);
405  }

◆ peekUnchecked()

virtual const Ptr<Chunk> inet::Chunk::peekUnchecked ( PeekPredicate  predicate,
PeekConverter  converter,
const Iterator iterator,
b  length,
int  flags 
) const
protectedpure virtual

◆ printFieldsToStream()

std::ostream & inet::Chunk::printFieldsToStream ( std::ostream &  stream,
int  level,
int  evFlags = 0 
) const
virtual

Prints an optinally formatted human readable string representation of the feilds of this chunk to the given stream.

Reimplemented in inet::SliceChunk, inet::BytesChunk, inet::EncryptedChunk, inet::BitsChunk, inet::BitCountChunk, inet::ByteCountChunk, inet::FieldsChunk, inet::cPacketChunk, and inet::EmptyChunk.

162 {
163  if (level <= PRINT_LEVEL_DETAIL)
164  stream << EV_FIELD(length, getChunkLength());
165  return stream;
166 }

Referenced by inet::ByteCountChunk::printFieldsToStream(), printToStream(), and str().

◆ printToStream()

std::ostream & inet::Chunk::printToStream ( std::ostream &  stream,
int  level,
int  evFlags = 0 
) const
overridevirtual

Prints an optinally formatted human readable string representation of the data present in this chunk to the given stream.

Reimplemented from inet::IPrintableObject.

Reimplemented in inet::SequenceChunk.

152 {
153  std::string className = getClassName();
154  auto index = className.rfind("::");
155  if (index != std::string::npos)
156  className = className.substr(index + 2);
157  stream << EV_FAINT << className << EV_NORMAL;
158  return printFieldsToStream(stream, level, evFlags);
159 }

◆ removeAt()

void inet::Chunk::removeAt ( b  offset,
b  length 
)
inline

Removes the requested part from this chunk.

584  {
585  auto chunkLength = getChunkLength();
586  CHUNK_CHECK_USAGE(b(0) <= length && length <= chunkLength, "length is invalid");
587  CHUNK_CHECK_USAGE(b(0) <= offset && offset + length <= chunkLength, "offset is invalid");
588  CHUNK_CHECK_IMPLEMENTATION(canRemoveAt(offset, length));
589  handleChange();
590  doRemoveAt(offset, length);
591  regionTags.moveTags(offset + length, chunkLength - offset - length, -length);
592  }

◆ removeAtBack()

void inet::Chunk::removeAtBack ( b  length)
inline

Removes the requested part from the end of this chunk.

572  {
573  auto chunkLength = getChunkLength();
574  CHUNK_CHECK_USAGE(b(0) <= length && length <= getChunkLength(), "length is invalid");
576  handleChange();
577  doRemoveAtBack(length);
578  regionTags.clearTags(chunkLength - length, length);
579  }

◆ removeAtFront()

void inet::Chunk::removeAtFront ( b  length)
inline

Removes the requested part from the beginning of this chunk.

560  {
561  CHUNK_CHECK_USAGE(b(0) <= length && length <= getChunkLength(), "length is invalid");
563  handleChange();
564  doRemoveAtFront(length);
565  regionTags.clearTags(b(0), length);
566  regionTags.moveTags(-length);
567  }

◆ removeTag()

template<typename T >
const Ptr<T> inet::Chunk::removeTag ( b  offset,
b  length 
)
inline

Removes the chunk tag for the provided type and range, or throws an exception if no such chunk tag is found.

794  {
795  checkMutable();
796  return regionTags.removeTag<T>(offset, length == b(-1) ? getChunkLength() - offset : length);
797  }

◆ removeTagIfPresent()

template<typename T >
const Ptr<T> inet::Chunk::removeTagIfPresent ( b  offset,
b  length 
)
inline

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

802  {
803  checkMutable();
804  return regionTags.removeTagIfPresent<T>(offset, length == b(-1) ? getChunkLength() - offset : length);
805  }

◆ removeTagsWherePresent()

template<typename T >
std::vector<SharingRegionTagSet::RegionTag<T> > inet::Chunk::removeTagsWherePresent ( b  offset,
b  length 
)
inline

Removes and returns all chunk tags for the provided type and range.

810  {
811  checkMutable();
812  return regionTags.removeTagsWherePresent<T>(offset, length == b(-1) ? getChunkLength() - offset : length);
813  }

◆ seekIterator()

void inet::Chunk::seekIterator ( Iterator iterator,
b  position 
) const
virtual

Reimplemented in inet::SequenceChunk.

140 {
141  iterator.setPosition(position);
142  iterator.setIndex(position == b(0) ? 0 : -1);
143 }

◆ serialize()

void inet::Chunk::serialize ( MemoryOutputStream stream,
const Ptr< const Chunk > &  chunk,
b  offset = b(0),
b  length = b(-1) 
)
static

Serializes a chunk into the given stream.

The bytes representing the chunk is written at the current position of the stream up to its length. The offset parameter and the offset + length value must be in the range [0, chunkLength]. If the length parameter is -1, then the chunk will be serialized up to its end.

176 {
177  CHUNK_CHECK_USAGE(length >= b(-1), "length is invalid");
178  CHUNK_CHECK_USAGE(b(0) <= offset && offset <= chunk->getChunkLength(), "offset is out of range");
179  const Chunk *chunkPointer = chunk.get();
180  auto serializer = ChunkSerializerRegistry::globalRegistry.getSerializer(typeid(*chunkPointer));
181 #if CHUNK_CHECK_IMPLEMENTATION_ENABLED
182  auto startPosition = stream.getLength();
183 #endif
184  serializer->serialize(stream, chunk, offset, length);
185 #if CHUNK_CHECK_IMPLEMENTATION_ENABLED
186  auto endPosition = stream.getLength();
187  auto expectedChunkLength = length == b(-1) ? chunk->getChunkLength() - offset : length;
188  CHUNK_CHECK_IMPLEMENTATION(expectedChunkLength == endPosition - startPosition);
189 #endif
190 }

Referenced by inet::tcp::Tcp::checkCrc(), inet::tcp::TcpCrcInsertionHook::computeCrc(), inet::Pim::computeCrc(), inet::Udp::computeCrc(), inet::BytesChunk::convertChunk(), inet::BitsChunk::convertChunk(), convertChunk(), inet::Ipv4Encap::encapsulate(), getBinDumpLine(), getHexDumpLine(), inet::sctp::SctpCrcInsertion::insertCrc(), inet::Icmp::insertCrc(), inet::Icmpv6::insertCrc(), inet::Ipv4::insertCrc(), inet::Igmpv2::insertCrc(), inet::Igmpv3::insertCrc(), inet::SequenceChunkSerializer::serialize(), inet::SliceChunkSerializer::serialize(), inet::Ipv4::setComputedCrc(), inet::ospfv2::setOspfCrc(), and inet::Ipv4::verifyCrc().

◆ simplify()

const Ptr<Chunk> inet::Chunk::simplify ( ) const
inline

Returns the simplified representation of this chunk eliminating all potential redundancies.

This function may return a nullptr for emptry chunks.

◆ str()

std::string inet::Chunk::str ( ) const
overridevirtual

Returns a human readable string representation of the data present in this chunk.

Reimplemented in inet::tcp::TcpHeader, inet::rtp::RtpHeader, inet::ArpPacket, and inet::MplsHeader.

169 {
170  std::stringstream stream;
172  return stream.tellp() == 0 ? "" : stream.str().substr(2);
173 }

Friends And Related Function Documentation

◆ ChunkDescriptor

friend class ChunkDescriptor
friend

◆ EncryptedChunk

friend class EncryptedChunk
friend

◆ SequenceChunk

friend class SequenceChunk
friend

◆ SliceChunk

friend class SliceChunk
friend

Member Data Documentation

◆ enableImplicitChunkSerialization

bool inet::Chunk::enableImplicitChunkSerialization = false
static

Peeking some part into a chunk that requires automatic serialization will throw an exception when implicit chunk serialization is disabled.

Referenced by convertChunk().

◆ flags

◆ id

int inet::Chunk::id
protected

The id is automatically assigned sequentially during construction.

◆ nextId

int inet::Chunk::nextId = 0
static

◆ PeekConverter

const typedef Ptr<Chunk>(* inet::Chunk::PeekConverter) (const Ptr< Chunk > &chunk, const Chunk::Iterator &iterator, b length, int flags)
protected

◆ regionTags

◆ unspecifiedLength

const b inet::Chunk::unspecifiedLength = b(-std::numeric_limits<int64_t>::max())
static

The documentation for this class was generated from the following files:
inet::Chunk::PF_ALLOW_NULLPTR
@ PF_ALLOW_NULLPTR
Definition: Chunk.h:278
inet::Chunk::peekUnchecked
virtual const Ptr< Chunk > peekUnchecked(PeekPredicate predicate, PeekConverter converter, const Iterator &iterator, b length, int flags) const =0
inet::units::units::T
compose< Wb, pow< m, -2 > > T
Definition: Units.h:951
inet::SharingRegionTagSet::mapAllTags
void mapAllTags(b offset, b length, std::function< void(b, b, const Ptr< const TagBase > &)> f) const
Definition: SharingRegionTagSet.cc:110
inet::SharingRegionTagSet::addTagsWhereAbsent
std::vector< SharingRegionTagSet::RegionTag< TagBase > > addTagsWhereAbsent(const std::type_info &typeInfo, b offset, b length, const Ptr< const TagBase > &tag)
Definition: SharingRegionTagSet.cc:63
CHUNK_CHECK_IMPLEMENTATION
#define CHUNK_CHECK_IMPLEMENTATION(condition)
Definition: Chunk.h:29
inet::SharingRegionTagSet::getAllTags
std::vector< RegionTag< T > > getAllTags(b offset, b length) const
Returns all the shared tags of the provided type and range.
Definition: SharingRegionTagSet.h:470
inet::Chunk::doRemoveAtFront
virtual void doRemoveAtFront(b length)
Definition: Chunk.h:380
inet::Chunk::Chunk
Chunk()
Definition: Chunk.cc:18
inet::IPrintableObject::PRINT_LEVEL_DETAIL
@ PRINT_LEVEL_DETAIL
Definition: IPrintableObject.h:24
inet::Chunk::getChunkLength
virtual b getChunkLength() const =0
Returns the length of data represented by this chunk.
inet::Chunk::canInsertAtBack
virtual bool canInsertAtBack(const Ptr< const Chunk > &chunk) const
Returns true if this chunk is capable of representing the result.
Definition: Chunk.h:497
inet::SharingRegionTagSet::copyTags
void copyTags(const SharingRegionTagSet &source, b sourceOffset, b offset, b length)
Copies the set of tags from the source region to the provided region.
Definition: SharingRegionTagSet.cc:219
inet::SharingRegionTagSet::getNumTags
int getNumTags() const
Returns the number of tags.
Definition: SharingRegionTagSet.h:300
inet::Chunk::doRemoveAtBack
virtual void doRemoveAtBack(b length)
Definition: Chunk.h:381
EV_FAINT
#define EV_FAINT
Definition: INETDefs.h:104
inet::sctp::min
double min(const double a, const double b)
Returns the minimum of a and b.
Definition: SctpAssociation.h:261
inet::SharingRegionTagSet::removeTag
const Ptr< TagBase > removeTag(int index)
Definition: SharingRegionTagSet.cc:100
inet::units::constants::e
const value< double, units::C > e(1.602176487e-19)
inet::Chunk::serialize
static void serialize(MemoryOutputStream &stream, const Ptr< const Chunk > &chunk, b offset=b(0), b length=b(-1))
Serializes a chunk into the given stream.
Definition: Chunk.cc:175
inet::Chunk::CT_ENCRYPTED
@ CT_ENCRYPTED
Definition: Chunk.h:268
inet::Chunk::canRemoveAtBack
virtual bool canRemoveAtBack(b length) const
Returns true if this chunk is capable of representing the result.
Definition: Chunk.h:550
inet::Chunk::enableImplicitChunkSerialization
static bool enableImplicitChunkSerialization
Peeking some part into a chunk that requires automatic serialization will throw an exception when imp...
Definition: Chunk.h:340
inet::Chunk::PF_ALLOW_IMPROPERLY_REPRESENTED
@ PF_ALLOW_IMPROPERLY_REPRESENTED
Definition: Chunk.h:282
inet::Chunk::handleChange
virtual void handleChange()
Definition: Chunk.cc:53
inet::Chunk::doInsertAtBack
virtual void doInsertAtBack(const Ptr< const Chunk > &chunk)
Definition: Chunk.h:378
inet::SharingRegionTagSet::findTag
const Ptr< const T > findTag(b offset, b length) const
Returns the shared tag of the provided type and range, or returns nullptr if no such tag is found.
Definition: SharingRegionTagSet.h:354
inet::Chunk::doInsertAtFront
virtual void doInsertAtFront(const Ptr< const Chunk > &chunk)
Definition: Chunk.h:377
inet::SharingRegionTagSet::addTagIfAbsent
const Ptr< T > addTagIfAbsent(b offset, b length)
Returns a newly added exclusively owned tag of the provided type and range if absent,...
Definition: SharingRegionTagSet.h:402
inet::Chunk::printFieldsToStream
virtual std::ostream & printFieldsToStream(std::ostream &stream, int level, int evFlags=0) const
Prints an optinally formatted human readable string representation of the feilds of this chunk to the...
Definition: Chunk.cc:161
inet::Chunk::CT_EMPTY
@ CT_EMPTY
Definition: Chunk.h:262
inet::IPrintableObject::PRINT_LEVEL_COMPLETE
@ PRINT_LEVEL_COMPLETE
Definition: IPrintableObject.h:26
inet::SharingRegionTagSet::mapAllTagsForUpdate
void mapAllTagsForUpdate(b offset, b length, std::function< void(b, b, const Ptr< TagBase > &)> f)
Definition: SharingRegionTagSet.cc:137
inet::Chunk::id
int id
The id is automatically assigned sequentially during construction.
Definition: Chunk.h:348
inet::Chunk::PF_ALLOW_SERIALIZATION
@ PF_ALLOW_SERIALIZATION
Definition: Chunk.h:283
inet::SharingRegionTagSet::getTag
const Ptr< const TagBase > & getTag(int index) const
Returns the shared tag at the given index.
Definition: SharingRegionTagSet.h:310
inet::Chunk::deserialize
static const Ptr< Chunk > deserialize(MemoryInputStream &stream, const std::type_info &typeInfo)
Deserializes a chunk from the given stream.
Definition: Chunk.cc:192
inet::units::units::B
intscale< b, 1, 8 > B
Definition: Units.h:1168
inet::Chunk::doInsertAt
virtual void doInsertAt(const Ptr< const Chunk > &chunk, b offset)
Definition: Chunk.h:379
inet::Chunk::PF_ALLOW_INCOMPLETE
@ PF_ALLOW_INCOMPLETE
Definition: Chunk.h:280
inet::Chunk::regionTags
SharingRegionTagSet regionTags
The set of region tags attached to the data represented by this chunk.
Definition: Chunk.h:356
inet::Chunk::canInsertAtFront
virtual bool canInsertAtFront(const Ptr< const Chunk > &chunk) const
Returns true if this chunk is capable of representing the result.
Definition: Chunk.h:492
CHUNK_CHECK_USAGE
#define CHUNK_CHECK_USAGE(condition, format,...)
Definition: Chunk.h:42
EV_FIELD
#define EV_FIELD(...)
Definition: INETDefs.h:112
inet::SharingRegionTagSet::moveTags
void moveTags(b shift)
Moves all tags with the provided shift.
Definition: SharingRegionTagSet.cc:230
inet::Chunk::PF_ALLOW_EMPTY
@ PF_ALLOW_EMPTY
Definition: Chunk.h:279
inet::Chunk::CT_BITCOUNT
@ CT_BITCOUNT
Definition: Chunk.h:263
inet::Chunk::isMutable
virtual bool isMutable() const
Definition: Chunk.h:447
inet::Chunk::canRemoveAtFront
virtual bool canRemoveAtFront(b length) const
Returns true if this chunk is capable of representing the result.
Definition: Chunk.h:545
inet::Chunk::CT_BYTES
@ CT_BYTES
Definition: Chunk.h:266
inet::Chunk::getChunkType
virtual ChunkType getChunkType() const =0
Returns the type of this chunk as an enum member.
inet::Chunk::CT_BITS
@ CT_BITS
Definition: Chunk.h:264
inet::Chunk::CT_BYTECOUNT
@ CT_BYTECOUNT
Definition: Chunk.h:265
inet::Chunk::peek
const Ptr< Chunk > peek(const Iterator &iterator, b length=unspecifiedLength, int flags=0) const
Returns the designated part of the data represented by this chunk in its default representation.
Definition: Chunk.cc:145
inet::units::values::b
value< int64_t, units::b > b
Definition: Units.h:1241
inet::Chunk::PF_ALLOW_INCORRECT
@ PF_ALLOW_INCORRECT
Definition: Chunk.h:281
inet::SharingRegionTagSet::addTag
void addTag(b offset, b length, const Ptr< const TagBase > &tag)
Definition: SharingRegionTagSet.cc:56
EV_NORMAL
#define EV_NORMAL
Definition: INETDefs.h:102
inet::Chunk::CF_INCOMPLETE
@ CF_INCOMPLETE
Definition: Chunk.h:252
inet::evFlags
int evFlags
Definition: INETDefs.cc:12
inet::Chunk::flags
int flags
The boolean chunk flags are merged into a single integer.
Definition: Chunk.h:352
inet::SharingRegionTagSet::parsimUnpack
virtual void parsimUnpack(cCommBuffer *buffer) override
Definition: SharingRegionTagSet.cc:40
inet::Chunk::CF_IMMUTABLE
@ CF_IMMUTABLE
Definition: Chunk.h:251
inet::Chunk::nextId
static int nextId
Definition: Chunk.h:342
inet::ChunkSerializerRegistry::getSerializer
const ChunkSerializer * getSerializer(const std::type_info &typeInfo) const
Definition: ChunkSerializerRegistry.cc:26
inet::Chunk::CT_SEQUENCE
@ CT_SEQUENCE
Definition: Chunk.h:270
inet::SharingRegionTagSet::getRegionTag
const RegionTag< TagBase > & getRegionTag(int index) const
Returns the shared region tag at the given index.
Definition: SharingRegionTagSet.h:320
inet::SharingRegionTagSet::clearTags
void clearTags(b offset, b length)
Clears the set of tags in the given region.
Definition: SharingRegionTagSet.cc:166
inet::Chunk::CF_IMPROPERLY_REPRESENTED
@ CF_IMPROPERLY_REPRESENTED
Definition: Chunk.h:254
inet::SharingRegionTagSet::removeTagsWherePresent
std::vector< RegionTag< T > > removeTagsWherePresent(b offset, b length)
Removes and returns all tags of the provided type and range and returns them for update.
Definition: SharingRegionTagSet.h:492
inet::ChunkSerializer::serialize
virtual void serialize(MemoryOutputStream &stream, const Ptr< const Chunk > &chunk, b offset, b length) const =0
Serializes a chunk into a stream by writing the bytes representing the chunk at the end of the stream...
inet::Chunk::PF_ALLOW_ALL
@ PF_ALLOW_ALL
Definition: Chunk.h:284
inet::Chunk::CT_CPACKET
@ CT_CPACKET
Definition: Chunk.h:269
inet::SharingRegionTagSet::removeTagIfPresent
const Ptr< T > removeTagIfPresent(b offset, b length)
Removes the tag of the provided type and range if present and returns it for update,...
Definition: SharingRegionTagSet.h:438
inet::ChunkSerializerRegistry::globalRegistry
static ChunkSerializerRegistry globalRegistry
Definition: ChunkSerializerRegistry.h:20
inet::Chunk::canRemoveAt
virtual bool canRemoveAt(b offset, b length) const
Returns true if this chunk is capable of representing the result.
Definition: Chunk.h:555
inet::Chunk::CF_INCORRECT
@ CF_INCORRECT
Definition: Chunk.h:253
inet::Chunk::CT_FIELDS
@ CT_FIELDS
Definition: Chunk.h:271
inet::SharingRegionTagSet::getAllTagsForUpdate
std::vector< RegionTag< T > > getAllTagsForUpdate(b offset, b length)
Returns all the exclusively owned tags of the provided type and range for update.
Definition: SharingRegionTagSet.h:481
inet::Chunk::doRemoveAt
virtual void doRemoveAt(b offset, b length)
Definition: Chunk.h:382
inet::Chunk::checkMutable
void checkMutable() const
Definition: Chunk.h:370
inet::SharingRegionTagSet::parsimPack
virtual void parsimPack(cCommBuffer *buffer) const override
Definition: SharingRegionTagSet.cc:28
inet::Chunk::CT_SLICE
@ CT_SLICE
Definition: Chunk.h:267