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