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