Added libdli to dali-toolkit as dali-scene-loader.
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / public-api / utils.h
1 #ifndef DALI_SCENE_LOADER_UTILS_H_
2 #define DALI_SCENE_LOADER_UTILS_H_
3 /*
4  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // INTERNAL INCLUDES
21 #include "dali-scene-loader/public-api/api.h"
22
23 // EXTERNAL INCLUDES
24 #include "dali/public-api/actors/actor.h"
25 #include "dali/public-api/rendering/renderer.h"
26 #include "dali/public-api/common/dali-common.h"
27 #include <sstream>
28 #include <cctype>
29
30 namespace Dali
31 {
32 namespace SceneLoader
33 {
34
35 /*
36  * @brief Fixed size backing buffer to use with std::ostreams where control over
37  *  allocations (which this does not make), is required.
38  * @note All stream insertions that would overflow the buffer that StreamBuffer
39  *  was created with, will fail.
40  */
41 class DALI_SCENE_LOADER_API StreamBuffer : public std::basic_streambuf<char>
42 {
43 public:
44   StreamBuffer(char* buffer, size_t size)
45   {
46     setp(buffer, buffer + size);
47   }
48 };
49
50 /*
51  * @brief Wraps an ostream with a pre-allocated, fixed size backing buffer
52  *  which a message can be formatted into. Upon destruction, it throws a
53  *  DaliException with the message.
54  */
55 class DALI_SCENE_LOADER_API ExceptionFlinger
56 {
57 public:
58   enum { MESSAGE_BUFFER_SIZE = 512 };
59
60   ExceptionFlinger(const char* location)
61   : mLocation(location),
62     mStreamBuffer(GetMessageBuffer(), MESSAGE_BUFFER_SIZE - 1),
63     mStream(&mStreamBuffer)
64   {}
65
66   ~ExceptionFlinger() noexcept(false)
67   {
68     operator<<('\0');
69     throw DaliException(mLocation, GetMessageBuffer());
70   }
71
72   template <typename T>
73   ExceptionFlinger& operator<<(const T& rhs)
74   {
75     mStream << rhs;
76     return *this;
77   }
78
79 private:
80   static char* GetMessageBuffer();
81
82   const char* mLocation;
83   StreamBuffer mStreamBuffer;
84   std::ostream mStream;
85 };
86
87 /*
88  * @brief Formats the given printf-style varargs into a std::string.
89  */
90 DALI_SCENE_LOADER_API std::string FormatString(const char* format, ...);
91
92 /*
93  * @return The @n th bit in a bitmask.
94  */
95 DALI_SCENE_LOADER_API constexpr size_t NthBit(size_t n) { return 1 << n; }
96
97 /*
98  * @return Whether all of @a mask 's bits are set on @a value.
99  */
100 inline
101 DALI_SCENE_LOADER_API bool MaskMatch(uint32_t value, uint32_t mask)
102 {
103   return (value & mask) == mask;
104 }
105
106 /*
107  * @brief Convert a four-letter(, null-terminated) string literal into a uint32_t.
108  */
109 inline
110 DALI_SCENE_LOADER_API constexpr uint32_t FourCC(const char(&fourCC)[5])
111 {
112   return (fourCC[3] << 24) | (fourCC[2] << 16) | (fourCC[1] << 8) | fourCC[0];
113 }
114
115 /*
116  * @brief Insensitive case compare function.
117  * @param[in] a, compare string
118  * @param[in] b, compare string
119  * @return true if strings are equal
120  */
121 inline
122 DALI_SCENE_LOADER_API bool CaseInsensitiveCharacterCompare( unsigned char a, unsigned char b )
123 {
124   // Converts to lower case in the current locale.
125   return std::tolower( a ) == std::tolower( b );
126 }
127
128 /*
129  * @return true if the lower cased ASCII strings are equal.
130  * @param[in] a, compare string
131  * @param[in] b, compare string
132  */
133 inline
134 DALI_SCENE_LOADER_API bool CaseInsensitiveStringCompare( const std::string& a, const std::string& b )
135 {
136   bool result = false;
137   if( a.length() == b.length() )
138   {
139     result = std::equal( a.begin(), a.end(), b.begin(), &CaseInsensitiveCharacterCompare );
140   }
141   return result;
142 }
143
144 /*
145  * @brief Attempts to load the contents of a text file; returns empty string on
146  *  failure. A pointer to a boolean may be passed in @a fail; this will be set
147  *  to true in case of failure (should only be checked if the returned string
148  *  was empty()).
149  */
150 DALI_SCENE_LOADER_API std::string LoadTextFile(const char* path, bool* fail = nullptr);
151
152 /*
153  * @brief Makes a number of calls to @a fn, passing to each one the given
154  *  @a actor then each of its children, in depth-first traversal.
155  * @note @a fn must not change the actor hierarchy during traversal.
156  * @note Use of a @a fn that is itself recursing in @a is also discouraged
157  *  for performance and stability reasons.
158  */
159 template <typename Func>
160 inline
161 DALI_SCENE_LOADER_API void VisitActor(Actor a, Func fn)
162 {
163   fn(a);
164
165   unsigned int numChildren = a.GetChildCount();
166   for(unsigned int i = 0; i < numChildren; ++i)
167   {
168     VisitActor(a.GetChildAt(i), fn);
169   }
170 }
171
172 /*
173  * @brief Convenience function to set the given actor @a 's anchor point
174  *  and parent origin to center.
175  */
176 inline
177 DALI_SCENE_LOADER_API void SetActorCentered(Actor a)
178 {
179   a.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
180   a.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
181 }
182
183 namespace TexturedQuadOptions
184 {
185 using Type = uint32_t;
186
187 enum DALI_SCENE_LOADER_API Values : Type
188 {
189   NONE = 0x00,
190   FLIP_VERTICAL = 0x01,
191 };
192 }
193
194 /*
195  * @brief Makes... geometry for a textured quad.
196  */
197 DALI_SCENE_LOADER_API Geometry MakeTexturedQuadGeometry(TexturedQuadOptions::Type options = TexturedQuadOptions::NONE);
198
199 /*
200  * @brief Fixes the path of a file. Replaces the '\\' separator by the '/' one.
201  * @param[in,out] path The path to be fixed.
202  */
203 DALI_SCENE_LOADER_API void ToUnixFileSeparators( std::string& path );
204
205 }
206 }
207
208 #endif /* DALI_SCENE_LOADER_UTILS_H_ */