OpenSDN source code
options_util.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3 // Copyright Contributors to the OpenSDN project
4 //
5 
6 #ifndef BASE_OPTIONS_UTIL_H_
7 #define BASE_OPTIONS_UTIL_H_
8 
9 #include <vector>
10 #include <algorithm>
11 #include <sstream>
12 #include <iterator>
13 
14 #include <boost/program_options.hpp>
15 
16 namespace options {
17 namespace util {
18 
19 // Implementation overloads
20 template <typename ElementType>
22  const boost::program_options::variables_map &var_map,
23  std::vector<ElementType> &var, const std::string &val,
24  std::vector<ElementType>*, bool if_not_defaulted) {
25  // Check if the value is present.
26  if (var_map.count(val) && (!if_not_defaulted ||
27  (if_not_defaulted && !var_map[val].defaulted()))) {
28  std::vector<ElementType> tmp(
29  var_map[val].as<std::vector<ElementType> >());
30  // Now split the individual elements
31  for (typename std::vector<ElementType>::const_iterator it =
32  tmp.begin();
33  it != tmp.end(); it++) {
34  std::stringstream ss(*it);
35  std::copy(std::istream_iterator<ElementType>(ss),
36  std::istream_iterator<ElementType>(),
37  std::back_inserter(var));
38  }
39  return true;
40  }
41  return false;
42 }
43 
44 template <typename ValueType>
46  const boost::program_options::variables_map &var_map,
47  ValueType &var, const std::string &val, ValueType*,
48  bool if_not_defaulted) {
49  // Check if the value is present.
50  if (var_map.count(val) && (!if_not_defaulted ||
51  (if_not_defaulted && !var_map[val].defaulted()))) {
52  var = var_map[val].as<ValueType>();
53  return true;
54  }
55  return false;
56 }
57 
58 template <typename ValueType>
59 bool GetOptValue(const boost::program_options::variables_map &var_map,
60  ValueType &var, const std::string &val) {
61  return GetOptValueImpl(var_map, var, val, static_cast<ValueType *>(0),
62  false);
63 }
64 
65 template <typename ValueType>
67  const boost::program_options::variables_map &var_map,
68  ValueType &var, const std::string &val) {
69  return GetOptValueImpl(var_map, var, val, static_cast<ValueType *>(0),
70  true);
71 }
72 
73 } // namespace util
74 } // namespace options
75 
76 #endif // BASE_OPTIONS_UTIL_H_
bool GetOptValue(const boost::program_options::variables_map &var_map, ValueType &var, const std::string &val)
Definition: options_util.h:59
bool GetOptValueImpl(const boost::program_options::variables_map &var_map, std::vector< ElementType > &var, const std::string &val, std::vector< ElementType > *, bool if_not_defaulted)
Definition: options_util.h:21
bool GetOptValueIfNotDefaulted(const boost::program_options::variables_map &var_map, ValueType &var, const std::string &val)
Definition: options_util.h:66