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 META_INTERFACE_CALL_CONTEXT_H
17 #define META_INTERFACE_CALL_CONTEXT_H
18 
19 #include <base/containers/string.h>
20 #include <core/plugin/intf_interface.h>
21 
22 #include <meta/base/interface_macros.h>
23 #include <meta/base/namespace.h>
24 #include <meta/base/types.h>
25 #include <meta/interface/intf_any.h>
26 
27 META_BEGIN_NAMESPACE()
28 
29 struct ArgumentNameValue {
30     BASE_NS::string name;
31     IAny::Ptr value;
32 };
33 
34 /**
35  * @brief Interface that contains parameter names and types, arguments and result type and value.
36  *        It is passed through when calling meta functions to provide arguments and other call context.
37  *        See meta/api/call_context.h for typed helper functions.
38  */
39 class ICallContext : public CORE_NS::IInterface {
40     META_INTERFACE(CORE_NS::IInterface, ICallContext, "2e9cac45-0e61-4152-8b2a-bc1c65fded3d")
41 public:
42     /**
43      * @brief Define parameter name, type and default value. The parameters are added in order.
44      * @param name Name of the parameter, fails if parameter already defined for same name.
45      * @param value Type and default value for the parameter.
46      * @return True on success
47      */
48     virtual bool DefineParameter(BASE_NS::string_view name, const IAny::Ptr& value) = 0;
49 
50     /**
51      * @brief Set argument value for parameter 'name'.
52      * @param name Name of the parameter, parameter must have been defined with the same name.
53      * @param value Value of the argument, the type must match with the define parameter type.
54      * @return True on success
55      */
56     virtual bool Set(BASE_NS::string_view name, const IAny& value) = 0;
57 
58     /**
59      * @brief Get argument value for parameter 'name'
60      * @param name Name of the parameter
61      * @return Any containing the argument value or nullptr if no such parameter
62      */
63     virtual IAny::Ptr Get(BASE_NS::string_view name) const = 0;
64 
65     /**
66      * @brief List all parameters, these should be in the function parameter order (left to right).
67      * @return List of parameters/argument values.
68      */
69     virtual BASE_NS::array_view<const ArgumentNameValue> GetParameters() const = 0;
70 
71     /**
72      * @brief Check if the call was successful.
73      * @return True if SetResult was called with correct result type.
74      */
75     virtual bool Succeeded() const = 0;
76 
77     /**
78      * @brief Define result type, null means void which is also the default.
79      * @param value Type of the result value, the value itself is overwritten by SetResult when setting result value.
80      * @return True on success.
81      */
82     virtual bool DefineResult(const IAny::Ptr& value) = 0;
83 
84     /**
85      * @brief Set result value
86      * @param value Return value, must match the defined result type.
87      * @return True on success
88      */
89     virtual bool SetResult(const IAny& value) = 0;
90 
91     /**
92      * @brief Set result value for void function
93      * @return True on success
94      */
95     virtual bool SetResult() = 0;
96 
97     /**
98      * @brief Get result value
99      * @return Any containing the result value or nullptr if void result value (ie. no result).
100      */
101     virtual IAny::Ptr GetResult() const = 0;
102 
103     /**
104      * @brief Reset internal state, so that the same context can be used again for calling.
105      * @note  This does not change defined parameters or return type, just resets arguments/call result.
106      */
107     virtual void Reset() = 0;
108 
109     /**
110      * @brief Report error in the context of the meta call (default implementation logs to console)
111      */
112     virtual void ReportError(BASE_NS::string_view error) = 0;
113 };
114 
115 META_END_NAMESPACE()
116 
117 META_TYPE(META_NS::ICallContext::Ptr)
118 META_TYPE(META_NS::ICallContext::ConstPtr)
119 META_TYPE(META_NS::ICallContext::WeakPtr)
120 META_TYPE(META_NS::ICallContext::ConstWeakPtr)
121 
122 #endif
123