From b7705ce2eeee9bcdfa3b651e6ffd5e937cb63792 Mon Sep 17 00:00:00 2001 From: Vijay Pai Date: Wed, 16 Oct 2019 11:36:54 -0700 Subject: [PATCH] Use C++11 range-based fors --- src/cpp/common/channel_arguments.cc | 37 +++++++++---------- src/cpp/common/tls_credentials_options.cc | 10 ++--- .../common/tls_credentials_options_util.cc | 8 ++-- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/cpp/common/channel_arguments.cc b/src/cpp/common/channel_arguments.cc index 932139890fe..4ef45970cdd 100644 --- a/src/cpp/common/channel_arguments.cc +++ b/src/cpp/common/channel_arguments.cc @@ -39,26 +39,26 @@ ChannelArguments::ChannelArguments(const ChannelArguments& other) args_.reserve(other.args_.size()); auto list_it_dst = strings_.begin(); auto list_it_src = other.strings_.begin(); - for (auto a = other.args_.begin(); a != other.args_.end(); ++a) { + for (const auto& a : other.args_) { grpc_arg ap; - ap.type = a->type; - GPR_ASSERT(list_it_src->c_str() == a->key); + ap.type = a.type; + GPR_ASSERT(list_it_src->c_str() == a.key); ap.key = const_cast(list_it_dst->c_str()); ++list_it_src; ++list_it_dst; - switch (a->type) { + switch (a.type) { case GRPC_ARG_INTEGER: - ap.value.integer = a->value.integer; + ap.value.integer = a.value.integer; break; case GRPC_ARG_STRING: - GPR_ASSERT(list_it_src->c_str() == a->value.string); + GPR_ASSERT(list_it_src->c_str() == a.value.string); ap.value.string = const_cast(list_it_dst->c_str()); ++list_it_src; ++list_it_dst; break; case GRPC_ARG_POINTER: - ap.value.pointer = a->value.pointer; - ap.value.pointer.p = a->value.pointer.vtable->copy(ap.value.pointer.p); + ap.value.pointer = a.value.pointer; + ap.value.pointer.p = a.value.pointer.vtable->copy(ap.value.pointer.p); break; } args_.push_back(ap); @@ -67,9 +67,9 @@ ChannelArguments::ChannelArguments(const ChannelArguments& other) ChannelArguments::~ChannelArguments() { grpc_core::ExecCtx exec_ctx; - for (auto it = args_.begin(); it != args_.end(); ++it) { - if (it->type == GRPC_ARG_POINTER) { - it->value.pointer.vtable->destroy(it->value.pointer.p); + for (auto& arg : args_) { + if (arg.type == GRPC_ARG_POINTER) { + arg.value.pointer.vtable->destroy(arg.value.pointer.p); } } } @@ -95,12 +95,12 @@ void ChannelArguments::SetSocketMutator(grpc_socket_mutator* mutator) { grpc_arg mutator_arg = grpc_socket_mutator_to_arg(mutator); bool replaced = false; grpc_core::ExecCtx exec_ctx; - for (auto it = args_.begin(); it != args_.end(); ++it) { - if (it->type == mutator_arg.type && - grpc::string(it->key) == grpc::string(mutator_arg.key)) { + for (auto& arg : args_) { + if (arg.type == mutator_arg.type && + grpc::string(arg.key) == grpc::string(mutator_arg.key)) { GPR_ASSERT(!replaced); - it->value.pointer.vtable->destroy(it->value.pointer.p); - it->value.pointer = mutator_arg.value.pointer; + arg.value.pointer.vtable->destroy(arg.value.pointer.p); + arg.value.pointer = mutator_arg.value.pointer; replaced = true; } } @@ -123,14 +123,13 @@ void ChannelArguments::SetUserAgentPrefix( } bool replaced = false; auto strings_it = strings_.begin(); - for (auto it = args_.begin(); it != args_.end(); ++it) { - const grpc_arg& arg = *it; + for (auto& arg : args_) { ++strings_it; if (arg.type == GRPC_ARG_STRING) { if (grpc::string(arg.key) == GRPC_ARG_PRIMARY_USER_AGENT_STRING) { GPR_ASSERT(arg.value.string == strings_it->c_str()); *(strings_it) = user_agent_prefix + " " + arg.value.string; - it->value.string = const_cast(strings_it->c_str()); + arg.value.string = const_cast(strings_it->c_str()); replaced = true; break; } diff --git a/src/cpp/common/tls_credentials_options.cc b/src/cpp/common/tls_credentials_options.cc index d06b47737ec..60d6a23fee1 100644 --- a/src/cpp/common/tls_credentials_options.cc +++ b/src/cpp/common/tls_credentials_options.cc @@ -106,15 +106,13 @@ void TlsCredentialReloadArg::set_key_materials_config( } ::grpc_core::InlinedVector<::grpc_core::PemKeyCertPair, 1> c_pem_key_cert_pair_list; - for (auto key_cert_pair = - key_materials_config->pem_key_cert_pair_list().begin(); - key_cert_pair != key_materials_config->pem_key_cert_pair_list().end(); - key_cert_pair++) { + for (const auto& key_cert_pair : + key_materials_config->pem_key_cert_pair_list()) { grpc_ssl_pem_key_cert_pair* ssl_pair = (grpc_ssl_pem_key_cert_pair*)gpr_malloc( sizeof(grpc_ssl_pem_key_cert_pair)); - ssl_pair->private_key = gpr_strdup(key_cert_pair->private_key.c_str()); - ssl_pair->cert_chain = gpr_strdup(key_cert_pair->cert_chain.c_str()); + ssl_pair->private_key = gpr_strdup(key_cert_pair.private_key.c_str()); + ssl_pair->cert_chain = gpr_strdup(key_cert_pair.cert_chain.c_str()); ::grpc_core::PemKeyCertPair c_pem_key_cert_pair = ::grpc_core::PemKeyCertPair(ssl_pair); c_pem_key_cert_pair_list.emplace_back(std::move(c_pem_key_cert_pair)); diff --git a/src/cpp/common/tls_credentials_options_util.cc b/src/cpp/common/tls_credentials_options_util.cc index 89709005dcb..d279afea807 100644 --- a/src/cpp/common/tls_credentials_options_util.cc +++ b/src/cpp/common/tls_credentials_options_util.cc @@ -36,14 +36,12 @@ grpc_tls_key_materials_config* ConvertToCKeyMaterialsConfig( grpc_tls_key_materials_config_create(); ::grpc_core::InlinedVector<::grpc_core::PemKeyCertPair, 1> c_pem_key_cert_pair_list; - for (auto key_cert_pair = config->pem_key_cert_pair_list().begin(); - key_cert_pair != config->pem_key_cert_pair_list().end(); - key_cert_pair++) { + for (const auto& key_cert_pair : config->pem_key_cert_pair_list()) { grpc_ssl_pem_key_cert_pair* ssl_pair = (grpc_ssl_pem_key_cert_pair*)gpr_malloc( sizeof(grpc_ssl_pem_key_cert_pair)); - ssl_pair->private_key = gpr_strdup(key_cert_pair->private_key.c_str()); - ssl_pair->cert_chain = gpr_strdup(key_cert_pair->cert_chain.c_str()); + ssl_pair->private_key = gpr_strdup(key_cert_pair.private_key.c_str()); + ssl_pair->cert_chain = gpr_strdup(key_cert_pair.cert_chain.c_str()); ::grpc_core::PemKeyCertPair c_pem_key_cert_pair = ::grpc_core::PemKeyCertPair(ssl_pair); c_pem_key_cert_pair_list.push_back(::std::move(c_pem_key_cert_pair));