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 SECURITY_MANAGER_BINARY_QUEUE_H
23 #define SECURITY_MANAGER_BINARY_QUEUE_H
25 //#include <dpl/abstract_input_output.h>
26 #include <dpl/exception.h>
27 #include <dpl/noncopyable.h>
31 namespace SecurityManager {
33 * Binary queue auto pointer
36 typedef std::auto_ptr<BinaryQueue> BinaryQueueAutoPtr;
39 * Binary stream implemented as constant size bucket list
41 * @todo Add optimized implementation for FlattenConsume
44 // : public AbstractInputOutput
50 DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
51 DECLARE_EXCEPTION_TYPE(Base, OutOfData)
54 typedef void (*BufferDeleter)(const void *buffer, size_t bufferSize,
56 static void BufferDeleterFree(const void *buffer,
66 virtual ~BucketVisitor();
72 * @param[in] buffer Constant pointer to bucket data buffer
73 * @param[in] bufferSize Number of bytes in bucket
75 virtual void OnVisitBucket(const void *buffer, size_t bufferSize) = 0;
87 BufferDeleter deleter;
90 Bucket(const void *buffer,
92 BufferDeleter deleter,
97 typedef std::list<Bucket *> BucketList;
101 static void DeleteBucket(Bucket *bucket);
103 class BucketVisitorCall
106 BucketVisitor *m_visitor;
109 BucketVisitorCall(BucketVisitor *visitor);
110 virtual ~BucketVisitorCall();
112 void operator()(Bucket *bucket) const;
117 * Construct empty binary queue
122 * Construct binary queue via bare copy of other binary queue
124 * @param[in] other Other binary queue to copy from
125 * @warning One cannot assume that bucket structure is preserved during copy
127 BinaryQueue(const BinaryQueue &other);
132 virtual ~BinaryQueue();
135 * Construct binary queue via bare copy of other binary queue
137 * @param[in] other Other binary queue to copy from
138 * @warning One cannot assume that bucket structure is preserved during copy
140 const BinaryQueue &operator=(const BinaryQueue &other);
143 * Append copy of @a bufferSize bytes from memory pointed by @a buffer
144 * to the end of binary queue. Uses default deleter based on free.
147 * @param[in] buffer Pointer to buffer to copy data from
148 * @param[in] bufferSize Number of bytes to copy
149 * @exception std::bad_alloc Cannot allocate memory to hold additional data
150 * @see BinaryQueue::BufferDeleterFree
152 void AppendCopy(const void *buffer, size_t bufferSize);
155 * Append @a bufferSize bytes from memory pointed by @a buffer
156 * to the end of binary queue. Uses custom provided deleter.
157 * Responsibility for deleting provided buffer is transfered to BinaryQueue.
160 * @param[in] buffer Pointer to data buffer
161 * @param[in] bufferSize Number of bytes available in buffer
162 * @param[in] deleter Pointer to deleter procedure used to free provided
164 * @param[in] userParam User parameter passed to deleter routine
165 * @exception std::bad_alloc Cannot allocate memory to hold additional data
167 void AppendUnmanaged(
170 BufferDeleter deleter =
171 &BinaryQueue::BufferDeleterFree,
172 void *userParam = NULL);
175 * Append copy of other binary queue to the end of this binary queue
178 * @param[in] other Constant reference to other binary queue to copy data
180 * @exception std::bad_alloc Cannot allocate memory to hold additional data
181 * @warning One cannot assume that bucket structure is preserved during copy
183 void AppendCopyFrom(const BinaryQueue &other);
186 * Move bytes from other binary queue to the end of this binary queue.
187 * This also removes all bytes from other binary queue.
188 * This method is designed to be as fast as possible (only pointer swaps)
189 * and is suggested over making copies of binary queues.
190 * Bucket structure is preserved after operation.
193 * @param[in] other Reference to other binary queue to move data from
194 * @exception std::bad_alloc Cannot allocate memory to hold additional data
196 void AppendMoveFrom(BinaryQueue &other);
199 * Append copy of binary queue to the end of other binary queue
202 * @param[in] other Constant reference to other binary queue to copy data to
203 * @exception std::bad_alloc Cannot allocate memory to hold additional data
204 * @warning One cannot assume that bucket structure is preserved during copy
206 void AppendCopyTo(BinaryQueue &other) const;
209 * Move bytes from binary queue to the end of other binary queue.
210 * This also removes all bytes from binary queue.
211 * This method is designed to be as fast as possible (only pointer swaps)
212 * and is suggested over making copies of binary queues.
213 * Bucket structure is preserved after operation.
216 * @param[in] other Reference to other binary queue to move data to
217 * @exception std::bad_alloc Cannot allocate memory to hold additional data
219 void AppendMoveTo(BinaryQueue &other);
222 * Retrieve total size of all data contained in binary queue
224 * @return Number of bytes in binary queue
229 * Remove all data from binary queue
236 * Check if binary queue is empty
238 * @return true if binary queue is empty, false otherwise
243 * Remove @a size bytes from beginning of binary queue
246 * @param[in] size Number of bytes to remove
247 * @exception BinaryQueue::Exception::OutOfData Number of bytes is larger
248 * than available bytes in binary queue
250 void Consume(size_t size);
253 * Retrieve @a bufferSize bytes from beginning of binary queue and copy them
254 * to user supplied buffer
257 * @param[in] buffer Pointer to user buffer to receive bytes
258 * @param[in] bufferSize Size of user buffer pointed by @a buffer
259 * @exception BinaryQueue::Exception::OutOfData Number of bytes to flatten
260 * is larger than available bytes in binary queue
262 void Flatten(void *buffer, size_t bufferSize) const;
265 * Retrieve @a bufferSize bytes from beginning of binary queue, copy them
266 * to user supplied buffer, and remove from binary queue
269 * @param[in] buffer Pointer to user buffer to receive bytes
270 * @param[in] bufferSize Size of user buffer pointed by @a buffer
271 * @exception BinaryQueue::Exception::OutOfData Number of bytes to flatten
272 * is larger than available bytes in binary queue
274 void FlattenConsume(void *buffer, size_t bufferSize);
277 * Visit each buffer with data using visitor object
280 * @param[in] visitor Pointer to bucket visitor
281 * @see BinaryQueue::BucketVisitor
283 void VisitBuckets(BucketVisitor *visitor) const;
286 * IAbstractInput interface
288 virtual BinaryQueueAutoPtr Read(size_t size);
291 * IAbstractOutput interface
293 virtual size_t Write(const BinaryQueue &buffer, size_t bufferSize);
296 } // namespace SecurityManager
298 #endif // SECURITY_MANAGER_BINARY_QUEUE_H