xref: /aosp_15_r20/external/tink/cc/internal/serializer_index.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 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 //
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 #ifndef TINK_INTERNAL_SERIALIZER_INDEX_H_
18 #define TINK_INTERNAL_SERIALIZER_INDEX_H_
19 
20 #include <string>
21 #include <typeindex>
22 
23 #include "tink/internal/serialization.h"
24 #include "tink/key.h"
25 #include "tink/parameters.h"
26 
27 namespace crypto {
28 namespace tink {
29 namespace internal {
30 
31 class SerializerIndex {
32  public:
33   // Create registry lookup key for the combination of the `KeyOrParameterT` and
34   // `SerializationT` types. Useful for key and parameters serializers.
35   template <typename KeyOrParameterT, typename SerializationT>
Create()36   static SerializerIndex Create() {
37     return SerializerIndex(std::type_index(typeid(KeyOrParameterT)),
38                            std::type_index(typeid(SerializationT)));
39   }
40 
41   // Create registry lookup key for `SerializationT` type and `parameters`.
42   // Useful for the serialization registry.
43   template <typename SerializationT>
Create(const Parameters & parameters)44   static SerializerIndex Create(const Parameters& parameters) {
45     return SerializerIndex(std::type_index(typeid(parameters)),
46                            std::type_index(typeid(SerializationT)));
47   }
48 
49   // Create registry lookup key for `SerializationT` type and `key`. Useful for
50   // the serialization registry.
51   template <typename SerializationT>
Create(const Key & key)52   static SerializerIndex Create(const Key& key) {
53     return SerializerIndex(std::type_index(typeid(key)),
54                            std::type_index(typeid(SerializationT)));
55   }
56 
57   // Returns true if key/parameters index and serialization type index match.
58   bool operator==(const SerializerIndex& other) const {
59     return kp_index_ == other.kp_index_ &&
60            serialization_index_ == other.serialization_index_;
61   }
62 
63   // Required function to make `SerializerIndex` hashable for Abseil hash maps.
64   template <typename H>
AbslHashValue(H h,const SerializerIndex & index)65   friend H AbslHashValue(H h, const SerializerIndex& index) {
66     return H::combine(std::move(h), index.kp_index_,
67                       index.serialization_index_);
68   }
69 
70  private:
SerializerIndex(std::type_index kp_index,std::type_index serialization_index)71   SerializerIndex(std::type_index kp_index, std::type_index serialization_index)
72       : kp_index_(kp_index), serialization_index_(serialization_index) {}
73 
74   std::type_index kp_index_;
75   std::type_index serialization_index_;
76 };
77 
78 }  // namespace internal
79 }  // namespace tink
80 }  // namespace crypto
81 
82 #endif  // TINK_INTERNAL_SERIALIZER_INDEX_H_
83