Serial-TCP/IPbridge  1.0
shared_buffer.h
Go to the documentation of this file.
1 
12 #ifndef SHARED_BUFFER_H
13 #define SHARED_BUFFER_H
14 
15 #include <string>
16 #include <utility>
17 #include <list>
18 #include <pthread.h>
19 
20 #include "threading_error.h"
21 
30 class SharedBuffer {
31  public:
33  typedef unsigned long MessageId;
34 
36  typedef std::pair<std::string, MessageId> MessageWithId;
37 
38  private:
40  typedef std::list<MessageWithId> Buffer;
41 
42  public:
46  SharedBuffer();
47 
51  ~SharedBuffer();
52 
60  SharedBuffer(const SharedBuffer &other);
61 
71  SharedBuffer & operator=(const SharedBuffer &other);
72 
80  bool operator==(const SharedBuffer &other);
81 
89  bool operator==(const SharedBuffer &other) const;
90 
98  bool operator!=(const SharedBuffer &other);
99 
107  bool operator!=(const SharedBuffer &other) const;
108 
112  Buffer::size_type size();
113 
117  Buffer::size_type size() const;
118 
125 
131  void addMessage(const std::string &message);
132 
141  void removeMessage(MessageId messageId);
142 
152  void changeMessage(MessageId messageId, const std::string &newMessage);
153 
154  public:
161 
162  private:
166  void lockBufferMutex();
167 
171  void unlockBufferMutex();
172 
179 
183  void initializeBufferMutex();
184 
195  void swap(SharedBuffer &other);
196 
197  private:
203  pthread_mutex_t _bufferMutex;
204 };
205 
206 #endif // #ifndef SHARED_BUFFER_H
207 
208 // End of file shared_buffer.h
void lockBufferMutex()
Locks the buffer mutex.
MessageId generateNewMessageId()
Generates and returns a new message ID.
unsigned long MessageId
Message ID.
Definition: shared_buffer.h:33
Buffer _buffer
Buffer.
Definition: shared_buffer.h:199
void changeMessage(MessageId messageId, const std::string &newMessage)
Changes the selected message with a new one.
static const MessageWithId INVALID_MESSAGE
Invalid message.
Definition: shared_buffer.h:160
void unlockBufferMutex()
Locks the buffer mutex.
ThreadingError class - declarations.
MessageId _lastMessageId
Last message ID.
Definition: shared_buffer.h:201
Message buffer that can be used in separate threads.
Definition: shared_buffer.h:30
std::list< MessageWithId > Buffer
Buffer type.
Definition: shared_buffer.h:40
Buffer::size_type size()
Returns the size of the buffer.
void initializeBufferMutex()
Initializes the buffer mutex.
void addMessage(const std::string &message)
Adds the selected message to the buffer.
bool operator==(const SharedBuffer &other)
Equality comparison.
void removeMessage(MessageId messageId)
Removes the selected message from the buffer.
pthread_mutex_t _bufferMutex
Buffer mutex.
Definition: shared_buffer.h:203
SharedBuffer()
Default constructor.
bool operator!=(const SharedBuffer &other)
Non-equality comparison.
MessageWithId getMessage()
Returns the first message from the buffer.
std::pair< std::string, MessageId > MessageWithId
Message with identification.
Definition: shared_buffer.h:36
SharedBuffer & operator=(const SharedBuffer &other)
Assignment operator.
~SharedBuffer()
Destructor.
void swap(SharedBuffer &other)
Swap the contents of this object with the other object.