1 #ifndef DALI_SCENE3D_LOADER_UTILS_H_
2 #define DALI_SCENE3D_LOADER_UTILS_H_
4 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
28 #include <dali-scene3d/public-api/api.h>
30 namespace Dali::Scene3D::Loader
33 * @brief Fixed size backing buffer to use with std::ostreams where control over
34 * allocations (which this does not make), is required.
35 * @note All stream insertions that would overflow the buffer that StreamBuffer
36 * was created with, will fail.
38 class DALI_SCENE3D_API StreamBuffer : public std::basic_streambuf<char>
41 StreamBuffer(char* buffer, size_t size) noexcept(true);
45 * @brief Wraps an ostream with a pre-allocated, fixed size backing buffer
46 * which a message can be formatted into. Upon destruction, it throws a
47 * DaliException with the message.
49 class DALI_SCENE3D_API ExceptionFlinger
54 MESSAGE_BUFFER_SIZE = 512
57 ExceptionFlinger(const char* location) noexcept(true);
59 [[noreturn]] ~ExceptionFlinger() noexcept(false);
62 ExceptionFlinger& operator<<(const T& rhs) noexcept(true)
71 const char* mLocation;
73 [[noreturn]] ~Impl() noexcept(false);
76 static char* GetMessageBuffer() noexcept(true);
79 StreamBuffer mStreamBuffer;
84 * @brief Formats the given printf-style varargs into a std::string.
86 DALI_SCENE3D_API std::string FormatString(const char* format, ...);
89 * @return The @n th bit in a bitmask.
91 DALI_SCENE3D_API constexpr size_t NthBit(size_t n)
97 * @return Whether all of @a mask 's bits are set on @a value.
99 inline DALI_SCENE3D_API bool MaskMatch(uint32_t value, uint32_t mask)
101 return (value & mask) == mask;
105 * @brief Convert a four-letter(, null-terminated) string literal into a uint32_t.
107 inline DALI_SCENE3D_API constexpr uint32_t FourCC(const char (&fourCC)[5])
109 return (fourCC[3] << 24) | (fourCC[2] << 16) | (fourCC[1] << 8) | fourCC[0];
113 * @brief Insensitive case compare function.
114 * @param[in] a, compare string
115 * @param[in] b, compare string
116 * @return true if strings are equal
118 inline DALI_SCENE3D_API bool CaseInsensitiveCharacterCompare(unsigned char a, unsigned char b)
120 // Converts to lower case in the current locale.
121 return std::tolower(a) == std::tolower(b);
125 * @return true if the lower cased ASCII strings are equal.
126 * @param[in] a, compare string
127 * @param[in] b, compare string
129 inline DALI_SCENE3D_API bool CaseInsensitiveStringCompare(const std::string& a, const std::string& b)
132 if(a.length() == b.length())
134 result = std::equal(a.begin(), a.end(), b.begin(), &CaseInsensitiveCharacterCompare);
140 * @brief Attempts to load the contents of a text file; returns empty string on
141 * failure. A pointer to a boolean may be passed in @a fail; this will be set
142 * to true in case of failure (should only be checked if the returned string
145 DALI_SCENE3D_API std::string LoadTextFile(const char* path, bool* fail = nullptr);
148 * @brief Makes a number of calls to @a fn, passing to each one the given
149 * @a actor then each of its children, in depth-first traversal.
150 * @note @a fn must not change the actor hierarchy during traversal.
151 * @note Use of a @a fn that is itself recursing in @a is also discouraged
152 * for performance and stability reasons.
154 template<typename Func>
155 inline DALI_SCENE3D_API void VisitActor(Actor a, Func fn)
159 unsigned int numChildren = a.GetChildCount();
160 for(unsigned int i = 0; i < numChildren; ++i)
162 VisitActor(a.GetChildAt(i), fn);
167 * @brief Convenience function to set the given actor @a 's anchor point
168 * and parent origin to center.
170 inline DALI_SCENE3D_API void SetActorCentered(Actor a)
172 a.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
173 a.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
176 namespace TexturedQuadOptions
178 using Type = uint32_t;
180 enum DALI_SCENE3D_API Values : Type
183 FLIP_VERTICAL = 0x01,
185 } // namespace TexturedQuadOptions
188 * @brief Makes... geometry for a textured quad.
190 DALI_SCENE3D_API Geometry MakeTexturedQuadGeometry(TexturedQuadOptions::Type options = TexturedQuadOptions::NONE);
193 * @brief Fixes the path of a file. Replaces the '\\' separator by the '/' one.
194 * @param[in,out] path The path to be fixed.
196 DALI_SCENE3D_API void ToUnixFileSeparators(std::string& path);
198 } // namespace Dali::Scene3D::Loader
200 #endif /* DALI_SCENE3D_LOADER_UTILS_H_ */