2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file binary_queue.h
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the header file of binary queue
22 #ifndef CENT_KEY_BINARY_QUEUE_H
23 #define CENT_KEY_BINARY_QUEUE_H
25 //#include <dpl/abstract_input_output.h>
26 #include <dpl/exception.h>
29 #include <symbol-visibility.h>
30 #include <noncopyable.h>
34 * Binary queue auto pointer
37 using BinaryQueueUniquePtr = std::unique_ptr<BinaryQueue>;
40 * Binary stream implemented as constant size bucket list
42 * @todo Add optimized implementation for FlattenConsume
44 class COMMON_API BinaryQueue {
45 // : public AbstractInputOutput
49 DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
50 DECLARE_EXCEPTION_TYPE(Base, OutOfData)
53 typedef void (*BufferDeleter)(const void *buffer, size_t bufferSize,
55 static void BufferDeleterFree(const void *buffer,
64 virtual ~BucketVisitor();
70 * @param[in] buffer Constant pointer to bucket data buffer
71 * @param[in] bufferSize Number of bytes in bucket
73 virtual void OnVisitBucket(const void *buffer, size_t bufferSize) = 0;
85 BufferDeleter deleter;
88 Bucket(const void *buffer,
90 BufferDeleter deleter,
95 typedef std::list<Bucket *> BucketList;
99 static void DeleteBucket(Bucket *bucket);
101 class BucketVisitorCall {
103 BucketVisitor *m_visitor;
106 BucketVisitorCall(BucketVisitor *visitor);
107 virtual ~BucketVisitorCall();
109 void operator()(Bucket *bucket) const;
114 * Construct empty binary queue
119 * Construct binary queue via bare copy of other binary queue
121 * @param[in] other Other binary queue to copy from
122 * @warning One cannot assume that bucket structure is preserved during copy
124 BinaryQueue(const BinaryQueue &other);
127 * Construct binary queue by moving data from other binary queue
129 * @param[in] other Other binary queue to move from
131 BinaryQueue(BinaryQueue &&) = default;
136 virtual ~BinaryQueue();
139 * Construct binary queue via bare copy of other binary queue
141 * @param[in] other Other binary queue to copy from
142 * @warning One cannot assume that bucket structure is preserved during copy
144 const BinaryQueue &operator=(const BinaryQueue &other);
147 * Assign data from other binary queue using move semantics
149 * @param[in] other Other binary queue to move from
151 BinaryQueue &operator=(BinaryQueue &&) = default;
154 * Append copy of @a bufferSize bytes from memory pointed by @a buffer
155 * to the end of binary queue. Uses default deleter based on free.
158 * @param[in] buffer Pointer to buffer to copy data from
159 * @param[in] bufferSize Number of bytes to copy
160 * @exception std::bad_alloc Cannot allocate memory to hold additional data
161 * @see BinaryQueue::BufferDeleterFree
163 void AppendCopy(const void *buffer, size_t bufferSize);
166 * Append @a bufferSize bytes from memory pointed by @a buffer
167 * to the end of binary queue. Uses custom provided deleter.
168 * Responsibility for deleting provided buffer is transfered to BinaryQueue.
171 * @param[in] buffer Pointer to data buffer
172 * @param[in] bufferSize Number of bytes available in buffer
173 * @param[in] deleter Pointer to deleter procedure used to free provided
175 * @param[in] userParam User parameter passed to deleter routine
176 * @exception std::bad_alloc Cannot allocate memory to hold additional data
178 void AppendUnmanaged(
181 BufferDeleter deleter =
182 &BinaryQueue::BufferDeleterFree,
183 void *userParam = NULL);
186 * Append copy of other binary queue to the end of this binary queue
189 * @param[in] other Constant reference to other binary queue to copy data
191 * @exception std::bad_alloc Cannot allocate memory to hold additional data
192 * @warning One cannot assume that bucket structure is preserved during copy
194 void AppendCopyFrom(const BinaryQueue &other);
197 * Move bytes from other binary queue to the end of this binary queue.
198 * This also removes all bytes from other binary queue.
199 * This method is designed to be as fast as possible (only pointer swaps)
200 * and is suggested over making copies of binary queues.
201 * Bucket structure is preserved after operation.
204 * @param[in] other Reference to other binary queue to move data from
205 * @exception std::bad_alloc Cannot allocate memory to hold additional data
207 void AppendMoveFrom(BinaryQueue &other);
210 * Append copy of binary queue to the end of other binary queue
213 * @param[in] other Constant reference to other binary queue to copy data to
214 * @exception std::bad_alloc Cannot allocate memory to hold additional data
215 * @warning One cannot assume that bucket structure is preserved during copy
217 void AppendCopyTo(BinaryQueue &other) const;
220 * Move bytes from binary queue to the end of other binary queue.
221 * This also removes all bytes from binary queue.
222 * This method is designed to be as fast as possible (only pointer swaps)
223 * and is suggested over making copies of binary queues.
224 * Bucket structure is preserved after operation.
227 * @param[in] other Reference to other binary queue to move data to
228 * @exception std::bad_alloc Cannot allocate memory to hold additional data
230 void AppendMoveTo(BinaryQueue &other);
233 * Retrieve total size of all data contained in binary queue
235 * @return Number of bytes in binary queue
240 * Remove all data from binary queue
247 * Check if binary queue is empty
249 * @return true if binary queue is empty, false otherwise
254 * Remove @a size bytes from beginning of binary queue
257 * @param[in] size Number of bytes to remove
258 * @exception BinaryQueue::Exception::OutOfData Number of bytes is larger
259 * than available bytes in binary queue
261 void Consume(size_t size);
264 * Retrieve @a bufferSize bytes from beginning of binary queue and copy them
265 * to user supplied buffer
268 * @param[in] buffer Pointer to user buffer to receive bytes
269 * @param[in] bufferSize Size of user buffer pointed by @a buffer
270 * @exception BinaryQueue::Exception::OutOfData Number of bytes to flatten
271 * is larger than available bytes in binary queue
273 void Flatten(void *buffer, size_t bufferSize) const;
276 * Retrieve @a bufferSize bytes from beginning of binary queue, copy them
277 * to user supplied buffer, and remove from binary queue
280 * @param[in] buffer Pointer to user buffer to receive bytes
281 * @param[in] bufferSize Size of user buffer pointed by @a buffer
282 * @exception BinaryQueue::Exception::OutOfData Number of bytes to flatten
283 * is larger than available bytes in binary queue
285 void FlattenConsume(void *buffer, size_t bufferSize);
288 * Visit each buffer with data using visitor object
291 * @param[in] visitor Pointer to bucket visitor
292 * @see BinaryQueue::BucketVisitor
294 void VisitBuckets(BucketVisitor *visitor) const;
297 * IAbstractInput interface
299 virtual BinaryQueueUniquePtr Read(size_t size);
302 * IAbstractOutput interface
304 virtual size_t Write(const BinaryQueue &buffer, size_t bufferSize);
309 #endif // CENT_KEY_BINARY_QUEUE_H