xref: /aosp_15_r20/system/libfmq/include/fmq/AidlMQDescriptorShimCpp.h (revision be431cd81a9a2349eaea34eb56fcf6d1608da596)
1 /*
2  * Copyright (C) 2020 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 #pragma once
17 #include <cutils/native_handle.h>
18 #include <limits>
19 #include <type_traits>
20 
21 #include <android/hardware/common/fmq/MQDescriptor.h>
22 #include <fmq/MQDescriptorBase.h>
23 
24 #include "AidlMQDescriptorShimBase.h"
25 
26 namespace android {
27 namespace details {
28 
29 using ::android::hardware::MQFlavor;
30 
31 struct BackendTypesStoreCpp {
32     template <typename T, typename flavor>
33     using MQDescriptorType = android::hardware::common::fmq::MQDescriptor<T, flavor>;
34     using SynchronizedReadWriteType = android::hardware::common::fmq::SynchronizedReadWrite;
35     using UnsynchronizedWriteType = android::hardware::common::fmq::UnsynchronizedWrite;
36 };
37 
38 template <typename T, MQFlavor flavor>
39 struct AidlMQDescriptorShimCpp : public AidlMQDescriptorShimBase<T, flavor, BackendTypesStoreCpp> {
40     // Takes ownership of handle
41     AidlMQDescriptorShimCpp(const std::vector<android::hardware::GrantorDescriptor>& grantors,
42                             native_handle_t* nHandle, size_t size);
43 
44     // Takes ownership of handle
45     AidlMQDescriptorShimCpp(
46             const android::hardware::common::fmq::MQDescriptor<
47                     T, typename std::conditional<
48                                flavor == hardware::kSynchronizedReadWrite,
49                                android::hardware::common::fmq::SynchronizedReadWrite,
50                                android::hardware::common::fmq::UnsynchronizedWrite>::type>& desc);
51 
52     // Takes ownership of handle
53     AidlMQDescriptorShimCpp(size_t bufferSize, native_handle_t* nHandle, size_t messageSize,
54                             bool configureEventFlag = false);
55 
AidlMQDescriptorShimCppAidlMQDescriptorShimCpp56     explicit AidlMQDescriptorShimCpp(const AidlMQDescriptorShimCpp& other)
57         : AidlMQDescriptorShimCpp(0, nullptr, 0) {
58         *this = other;
59     }
60 };
61 
62 template <typename T, MQFlavor flavor>
AidlMQDescriptorShimCpp(const android::hardware::common::fmq::MQDescriptor<T,typename std::conditional<flavor==hardware::kSynchronizedReadWrite,android::hardware::common::fmq::SynchronizedReadWrite,android::hardware::common::fmq::UnsynchronizedWrite>::type> & desc)63 AidlMQDescriptorShimCpp<T, flavor>::AidlMQDescriptorShimCpp(
64         const android::hardware::common::fmq::MQDescriptor<
65                 T, typename std::conditional<
66                            flavor == hardware::kSynchronizedReadWrite,
67                            android::hardware::common::fmq::SynchronizedReadWrite,
68                            android::hardware::common::fmq::UnsynchronizedWrite>::type>& desc)
69     : AidlMQDescriptorShimBase<T, flavor, BackendTypesStoreCpp>(desc) {}
70 
71 template <typename T, MQFlavor flavor>
AidlMQDescriptorShimCpp(const std::vector<android::hardware::GrantorDescriptor> & grantors,native_handle_t * nhandle,size_t size)72 AidlMQDescriptorShimCpp<T, flavor>::AidlMQDescriptorShimCpp(
73         const std::vector<android::hardware::GrantorDescriptor>& grantors, native_handle_t* nhandle,
74         size_t size)
75     : AidlMQDescriptorShimBase<T, flavor, BackendTypesStoreCpp>(grantors, nhandle, size) {}
76 
77 template <typename T, MQFlavor flavor>
AidlMQDescriptorShimCpp(size_t bufferSize,native_handle_t * nHandle,size_t messageSize,bool configureEventFlag)78 AidlMQDescriptorShimCpp<T, flavor>::AidlMQDescriptorShimCpp(size_t bufferSize,
79                                                             native_handle_t* nHandle,
80                                                             size_t messageSize,
81                                                             bool configureEventFlag)
82     : AidlMQDescriptorShimBase<T, flavor, BackendTypesStoreCpp>(bufferSize, nHandle, messageSize,
83                                                                 configureEventFlag) {}
84 
85 }  // namespace details
86 }  // namespace android
87