Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Belle2016-11-16 12:39:21 +0000
committerJan Belle2016-11-16 13:30:31 +0000
commita8fedb5407356a648b0302396b38ca825b150239 (patch)
tree6b3e35d158eb8997521fdcab9a2bb63f0373926c /runtime
parent8a4078146f66f99667774c2d1348a4d424d6c8a1 (diff)
downloadorg.eclipse.etrice-a8fedb5407356a648b0302396b38ca825b150239.tar.gz
org.eclipse.etrice-a8fedb5407356a648b0302396b38ca825b150239.tar.xz
org.eclipse.etrice-a8fedb5407356a648b0302396b38ca825b150239.zip
Added static implementations for queue and stack to the cpp runtime
[runtime.cpp] Added classes StaticDeque, StaticQueue, StaticStack [runtime.cpp.tests] Added testcases for the new classes Change-Id: I754c2cf1c2a970b076e4d4e7149f2b5ddb7ff295
Diffstat (limited to 'runtime')
-rw-r--r--runtime/org.eclipse.etrice.runtime.cpp/src/common/containers/StaticDeque.h115
-rw-r--r--runtime/org.eclipse.etrice.runtime.cpp/src/common/containers/StaticQueue.h34
-rw-r--r--runtime/org.eclipse.etrice.runtime.cpp/src/common/containers/StaticStack.h34
3 files changed, 183 insertions, 0 deletions
diff --git a/runtime/org.eclipse.etrice.runtime.cpp/src/common/containers/StaticDeque.h b/runtime/org.eclipse.etrice.runtime.cpp/src/common/containers/StaticDeque.h
new file mode 100644
index 000000000..e24c69c29
--- /dev/null
+++ b/runtime/org.eclipse.etrice.runtime.cpp/src/common/containers/StaticDeque.h
@@ -0,0 +1,115 @@
+/*******************************************************************************
+ * Copyright (c) 2016 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Jan Belle (initial contribution)
+ *
+ *******************************************************************************/
+
+#ifndef SRC_COMMON_CONTAINERS_StaticDeque_H_
+#define SRC_COMMON_CONTAINERS_StaticDeque_H_
+
+#include <stdint.h>
+
+/**
+ * Implementation of a double ended queue with a circular buffer
+ * The type T must implement the copy constructor
+ */
+template<class T, int maxSize>
+class StaticDeque {
+public:
+
+ StaticDeque(void) : m_first(0), m_size(0) {}
+
+ StaticDeque(const StaticDeque<T, maxSize>& copy) : m_first(0), m_size(0) {
+ operator=(copy);
+ }
+
+ ~StaticDeque(void) {
+ reset();
+ }
+
+ StaticDeque<T, maxSize>& operator=(const StaticDeque<T, maxSize>& copy) {
+ reset();
+ for(int i = 0; i < copy.m_size; ++i) {
+ push_back(copy[i]);
+ }
+ return *this;
+ }
+
+ // Returns the number of objects
+ int size(void) const { return m_size; }
+
+ bool empty(void) const { return !m_size; }
+
+ // Returns a reference to an arbitrary object at the specified position
+ T& operator[] (int position) const {
+ // TODO JB: Handle position out of bounds exception
+ return (T&) m_memory[(m_first + position * sizeof(T)) % (sizeof(T) * maxSize)];
+ }
+
+ // Copies the object and stores it at the front
+ void push_front(const T& object) {
+ // TODO JB: Handle out of memory exception
+ if(m_size < maxSize) {
+ ++m_size;
+ m_first = (m_first + (maxSize - 1) * sizeof(T)) % (sizeof(T) * maxSize);
+ new (&operator[](0)) T(object);
+ }
+ }
+
+ // Copies the object and stores it at the end
+ void push_back(const T& object) {
+ // TODO JB: Handle out of memory exception
+ if(m_size < maxSize) {
+ ++m_size;
+ new (&operator[](m_size - 1)) T(object);
+ }
+ }
+
+ // Removes the first object
+ void pop_front(void) {
+ // TODO JB: Handle deque empty exception
+ if(m_size > 0) {
+ operator[](0).~T();
+ m_first = (m_first + sizeof(T)) % (sizeof(T) * maxSize);
+ --m_size;
+ }
+ }
+
+ // Removes the last element
+ void pop_back(void) {
+ // TODO JB: Handle deque empty exception
+ if(m_size > 0) {
+ operator[](m_size - 1).~T();
+ --m_size;
+ }
+ }
+
+ // Returns a reference to the first object
+ T& front(void) const {
+ return operator[](0);
+ }
+
+ // Returns a reference to the last object
+ T& back(void) const {
+ return operator[](m_size - 1);
+ }
+
+private:
+ uint8_t m_memory[sizeof(T) * maxSize]; // array for the objects
+ int m_first; // index of the first object
+ int m_size; // current size of the queue
+
+ void reset(void) {
+ while(m_size > 0) {
+ pop_back();
+ }
+ }
+};
+
+#endif /* SRC_COMMON_CONTAINERS_StaticDeque_H_ */
diff --git a/runtime/org.eclipse.etrice.runtime.cpp/src/common/containers/StaticQueue.h b/runtime/org.eclipse.etrice.runtime.cpp/src/common/containers/StaticQueue.h
new file mode 100644
index 000000000..7612e9944
--- /dev/null
+++ b/runtime/org.eclipse.etrice.runtime.cpp/src/common/containers/StaticQueue.h
@@ -0,0 +1,34 @@
+/*******************************************************************************
+ * Copyright (c) 2016 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Jan Belle (initial contribution)
+ *
+ *******************************************************************************/
+
+#ifndef SRC_COMMON_CONTAINERS_STATICQUEUE_H_
+#define SRC_COMMON_CONTAINERS_STATICQUEUE_H_
+
+#include "common/containers/StaticDeque.h"
+
+/**
+ * Queue implementation with a circular buffer
+ * The type T must implement the copy constructor
+ */
+template<class T, int maxSize>
+class StaticQueue : private StaticDeque<T, maxSize> {
+public:
+ StaticQueue(void) : StaticDeque<T, maxSize>() {}
+
+ int size(void) const { return StaticDeque<T, maxSize>::size(); }
+ bool empty(void) const { return StaticDeque<T, maxSize>::empty(); }
+ T& front(void) const { return StaticDeque<T, maxSize>::front(); }
+ void pop(void) { StaticDeque<T, maxSize>::pop_front(); }
+ void push(const T& object) { StaticDeque<T, maxSize>::push_back(object); }
+};
+
+#endif /* SRC_COMMON_CONTAINERS_STATICQUEUE_H_ */
diff --git a/runtime/org.eclipse.etrice.runtime.cpp/src/common/containers/StaticStack.h b/runtime/org.eclipse.etrice.runtime.cpp/src/common/containers/StaticStack.h
new file mode 100644
index 000000000..11278f148
--- /dev/null
+++ b/runtime/org.eclipse.etrice.runtime.cpp/src/common/containers/StaticStack.h
@@ -0,0 +1,34 @@
+/*******************************************************************************
+ * Copyright (c) 2016 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Jan Belle (initial contribution)
+ *
+ *******************************************************************************/
+
+#ifndef SRC_COMMON_CONTAINERS_STATICSTACK_H_
+#define SRC_COMMON_CONTAINERS_STATICSTACK_H_
+
+#include "common/containers/StaticDeque.h"
+
+/**
+ * Stack implementation with a circular buffer
+ * The type T must implement the copy constructor
+ */
+template<class T, int maxSize>
+class StaticStack : private StaticDeque<T, maxSize> {
+public:
+ StaticStack(void) : StaticDeque<T, maxSize>() {}
+
+ int size(void) const { return StaticDeque<T, maxSize>::size(); }
+ bool empty(void) const { return StaticDeque<T, maxSize>:: empty(); }
+ T& top(void) const { return StaticDeque<T, maxSize>::back(); }
+ void pop(void) { StaticDeque<T, maxSize>::pop_back(); }
+ void push(const T& object) { StaticDeque<T, maxSize>::push_back(object); }
+};
+
+#endif /* SRC_COMMON_CONTAINERS_STATICSTACK_H_ */

Back to the top