Revert "License conversion from Flora to Apache 2.0"
[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) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // EXTERNAL INCLUDES
21 #include <cstddef>
22
23 namespace Dali
24 {
25
26 namespace Internal
27 {
28
29 /**
30  * Utility class to reserve a buffer for storing messages.
31  */
32 class MessageBuffer
33 {
34 public:
35
36   /**
37    * Create a new MessageBuffer
38    * @param[in] The smallest capacity which the buffer will allocate, with respect to the size of type "char".
39    * @note The buffer will not allocate memory until the first call to ReserveMessageSlot().
40    */
41   MessageBuffer( std::size_t initialCapacity );
42
43   /**
44    * Non-virtual destructor; not suitable as a base class
45    */
46   ~MessageBuffer();
47
48   /**
49    * Reserve space for another message in the buffer.
50    * @pre size is greater than zero.
51    * @param[in] The message size with respect to the size of type "char".
52    * @return A pointer to the address allocated for the message.
53    */
54   unsigned int* ReserveMessageSlot( std::size_t size );
55
56   /**
57    * Query the capacity of the message buffer.
58    * @return The capacity with respect to the size of type "char".
59    */
60   std::size_t GetCapacity() const;
61
62   /**
63    * Used to iterate though the messages in the buffer.
64    */
65   class Iterator
66   {
67   public:
68
69     // Constructor
70     Iterator(unsigned int* current);
71
72     // Inlined for performance
73     bool IsValid()
74     {
75       // Valid until end marker has been found
76       return 0 != mMessageSize;
77     }
78
79     // Inlined for performance
80     unsigned int* Get()
81     {
82       return ( 0 != mMessageSize ) ? mCurrent : NULL;
83     }
84
85     // Inlined for performance
86     void Next()
87     {
88       // Jump to next object and read size
89       mCurrent += mMessageSize;
90       mMessageSize = *mCurrent++;
91     }
92
93     // Copy constructor
94     Iterator(const Iterator& copy);
95
96   private:
97
98     // Undefined
99     Iterator& operator=(const Iterator& rhs);
100
101   private:
102
103     unsigned int* mCurrent;
104     unsigned int mMessageSize;
105   };
106
107   /**
108    * Returns an iterator to the first message in the buffer.
109    * There is no past-the-end iterator; use Iterator::IsValid() to determine when the has been reached.
110    * @note Adding more messages with ReserveMessageSlot() may corrupt this iterator.
111    * @return The iterator.
112    */
113   Iterator Begin() const;
114
115   /**
116    * Sets the size of the buffer to zero (does not deallocate memory)
117    */
118   void Reset();
119
120 private:
121
122   // Undefined
123   MessageBuffer(const MessageBuffer&);
124
125   // Undefined
126   MessageBuffer& operator=(const MessageBuffer& rhs);
127
128   /**
129    * Helper to increase the capacity of the buffer.
130    * @pre The newCapacity is greater than mCapacity.
131    * @param[in] The newCapacity
132    */
133   void IncreaseCapacity( std::size_t newCapacity );
134
135 private:
136
137   std::size_t mInitialCapacity; ///< The capacity to allocate during first call to ReserveMessageSlot
138
139   unsigned int* mData;     ///< The data allocated for the message buffer
140   unsigned int* mNextSlot; ///< The next free location in the buffer
141
142   std::size_t mCapacity; ///< The memory allocated with respect to sizeof(unsigned int)
143   std::size_t mSize;     ///< The memory reserved for messages with respect to sizeof(unsigned int)
144 };
145
146 } // namespace Internal
147
148 } // namespace Dali
149
150 #endif // __DALI_INTERNAL_MESSAGE_BUFFER_H__