dd03f5ecdeb1ad8fd8b5975acbee5565452b7c44
[platform/core/security/security-manager.git] / src / server / dpl / core / include / dpl / binary_queue.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        binary_queue.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the header file of binary queue
21  */
22 #ifndef SECURITY_MANAGER_BINARY_QUEUE_H
23 #define SECURITY_MANAGER_BINARY_QUEUE_H
24
25 //#include <dpl/abstract_input_output.h>
26 #include <dpl/exception.h>
27 #include <dpl/noncopyable.h>
28 #include <memory>
29 #include <list>
30
31 namespace SecurityManager {
32 /**
33  * Binary queue auto pointer
34  */
35 class BinaryQueue;
36 typedef std::auto_ptr<BinaryQueue> BinaryQueueAutoPtr;
37
38 /**
39  * Binary stream implemented as constant size bucket list
40  *
41  * @todo Add optimized implementation for FlattenConsume
42  */
43 class BinaryQueue
44 //  : public AbstractInputOutput
45 {
46   public:
47     class Exception
48     {
49       public:
50         DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
51         DECLARE_EXCEPTION_TYPE(Base, OutOfData)
52     };
53
54     typedef void (*BufferDeleter)(const void *buffer, size_t bufferSize,
55                                   void *userParam);
56     static void BufferDeleterFree(const void *buffer,
57                                   size_t bufferSize,
58                                   void *userParam);
59
60     class BucketVisitor
61     {
62       public:
63         /**
64          * Destructor
65          */
66         virtual ~BucketVisitor();
67
68         /**
69          * Visit bucket
70          *
71          * @return none
72          * @param[in] buffer Constant pointer to bucket data buffer
73          * @param[in] bufferSize Number of bytes in bucket
74          */
75         virtual void OnVisitBucket(const void *buffer, size_t bufferSize) = 0;
76     };
77
78   private:
79     struct Bucket :
80         private Noncopyable
81     {
82         const void *buffer;
83         const void *ptr;
84         size_t size;
85         size_t left;
86
87         BufferDeleter deleter;
88         void *param;
89
90         Bucket(const void *buffer,
91                size_t bufferSize,
92                BufferDeleter deleter,
93                void *userParam);
94         virtual ~Bucket();
95     };
96
97     typedef std::list<Bucket *> BucketList;
98     BucketList m_buckets;
99     size_t m_size;
100
101     static void DeleteBucket(Bucket *bucket);
102
103     class BucketVisitorCall
104     {
105       private:
106         BucketVisitor *m_visitor;
107
108       public:
109         BucketVisitorCall(BucketVisitor *visitor);
110         virtual ~BucketVisitorCall();
111
112         void operator()(Bucket *bucket) const;
113     };
114
115   public:
116     /**
117      * Construct empty binary queue
118      */
119     BinaryQueue();
120
121     /**
122      * Construct binary queue via bare copy of other binary queue
123      *
124      * @param[in] other Other binary queue to copy from
125      * @warning One cannot assume that bucket structure is preserved during copy
126      */
127     BinaryQueue(const BinaryQueue &other);
128
129     /**
130      * Destructor
131      */
132     virtual ~BinaryQueue();
133
134     /**
135      * Construct binary queue via bare copy of other binary queue
136      *
137      * @param[in] other Other binary queue to copy from
138      * @warning One cannot assume that bucket structure is preserved during copy
139      */
140     const BinaryQueue &operator=(const BinaryQueue &other);
141
142     /**
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.
145      *
146      * @return none
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
151      */
152     void AppendCopy(const void *buffer, size_t bufferSize);
153
154     /**
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.
158      *
159      * @return none
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
163      * buffer
164      * @param[in] userParam User parameter passed to deleter routine
165      * @exception std::bad_alloc Cannot allocate memory to hold additional data
166      */
167     void AppendUnmanaged(
168         const void *buffer,
169         size_t bufferSize,
170         BufferDeleter deleter =
171             &BinaryQueue::BufferDeleterFree,
172         void *userParam = NULL);
173
174     /**
175      * Append copy of other binary queue to the end of this binary queue
176      *
177      * @return none
178      * @param[in] other Constant reference to other binary queue to copy data
179      * from
180      * @exception std::bad_alloc Cannot allocate memory to hold additional data
181      * @warning One cannot assume that bucket structure is preserved during copy
182      */
183     void AppendCopyFrom(const BinaryQueue &other);
184
185     /**
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.
191      *
192      * @return none
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
195      */
196     void AppendMoveFrom(BinaryQueue &other);
197
198     /**
199      * Append copy of binary queue to the end of other binary queue
200      *
201      * @return none
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
205      */
206     void AppendCopyTo(BinaryQueue &other) const;
207
208     /**
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.
214      *
215      * @return none
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
218      */
219     void AppendMoveTo(BinaryQueue &other);
220
221     /**
222      * Retrieve total size of all data contained in binary queue
223      *
224      * @return Number of bytes in binary queue
225      */
226     size_t Size() const;
227
228     /**
229      * Remove all data from binary queue
230      *
231      * @return none
232      */
233     void Clear();
234
235     /**
236      * Check if binary queue is empty
237      *
238      * @return true if binary queue is empty, false otherwise
239      */
240     bool Empty() const;
241
242     /**
243      * Remove @a size bytes from beginning of binary queue
244      *
245      * @return none
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
249      */
250     void Consume(size_t size);
251
252     /**
253      * Retrieve @a bufferSize bytes from beginning of binary queue and copy them
254      * to user supplied buffer
255      *
256      * @return none
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
261      */
262     void Flatten(void *buffer, size_t bufferSize) const;
263
264     /**
265      * Retrieve @a bufferSize bytes from beginning of binary queue, copy them
266      * to user supplied buffer, and remove from binary queue
267      *
268      * @return none
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
273      */
274     void FlattenConsume(void *buffer, size_t bufferSize);
275
276     /**
277      * Visit each buffer with data using visitor object
278      *
279      * @return none
280      * @param[in] visitor Pointer to bucket visitor
281      * @see BinaryQueue::BucketVisitor
282      */
283     void VisitBuckets(BucketVisitor *visitor) const;
284
285     /**
286      * IAbstractInput interface
287      */
288     virtual BinaryQueueAutoPtr Read(size_t size);
289
290     /**
291      * IAbstractOutput interface
292      */
293     virtual size_t Write(const BinaryQueue &buffer, size_t bufferSize);
294 };
295
296 } // namespace SecurityManager
297
298 #endif // SECURITY_MANAGER_BINARY_QUEUE_H