/Users/deen/code/yugabyte-db/src/yb/util/protobuf_util.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | // Portions Copyright (c) YugaByte, Inc. |
34 | | |
35 | | #ifndef YB_UTIL_PROTOBUF_UTIL_H |
36 | | #define YB_UTIL_PROTOBUF_UTIL_H |
37 | | |
38 | | #include <float.h> |
39 | | |
40 | | #include <chrono> |
41 | | #include <sstream> |
42 | | #include <string> |
43 | | #include <type_traits> |
44 | | |
45 | | #include <boost/mpl/and.hpp> |
46 | | #include <google/protobuf/stubs/port.h> |
47 | | #include <google/protobuf/generated_enum_reflection.h> |
48 | | #include <google/protobuf/message_lite.h> |
49 | | |
50 | | #include "yb/util/enums.h" |
51 | | #include "yb/util/format.h" |
52 | | #include "yb/util/math_util.h" |
53 | | #include "yb/util/tostring.h" |
54 | | #include "yb/util/type_traits.h" |
55 | | |
56 | | #define PB_ENUM_FORMATTERS(EnumType) \ |
57 | 37 | inline std::string PBEnumToString(EnumType value) { \ |
58 | 37 | if (BOOST_PP_CAT(EnumType, _IsValid)(value)) { \ |
59 | 37 | return BOOST_PP_CAT(EnumType, _Name)(value); \ |
60 | 37 | } else { \ |
61 | 0 | return ::yb::Format("<unknown " BOOST_PP_STRINGIZE(EnumType) " : $0>", \ |
62 | 0 | ::yb::to_underlying(value)); \ |
63 | 0 | } \ |
64 | 37 | } \ Unexecuted instantiation: PBEnumToString(yb::consensus::LeaderLeaseStatus) PBEnumToString(yb::AppStatusPB_ErrorCode) Line | Count | Source | 57 | 2 | inline std::string PBEnumToString(EnumType value) { \ | 58 | 2 | if (BOOST_PP_CAT(EnumType, _IsValid)(value)) { \ | 59 | 2 | return BOOST_PP_CAT(EnumType, _Name)(value); \ | 60 | 2 | } else { \ | 61 | 0 | return ::yb::Format("<unknown " BOOST_PP_STRINGIZE(EnumType) " : $0>", \ | 62 | 0 | ::yb::to_underlying(value)); \ | 63 | 0 | } \ | 64 | 2 | } \ |
PBEnumToString(yb::PeerRole) Line | Count | Source | 57 | 34 | inline std::string PBEnumToString(EnumType value) { \ | 58 | 34 | if (BOOST_PP_CAT(EnumType, _IsValid)(value)) { \ | 59 | 34 | return BOOST_PP_CAT(EnumType, _Name)(value); \ | 60 | 34 | } else { \ | 61 | 0 | return ::yb::Format("<unknown " BOOST_PP_STRINGIZE(EnumType) " : $0>", \ | 62 | 0 | ::yb::to_underlying(value)); \ | 63 | 0 | } \ | 64 | 34 | } \ |
Unexecuted instantiation: PBEnumToString(yb::tablet::RaftGroupStatePB) yb::tools::enterprise::PBEnumToString(yb::master::SysSnapshotEntryPB_State) Line | Count | Source | 57 | 1 | inline std::string PBEnumToString(EnumType value) { \ | 58 | 1 | if (BOOST_PP_CAT(EnumType, _IsValid)(value)) { \ | 59 | 1 | return BOOST_PP_CAT(EnumType, _Name)(value); \ | 60 | 1 | } else { \ | 61 | 0 | return ::yb::Format("<unknown " BOOST_PP_STRINGIZE(EnumType) " : $0>", \ | 62 | 0 | ::yb::to_underlying(value)); \ | 63 | 0 | } \ | 64 | 1 | } \ |
|
65 | 0 | inline std::string ToString(EnumType value) { \ |
66 | 0 | return PBEnumToString(value); \ |
67 | 0 | } \ Unexecuted instantiation: ToString(yb::consensus::LeaderLeaseStatus) Unexecuted instantiation: ToString(yb::PeerRole) Unexecuted instantiation: ToString(yb::AppStatusPB_ErrorCode) Unexecuted instantiation: ToString(yb::tablet::RaftGroupStatePB) Unexecuted instantiation: yb::tools::enterprise::ToString(yb::master::SysSnapshotEntryPB_State) |
68 | 1 | __attribute__((unused)) inline std::ostream& operator << (std::ostream& out, EnumType value) { \ |
69 | 1 | return out << PBEnumToString(value); \ |
70 | 1 | } Unexecuted instantiation: operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, yb::consensus::LeaderLeaseStatus) Unexecuted instantiation: operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, yb::PeerRole) Unexecuted instantiation: operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, yb::AppStatusPB_ErrorCode) Unexecuted instantiation: operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, yb::tablet::RaftGroupStatePB) yb::tools::enterprise::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, yb::master::SysSnapshotEntryPB_State) Line | Count | Source | 68 | 1 | __attribute__((unused)) inline std::ostream& operator << (std::ostream& out, EnumType value) { \ | 69 | 1 | return out << PBEnumToString(value); \ | 70 | 1 | } |
|
71 | | |
72 | | namespace yb { |
73 | | |
74 | | template<typename T> |
75 | | std::vector<T> GetAllPbEnumValues() { |
76 | | const auto* desc = google::protobuf::GetEnumDescriptor<T>(); |
77 | | std::vector<T> result; |
78 | | result.reserve(desc->value_count()); |
79 | | for (int i = 0; i < desc->value_count(); ++i) { |
80 | | result.push_back(T(desc->value(i)->number())); |
81 | | } |
82 | | return result; |
83 | | } |
84 | | |
85 | | template <class Source, class Dest> |
86 | | void AppendToRepeated(const Source& source, Dest* dest) { |
87 | | dest->Reserve(dest->size() + source.size()); |
88 | | for (const auto& elem : source) { |
89 | | dest->Add(elem); |
90 | | } |
91 | | } |
92 | | |
93 | | } // namespace yb |
94 | | |
95 | | #endif // YB_UTIL_PROTOBUF_UTIL_H |