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_INTERNAL_COMPILER_RUBY_GENERATOR_MAP_INL_H
20 #define GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_MAP_INL_H
21
22 #include "src/compiler/config.h"
23
24 #include <initializer_list>
25 #include <iostream>
26 #include <map>
27 #include <ostream> // NOLINT
28 #include <vector>
29
30 using std::initializer_list;
31 using std::map;
32 using std::vector;
33
34 namespace grpc_ruby_generator {
35
36 // Converts an initializer list of the form { key0, value0, key1, value1, ... }
37 // into a map of key* to value*. Is merely a readability helper for later code.
ListToDict(const initializer_list<grpc::string> & values)38 inline std::map<grpc::string, grpc::string> ListToDict(
39 const initializer_list<grpc::string>& values) {
40 if (values.size() % 2 != 0) {
41 std::cerr << "Not every 'key' has a value in `values`." << std::endl;
42 }
43 std::map<grpc::string, grpc::string> value_map;
44 auto value_iter = values.begin();
45 for (unsigned i = 0; i < values.size() / 2; ++i) {
46 grpc::string key = *value_iter;
47 ++value_iter;
48 grpc::string value = *value_iter;
49 value_map[key] = value;
50 ++value_iter;
51 }
52 return value_map;
53 }
54
55 } // namespace grpc_ruby_generator
56
57 #endif // GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_MAP_INL_H
58