• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_TRACE_PROCESSOR_SQLITE_BINDINGS_SQLITE_FUNCTION_H_
18 #define SRC_TRACE_PROCESSOR_SQLITE_BINDINGS_SQLITE_FUNCTION_H_
19 
20 #include <sqlite3.h>  // IWYU pragma: export
21 
22 namespace perfetto::trace_processor {
23 
24 // Prototype for a function which can be registered with SQLite.
25 //
26 // See https://www.sqlite.org/c3ref/create_function.html for details on how
27 // to implement the methods of this class.
28 template <typename Impl>
29 class SqliteFunction {
30  public:
31   // The type of the context object which will be passed to the function.
32   // Can be redefined in any sub-classes to override the context.
33   using UserDataContext = void;
34 
35   // The xStep function which will be executed by SQLite to add a row of values
36   // to the current window.
37   //
38   // Implementations MUST define this function themselves; this function is
39   // declared but *not* defined so linker errors will be thrown if not defined.
40   static void Step(sqlite3_context*, int argc, sqlite3_value** argv);
41 
42   // Returns the pointer to the user data structure which is passed when
43   // creating the function.
GetUserData(sqlite3_context * ctx)44   static auto GetUserData(sqlite3_context* ctx) {
45     return static_cast<typename Impl::UserDataContext*>(sqlite3_user_data(ctx));
46   }
47 };
48 
49 }  // namespace perfetto::trace_processor
50 
51 #endif  // SRC_TRACE_PROCESSOR_SQLITE_BINDINGS_SQLITE_FUNCTION_H_
52