xref: /aosp_15_r20/system/update_engine/liburing_cpp/include/liburing_cpp/IoUringCQE.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1 //
2 // Copyright (C) 2022 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 __IO_URING_CQE_CPP_H
18 #define __IO_URING_CQE_CPP_H
19 
20 #include <stdint.h>
21 
22 #include <type_traits>
23 
24 namespace io_uring_cpp {
25 
26 struct IoUringCQE {
27   int32_t res;
28   uint32_t flags;
29   template <typename T>
GetDataIoUringCQE30   T GetData() const {
31     static_assert(
32         std::is_trivially_copy_constructible_v<T>,
33         "Only trivially copiable types can be passed for io_uring data");
34     static_assert(sizeof(T) <= 8,
35                   "io_uring SQE's data field has size of 8 bytes, can't pass "
36                   "data larger than that.");
37     return *reinterpret_cast<const T*>(&userdata);
38   }
39 
IoUringCQEIoUringCQE40   constexpr IoUringCQE(int32_t res, uint32_t flags, uint64_t userdata)
41       : res(res), flags(flags), userdata(userdata) {}
42 
43  private:
44   uint64_t userdata;
45 };
46 
47 }  // namespace io_uring_cpp
48 
49 #endif
50