From 4105505b334013c638b486bb34f26f7b090b46ef Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Wed, 17 Feb 2016 18:01:18 -0800 Subject: [PATCH] Fixed refactoring of grpc_arg pointer vtable --- include/grpc++/support/channel_arguments.h | 11 +++++++++++ src/cpp/common/channel_arguments.cc | 13 ++----------- test/cpp/common/channel_arguments_test.cc | 15 ++++++++++----- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/include/grpc++/support/channel_arguments.h b/include/grpc++/support/channel_arguments.h index 4e530d4b89c..a9ede35f903 100644 --- a/include/grpc++/support/channel_arguments.h +++ b/include/grpc++/support/channel_arguments.h @@ -95,6 +95,17 @@ class ChannelArguments { friend class SecureChannelCredentials; friend class testing::ChannelArgumentsTest; + /// Default pointer argument operations. + struct PointerVtableMembers { + static void* Copy(void* in) { return in; } + static void Destroy(void* in) {} + static int Compare(void* a, void* b) { + if (a < b) return -1; + if (a > b) return 1; + return 0; + } + }; + // Returns empty string when it is not set. grpc::string GetSslTargetNameOverride() const; diff --git a/src/cpp/common/channel_arguments.cc b/src/cpp/common/channel_arguments.cc index 989c1058324..d7faa5e173d 100644 --- a/src/cpp/common/channel_arguments.cc +++ b/src/cpp/common/channel_arguments.cc @@ -124,18 +124,9 @@ void ChannelArguments::SetInt(const grpc::string& key, int value) { } void ChannelArguments::SetPointer(const grpc::string& key, void* value) { - struct VtableMembers { - static void* Copy(void* in) { return in; } - static void Destroy(void* in) {} - static int Compare(void* a, void* b) { - if (a < b) return -1; - if (a > b) return 1; - return 0; - } - }; static const grpc_arg_pointer_vtable vtable = { - &VtableMembers::Copy, &VtableMembers::Destroy, &VtableMembers::Compare}; - + &PointerVtableMembers::Copy, &PointerVtableMembers::Destroy, + &PointerVtableMembers::Compare}; grpc_arg arg; arg.type = GRPC_ARG_POINTER; strings_.push_back(key); diff --git a/test/cpp/common/channel_arguments_test.cc b/test/cpp/common/channel_arguments_test.cc index 5ee63bf6885..06e41e29ba7 100644 --- a/test/cpp/common/channel_arguments_test.cc +++ b/test/cpp/common/channel_arguments_test.cc @@ -41,6 +41,11 @@ namespace testing { class ChannelArgumentsTest : public ::testing::Test { protected: + ChannelArgumentsTest() + : pointer_vtable_({&ChannelArguments::PointerVtableMembers::Copy, + &ChannelArguments::PointerVtableMembers::Destroy, + &ChannelArguments::PointerVtableMembers::Compare}) {} + void SetChannelArgs(const ChannelArguments& channel_args, grpc_channel_args* args) { channel_args.SetChannelArgs(args); @@ -74,14 +79,15 @@ class ChannelArgumentsTest : public ::testing::Test { return grpc::string(arg.value.string) == expected_arg.value.string; } else if (arg.type == GRPC_ARG_POINTER) { return arg.value.pointer.p == expected_arg.value.pointer.p && - arg.value.pointer.copy == expected_arg.value.pointer.copy && - arg.value.pointer.destroy == - expected_arg.value.pointer.destroy; + arg.value.pointer.vtable->copy == expected_arg.value.pointer.vtable->copy && + arg.value.pointer.vtable->destroy == + expected_arg.value.pointer.vtable->destroy; } } } return false; } + grpc_arg_pointer_vtable pointer_vtable_; ChannelArguments channel_args_; }; @@ -151,8 +157,7 @@ TEST_F(ChannelArgumentsTest, SetPointer) { arg0.type = GRPC_ARG_POINTER; arg0.key = const_cast(key0.c_str()); arg0.value.pointer.p = &key0; - arg0.value.pointer.copy = nullptr; - arg0.value.pointer.destroy = nullptr; + arg0.value.pointer.vtable = &pointer_vtable_; grpc::string key(key0); channel_args_.SetPointer(key, arg0.value.pointer.p);