TeoCCl library  0.1.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
queue.hpp
Go to the documentation of this file.
1 /*
2  * The MIT License
3  *
4  * Copyright 2016-2018 Kirill Scherba <kirill@scherba.ru>.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
35 #ifndef QUEUE_HXX
36 #define QUEUE_HXX
37 
38 #include <string>
39 #include <stdlib.h>
40 
41 #include "teoccl/queue.h"
42 
43 namespace teo {
44 
45  class Queue {
46 
47  private:
48 
49  teoQueue *que;
50 
51  public:
52 
53  Queue() { que = teoQueueNew(); }
54  virtual ~Queue() { teoQueueDestroy(que); }
55 
56  public:
57 
58  // free
59  inline int freeAll() { return teoQueueFree(que); }
60 
61  // newData
62  inline teoQueueData *newData(void *data, size_t data_length) {
63  return teoQueueNewData(data, data_length);
64  }
65  inline teoQueueData *newData(std::string data) {
66  return newData((void*)data.c_str(), data.size() + 1);
67  }
68  template<typename T> teoQueueData *newData(const T &data) {
69  return newData((void*)&data, sizeof(T));
70  }
71 
72  // free data
73  inline int freeData(teoQueueData *qd) {
74  free((void*)qd);
75  return 0;
76  }
77 
78  // add
79  inline teoQueueData *add(void *data, size_t data_length) {
80  return teoQueueAdd(que, data, data_length);
81  }
82  inline teoQueueData *add(const std::string &data) {
83  return add((void*)data.c_str(), data.size() + 1);
84  }
85  template<typename T> teoQueueData *add(const T &data) {
86  return add((void*)&data, sizeof(T));
87  }
88 
89  // add top
90  inline teoQueueData *addTop(void *data, size_t data_length) {
91  return teoQueueAddTop(que, data, data_length);
92  }
93  inline teoQueueData *addTop(std::string data) {
94  return addTop((void*)data.c_str(), data.size() + 1);
95  }
96  template<typename T> teoQueueData *addTop(const T &data) {
97  return addTop((void*)&data, sizeof(T));
98  }
99 
100  // add after
101  inline teoQueueData *addAfter(void *data, size_t data_length, teoQueueData *after) {
102  return teoQueueAddAfter(que, data, data_length, after);
103  }
104  inline teoQueueData *addAfter(std::string data, teoQueueData *after) {
105  return addAfter((void*)data.c_str(), data.size() + 1, after);
106  }
107  template<typename T> teoQueueData *addAfter(const T &data, teoQueueData *after) {
108  return addAfter((void*)&data, sizeof(T), after);
109  }
110 
111  // remove (not free)
112  inline teoQueueData *remove(teoQueueData *qd) {
113  return teoQueueRemove(que, qd);
114  }
115 
116  // delete (remove and free)
117  inline int del(teoQueueData *qd) {
118  return teoQueueDelete(que, qd);
119  }
120 
121  // delete first (remove and free)
122  inline int delFirst() {
123  return teoQueueDeleteFirst(que);
124  }
125 
126  // delete last (remove and free)
127  inline int delLast() {
128  return teoQueueDeleteLast(que);
129  }
130 
131  // move to top
133  return teoQueueMoveToTop(que, qd);
134  }
135 
136  // move to end
138  return teoQueueMoveToEnd(que, qd);
139  }
140 
141  // put
143  return teoQueuePut(que, qd);
144  }
145 
146  // size
147  inline size_t size() {
148  return teoQueueSize(que);
149  }
150 
151  // get iterator new
153  return teoQueueIteratorNew(que);
154  }
155 
156  // iterator next element
158  return teoQueueIteratorNext(it);
159  }
160 
161  // iterator previous element
163  return teoQueueIteratorPrev(it);
164  }
165 
166  // iterator current element
168  return teoQueueIteratorElement(it);
169  }
170 
171  // iterator reset (or swith to new Queue)
173  return teoQueueIteratorReset(it, q ? q->que : NULL);
174  }
175 
176  // iterator free
177  inline int iteratorFree(teoQueueIterator *it) {
178  return teoQueueIteratorFree(it);
179  }
180 
181  // foreach
182  inline int foreach(teoQueueForeachFunction callback, void *user_data = NULL) {
183  return teoQueueForeach(que, callback, user_data);
184  }
185 
186  // get data
187  template<typename T>
188  static inline T getData(void *data) {
189  return (T)data;
190  }
191 
192  };
193 
194 }
195 
196 #endif /* QUEUE_HXX */
teoQueueData * teoQueueIteratorNext(teoQueueIterator *it)
Get next element from Teo Queue iterator.
Definition: queue.c:421
teoQueueData * add(const std::string &data)
Definition: queue.hpp:82
teoQueueData * addAfter(const T &data, teoQueueData *after)
Definition: queue.hpp:107
teoQueueData * teoQueueMoveToEnd(teoQueue *q, teoQueueData *qd)
Move element from this queue to the end of queue.
Definition: queue.c:336
int teoQueueDeleteLast(teoQueue *q)
Delete last element from queue and free it.
Definition: queue.c:325
int del(teoQueueData *qd)
Definition: queue.hpp:117
Definition: queue.h:58
int delLast()
Definition: queue.hpp:127
Definition: queue.hpp:45
static T getData(void *data)
Definition: queue.hpp:188
teoQueueData * addTop(const T &data)
Definition: queue.hpp:96
teoQueueIterator * teoQueueIteratorNew(teoQueue *q)
Create new Teo Queue iterator.
Definition: queue.c:385
size_t size()
Definition: queue.hpp:147
teoQueueData * teoQueueNewData(void *data, size_t data_length)
Create new teoQueueData buffer.
Definition: queue.c:135
teoQueueData * put(teoQueueData *qd)
Definition: queue.hpp:142
teoQueueData * teoQueueRemove(teoQueue *q, teoQueueData *qd)
Remove element from queue but not free it.
Definition: queue.c:274
teoQueueData * newData(std::string data)
Definition: queue.hpp:65
int teoQueueFree(teoQueue *q)
Remove all elements from Teo queue.
Definition: queue.c:74
int freeAll()
Definition: queue.hpp:59
Definition: queue.h:66
int iteratorFree(teoQueueIterator *it)
Definition: queue.hpp:177
teoQueueData * addTop(std::string data)
Definition: queue.hpp:93
virtual ~Queue()
Definition: queue.hpp:54
Queue()
Definition: queue.hpp:53
int(* teoQueueForeachFunction)(teoQueue *q, int idx, teoQueueData *data, void *user_data)
Definition: queue.h:102
teoQueueData * addAfter(void *data, size_t data_length, teoQueueData *after)
Definition: queue.hpp:101
teoQueueData * teoQueueAddTop(teoQueue *q, void *data, size_t data_length)
Add new element to the top of Teo queue.
Definition: queue.c:183
teoQueueData * teoQueueAdd(teoQueue *q, void *data, size_t data_length)
Add new element to the end of Teo queue.
Definition: queue.c:162
Queue module.
teoQueueData * moveToTop(teoQueueData *qd)
Definition: queue.hpp:132
Queue module, linked list with data in body.
Definition: queue.h:49
int teoQueueForeach(teoQueue *q, teoQueueForeachFunction callback, void *user_data)
Loop through queue and call callback function with index and data in parameters.
Definition: queue.c:479
teoQueueData * moveToEnd(teoQueueData *qd)
Definition: queue.hpp:137
teoQueueData * teoQueueIteratorElement(teoQueueIterator *it)
Get current Teo Queue iterator element.
Definition: queue.c:454
int teoQueueDeleteFirst(teoQueue *q)
Delete first element from queue and free it.
Definition: queue.c:315
int delFirst()
Definition: queue.hpp:122
teoQueueIterator * iteratorElement(teoQueueIterator *it, Queue *q=NULL)
Definition: queue.hpp:172
teoQueueData * iteratorPrev(teoQueueIterator *it)
Definition: queue.hpp:162
teoQueue * teoQueueNew()
Create new Teo Queue.
Definition: queue.c:41
int freeData(teoQueueData *qd)
Definition: queue.hpp:73
teoQueueData * teoQueueMoveToTop(teoQueue *q, teoQueueData *qd)
Move element from this queue to the top of queue.
Definition: queue.c:361
teoQueueData * addAfter(std::string data, teoQueueData *after)
Definition: queue.hpp:104
teoQueueData * teoQueuePut(teoQueue *q, teoQueueData *qd)
Put (add, copy) existing queue record to the end of selected queue.
Definition: queue.c:109
teoQueueData * teoQueueAddAfter(teoQueue *q, void *data, size_t data_length, teoQueueData *qd)
Add new element after selected in qd field.
Definition: queue.c:199
teoQueueData * iteratorElement(teoQueueIterator *it)
Definition: queue.hpp:167
teoQueueData * addTop(void *data, size_t data_length)
Definition: queue.hpp:90
teoQueueData * teoQueueIteratorPrev(teoQueueIterator *it)
Get previous element from Teo Queue iterator.
Definition: queue.c:438
teoQueueData * add(void *data, size_t data_length)
Definition: queue.hpp:79
int teoQueueIteratorFree(teoQueueIterator *it)
Free (destroy) Teo Queue iterator.
Definition: queue.c:465
teoQueueData * newData(void *data, size_t data_length)
Definition: queue.hpp:62
size_t teoQueueSize(teoQueue *q)
Get number of elements in Teo queue.
Definition: queue.c:97
teoQueueIterator * teoQueueIteratorReset(teoQueueIterator *it, teoQueue *q)
Reset iterator (or swith to new Queue)
Definition: queue.c:404
teoQueueIterator * iterator()
Definition: queue.hpp:152
int teoQueueDelete(teoQueue *q, teoQueueData *qd)
Delete element from queue and free it.
Definition: queue.c:300
teoQueueData * iteratorNext(teoQueueIterator *it)
Definition: queue.hpp:157
teoQueueData * newData(const T &data)
Definition: queue.hpp:68
int teoQueueDestroy(teoQueue *q)
Destroy Teo Queue.
Definition: queue.c:57
teoQueueData * add(const T &data)
Definition: queue.hpp:85