• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The gRPC Authors
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 #ifndef GRPC_EVENT_ENGINE_EXTENSIBLE_H
16 #define GRPC_EVENT_ENGINE_EXTENSIBLE_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include "absl/strings/string_view.h"
21 
22 namespace grpc_event_engine {
23 namespace experimental {
24 
25 class Extensible {
26  public:
27   /// A method which allows users to query whether an implementation supports a
28   /// specified extension. The name of the extension is provided as an input.
29   ///
30   /// An extension could be any type with a unique string id. Each extension may
31   /// support additional capabilities and if the implementation supports the
32   /// queried extension, it should return a valid pointer to the extension type.
33   ///
34   /// E.g., use case of an EventEngine::Endpoint supporting a custom extension.
35   ///
36   /// class CustomEndpointExtension {
37   ///  public:
38   ///   static std::string EndpointExtensionName() {
39   ///     return "my.namespace.extension_name";
40   ///   }
41   ///   virtual void Process() = 0;
42   /// }
43   ///
44   /// class CustomEndpoint :
45   ///        public EventEngine::Endpoint, public CustomEndpointExtension {
46   ///   public:
47   ///     void* QueryExtension(absl::string_view id) override {
48   ///       if (id == CustomEndpointExtension::EndpointExtensionName()) {
49   ///         return static_cast<CustomEndpointExtension*>(this);
50   ///       }
51   ///       return nullptr;
52   ///     }
53   ///     void Process() override { ... }
54   ///     ...
55   /// }
56   ///
57   /// auto endpoint =
58   ///     static_cast<CustomEndpointExtension*>(endpoint->QueryExtension(
59   ///         CustomEndpointExtension::EndpointExtensionName()));
60   /// if (endpoint != nullptr) endpoint->Process();
61   ///
QueryExtension(absl::string_view)62   virtual void* QueryExtension(absl::string_view /*id*/) { return nullptr; }
63 
64  protected:
65   ~Extensible() = default;
66 };
67 
68 }  // namespace experimental
69 }  // namespace grpc_event_engine
70 
71 #endif  // GRPC_EVENT_ENGINE_EXTENSIBLE_H
72