Refactor BinaryQueue and tests
[platform/core/security/key-manager.git] / src / manager / dpl / core / include / dpl / binary_queue.h
1 /*
2  * Copyright (c) 2011 - 2020 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 CENT_KEY_BINARY_QUEUE_H
23 #define CENT_KEY_BINARY_QUEUE_H
24
25 #include <dpl/exception.h>
26 #include <list>
27 #include <vector>
28 #include <symbol-visibility.h>
29
30 namespace CKM {
31
32 /**
33  * Binary stream implemented as constant size bucket list
34  */
35 class COMMON_API BinaryQueue final {
36 public:
37         class Exception {
38         public:
39                 DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
40                 DECLARE_EXCEPTION_TYPE(Base, OutOfData)
41         };
42
43 private:
44         typedef std::vector<unsigned char> Bucket;
45
46         typedef std::list<Bucket> BucketList;
47         BucketList m_buckets;
48         size_t m_size;
49
50 public:
51         /**
52          * Construct empty binary queue
53          */
54         BinaryQueue();
55
56         BinaryQueue(const BinaryQueue &other) = delete;
57
58         /**
59          * Construct binary queue by moving data from other binary queue
60          *
61          * @param[in] other Other binary queue to move from
62          */
63         BinaryQueue(BinaryQueue &&) = default;
64
65         /**
66          * Append copy of @a bufferSize bytes from memory pointed by @a buffer
67          * to the end of binary queue. Uses default deleter based on free.
68          *
69          * @return none
70          * @param[in] buffer Pointer to buffer to copy data from
71          * @param[in] bufferSize Number of bytes to copy
72          * @exception std::bad_alloc Cannot allocate memory to hold additional data
73          */
74         void AppendCopy(const void *buffer, size_t bufferSize);
75
76         /**
77          * Retrieve total size of all data contained in binary queue
78          *
79          * @return Number of bytes in binary queue
80          */
81         size_t Size() const;
82
83         /**
84          * Retrieve @a bufferSize bytes from beginning of binary queue, copy them
85          * to user supplied buffer, and remove from binary queue
86          *
87          * @return none
88          * @param[in] buffer Pointer to user buffer to receive bytes
89          * @param[in] bufferSize Size of user buffer pointed by @a buffer
90          * @exception BinaryQueue::Exception::OutOfData Number of bytes to flatten
91          *            is larger than available bytes in binary queue
92          */
93         void FlattenConsume(void *buffer, size_t bufferSize);
94 };
95
96 } // namespace CKM
97
98 #endif // CENT_KEY_BINARY_QUEUE_H