BusBricks: /Users/felixschulke/Developement/Arduino/Modbus_RTU/Modbus_RTU/Modbus-RTU/lib/Service/Content_stack.h Source File
BusBricks  0.1
Customize bus-communication
Loading...
Searching...
No Matches
Content_stack.h
Go to the documentation of this file.
1
24#ifndef CONTENT_STACK_H
25#define CONTENT_STACK_H
26#ifdef ARDUINO
27 #include <Arduino.h> // include Arduino-Library for platformIO-build
28#else
29 #include <mockArduino.h>
30 using namespace arduinoMocking;
31 #include <cstring>
32#endif
33
34
44template<typename content_class, int MaxSize>
46private:
48 content_class elements[MaxSize];
49
51 int size;
52public:
57 Content_stack(): size(0) {}
58
64
72 bool addElement(content_class element){
73 if (size >= MaxSize) {
74 return false;} // return false, if stacksize has reached maximum
75 elements[size] = element;
76 size++;
77 return true; // return true, if added successful
78 };
79
87 bool deleteElement(int index=0){
88 if (index < 0 || index >= size) {
89 return false;} // return false, if index of element to delete is out of range
90 // delete element, slice remaining elements, reduce size by 1
91 for (int i = index; i < size; ++i) {
92 if (i==size-1) elements[i]=content_class();
93 if (i<size-1) elements[i] = elements[i + 1];
94 }
95 size--;
96 return true; // return true, if deleted successfully
97 };
98
99
100
107 content_class* getElement(int index = 0){
108 if (index >= size || size+index<0) {
109 return nullptr;}
110
111 if (index<0){
112 return &elements[size+index]; // neg. index: return element indexed from end of stack (size)
113 }
114 return &elements[index]; // pos. index: return element indexed from beginning of stack (0)
115 };
116
123 bool empty(){
124 bool retVal = (size == 0) ? true:false;
125 return retVal;
126 }
127
134 bool full(){
135 bool retVal = (size == MaxSize) ? true:false;
136 return retVal;
137 }
138};
139
140#endif // CONTENT_STACK_H
Content-Stack-Template the content-stack stores the added items (call-by-value / copy) on internal ar...
Definition Content_stack.h:45
bool full()
Check if the Stack is full (size reached MaxSize)
Definition Content_stack.h:134
bool addElement(content_class element)
Add item to stack.
Definition Content_stack.h:72
bool empty()
Check if the Stack is empty (size is 0)
Definition Content_stack.h:123
content_class * getElement(int index=0)
Get Pointer to element in the stack. Accepts positive and negative indexing.
Definition Content_stack.h:107
bool deleteElement(int index=0)
Delete item on index-position from stack and shift all items with higher index, accepts only positive...
Definition Content_stack.h:87
~Content_stack()
Destroy the Content_stack object.
Definition Content_stack.h:63
Content_stack()
Construct Content_stack with zero elements.
Definition Content_stack.h:57
Provides mock implementations of Arduino framework functions and classes for native builds.
Definition mockArduino.cpp:28