Debian Codegraph @

extra/protobuf/protobuf-24.4/src/google/protobuf/feature_resolver.h

raw
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #ifndef GOOGLE_PROTOBUF_FEATURE_RESOLVER_H__
32 #define GOOGLE_PROTOBUF_FEATURE_RESOLVER_H__
33
34 #include <memory>
35 #include <string>
36 #include <utility>
37
38 #include "absl/container/flat_hash_set.h"
39 #include "absl/status/status.h"
40 #include "absl/strings/string_view.h"
41 #include "google/protobuf/descriptor.h"
42 #include "google/protobuf/descriptor.pb.h"
43 #include "google/protobuf/dynamic_message.h"
44
45 // Must be included last.
46 #include "google/protobuf/port_def.inc"
47
48 namespace google {
49 namespace protobuf {
50
51 // These helpers implement the unique behaviors of edition features. For more
52 // details, see go/protobuf-editions-features.
53 class PROTOBUF_EXPORT FeatureResolver {
54 public:
55 FeatureResolver(FeatureResolver&&) = default;
56 FeatureResolver& operator=(FeatureResolver&&) = delete;
57
58 // Creates a new FeatureResolver at a specific edition. This validates the
59 // built-in features for the given edition, and calculates the default feature
60 // set.
61 static absl::StatusOr<FeatureResolver> Create(absl::string_view edition,
62 const Descriptor* descriptor);
63
64 // Registers a potential extension of the FeatureSet proto. Any visible
65 // extensions will be used during merging. Returns an error if the extension
66 // is a feature extension but fails validation.
67 absl::Status RegisterExtensions(const FileDescriptor& file);
68
69 // Creates a new feature set using inheritance and default behavior. This is
70 // designed to be called recursively, and the parent feature set is expected
71 // to be a fully merged one.
72 absl::StatusOr<FeatureSet> MergeFeatures(
73 const FeatureSet& merged_parent, const FeatureSet& unmerged_child) const;
74
75 private:
76 FeatureResolver(absl::string_view edition, const Descriptor& descriptor,
77 std::unique_ptr<DynamicMessageFactory> message_factory,
78 std::unique_ptr<Message> defaults)
79 : edition_(edition),
80 descriptor_(descriptor),
81 message_factory_(std::move(message_factory)),
82 defaults_(std::move(defaults)) {}
83
84 absl::Status RegisterExtensions(const Descriptor& message);
85 absl::Status RegisterExtension(const FieldDescriptor& extension);
86
87 std::string edition_;
88 const Descriptor& descriptor_;
89 absl::flat_hash_set<const FieldDescriptor*> extensions_;
90 std::unique_ptr<DynamicMessageFactory> message_factory_;
91 std::unique_ptr<Message> defaults_;
92 };
93
94 } // namespace protobuf
95 } // namespace google
96
97 #endif // GOOGLE_PROTOBUF_FEATURE_RESOLVER_H__
98
99 #include "google/protobuf/port_undef.inc"