[dali_2.1.1] Merge branch 'devel/master'
[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) 2021 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 <cctype>
25 #include <sstream>
26 #include "dali/public-api/actors/actor.h"
27 #include "dali/public-api/common/dali-common.h"
28 #include "dali/public-api/rendering/renderer.h"
29
30 namespace Dali
31 {
32 namespace SceneLoader
33 {
34 /*
35  * @brief Fixed size backing buffer to use with std::ostreams where control over
36  *  allocations (which this does not make), is required.
37  * @note All stream insertions that would overflow the buffer that StreamBuffer
38  *  was created with, will fail.
39  */
40 class DALI_SCENE_LOADER_API StreamBuffer : public std::basic_streambuf<char>
41 {
42 public:
43   StreamBuffer(char* buffer, size_t size) noexcept(true);
44 };
45
46 /*
47  * @brief Wraps an ostream with a pre-allocated, fixed size backing buffer
48  *  which a message can be formatted into. Upon destruction, it throws a
49  *  DaliException with the message.
50  */
51 class DALI_SCENE_LOADER_API ExceptionFlinger
52 {
53 public:
54   enum
55   {
56     MESSAGE_BUFFER_SIZE = 512
57   };
58
59   ExceptionFlinger(const char* location) noexcept(true);
60
61   [[noreturn]] ~ExceptionFlinger() noexcept(false);
62
63   template<typename T>
64   ExceptionFlinger& operator<<(const T& rhs) noexcept(true)
65   {
66     mStream << rhs;
67     return *this;
68   }
69
70 private:
71   struct Impl
72   {
73     const char* mLocation;
74
75     [[noreturn]] ~Impl() noexcept(false);
76   };
77
78   static char* GetMessageBuffer() noexcept(true);
79
80   Impl         mImpl;
81   StreamBuffer mStreamBuffer;
82   std::ostream mStream;
83 };
84
85 /*
86  * @brief Formats the given printf-style varargs into a std::string.
87  */
88 DALI_SCENE_LOADER_API std::string FormatString(const char* format, ...);
89
90 /*
91  * @return The @n th bit in a bitmask.
92  */
93 DALI_SCENE_LOADER_API constexpr size_t NthBit(size_t n)
94 {
95   return 1 << n;
96 }
97
98 /*
99  * @return Whether all of @a mask 's bits are set on @a value.
100  */
101 inline 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 DALI_SCENE_LOADER_API constexpr uint32_t FourCC(const char (&fourCC)[5])
110 {
111   return (fourCC[3] << 24) | (fourCC[2] << 16) | (fourCC[1] << 8) | fourCC[0];
112 }
113
114 /*
115  * @brief Insensitive case compare function.
116  * @param[in] a, compare string
117  * @param[in] b, compare string
118  * @return true if strings are equal
119  */
120 inline DALI_SCENE_LOADER_API bool CaseInsensitiveCharacterCompare(unsigned char a, unsigned char b)
121 {
122   // Converts to lower case in the current locale.
123   return std::tolower(a) == std::tolower(b);
124 }
125
126 /*
127  * @return true if the lower cased ASCII strings are equal.
128  * @param[in] a, compare string
129  * @param[in] b, compare string
130  */
131 inline DALI_SCENE_LOADER_API bool CaseInsensitiveStringCompare(const std::string& a, const std::string& b)
132 {
133   bool result = false;
134   if(a.length() == b.length())
135   {
136     result = std::equal(a.begin(), a.end(), b.begin(), &CaseInsensitiveCharacterCompare);
137   }
138   return result;
139 }
140
141 /*
142  * @brief Attempts to load the contents of a text file; returns empty string on
143  *  failure. A pointer to a boolean may be passed in @a fail; this will be set
144  *  to true in case of failure (should only be checked if the returned string
145  *  was empty()).
146  */
147 DALI_SCENE_LOADER_API std::string LoadTextFile(const char* path, bool* fail = nullptr);
148
149 /*
150  * @brief Makes a number of calls to @a fn, passing to each one the given
151  *  @a actor then each of its children, in depth-first traversal.
152  * @note @a fn must not change the actor hierarchy during traversal.
153  * @note Use of a @a fn that is itself recursing in @a is also discouraged
154  *  for performance and stability reasons.
155  */
156 template<typename Func>
157 inline DALI_SCENE_LOADER_API void VisitActor(Actor a, Func fn)
158 {
159   fn(a);
160
161   unsigned int numChildren = a.GetChildCount();
162   for(unsigned int i = 0; i < numChildren; ++i)
163   {
164     VisitActor(a.GetChildAt(i), fn);
165   }
166 }
167
168 /*
169  * @brief Convenience function to set the given actor @a 's anchor point
170  *  and parent origin to center.
171  */
172 inline DALI_SCENE_LOADER_API void SetActorCentered(Actor a)
173 {
174   a.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
175   a.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
176 }
177
178 namespace TexturedQuadOptions
179 {
180 using Type = uint32_t;
181
182 enum DALI_SCENE_LOADER_API Values : Type
183 {
184   NONE          = 0x00,
185   FLIP_VERTICAL = 0x01,
186 };
187 } // namespace TexturedQuadOptions
188
189 /*
190  * @brief Makes... geometry for a textured quad.
191  */
192 DALI_SCENE_LOADER_API Geometry MakeTexturedQuadGeometry(TexturedQuadOptions::Type options = TexturedQuadOptions::NONE);
193
194 /*
195  * @brief Fixes the path of a file. Replaces the '\\' separator by the '/' one.
196  * @param[in,out] path The path to be fixed.
197  */
198 DALI_SCENE_LOADER_API void ToUnixFileSeparators(std::string& path);
199
200 } // namespace SceneLoader
201 } // namespace Dali
202
203 #endif /* DALI_SCENE_LOADER_UTILS_H_ */