xref: /aosp_15_r20/external/tink/cc/internal/parser_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_PARSER_INDEX_H_
18 #define TINK_INTERNAL_PARSER_INDEX_H_
19 
20 #include <string>
21 #include <typeindex>
22 
23 #include "absl/strings/string_view.h"
24 #include "tink/internal/serialization.h"
25 
26 namespace crypto {
27 namespace tink {
28 namespace internal {
29 
30 class ParserIndex {
31  public:
32   // Create registry lookup key for a `SerializationT` type object with
33   // `object_identifier`. Useful for key and parameters parsers.
34   template <typename SerializationT>
Create(absl::string_view object_identifier)35   static ParserIndex Create(absl::string_view object_identifier) {
36     return ParserIndex(std::type_index(typeid(SerializationT)),
37                        object_identifier);
38   }
39 
40   // Create registry lookup key for `serialization`. Useful for the
41   // serialization registry.
Create(const Serialization & serialization)42   static ParserIndex Create(const Serialization& serialization) {
43     return ParserIndex(std::type_index(typeid(serialization)),
44                        serialization.ObjectIdentifier());
45   }
46 
47   // Returns true if serialization type index and object identifier match.
48   bool operator==(const ParserIndex& other) const {
49     return index_ == other.index_ &&
50            object_identifier_ == other.object_identifier_;
51   }
52 
53   // Required function to make `ParserIndex` hashable for Abseil hash maps.
54   template <typename H>
AbslHashValue(H h,const ParserIndex & index)55   friend H AbslHashValue(H h, const ParserIndex& index) {
56     return H::combine(std::move(h), index.index_, index.object_identifier_);
57   }
58 
59  private:
ParserIndex(std::type_index index,absl::string_view object_identifier)60   ParserIndex(std::type_index index, absl::string_view object_identifier)
61       : index_(index), object_identifier_(object_identifier) {}
62 
63   std::type_index index_;
64   std::string object_identifier_;
65 };
66 
67 }  // namespace internal
68 }  // namespace tink
69 }  // namespace crypto
70 
71 #endif  // TINK_INTERNAL_PARSER_INDEX_H_
72