1 /*
2 * Copyright (C) 2023 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 API_CORE_ECS_ISYSTEM_GRAPH_H
17 #define API_CORE_ECS_ISYSTEM_GRAPH_H
18
19 #include <base/containers/refcnt_ptr.h>
20 #include <base/containers/string.h>
21 #include <base/containers/string_view.h>
22 #include <base/containers/unique_ptr.h>
23 #include <core/ecs/intf_component_manager.h>
24 #include <core/namespace.h>
25 #include <core/plugin/intf_interface.h>
26
27 CORE_BEGIN_NAMESPACE()
28 class IFileManager;
29 class IEcs;
30 class ISystem;
31 class IPluginRegister;
32
33 /** @ingroup group_ecs_isystemgraph */
34 /**
35 * ISystemGraphLoader.
36 * Interface class for creating a SystemGraphDesc from a JSON file.
37 */
38 class ISystemGraphLoader {
39 public:
40 /** Describes result of the loading operation. */
41 struct LoadResult {
42 LoadResult() = default;
LoadResultLoadResult43 explicit LoadResult(BASE_NS::string&& error) : success(false), error(BASE_NS::move(error)) {}
44
45 /** Indicates, whether the parsing operation is successful. */
46 bool success { true };
47
48 /** In case of parsing error, contains the description of the error. */
49 BASE_NS::string error;
50 };
51
52 /** Load a system graph description from a file.
53 * @param uri URI to a file containing the system graph description in JSON format.
54 * @param ecs Ecs instance to populate with systems and components.
55 * @return If loading fails LoadResult::success is false and LoadResult::error contains an error message.
56 */
57 virtual LoadResult Load(BASE_NS::string_view uri, IEcs& ecs) = 0;
58
59 /** Load a system graph description from a JSON string.
60 * @param json String containing the system graph description in JSON format.
61 * @param ecs Ecs instance to populate with systems and components.
62 * @return If loading fails LoadResult::success is false and LoadResult::error contains an error message.
63 */
64 virtual LoadResult LoadString(BASE_NS::string_view json, IEcs& ecs) = 0;
65
66 struct Deleter {
67 constexpr Deleter() noexcept = default;
operatorDeleter68 void operator()(ISystemGraphLoader* ptr) const
69 {
70 ptr->Destroy();
71 }
72 };
73 using Ptr = BASE_NS::unique_ptr<ISystemGraphLoader, struct Deleter>;
74
75 protected:
76 ISystemGraphLoader() = default;
77 virtual ~ISystemGraphLoader() = default;
78 virtual void Destroy() = 0;
79 };
80
81 // factory inteface.
82 class ISystemGraphLoaderFactory : public IInterface {
83 public:
84 static constexpr auto UID = BASE_NS::Uid { "26f8f9de-53e9-4a09-85a6-45b69eb3efb6" };
85
86 using Ptr = BASE_NS::refcnt_ptr<ISystemGraphLoaderFactory>;
87
88 virtual ISystemGraphLoader::Ptr Create(IFileManager& fileManager) = 0;
89
90 protected:
91 ISystemGraphLoaderFactory() = default;
92 virtual ~ISystemGraphLoaderFactory() = default;
93 };
94
GetName(const ISystemGraphLoaderFactory *)95 inline constexpr BASE_NS::string_view GetName(const ISystemGraphLoaderFactory*)
96 {
97 return "ISystemGraphLoaderFactory";
98 }
99 CORE_END_NAMESPACE()
100
101 #endif // API_CORE_ECS_ISYSTEM_GRAPH_H
102