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