Serial-TCP/IPbridge  1.0
Public Types | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
SharedBuffer Class Reference

Message buffer that can be used in separate threads. More...

#include <shared_buffer.h>

Public Types

typedef unsigned long MessageId
 Message ID. More...
 
typedef std::pair< std::string,
MessageId
MessageWithId
 Message with identification. More...
 

Public Member Functions

 SharedBuffer ()
 Default constructor. More...
 
 ~SharedBuffer ()
 Destructor. More...
 
 SharedBuffer (const SharedBuffer &other)
 Copy constructor. More...
 
SharedBufferoperator= (const SharedBuffer &other)
 Assignment operator. More...
 
bool operator== (const SharedBuffer &other)
 Equality comparison. More...
 
bool operator== (const SharedBuffer &other) const
 Equality comparison. More...
 
bool operator!= (const SharedBuffer &other)
 Non-equality comparison. More...
 
bool operator!= (const SharedBuffer &other) const
 Non-equality comparison. More...
 
Buffer::size_type size ()
 Returns the size of the buffer. More...
 
Buffer::size_type size () const
 Returns the size of the buffer. More...
 
MessageWithId getMessage ()
 Returns the first message from the buffer. More...
 
void addMessage (const std::string &message)
 Adds the selected message to the buffer. More...
 
void removeMessage (MessageId messageId)
 Removes the selected message from the buffer. More...
 
void changeMessage (MessageId messageId, const std::string &newMessage)
 Changes the selected message with a new one. More...
 

Static Public Attributes

static const MessageWithId INVALID_MESSAGE
 Invalid message. More...
 

Private Types

typedef std::list< MessageWithIdBuffer
 Buffer type. More...
 

Private Member Functions

void lockBufferMutex ()
 Locks the buffer mutex. More...
 
void unlockBufferMutex ()
 Locks the buffer mutex. More...
 
MessageId generateNewMessageId ()
 Generates and returns a new message ID. More...
 
void initializeBufferMutex ()
 Initializes the buffer mutex. More...
 
void swap (SharedBuffer &other)
 Swap the contents of this object with the other object. More...
 

Private Attributes

Buffer _buffer
 Buffer. More...
 
MessageId _lastMessageId
 Last message ID. More...
 
pthread_mutex_t _bufferMutex
 Buffer mutex. More...
 

Detailed Description

Message buffer that can be used in separate threads.

All of the class operations are thread-safe. Each member function can throw ThreadingError in case of some threading-related problem.

It provides value semantics (objects copying and comparison is allowed).

Member Typedef Documentation

typedef std::list<MessageWithId> SharedBuffer::Buffer
private

Buffer type.

typedef unsigned long SharedBuffer::MessageId

Message ID.

typedef std::pair<std::string, MessageId> SharedBuffer::MessageWithId

Message with identification.

Constructor & Destructor Documentation

SharedBuffer::SharedBuffer ( )

Default constructor.

SharedBuffer::~SharedBuffer ( )

Destructor.

SharedBuffer::SharedBuffer ( const SharedBuffer other)

Copy constructor.

Parameters
otherObject that will be used as a model in the object creation.

Constructs a copy of the other object.

Member Function Documentation

void SharedBuffer::addMessage ( const std::string &  message)

Adds the selected message to the buffer.

Parameters
messageThe message to be added.
void SharedBuffer::changeMessage ( MessageId  messageId,
const std::string &  newMessage 
)

Changes the selected message with a new one.

Parameters
messageIdThe ID of the message to be changed.
newMessageThe new message.

If there is no message with the selected ID, then this function does nothing.

MessageId SharedBuffer::generateNewMessageId ( )
private

Generates and returns a new message ID.

Returns
Generated message ID.
MessageWithId SharedBuffer::getMessage ( )

Returns the first message from the buffer.

If there are no messages in the buffer, it returns INVALID_MESSAGE.

void SharedBuffer::initializeBufferMutex ( )
private

Initializes the buffer mutex.

void SharedBuffer::lockBufferMutex ( )
private

Locks the buffer mutex.

bool SharedBuffer::operator!= ( const SharedBuffer other)

Non-equality comparison.

Parameters
otherObject that will be compared with this object.
Returns
True if this object is not equal to the other object.
bool SharedBuffer::operator!= ( const SharedBuffer other) const

Non-equality comparison.

Parameters
otherObject that will be compared with this object.
Returns
True if this object is not equal to the other object.
SharedBuffer& SharedBuffer::operator= ( const SharedBuffer other)

Assignment operator.

Parameters
otherObject that will be assigned to this object.
Returns
Reference to this object.

Guarantees the strong exception safety.

bool SharedBuffer::operator== ( const SharedBuffer other)

Equality comparison.

Parameters
otherObject that will be compared with this object.
Returns
True if this object is equal to the other object.
bool SharedBuffer::operator== ( const SharedBuffer other) const

Equality comparison.

Parameters
otherObject that will be compared with this object.
Returns
True if this object is equal to the other object.
void SharedBuffer::removeMessage ( MessageId  messageId)

Removes the selected message from the buffer.

Parameters
messageIdThe ID of the message to be removed.

If there is no message with the selected ID, then this function does nothing.

Buffer::size_type SharedBuffer::size ( )

Returns the size of the buffer.

Buffer::size_type SharedBuffer::size ( ) const

Returns the size of the buffer.

void SharedBuffer::swap ( SharedBuffer other)
private

Swap the contents of this object with the other object.

Parameters
otherObject that will be swapped with this object.

Guarantees nothrow exception safety.

Preconditions:

  • mutex has to be locked
void SharedBuffer::unlockBufferMutex ( )
private

Locks the buffer mutex.

Member Data Documentation

Buffer SharedBuffer::_buffer
private

Buffer.

pthread_mutex_t SharedBuffer::_bufferMutex
private

Buffer mutex.

MessageId SharedBuffer::_lastMessageId
private

Last message ID.

const MessageWithId SharedBuffer::INVALID_MESSAGE
static

Invalid message.

Can be returned by getMessage() if there are no messages in the buffer.


The documentation for this class was generated from the following file: