| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* 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, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef GRPC_CORE_LIB_CHANNEL_CHANNEL_ARGS_H |
| 20 |
#define GRPC_CORE_LIB_CHANNEL_CHANNEL_ARGS_H |
| 21 |
|
| 22 |
#include <grpc/support/port_platform.h> |
| 23 |
|
| 24 |
#include <stddef.h> |
| 25 |
|
| 26 |
#include <algorithm> // IWYU pragma: keep |
| 27 |
#include <iosfwd> |
| 28 |
#include <memory> |
| 29 |
#include <string> |
| 30 |
#include <utility> |
| 31 |
|
| 32 |
#include "absl/meta/type_traits.h" |
| 33 |
#include "absl/strings/string_view.h" |
| 34 |
#include "absl/types/optional.h" |
| 35 |
#include "absl/types/variant.h" |
| 36 |
|
| 37 |
#include <grpc/event_engine/event_engine.h> |
| 38 |
#include <grpc/impl/codegen/grpc_types.h> |
| 39 |
|
| 40 |
#include "src/core/lib/avl/avl.h" |
| 41 |
#include "src/core/lib/gpr/useful.h" |
| 42 |
#include "src/core/lib/gprpp/debug_location.h" |
| 43 |
#include "src/core/lib/gprpp/dual_ref_counted.h" |
| 44 |
#include "src/core/lib/gprpp/ref_counted.h" |
| 45 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 46 |
#include "src/core/lib/gprpp/time.h" |
| 47 |
#include "src/core/lib/surface/channel_stack_type.h" |
| 48 |
|
| 49 |
// TODO(hork): When we're ready to allow setting via a channel arg from the |
| 50 |
// application, replace this with a macro in |
| 51 |
// include/grpc/impl/codegen/grpc_types.h. |
| 52 |
#define GRPC_INTERNAL_ARG_EVENT_ENGINE "grpc.internal.event_engine" |
| 53 |
|
| 54 |
// Channel args are intentionally immutable, to avoid the need for locking. |
| 55 |
|
| 56 |
namespace grpc_core { |
| 57 |
|
| 58 |
// Define a traits object for vtable lookup - allows us to integrate with |
| 59 |
// existing code easily (just define the trait!) and allows some magic in |
| 60 |
// ChannelArgs to automatically derive a vtable from a T*. |
| 61 |
// To participate as a pointer, instances should expose the function: |
| 62 |
// // Gets the vtable for this type |
| 63 |
// static const grpc_arg_pointer_vtable* VTable(); |
| 64 |
// // Performs any mutations required for channel args to own a pointer |
| 65 |
// // Only needed if ChannelArgs::Set is to be called with a raw pointer. |
| 66 |
// static void* TakeUnownedPointer(T* p); |
| 67 |
template <typename T, typename Ignored = void /* for SFINAE */> |
| 68 |
struct ChannelArgTypeTraits; |
| 69 |
|
| 70 |
namespace channel_args_detail { |
| 71 |
inline int PointerCompare(void* a_ptr, const grpc_arg_pointer_vtable* a_vtable, |
| 72 |
void* b_ptr, |
| 73 |
const grpc_arg_pointer_vtable* b_vtable) { |
| 74 |
int c = QsortCompare(a_ptr, b_ptr); |
| 75 |
if (c == 0) return 0; |
| 76 |
c = QsortCompare(a_vtable, b_vtable); |
| 77 |
if (c != 0) return c; |
| 78 |
return a_vtable->cmp(a_ptr, b_ptr); |
| 79 |
} |
| 80 |
|
| 81 |
// The type returned by calling Ref() on a T - used to determine the basest-type |
| 82 |
// before the crt refcount base class. |
| 83 |
template <typename T> |
| 84 |
using RefType = absl::remove_cvref_t<decltype(*std::declval<T>().Ref())>; |
| 85 |
} // namespace channel_args_detail |
| 86 |
|
| 87 |
// Specialization for ref-counted pointers. |
| 88 |
// Types should expose: |
| 89 |
// static int ChannelArgsCompare(const T* a, const T* b); |
| 90 |
template <typename T> |
| 91 |
struct ChannelArgTypeTraits< |
| 92 |
T, absl::enable_if_t< |
| 93 |
std::is_base_of<RefCounted<channel_args_detail::RefType<T>>, |
| 94 |
channel_args_detail::RefType<T>>::value || |
| 95 |
std::is_base_of<RefCounted<channel_args_detail::RefType<T>, |
| 96 |
NonPolymorphicRefCount>, |
| 97 |
channel_args_detail::RefType<T>>::value || |
| 98 |
std::is_base_of<DualRefCounted<channel_args_detail::RefType<T>>, |
| 99 |
channel_args_detail::RefType<T>>::value, |
| 100 |
void>> { |
| 101 |
static const grpc_arg_pointer_vtable* VTable() { |
| 102 |
static const grpc_arg_pointer_vtable tbl = { |
| 103 |
// copy |
| 104 |
[](void* p) -> void* { |
| 105 |
return p == nullptr ? nullptr |
| 106 |
: static_cast<T*>(p) |
| 107 |
->Ref(DEBUG_LOCATION, "ChannelArgs copy") |
| 108 |
.release(); |
| 109 |
}, |
| 110 |
// destroy |
| 111 |
[](void* p) { |
| 112 |
if (p != nullptr) { |
| 113 |
static_cast<T*>(p)->Unref(DEBUG_LOCATION, "ChannelArgs destroy"); |
| 114 |
} |
| 115 |
}, |
| 116 |
// compare |
| 117 |
[](void* p1, void* p2) { |
| 118 |
return T::ChannelArgsCompare(static_cast<const T*>(p1), |
| 119 |
static_cast<const T*>(p2)); |
| 120 |
}, |
| 121 |
}; |
| 122 |
return &tbl; |
| 123 |
}; |
| 124 |
}; |
| 125 |
|
| 126 |
// Specialization for shared_ptr |
| 127 |
// Incurs an allocation because shared_ptr.release is not a thing. |
| 128 |
template <typename T> |
| 129 |
struct is_shared_ptr : std::false_type {}; |
| 130 |
template <typename T> |
| 131 |
struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {}; |
| 132 |
template <typename T> |
| 133 |
struct ChannelArgTypeTraits<T, |
| 134 |
absl::enable_if_t<is_shared_ptr<T>::value, void>> { |
| 135 |
static void* TakeUnownedPointer(T* p) { return p; } |
| 136 |
static const grpc_arg_pointer_vtable* VTable() { |
| 137 |
static const grpc_arg_pointer_vtable tbl = { |
| 138 |
// copy |
| 139 |
[](void* p) -> void* { return new T(*static_cast<T*>(p)); }, |
| 140 |
// destroy |
| 141 |
[](void* p) { delete static_cast<T*>(p); }, |
| 142 |
// compare |
| 143 |
[](void* p1, void* p2) { |
| 144 |
return QsortCompare(static_cast<const T*>(p1)->get(), |
| 145 |
static_cast<const T*>(p2)->get()); |
| 146 |
}, |
| 147 |
}; |
| 148 |
return &tbl; |
| 149 |
}; |
| 150 |
}; |
| 151 |
|
| 152 |
// If a type declares some member 'struct RawPointerChannelArgTag {}' then |
| 153 |
// we automatically generate a vtable for it that does not do any ownership |
| 154 |
// management and compares the type by pointer identity. |
| 155 |
// This is intended to be relatively ugly because *most types should worry about |
| 156 |
// ownership*. |
| 157 |
template <typename T> |
| 158 |
struct ChannelArgTypeTraits<T, |
| 159 |
absl::void_t<typename T::RawPointerChannelArgTag>> { |
| 160 |
static void* TakeUnownedPointer(T* p) { return p; } |
| 161 |
static const grpc_arg_pointer_vtable* VTable() { |
| 162 |
static const grpc_arg_pointer_vtable tbl = { |
| 163 |
// copy |
| 164 |
[](void* p) -> void* { return p; }, |
| 165 |
// destroy |
| 166 |
[](void*) {}, |
| 167 |
// compare |
| 168 |
[](void* p1, void* p2) { return QsortCompare(p1, p2); }, |
| 169 |
}; |
| 170 |
return &tbl; |
| 171 |
}; |
| 172 |
}; |
| 173 |
|
| 174 |
// GetObject support for shared_ptr and RefCountedPtr |
| 175 |
template <typename T> |
| 176 |
struct WrapInSharedPtr |
| 177 |
: std::integral_constant< |
| 178 |
bool, std::is_base_of<std::enable_shared_from_this<T>, T>::value> {}; |
| 179 |
template <> |
| 180 |
struct WrapInSharedPtr<grpc_event_engine::experimental::EventEngine> |
| 181 |
: std::true_type {}; |
| 182 |
template <typename T, typename Ignored = void /* for SFINAE */> |
| 183 |
struct GetObjectImpl; |
| 184 |
// std::shared_ptr implementation |
| 185 |
template <typename T> |
| 186 |
struct GetObjectImpl<T, absl::enable_if_t<WrapInSharedPtr<T>::value, void>> { |
| 187 |
using Result = T*; |
| 188 |
using ReffedResult = std::shared_ptr<T>; |
| 189 |
using StoredType = std::shared_ptr<T>*; |
| 190 |
static Result Get(StoredType p) { return p->get(); }; |
| 191 |
static ReffedResult GetReffed(StoredType p) { return ReffedResult(*p); }; |
| 192 |
static ReffedResult GetReffed(StoredType p, |
| 193 |
const DebugLocation& /* location */, |
| 194 |
const char* /* reason */) { |
| 195 |
return GetReffed(*p); |
| 196 |
}; |
| 197 |
}; |
| 198 |
// RefCountedPtr |
| 199 |
template <typename T> |
| 200 |
struct GetObjectImpl<T, absl::enable_if_t<!WrapInSharedPtr<T>::value, void>> { |
| 201 |
using Result = T*; |
| 202 |
using ReffedResult = RefCountedPtr<T>; |
| 203 |
using StoredType = Result; |
| 204 |
static Result Get(StoredType p) { return p; }; |
| 205 |
static ReffedResult GetReffed(StoredType p) { |
| 206 |
if (p == nullptr) return nullptr; |
| 207 |
return p->Ref(); |
| 208 |
}; |
| 209 |
static ReffedResult GetReffed(StoredType p, const DebugLocation& location, |
| 210 |
const char* reason) { |
| 211 |
if (p == nullptr) return nullptr; |
| 212 |
return p->Ref(location, reason); |
| 213 |
}; |
| 214 |
}; |
| 215 |
|
| 216 |
// Provide the canonical name for a type's channel arg key |
| 217 |
template <typename T> |
| 218 |
struct ChannelArgNameTraits { |
| 219 |
static absl::string_view ChannelArgName() { return T::ChannelArgName(); } |
| 220 |
}; |
| 221 |
template <typename T> |
| 222 |
struct ChannelArgNameTraits<std::shared_ptr<T>> { |
| 223 |
static absl::string_view ChannelArgName() { return T::ChannelArgName(); } |
| 224 |
}; |
| 225 |
// Specialization for the EventEngine |
| 226 |
template <> |
| 227 |
struct ChannelArgNameTraits<grpc_event_engine::experimental::EventEngine> { |
| 228 |
static absl::string_view ChannelArgName() { |
| 229 |
return GRPC_INTERNAL_ARG_EVENT_ENGINE; |
| 230 |
} |
| 231 |
}; |
| 232 |
class ChannelArgs { |
| 233 |
public: |
| 234 |
class Pointer { |
| 235 |
public: |
| 236 |
Pointer(void* p, const grpc_arg_pointer_vtable* vtable); |
| 237 |
~Pointer() { vtable_->destroy(p_); } |
| 238 |
|
| 239 |
Pointer(const Pointer& other); |
| 240 |
Pointer& operator=(Pointer other) { |
| 241 |
std::swap(p_, other.p_); |
| 242 |
std::swap(vtable_, other.vtable_); |
| 243 |
return *this; |
| 244 |
} |
| 245 |
Pointer(Pointer&& other) noexcept; |
| 246 |
Pointer& operator=(Pointer&& other) noexcept { |
| 247 |
std::swap(p_, other.p_); |
| 248 |
std::swap(vtable_, other.vtable_); |
| 249 |
return *this; |
| 250 |
} |
| 251 |
|
| 252 |
friend int QsortCompare(const Pointer& a, const Pointer& b) { |
| 253 |
return channel_args_detail::PointerCompare(a.p_, a.vtable_, b.p_, |
| 254 |
b.vtable_); |
| 255 |
} |
| 256 |
|
| 257 |
bool operator==(const Pointer& rhs) const { |
| 258 |
return QsortCompare(*this, rhs) == 0; |
| 259 |
} |
| 260 |
bool operator<(const Pointer& rhs) const { |
| 261 |
return QsortCompare(*this, rhs) < 0; |
| 262 |
} |
| 263 |
bool operator!=(const Pointer& rhs) const { |
| 264 |
return QsortCompare(*this, rhs) != 0; |
| 265 |
} |
| 266 |
|
| 267 |
void* c_pointer() const { return p_; } |
| 268 |
const grpc_arg_pointer_vtable* c_vtable() const { return vtable_; } |
| 269 |
|
| 270 |
private: |
| 271 |
static const grpc_arg_pointer_vtable* EmptyVTable(); |
| 272 |
|
| 273 |
void* p_; |
| 274 |
const grpc_arg_pointer_vtable* vtable_; |
| 275 |
}; |
| 276 |
|
| 277 |
using Value = absl::variant<int, std::string, Pointer>; |
| 278 |
|
| 279 |
struct ChannelArgsDeleter { |
| 280 |
void operator()(const grpc_channel_args* p) const; |
| 281 |
}; |
| 282 |
using CPtr = |
| 283 |
std::unique_ptr<const grpc_channel_args, ChannelArgs::ChannelArgsDeleter>; |
| 284 |
|
| 285 |
ChannelArgs(); |
| 286 |
~ChannelArgs(); |
| 287 |
ChannelArgs(const ChannelArgs&); |
| 288 |
ChannelArgs& operator=(const ChannelArgs&); |
| 289 |
ChannelArgs(ChannelArgs&&) noexcept; |
| 290 |
ChannelArgs& operator=(ChannelArgs&&) noexcept; |
| 291 |
|
| 292 |
static ChannelArgs FromC(const grpc_channel_args* args); |
| 293 |
static ChannelArgs FromC(const grpc_channel_args& args) { |
| 294 |
return FromC(&args); |
| 295 |
} |
| 296 |
// Construct a new grpc_channel_args struct. |
| 297 |
CPtr ToC() const; |
| 298 |
|
| 299 |
// Returns the union of this channel args with other. |
| 300 |
// If a key is present in both, the value from this is used. |
| 301 |
GRPC_MUST_USE_RESULT ChannelArgs UnionWith(ChannelArgs other) const; |
| 302 |
|
| 303 |
const Value* Get(absl::string_view name) const; |
| 304 |
GRPC_MUST_USE_RESULT ChannelArgs Set(absl::string_view name, |
| 305 |
Pointer value) const; |
| 306 |
GRPC_MUST_USE_RESULT ChannelArgs Set(absl::string_view name, int value) const; |
| 307 |
GRPC_MUST_USE_RESULT ChannelArgs Set(absl::string_view name, |
| 308 |
absl::string_view value) const; |
| 309 |
GRPC_MUST_USE_RESULT ChannelArgs Set(absl::string_view name, |
| 310 |
std::string value) const; |
| 311 |
GRPC_MUST_USE_RESULT ChannelArgs Set(absl::string_view name, |
| 312 |
const char* value) const; |
| 313 |
GRPC_MUST_USE_RESULT ChannelArgs Set(grpc_arg arg) const; |
| 314 |
template <typename T> |
| 315 |
GRPC_MUST_USE_RESULT absl::enable_if_t< |
| 316 |
std::is_same<const grpc_arg_pointer_vtable*, |
| 317 |
decltype(ChannelArgTypeTraits<T>::VTable())>::value, |
| 318 |
ChannelArgs> |
| 319 |
Set(absl::string_view name, T* value) const { |
| 320 |
return Set(name, Pointer(ChannelArgTypeTraits<T>::TakeUnownedPointer(value), |
| 321 |
ChannelArgTypeTraits<T>::VTable())); |
| 322 |
} |
| 323 |
template <typename T> |
| 324 |
GRPC_MUST_USE_RESULT auto Set(absl::string_view name, |
| 325 |
RefCountedPtr<T> value) const |
| 326 |
-> absl::enable_if_t< |
| 327 |
std::is_same<const grpc_arg_pointer_vtable*, |
| 328 |
decltype(ChannelArgTypeTraits< |
| 329 |
absl::remove_cvref_t<T>>::VTable())>::value, |
| 330 |
ChannelArgs> { |
| 331 |
return Set( |
| 332 |
name, Pointer(value.release(), |
| 333 |
ChannelArgTypeTraits<absl::remove_cvref_t<T>>::VTable())); |
| 334 |
} |
| 335 |
template <typename T> |
| 336 |
GRPC_MUST_USE_RESULT absl::enable_if_t< |
| 337 |
std::is_same< |
| 338 |
const grpc_arg_pointer_vtable*, |
| 339 |
decltype(ChannelArgTypeTraits<std::shared_ptr<T>>::VTable())>::value, |
| 340 |
ChannelArgs> |
| 341 |
Set(absl::string_view name, std::shared_ptr<T> value) const { |
| 342 |
auto* store_value = new std::shared_ptr<T>(value); |
| 343 |
return Set( |
| 344 |
name, |
| 345 |
Pointer(ChannelArgTypeTraits<std::shared_ptr<T>>::TakeUnownedPointer( |
| 346 |
store_value), |
| 347 |
ChannelArgTypeTraits<std::shared_ptr<T>>::VTable())); |
| 348 |
} |
| 349 |
template <typename T> |
| 350 |
GRPC_MUST_USE_RESULT ChannelArgs SetIfUnset(absl::string_view name, |
| 351 |
T value) const { |
| 352 |
if (Contains(name)) return *this; |
| 353 |
return Set(name, std::move(value)); |
| 354 |
} |
| 355 |
GRPC_MUST_USE_RESULT ChannelArgs Remove(absl::string_view name) const; |
| 356 |
bool Contains(absl::string_view name) const; |
| 357 |
|
| 358 |
template <typename T> |
| 359 |
bool ContainsObject() const { |
| 360 |
return Get(ChannelArgNameTraits<T>::ChannelArgName()) != nullptr; |
| 361 |
} |
| 362 |
|
| 363 |
absl::optional<int> GetInt(absl::string_view name) const; |
| 364 |
absl::optional<absl::string_view> GetString(absl::string_view name) const; |
| 365 |
absl::optional<std::string> GetOwnedString(absl::string_view name) const; |
| 366 |
void* GetVoidPointer(absl::string_view name) const; |
| 367 |
template <typename T> |
| 368 |
typename GetObjectImpl<T>::StoredType GetPointer( |
| 369 |
absl::string_view name) const { |
| 370 |
return static_cast<typename GetObjectImpl<T>::StoredType>( |
| 371 |
GetVoidPointer(name)); |
| 372 |
} |
| 373 |
absl::optional<Duration> GetDurationFromIntMillis( |
| 374 |
absl::string_view name) const; |
| 375 |
absl::optional<bool> GetBool(absl::string_view name) const; |
| 376 |
|
| 377 |
// Object based get/set. |
| 378 |
// Deal with the common case that we set a pointer to an object under |
| 379 |
// the same name in every usage. |
| 380 |
// Expects ChannelArgTypeTraits to exist for T, and T to expose: |
| 381 |
// static string_view ChannelArgName(); |
| 382 |
template <typename T> |
| 383 |
GRPC_MUST_USE_RESULT ChannelArgs SetObject(T* p) const { |
| 384 |
return Set(T::ChannelArgName(), p); |
| 385 |
} |
| 386 |
template <typename T> |
| 387 |
GRPC_MUST_USE_RESULT ChannelArgs SetObject(RefCountedPtr<T> p) const { |
| 388 |
return Set(T::ChannelArgName(), std::move(p)); |
| 389 |
} |
| 390 |
template <typename T> |
| 391 |
GRPC_MUST_USE_RESULT ChannelArgs SetObject(std::shared_ptr<T> p) const { |
| 392 |
return Set(ChannelArgNameTraits<T>::ChannelArgName(), std::move(p)); |
| 393 |
} |
| 394 |
template <typename T> |
| 395 |
typename GetObjectImpl<T>::Result GetObject() const { |
| 396 |
return GetObjectImpl<T>::Get( |
| 397 |
GetPointer<T>(ChannelArgNameTraits<T>::ChannelArgName())); |
| 398 |
} |
| 399 |
template <typename T> |
| 400 |
typename GetObjectImpl<T>::ReffedResult GetObjectRef() const { |
| 401 |
return GetObjectImpl<T>::GetReffed( |
| 402 |
GetPointer<T>(ChannelArgNameTraits<T>::ChannelArgName())); |
| 403 |
} |
| 404 |
template <typename T> |
| 405 |
typename GetObjectImpl<T>::ReffedResult GetObjectRef( |
| 406 |
const DebugLocation& location, const char* reason) const { |
| 407 |
return GetObjectImpl<T>::GetReffed( |
| 408 |
GetPointer<T>(ChannelArgNameTraits<T>::ChannelArgName()), location, |
| 409 |
reason); |
| 410 |
} |
| 411 |
|
| 412 |
bool operator!=(const ChannelArgs& other) const; |
| 413 |
bool operator<(const ChannelArgs& other) const; |
| 414 |
bool operator==(const ChannelArgs& other) const; |
| 415 |
|
| 416 |
// Helpers for commonly accessed things |
| 417 |
|
| 418 |
bool WantMinimalStack() const; |
| 419 |
std::string ToString() const; |
| 420 |
|
| 421 |
private: |
| 422 |
explicit ChannelArgs(AVL<std::string, Value> args); |
| 423 |
|
| 424 |
GRPC_MUST_USE_RESULT ChannelArgs Set(absl::string_view name, |
| 425 |
Value value) const; |
| 426 |
|
| 427 |
AVL<std::string, Value> args_; |
| 428 |
}; |
| 429 |
|
| 430 |
std::ostream& operator<<(std::ostream& out, const ChannelArgs& args); |
| 431 |
|
| 432 |
} // namespace grpc_core |
| 433 |
|
| 434 |
/** Copy the arguments in \a src into a new instance */ |
| 435 |
grpc_channel_args* grpc_channel_args_copy(const grpc_channel_args* src); |
| 436 |
|
| 437 |
/** Copy the arguments in \a src into a new instance, stably sorting keys */ |
| 438 |
grpc_channel_args* grpc_channel_args_normalize(const grpc_channel_args* src); |
| 439 |
|
| 440 |
/** Copy the arguments in \a src and append \a to_add. If \a to_add is NULL, it |
| 441 |
* is equivalent to calling \a grpc_channel_args_copy. */ |
| 442 |
grpc_channel_args* grpc_channel_args_copy_and_add(const grpc_channel_args* src, |
| 443 |
const grpc_arg* to_add, |
| 444 |
size_t num_to_add); |
| 445 |
|
| 446 |
/** Copies the arguments in \a src except for those whose keys are in |
| 447 |
\a to_remove. */ |
| 448 |
grpc_channel_args* grpc_channel_args_copy_and_remove( |
| 449 |
const grpc_channel_args* src, const char** to_remove, size_t num_to_remove); |
| 450 |
|
| 451 |
/** Copies the arguments from \a src except for those whose keys are in |
| 452 |
\a to_remove and appends the arguments in \a to_add. */ |
| 453 |
grpc_channel_args* grpc_channel_args_copy_and_add_and_remove( |
| 454 |
const grpc_channel_args* src, const char** to_remove, size_t num_to_remove, |
| 455 |
const grpc_arg* to_add, size_t num_to_add); |
| 456 |
|
| 457 |
/** Perform the union of \a a and \a b, prioritizing \a a entries */ |
| 458 |
grpc_channel_args* grpc_channel_args_union(const grpc_channel_args* a, |
| 459 |
const grpc_channel_args* b); |
| 460 |
|
| 461 |
/** Destroy arguments created by \a grpc_channel_args_copy */ |
| 462 |
void grpc_channel_args_destroy(grpc_channel_args* a); |
| 463 |
inline void grpc_channel_args_destroy(const grpc_channel_args* a) { |
| 464 |
grpc_channel_args_destroy(const_cast<grpc_channel_args*>(a)); |
| 465 |
} |
| 466 |
|
| 467 |
int grpc_channel_args_compare(const grpc_channel_args* a, |
| 468 |
const grpc_channel_args* b); |
| 469 |
|
| 470 |
/** Returns the value of argument \a name from \a args, or NULL if not found. */ |
| 471 |
const grpc_arg* grpc_channel_args_find(const grpc_channel_args* args, |
| 472 |
const char* name); |
| 473 |
|
| 474 |
bool grpc_channel_args_want_minimal_stack(const grpc_channel_args* args); |
| 475 |
|
| 476 |
typedef struct grpc_integer_options { |
| 477 |
int default_value; // Return this if value is outside of expected bounds. |
| 478 |
int min_value; |
| 479 |
int max_value; |
| 480 |
} grpc_integer_options; |
| 481 |
|
| 482 |
/** Returns the value of \a arg, subject to the constraints in \a options. */ |
| 483 |
int grpc_channel_arg_get_integer(const grpc_arg* arg, |
| 484 |
const grpc_integer_options options); |
| 485 |
/** Similar to the above, but needs to find the arg from \a args by the name |
| 486 |
* first. */ |
| 487 |
int grpc_channel_args_find_integer(const grpc_channel_args* args, |
| 488 |
const char* name, |
| 489 |
const grpc_integer_options options); |
| 490 |
|
| 491 |
/** Returns the value of \a arg if \a arg is of type GRPC_ARG_STRING. |
| 492 |
Otherwise, emits a warning log, and returns nullptr. |
| 493 |
If arg is nullptr, returns nullptr, and does not emit a warning. */ |
| 494 |
char* grpc_channel_arg_get_string(const grpc_arg* arg); |
| 495 |
/** Similar to the above, but needs to find the arg from \a args by the name |
| 496 |
* first. */ |
| 497 |
char* grpc_channel_args_find_string(const grpc_channel_args* args, |
| 498 |
const char* name); |
| 499 |
/** If \a arg is of type GRPC_ARG_INTEGER, returns true if it's non-zero. |
| 500 |
* Returns \a default_value if \a arg is of other types. */ |
| 501 |
bool grpc_channel_arg_get_bool(const grpc_arg* arg, bool default_value); |
| 502 |
/** Similar to the above, but needs to find the arg from \a args by the name |
| 503 |
* first. */ |
| 504 |
bool grpc_channel_args_find_bool(const grpc_channel_args* args, |
| 505 |
const char* name, bool default_value); |
| 506 |
|
| 507 |
template <typename T> |
| 508 |
T* grpc_channel_args_find_pointer(const grpc_channel_args* args, |
| 509 |
const char* name) { |
| 510 |
const grpc_arg* arg = grpc_channel_args_find(args, name); |
| 511 |
if (arg == nullptr || arg->type != GRPC_ARG_POINTER) return nullptr; |
| 512 |
return static_cast<T*>(arg->value.pointer.p); |
| 513 |
} |
| 514 |
|
| 515 |
// Helpers for creating channel args. |
| 516 |
grpc_arg grpc_channel_arg_string_create(char* name, char* value); |
| 517 |
grpc_arg grpc_channel_arg_integer_create(char* name, int value); |
| 518 |
grpc_arg grpc_channel_arg_pointer_create(char* name, void* value, |
| 519 |
const grpc_arg_pointer_vtable* vtable); |
| 520 |
|
| 521 |
// Returns a string representing channel args in human-readable form. |
| 522 |
std::string grpc_channel_args_string(const grpc_channel_args* args); |
| 523 |
|
| 524 |
namespace grpc_core { |
| 525 |
// Ensure no duplicate channel args (with some backwards compatibility hacks). |
| 526 |
// Eliminate any grpc.internal.* args. |
| 527 |
// Return a C++ object. |
| 528 |
ChannelArgs ChannelArgsBuiltinPrecondition(const grpc_channel_args* src); |
| 529 |
} // namespace grpc_core |
| 530 |
|
| 531 |
// Takes ownership of the old_args |
| 532 |
typedef grpc_core::ChannelArgs ( |
| 533 |
*grpc_channel_args_client_channel_creation_mutator)( |
| 534 |
const char* target, const grpc_core::ChannelArgs& old_args, |
| 535 |
grpc_channel_stack_type type); |
| 536 |
|
| 537 |
// Should be called only once globaly before grpc is init'ed. |
| 538 |
void grpc_channel_args_set_client_channel_creation_mutator( |
| 539 |
grpc_channel_args_client_channel_creation_mutator cb); |
| 540 |
// This will be called at the creation of each channel. |
| 541 |
grpc_channel_args_client_channel_creation_mutator |
| 542 |
grpc_channel_args_get_client_channel_creation_mutator(); |
| 543 |
|
| 544 |
#endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_ARGS_H */ |