1 /*
2  * Copyright 2019 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 #pragma once
18 
19 #include <optional>
20 
21 namespace bluetooth {
22 namespace packet {
23 namespace parser {
24 
25 // Checks for Initialize(), AddByte(), and GetChecksum().
26 // T and TRET are the checksum class Type and the checksum return type
27 // C and CRET are the substituted types for T and TRET
28 template <typename T, typename TRET>
29 class ChecksumTypeChecker {
30  public:
31   template <class C, void (C::*)()>
32   struct InitializeChecker {};
33 
34   template <class C, void (C::*)(uint8_t byte)>
35   struct AddByteChecker {};
36 
37   template <class C, typename CRET, CRET (C::*)() const>
38   struct GetChecksumChecker {};
39 
40   // If all the methods are defined, this one matches
41   template <class C, typename CRET>
42   static int Test(InitializeChecker<C, &C::Initialize>*, AddByteChecker<C, &C::AddByte>*,
43                   GetChecksumChecker<C, CRET, &C::GetChecksum>*);
44 
45   // This one matches everything else
46   template <class C, typename CRET>
47   static char Test(...);
48 
49   // This checks which template was matched
50   static constexpr bool value = (sizeof(Test<T, TRET>(0, 0, 0)) == sizeof(int));
51 };
52 }  // namespace parser
53 }  // namespace packet
54 }  // namespace bluetooth
55