Skip to content

Commit a5bb431

Browse files
committed
Created C++ wrapper class and changed void* to const void*
1 parent b245256 commit a5bb431

4 files changed

Lines changed: 61 additions & 7 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ Feel free to improve this library. Fork it, make your changes, then submit a pul
2929

3030
## API
3131

32+
## But I like C++'s object syntax... :(
33+
34+
Fine. I reluctantly wrapped the C in a C++ class called `RingBufC`. All the methods are the same, except you no longer have to pass the this/self pointer. You can use either.
35+
36+
```
37+
// If you want to use C...
38+
39+
RingBuf *buf = RingBuf_new(sizeof(char*), 100);
40+
buf->add(buf, "I like C.");
41+
```
42+
43+
```
44+
// If you want to use the C++ wrapper
45+
46+
RingBufC buf(sizeof(char*), 100);
47+
buf.add("C++ has pretty object.method() syntax");
48+
```
49+
50+
3251

3352
### Constructor
3453

RingBuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ int RingBufIncrStart(RingBuf *self)
8282
/////// PUBLIC METHODS //////////
8383

8484
// Add an object struct to RingBuf
85-
int RingBufAdd(RingBuf *self, void *object)
85+
int RingBufAdd(RingBuf *self, const void *object)
8686
{
8787
int index;
8888
// Perform all atomic opertaions

RingBuf.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
#include "Arduino.h"
1111

12+
#ifndef __cplusplus
13+
#ifndef bool
14+
#define bool uint8_t
15+
#endif
16+
#endif
17+
1218
typedef struct RingBuf RingBuf;
1319

1420
typedef struct RingBuf
@@ -31,7 +37,7 @@ typedef struct RingBuf
3137
// Returns number of elemnts in buffer
3238
unsigned int (*numElements)(RingBuf*);
3339
// Add Event, Returns index where added in buffer, -1 on full buffer
34-
int (*add) (RingBuf*, void*);
40+
int (*add) (RingBuf*, const void*);
3541
// Returns pointer to nth element, NULL when nth element is empty
3642
void *(*peek) (RingBuf*, unsigned int);
3743
// Removes element and copies it to location pointed to by void *
@@ -51,7 +57,7 @@ int RingBuf_delete(RingBuf *self);
5157
int RingBufNextEndIndex(RingBuf *self);
5258
int RingBufIncrEnd(RingBuf *self);
5359
int RingBufIncrStart(RingBuf *self);
54-
int RingBufAdd(RingBuf *self, void *object);
60+
int RingBufAdd(RingBuf *self, const void *object);
5561
void *RingBufPeek(RingBuf *self, unsigned int num);
5662
void *RingBufPull(RingBuf *self, void *object);
5763
bool RingBufIsFull(RingBuf *self);
@@ -62,4 +68,31 @@ unsigned int RingBufNumElements(RingBuf *self);
6268
}
6369
#endif
6470

71+
72+
// For those of you who cant live without pretty C++ objects....
73+
#ifdef __cplusplus
74+
class RingBufC
75+
{
76+
77+
public:
78+
RingBufC(int size, int len) { buf = RingBuf_new(size, len); }
79+
~RingBufC() { RingBuf_delete(buf); }
80+
81+
bool isFull() { return RingBufIsFull(buf); }
82+
bool isEmpty() { return RingBufIsEmpty(buf); }
83+
unsigned int numElements() { return RingBufNumElements(buf); }
84+
85+
unsigned int add(const void *object) { return RingBufAdd(buf, object); }
86+
void *peek(unsigned int num) { return RingBufPeek(buf, num); }
87+
void *pull(void *object) { return RingBufPull(buf, object); }
88+
89+
// Use this to check if memory allocation failed
90+
bool allocFailed() { return !buf; }
91+
92+
private:
93+
RingBuf *buf;
94+
};
95+
#endif
96+
97+
6598
#endif

keywords.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
RingBuf KEYWORD1
2+
RingBufC KEYWORD1
23

34
RingBuf_new KEYWORD2
4-
RingBuf_delete KEYWORD2
5+
RingBuf_delete KEYWORD2
56

6-
isFull KEYWORD2
7-
isEmpty KEYWORD2
7+
isFull KEYWORD2
8+
isEmpty KEYWORD2
89
numElements KEYWORD2
910
add KEYWORD2
1011
peek KEYWORD2
11-
pull KEYWORD2
12+
pull KEYWORD2
13+
allocFailed KEYWORD2

0 commit comments

Comments
 (0)