Purge underscored header file barriers
[platform/core/uifw/dali-core.git] / dali / internal / common / message-buffer.h
1 #ifndef DALI_INTERNAL_MESSAGE_BUFFER_H
2 #define DALI_INTERNAL_MESSAGE_BUFFER_H
3
4 /*
5  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <cstddef> // size_t
23 #include <cstdint> // uint32_t
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 /**
32  * Utility class to reserve a buffer for storing messages.
33  */
34 class MessageBuffer
35 {
36 public:
37   typedef std::ptrdiff_t WordType;
38
39   /**
40    * Create a new MessageBuffer
41    * @param[in] The smallest capacity which the buffer will allocate, with respect to the size of type "char".
42    * @note The buffer will not allocate memory until the first call to ReserveMessageSlot().
43    */
44   MessageBuffer( std::size_t initialCapacity );
45
46   /**
47    * Non-virtual destructor; not suitable as a base class
48    */
49   ~MessageBuffer();
50
51   /**
52    * Reserve space for another message in the buffer.
53    * @pre size is greater than zero.
54    * @param[in] size The message size with respect to the size of type "char".
55    * @return A pointer to the address allocated for the message, aligned to a word boundary
56    */
57   uint32_t* ReserveMessageSlot( std::size_t size );
58
59   /**
60    * Query the capacity of the message buffer.
61    * @return The capacity with respect to the size of type "char".
62    */
63   std::size_t GetCapacity() const;
64
65   /**
66    * Used to iterate though the messages in the buffer.
67    */
68   class Iterator
69   {
70   public:
71
72     // Constructor
73     Iterator(WordType* current);
74
75     // Inlined for performance
76     bool IsValid()
77     {
78       // Valid until end marker has been found
79       return 0 != mMessageSize;
80     }
81
82     // Inlined for performance
83     WordType* Get()
84     {
85       return ( 0 != mMessageSize ) ? mCurrent : NULL;
86     }
87
88     // Inlined for performance
89     void Next()
90     {
91       // Jump to next object and read size
92       mCurrent += mMessageSize;
93       mMessageSize = *mCurrent++;
94     }
95
96     // Copy constructor
97     Iterator(const Iterator& copy);
98
99   private:
100
101     // Undefined
102     Iterator& operator=(const Iterator& rhs);
103
104   private:
105
106     WordType* mCurrent;
107     std::size_t mMessageSize;
108   };
109
110   /**
111    * Returns an iterator to the first message in the buffer.
112    * There is no past-the-end iterator; use Iterator::IsValid() to determine when the has been reached.
113    * @note Adding more messages with ReserveMessageSlot() may corrupt this iterator.
114    * @return The iterator.
115    */
116   Iterator Begin() const;
117
118   /**
119    * Sets the size of the buffer to zero (does not deallocate memory)
120    */
121   void Reset();
122
123 private:
124
125   // Undefined
126   MessageBuffer(const MessageBuffer&);
127
128   // Undefined
129   MessageBuffer& operator=(const MessageBuffer& rhs);
130
131   /**
132    * Helper to increase the capacity of the buffer.
133    * @pre The newCapacity is greater than mCapacity.
134    * @param[in] The newCapacity
135    */
136   void IncreaseCapacity( std::size_t newCapacity );
137
138 private:
139
140   std::size_t mInitialCapacity; ///< The capacity to allocate during first call to ReserveMessageSlot
141
142   WordType* mData;     ///< The data allocated for the message buffer
143   WordType* mNextSlot; ///< The next free location in the buffer
144
145   std::size_t mCapacity; ///< The memory allocated with respect to sizeof(WordType)
146   std::size_t mSize;     ///< The memory reserved for messages with respect to sizeof(WordType)
147 };
148
149 } // namespace Internal
150
151 } // namespace Dali
152
153 #endif // DALI_INTERNAL_MESSAGE_BUFFER_H