Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include <grpc++/support/channel_arguments.h>
35 :
36 : #include <grpc/support/log.h>
37 : #include "src/core/channel/channel_args.h"
38 :
39 : namespace grpc {
40 :
41 182 : ChannelArguments::ChannelArguments(const ChannelArguments& other)
42 182 : : strings_(other.strings_) {
43 182 : args_.reserve(other.args_.size());
44 182 : auto list_it_dst = strings_.begin();
45 182 : auto list_it_src = other.strings_.begin();
46 309 : for (auto a = other.args_.begin(); a != other.args_.end(); ++a) {
47 : grpc_arg ap;
48 127 : ap.type = a->type;
49 127 : GPR_ASSERT(list_it_src->c_str() == a->key);
50 127 : ap.key = const_cast<char*>(list_it_dst->c_str());
51 127 : ++list_it_src;
52 127 : ++list_it_dst;
53 127 : switch (a->type) {
54 : case GRPC_ARG_INTEGER:
55 0 : ap.value.integer = a->value.integer;
56 0 : break;
57 : case GRPC_ARG_STRING:
58 127 : GPR_ASSERT(list_it_src->c_str() == a->value.string);
59 127 : ap.value.string = const_cast<char*>(list_it_dst->c_str());
60 127 : ++list_it_src;
61 127 : ++list_it_dst;
62 127 : break;
63 : case GRPC_ARG_POINTER:
64 0 : ap.value.pointer = a->value.pointer;
65 0 : ap.value.pointer.p = a->value.pointer.copy(ap.value.pointer.p);
66 0 : break;
67 : }
68 127 : args_.push_back(ap);
69 : }
70 182 : }
71 :
72 0 : void ChannelArguments::Swap(ChannelArguments& other) {
73 0 : args_.swap(other.args_);
74 0 : strings_.swap(other.strings_);
75 0 : }
76 :
77 0 : void ChannelArguments::SetCompressionAlgorithm(
78 : grpc_compression_algorithm algorithm) {
79 0 : SetInt(GRPC_COMPRESSION_ALGORITHM_ARG, algorithm);
80 0 : }
81 :
82 2 : void ChannelArguments::SetInt(const grpc::string& key, int value) {
83 : grpc_arg arg;
84 2 : arg.type = GRPC_ARG_INTEGER;
85 2 : strings_.push_back(key);
86 2 : arg.key = const_cast<char*>(strings_.back().c_str());
87 2 : arg.value.integer = value;
88 :
89 2 : args_.push_back(arg);
90 2 : }
91 :
92 311 : void ChannelArguments::SetString(const grpc::string& key,
93 : const grpc::string& value) {
94 : grpc_arg arg;
95 311 : arg.type = GRPC_ARG_STRING;
96 311 : strings_.push_back(key);
97 311 : arg.key = const_cast<char*>(strings_.back().c_str());
98 311 : strings_.push_back(value);
99 311 : arg.value.string = const_cast<char*>(strings_.back().c_str());
100 :
101 311 : args_.push_back(arg);
102 311 : }
103 :
104 187 : void ChannelArguments::SetChannelArgs(grpc_channel_args* channel_args) const {
105 187 : channel_args->num_args = args_.size();
106 187 : if (channel_args->num_args > 0) {
107 185 : channel_args->args = const_cast<grpc_arg*>(&args_[0]);
108 : }
109 187 : }
110 :
111 : } // namespace grpc
|