1 | // |
---|
2 | // You received this file as part of RRLib |
---|
3 | // Robotics Research Library |
---|
4 | // |
---|
5 | // Copyright (C) Finroc GbR (finroc.org) |
---|
6 | // |
---|
7 | // This program is free software; you can redistribute it and/or modify |
---|
8 | // it under the terms of the GNU General Public License as published by |
---|
9 | // the Free Software Foundation; either version 2 of the License, or |
---|
10 | // (at your option) any later version. |
---|
11 | // |
---|
12 | // This program is distributed in the hope that it will be useful, |
---|
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | // GNU General Public License for more details. |
---|
16 | // |
---|
17 | // You should have received a copy of the GNU General Public License along |
---|
18 | // with this program; if not, write to the Free Software Foundation, Inc., |
---|
19 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
---|
20 | // |
---|
21 | //---------------------------------------------------------------------- |
---|
22 | /*!\file rrlib/concurrent_containers/queue/tQueueImplementation.h |
---|
23 | * |
---|
24 | * \author Max Reichardt |
---|
25 | * |
---|
26 | * \date 2012-09-25 |
---|
27 | * |
---|
28 | * \brief Contains tQueueImplementation |
---|
29 | * |
---|
30 | * \b tQueueImplementation |
---|
31 | * |
---|
32 | * Implementations for different types of queues. |
---|
33 | * |
---|
34 | */ |
---|
35 | //---------------------------------------------------------------------- |
---|
36 | #ifndef __rrlib__concurrent_containers__queue__tQueueImplementation_h__ |
---|
37 | #define __rrlib__concurrent_containers__queue__tQueueImplementation_h__ |
---|
38 | |
---|
39 | //---------------------------------------------------------------------- |
---|
40 | // External includes (system with <>, local with "") |
---|
41 | //---------------------------------------------------------------------- |
---|
42 | |
---|
43 | //---------------------------------------------------------------------- |
---|
44 | // Internal includes with "" |
---|
45 | //---------------------------------------------------------------------- |
---|
46 | #include "rrlib/concurrent_containers/tQueueable.h" |
---|
47 | #include "rrlib/concurrent_containers/tQueueFragment.h" |
---|
48 | #include "rrlib/concurrent_containers/queue/tUniquePtrQueueImplementation.h" |
---|
49 | |
---|
50 | //---------------------------------------------------------------------- |
---|
51 | // Namespace declaration |
---|
52 | //---------------------------------------------------------------------- |
---|
53 | namespace rrlib |
---|
54 | { |
---|
55 | namespace concurrent_containers |
---|
56 | { |
---|
57 | namespace queue |
---|
58 | { |
---|
59 | |
---|
60 | //---------------------------------------------------------------------- |
---|
61 | // Forward declarations / typedefs / enums |
---|
62 | //---------------------------------------------------------------------- |
---|
63 | |
---|
64 | //---------------------------------------------------------------------- |
---|
65 | // Class declaration |
---|
66 | //---------------------------------------------------------------------- |
---|
67 | //! Queue Implementation |
---|
68 | /*! |
---|
69 | * Implementations for different types of queues. |
---|
70 | */ |
---|
71 | template <typename T, tConcurrency CONCURRENCY, tDequeueMode DEQUEUE_MODE, bool BOUNDED> |
---|
72 | class tQueueImplementation |
---|
73 | { |
---|
74 | // this combination of parameters is not supported yet |
---|
75 | }; |
---|
76 | |
---|
77 | template <tDequeueMode DEQUEUE_MODE> |
---|
78 | struct tUniquePtrQueueElementDeleter |
---|
79 | { |
---|
80 | template <typename QUEUE> |
---|
81 | static void DeleteElements(QUEUE& queue) |
---|
82 | { |
---|
83 | // Since we have unique pointers enqueued, we need to delete all enqueued elements |
---|
84 | bool success = true; |
---|
85 | while (success) |
---|
86 | { |
---|
87 | queue.Dequeue(success); |
---|
88 | } |
---|
89 | //TODO: DeleteLast() ? |
---|
90 | } |
---|
91 | }; |
---|
92 | |
---|
93 | template <> |
---|
94 | struct tUniquePtrQueueElementDeleter<tDequeueMode::ALL> |
---|
95 | { |
---|
96 | template <typename QUEUE> |
---|
97 | static void DeleteElements(QUEUE& queue) |
---|
98 | { |
---|
99 | queue.DequeueAll(); |
---|
100 | } |
---|
101 | }; |
---|
102 | |
---|
103 | template <typename T, typename D, tConcurrency CONCURRENCY, tDequeueMode DEQUEUE_MODE, bool BOUNDED> |
---|
104 | class tQueueImplementation<std::unique_ptr<T, D>, CONCURRENCY, DEQUEUE_MODE, BOUNDED> : |
---|
105 | public tUniquePtrQueueImplementation<T, D, CONCURRENCY, DEQUEUE_MODE, BOUNDED, std::is_base_of<tQueueableMost, T>::value> |
---|
106 | { |
---|
107 | typedef tUniquePtrQueueImplementation<T, D, CONCURRENCY, DEQUEUE_MODE, BOUNDED, std::is_base_of<tQueueableMost, T>::value> tBase; |
---|
108 | |
---|
109 | static_assert(sizeof(std::unique_ptr<T, D>) == sizeof(void*), "Only unique pointers with Deleter of size 0 may be used in queue. Otherwise, this would be too much info to store in an atomic."); |
---|
110 | |
---|
111 | public: |
---|
112 | |
---|
113 | ~tQueueImplementation() |
---|
114 | { |
---|
115 | tUniquePtrQueueElementDeleter<DEQUEUE_MODE>::DeleteElements(*this); |
---|
116 | } |
---|
117 | |
---|
118 | inline std::unique_ptr<T, D> Dequeue(bool& success) |
---|
119 | { |
---|
120 | std::unique_ptr<T, D> ptr = tBase::Dequeue(); |
---|
121 | success = ptr.get(); |
---|
122 | return ptr; |
---|
123 | } |
---|
124 | |
---|
125 | }; |
---|
126 | |
---|
127 | //---------------------------------------------------------------------- |
---|
128 | // End of namespace declaration |
---|
129 | //---------------------------------------------------------------------- |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | #endif |
---|