• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 META_NS::GetValue(d->GetProperty<Type>(name));
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 (auto p = d->GetProperty<META_NS::SharedPtrIInterface>(name)) {
36         res = interface_pointer_cast<Interface>(p->GetValue());
37     }
38     return res;
39 }
40 
ComponentName(BASE_NS::string_view path)41 inline BASE_NS::string_view ComponentName(BASE_NS::string_view path)
42 {
43     return path.substr(0, path.find_first_of('.'));
44 }
45 
FullPropertyName(BASE_NS::string_view path)46 inline BASE_NS::string_view FullPropertyName(BASE_NS::string_view path)
47 {
48     auto pos = path.find_first_of('.');
49     if (pos != BASE_NS::string_view::npos) {
50         return path.substr(pos + 1);
51     }
52     return path;
53 }
54 
PropertyName(BASE_NS::string_view path)55 inline BASE_NS::string_view PropertyName(BASE_NS::string_view path)
56 {
57     auto pos = path.find_last_of('.');
58     if (pos != BASE_NS::string_view::npos) {
59         return path.substr(pos + 1);
60     }
61     return path;
62 }
63 
NormalisePath(BASE_NS::string_view path)64 inline BASE_NS::string_view NormalisePath(BASE_NS::string_view path)
65 {
66     if (path.empty() || path == "/") {
67         return "/";
68     }
69     if (path.ends_with('/')) {
70         path.remove_suffix(1);
71     }
72     return path;
73 }
74 
ParentPath(BASE_NS::string_view path)75 inline BASE_NS::string_view ParentPath(BASE_NS::string_view path)
76 {
77     auto pos = path.find_last_of('/');
78     if (pos != BASE_NS::string_view::npos) {
79         return path.substr(0, pos);
80     }
81     return {};
82 }
83 
EntityName(BASE_NS::string_view path)84 inline BASE_NS::string_view EntityName(BASE_NS::string_view path)
85 {
86     auto pos = path.find_last_of('/');
87     if (pos != BASE_NS::string_view::npos) {
88         return path.substr(pos + 1);
89     }
90     return path;
91 }
92 
FirstSegment(BASE_NS::string_view path)93 inline BASE_NS::string_view FirstSegment(BASE_NS::string_view path)
94 {
95     // skip starting /
96     if (path.starts_with('/')) {
97         path.remove_prefix(1);
98     }
99     return path.substr(0, path.find_first_of('/'));
100 }
101 
102 SCENE_END_NAMESPACE()
103 
104 #endif