2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright (C) 2017 ScyllaDB
24 #include <seastar/core/sstring.hh>
26 #include <boost/any.hpp>
27 #include <boost/program_options.hpp>
30 #include <unordered_map>
35 namespace program_options {
38 /// \brief Wrapper for command-line options with arbitrary string associations.
40 /// This type, to be used with Boost.Program_options, will result in an option that stores an arbitrary number of
41 /// string associations.
43 /// Values are specified in the form "key0=value0:[key1=value1:...]". Options of this type can be specified multiple
44 /// times, and the values will be merged (with the last-provided value for a key taking precedence).
46 /// \note We need a distinct type (rather than a simple type alias) for overload resolution in the implementation, but
47 /// advertizing our inheritance of \c std::unordered_map would introduce the possibility of memory leaks since STL
48 /// containers do not declare virtual destructors.
50 class string_map final : private std::unordered_map<sstring, sstring> {
52 using base = std::unordered_map<sstring, sstring>;
54 using base::value_type;
56 using base::mapped_type;
64 using base::operator[];
68 friend bool operator==(const string_map&, const string_map&);
69 friend bool operator!=(const string_map&, const string_map&);
72 inline bool operator==(const string_map& lhs, const string_map& rhs) {
73 return static_cast<const string_map::base&>(lhs) == static_cast<const string_map::base&>(rhs);
76 inline bool operator!=(const string_map& lhs, const string_map& rhs) {
81 /// \brief Query the value of a key in a \c string_map, or a default value if the key doesn't exist.
83 sstring get_or_default(const string_map&, const sstring& key, const sstring& def = sstring());
85 std::istream& operator>>(std::istream& is, string_map&);
86 std::ostream& operator<<(std::ostream& os, const string_map&);
91 // Required implementation hook for Boost.Program_options.
93 void validate(boost::any& out, const std::vector<std::string>& in, string_map*, int);