xref: /aosp_15_r20/external/icing/icing/query/advanced_query_parser/function.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2023 Google LLC
2 //
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 #ifndef ICING_QUERY_ADVANCED_QUERY_PARSER_FUNCTION_H_
15 #define ICING_QUERY_ADVANCED_QUERY_PARSER_FUNCTION_H_
16 
17 #include <functional>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 #include "icing/text_classifier/lib3/utils/base/statusor.h"
23 #include "icing/query/advanced_query_parser/param.h"
24 #include "icing/query/advanced_query_parser/pending-value.h"
25 
26 namespace icing {
27 namespace lib {
28 
29 class Function {
30  public:
31   using EvalFunction = std::function<libtextclassifier3::StatusOr<PendingValue>(
32       std::vector<PendingValue>&&)>;
33 
34   static libtextclassifier3::StatusOr<Function> Create(
35       DataType return_type, std::string name, std::vector<Param> params,
36       EvalFunction eval);
37 
38   Function(const Function& rhs) = default;
39   Function(Function&& rhs) = default;
40 
41   Function& operator=(const Function& rhs) = default;
42   Function& operator=(Function&& rhs) = default;
43 
name()44   const std::string& name() const { return name_; }
45 
46   libtextclassifier3::StatusOr<PendingValue> Eval(
47       std::vector<PendingValue>&& args) const;
48 
49   libtextclassifier3::StatusOr<DataType> get_param_type(int i) const;
50 
51  private:
Function(DataType return_type,std::string name,std::vector<Param> params,EvalFunction eval)52   Function(DataType return_type, std::string name, std::vector<Param> params,
53            EvalFunction eval)
54       : name_(std::move(name)),
55         params_(std::move(params)),
56         eval_(std::move(eval)),
57         return_type_(return_type) {}
58 
59   std::string name_;
60   std::vector<Param> params_;
61   EvalFunction eval_;
62   DataType return_type_;
63 };
64 
65 }  // namespace lib
66 }  // namespace icing
67 
68 #endif  // ICING_QUERY_ADVANCED_QUERY_PARSER_FUNCTION_H_
69