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