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 175 : ChannelArguments::ChannelArguments(const ChannelArguments& other)
42 175 : : strings_(other.strings_) {
43 175 : args_.reserve(other.args_.size());
44 175 : auto list_it_dst = strings_.begin();
45 175 : auto list_it_src = other.strings_.begin();
46 304 : for (auto a = other.args_.begin(); a != other.args_.end(); ++a) {
47 : grpc_arg ap;
48 129 : ap.type = a->type;
49 129 : GPR_ASSERT(list_it_src->c_str() == a->key);
50 129 : ap.key = const_cast<char*>(list_it_dst->c_str());
51 129 : ++list_it_src;
52 129 : ++list_it_dst;
53 129 : 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 129 : GPR_ASSERT(list_it_src->c_str() == a->value.string);
59 129 : ap.value.string = const_cast<char*>(list_it_dst->c_str());
60 129 : ++list_it_src;
61 129 : ++list_it_dst;
62 129 : break;
63 : case GRPC_ARG_POINTER:
64 0 : ap.value.pointer = a->value.pointer;
65 0 : ap.value.pointer.p = a->value.pointer.copy
66 0 : ? a->value.pointer.copy(ap.value.pointer.p)
67 0 : : ap.value.pointer.p;
68 0 : break;
69 : }
70 129 : args_.push_back(ap);
71 : }
72 175 : }
73 :
74 0 : void ChannelArguments::Swap(ChannelArguments& other) {
75 0 : args_.swap(other.args_);
76 0 : strings_.swap(other.strings_);
77 0 : }
78 :
79 0 : void ChannelArguments::SetCompressionAlgorithm(
80 : grpc_compression_algorithm algorithm) {
81 0 : SetInt(GRPC_COMPRESSION_ALGORITHM_ARG, algorithm);
82 0 : }
83 :
84 245 : void ChannelArguments::SetInt(const grpc::string& key, int value) {
85 : grpc_arg arg;
86 245 : arg.type = GRPC_ARG_INTEGER;
87 245 : strings_.push_back(key);
88 245 : arg.key = const_cast<char*>(strings_.back().c_str());
89 245 : arg.value.integer = value;
90 :
91 245 : args_.push_back(arg);
92 245 : }
93 :
94 1 : void ChannelArguments::SetPointer(const grpc::string& key, void* value) {
95 : grpc_arg arg;
96 1 : arg.type = GRPC_ARG_POINTER;
97 1 : strings_.push_back(key);
98 1 : arg.key = const_cast<char*>(strings_.back().c_str());
99 1 : arg.value.pointer.p = value;
100 1 : arg.value.pointer.copy = nullptr;
101 1 : arg.value.pointer.destroy = nullptr;
102 1 : args_.push_back(arg);
103 1 : }
104 :
105 306 : void ChannelArguments::SetString(const grpc::string& key,
106 : const grpc::string& value) {
107 : grpc_arg arg;
108 306 : arg.type = GRPC_ARG_STRING;
109 306 : strings_.push_back(key);
110 306 : arg.key = const_cast<char*>(strings_.back().c_str());
111 306 : strings_.push_back(value);
112 306 : arg.value.string = const_cast<char*>(strings_.back().c_str());
113 :
114 306 : args_.push_back(arg);
115 306 : }
116 :
117 343 : void ChannelArguments::SetChannelArgs(grpc_channel_args* channel_args) const {
118 343 : channel_args->num_args = args_.size();
119 343 : if (channel_args->num_args > 0) {
120 340 : channel_args->args = const_cast<grpc_arg*>(&args_[0]);
121 : }
122 343 : }
123 :
124 : } // namespace grpc
|