1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef SCENE_EXT_UTIL_H
17 #define SCENE_EXT_UTIL_H
18
19 #include <meta/api/future.h>
20 #include <meta/api/task_queue.h>
21
SCENE_BEGIN_NAMESPACE()22 SCENE_BEGIN_NAMESPACE()
23
24 template<typename Type>
25 Type GetBuildArg(const META_NS::IMetadata::Ptr& d, BASE_NS::string_view name)
26 {
27 return d ? META_NS::GetValue(d->GetProperty<Type>(name)) : Type {};
28 }
29
30 template<typename Interface>
GetInterfaceBuildArg(const META_NS::IMetadata::Ptr & d,BASE_NS::string_view name)31 typename BASE_NS::shared_ptr<Interface> GetInterfaceBuildArg(
32 const META_NS::IMetadata::Ptr& d, BASE_NS::string_view name)
33 {
34 BASE_NS::shared_ptr<Interface> res;
35 if (d) {
36 if (auto p = d->GetProperty<META_NS::SharedPtrIInterface>(name)) {
37 res = interface_pointer_cast<Interface>(p->GetValue());
38 }
39 }
40 return res;
41 }
42
ComponentName(BASE_NS::string_view path)43 inline BASE_NS::string_view ComponentName(BASE_NS::string_view path)
44 {
45 return path.substr(0, path.find_first_of('.'));
46 }
47
FullPropertyName(BASE_NS::string_view path)48 inline BASE_NS::string_view FullPropertyName(BASE_NS::string_view path)
49 {
50 auto pos = path.find_first_of('.');
51 if (pos != BASE_NS::string_view::npos) {
52 return path.substr(pos + 1);
53 }
54 return path;
55 }
56
PropertyName(BASE_NS::string_view path)57 inline BASE_NS::string_view PropertyName(BASE_NS::string_view path)
58 {
59 auto pos = path.find_last_of('.');
60 if (pos != BASE_NS::string_view::npos) {
61 return path.substr(pos + 1);
62 }
63 return path;
64 }
65
NormalisePath(BASE_NS::string_view path)66 inline BASE_NS::string_view NormalisePath(BASE_NS::string_view path)
67 {
68 if (path.empty() || path == "/") {
69 return "/";
70 }
71 if (path.ends_with('/')) {
72 path.remove_suffix(1);
73 }
74 return path;
75 }
76
ParentPath(BASE_NS::string_view path)77 inline BASE_NS::string_view ParentPath(BASE_NS::string_view path)
78 {
79 if (path.ends_with('/')) {
80 path.remove_suffix(1);
81 }
82 auto pos = path.find_last_of('/');
83 if (pos != BASE_NS::string_view::npos) {
84 return path.substr(0, pos);
85 }
86 return {};
87 }
88
EntityName(BASE_NS::string_view path)89 inline BASE_NS::string_view EntityName(BASE_NS::string_view path)
90 {
91 if (path.ends_with('/')) {
92 path.remove_suffix(1);
93 }
94 auto pos = path.find_last_of('/');
95 if (pos != BASE_NS::string_view::npos) {
96 return path.substr(pos + 1);
97 }
98 return path;
99 }
100
FirstSegment(BASE_NS::string_view path)101 inline BASE_NS::string_view FirstSegment(BASE_NS::string_view path)
102 {
103 // skip starting /
104 if (path.starts_with('/')) {
105 path.remove_prefix(1);
106 }
107 return path.substr(0, path.find_first_of('/'));
108 }
109
110 SCENE_END_NAMESPACE()
111
112 #endif