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