rllib  1
Classes | Public Types | Public Member Functions | Private Types | Private Attributes | List of all members
rlFifo Class Reference

#include <rlfifo.h>

Collaboration diagram for rlFifo:
Collaboration graph
[legend]

Classes

struct  _MessageList_
 

Public Types

enum  FifoEnum { DATA_AVAILABLE =-1, NO_DATA_AVAILABLE =-2, MESSAGE_TO_BIG =-3, FIFO_FULL =-4 }
 

Public Member Functions

 rlFifo (int maxmessages=0)
 
virtual ~rlFifo ()
 
int read (void *buf, int maxlen)
 
int poll ()
 
int nmesAvailable ()
 
int write (const void *buf, int len)
 
int printf (const char *format,...)
 

Private Types

typedef struct rlFifo::_MessageList_ MessageList
 

Private Attributes

int maxmes
 
int nmes
 
MessageListlist
 
WSEMAPHORE semaphore
 
pthread_mutex_t mutex
 

Detailed Description

class for a fifo. (first in first out)

Definition at line 25 of file rlfifo.h.

Member Typedef Documentation

◆ MessageList

Member Enumeration Documentation

◆ FifoEnum

Enumerator
DATA_AVAILABLE 
NO_DATA_AVAILABLE 
MESSAGE_TO_BIG 
FIFO_FULL 

Definition at line 39 of file rlfifo.h.

Constructor & Destructor Documentation

◆ rlFifo()

rlFifo::rlFifo ( int  maxmessages = 0)

Definition at line 22 of file rlfifo.cpp.

23 {
24  maxmes = maxmessages;
25  nmes = 0;
26  list = NULL;
29 }
int rlwthread_mutex_init(pthread_mutex_t *mptr, const pthread_mutexattr_t *attr)
Definition: rlwthread.cpp:160
WSEMAPHORE semaphore
Definition: rlfifo.h:53
int nmes
Definition: rlfifo.h:51
MessageList * list
Definition: rlfifo.h:52
pthread_mutex_t mutex
Definition: rlfifo.h:54
int maxmes
Definition: rlfifo.h:50
int rlwrapinit_semaphore(WSEMAPHORE *s, int cmax)
Definition: rlwthread.cpp:268

◆ ~rlFifo()

rlFifo::~rlFifo ( )
virtual

Definition at line 31 of file rlfifo.cpp.

32 {
33 MessageList *ptr,*lastptr;
34 
35  ptr = list;
36  if(ptr != NULL)
37  {
38  do
39  {
40  lastptr = ptr;
41  ptr = ptr->next;
42  delete [] lastptr->mes;
43  delete lastptr;
44  }
45  while(ptr != NULL);
46  }
49 }
int rlwthread_mutex_destroy(pthread_mutex_t *mptr)
Definition: rlwthread.cpp:178
WSEMAPHORE semaphore
Definition: rlfifo.h:53
struct _MessageList_ * next
Definition: rlfifo.h:32
MessageList * list
Definition: rlfifo.h:52
struct rlFifo::_MessageList_ MessageList
pthread_mutex_t mutex
Definition: rlfifo.h:54
int rlwrapdestroy_semaphore(WSEMAPHORE *s)
Definition: rlwthread.cpp:294

Member Function Documentation

◆ nmesAvailable()

int rlFifo::nmesAvailable ( )

Definition at line 99 of file rlfifo.cpp.

100 {
101  if(nmes > 0) return nmes;
102  return 0;
103 }
int nmes
Definition: rlfifo.h:51

◆ poll()

int rlFifo::poll ( )

Definition at line 93 of file rlfifo.cpp.

94 {
95  if(nmes > 0) return DATA_AVAILABLE;
96  return NO_DATA_AVAILABLE;
97 }
int nmes
Definition: rlfifo.h:51

◆ printf()

int rlFifo::printf ( const char *  format,
  ... 
)

Definition at line 142 of file rlfifo.cpp.

143 {
144  int ret;
145  char message[rl_PRINTF_LENGTH]; // should be big enough
146 
147  va_list ap;
148  va_start(ap,format);
149  ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
150  va_end(ap);
151  if(ret < 0) return ret;
152  return write(message,strlen(message));
153 }
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
int rlvsnprintf(char *text, int len, const char *format, va_list ap)
Definition: rlcutil.cpp:197
int write(const void *buf, int len)
Definition: rlfifo.cpp:105

◆ read()

int rlFifo::read ( void *  buf,
int  maxlen 
)

Definition at line 51 of file rlfifo.cpp.

52 {
53  int retlen;
54  MessageList *ptr;
55  char *cbuf;
56 
57  cbuf = (char *) buf;
60 
61  if(list->len > maxlen)
62  {
65  return MESSAGE_TO_BIG;
66  }
67 
68  ptr = list;
69  if(ptr->next == NULL)
70  {
71  retlen = ptr->len;
72  memcpy(buf,ptr->mes,retlen);
73  delete [] ptr->mes;
74  delete ptr;
75  list = NULL;
76  nmes--;
78  if(retlen < maxlen && retlen >= 0) cbuf[retlen] = '\0';
79  return retlen;
80  }
81  ptr = ptr->next;
82  retlen = list->len;
83  memcpy(buf,list->mes,retlen);
84  delete [] list->mes;
85  delete list;
86  list = ptr;
87  nmes--;
89  if(retlen < maxlen && retlen >= 0) cbuf[retlen] = '\0';
90  return retlen;
91 }
int rlwthread_mutex_lock(pthread_mutex_t *mptr)
Definition: rlwthread.cpp:193
WSEMAPHORE semaphore
Definition: rlfifo.h:53
int nmes
Definition: rlfifo.h:51
MessageList * list
Definition: rlfifo.h:52
struct rlFifo::_MessageList_ MessageList
pthread_mutex_t mutex
Definition: rlfifo.h:54
int rlwrapincrement_semaphore(WSEMAPHORE *s)
Definition: rlwthread.cpp:310
int rlwthread_mutex_unlock(pthread_mutex_t *mptr)
Definition: rlwthread.cpp:238
int rlwrapwait_semaphore(WSEMAPHORE *s)
Definition: rlwthread.cpp:341

◆ write()

int rlFifo::write ( const void *  buf,
int  len 
)

Definition at line 105 of file rlfifo.cpp.

106 {
107  MessageList *ptr;
108 
109  if(maxmes == 0);
110  else if(nmes >= maxmes) return FIFO_FULL;
111 
113  if(list == NULL)
114  {
115  list = new MessageList;
116  list->mes = new char [len];
117  list->len = len;
118  list->next = NULL;
119  nmes++;
120  memcpy(list->mes,buf,len);
123  return len;
124  }
125 
126  // go to end of list
127  ptr = list;
128  while(ptr->next != NULL) ptr = ptr->next;
129 
130  ptr->next = new MessageList;
131  ptr = ptr->next;
132  ptr->mes = new char [len];
133  ptr->len = len;
134  ptr->next = NULL;
135  nmes++;
136  memcpy(ptr->mes,buf,len);
139  return len;
140 }
int rlwthread_mutex_lock(pthread_mutex_t *mptr)
Definition: rlwthread.cpp:193
WSEMAPHORE semaphore
Definition: rlfifo.h:53
int nmes
Definition: rlfifo.h:51
struct _MessageList_ * next
Definition: rlfifo.h:32
MessageList * list
Definition: rlfifo.h:52
struct rlFifo::_MessageList_ MessageList
pthread_mutex_t mutex
Definition: rlfifo.h:54
int rlwrapincrement_semaphore(WSEMAPHORE *s)
Definition: rlwthread.cpp:310
int maxmes
Definition: rlfifo.h:50
int rlwthread_mutex_unlock(pthread_mutex_t *mptr)
Definition: rlwthread.cpp:238

Member Data Documentation

◆ list

MessageList* rlFifo::list
private

Definition at line 52 of file rlfifo.h.

◆ maxmes

int rlFifo::maxmes
private

Definition at line 50 of file rlfifo.h.

◆ mutex

pthread_mutex_t rlFifo::mutex
private

Definition at line 54 of file rlfifo.h.

◆ nmes

int rlFifo::nmes
private

Definition at line 51 of file rlfifo.h.

◆ semaphore

WSEMAPHORE rlFifo::semaphore
private

Definition at line 53 of file rlfifo.h.


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