Line data Source code
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> 21 364 : bool GetOptValueImpl( 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 372 : if (var_map.count(val) && (!if_not_defaulted || 27 8 : (if_not_defaulted && !var_map[val].defaulted()))) { 28 352 : std::vector<ElementType> tmp( 29 352 : var_map[val].as<std::vector<ElementType> >()); 30 : // Now split the individual elements 31 352 : for (typename std::vector<ElementType>::const_iterator it = 32 704 : tmp.begin(); 33 791 : it != tmp.end(); it++) { 34 439 : std::stringstream ss(*it); 35 878 : std::copy(std::istream_iterator<ElementType>(ss), 36 878 : std::istream_iterator<ElementType>(), 37 : std::back_inserter(var)); 38 : } 39 352 : return true; 40 352 : } 41 12 : return false; 42 : } 43 : 44 : template <typename ValueType> 45 4223 : bool GetOptValueImpl( 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 4231 : if (var_map.count(val) && (!if_not_defaulted || 51 8 : (if_not_defaulted && !var_map[val].defaulted()))) { 52 3875 : var = var_map[val].as<ValueType>(); 53 3875 : return true; 54 : } 55 348 : return false; 56 : } 57 : 58 : template <typename ValueType> 59 4563 : bool GetOptValue(const boost::program_options::variables_map &var_map, 60 : ValueType &var, const std::string &val) { 61 4563 : return GetOptValueImpl(var_map, var, val, static_cast<ValueType *>(0), 62 4563 : false); 63 : } 64 : 65 : template <typename ValueType> 66 24 : bool GetOptValueIfNotDefaulted( 67 : const boost::program_options::variables_map &var_map, 68 : ValueType &var, const std::string &val) { 69 24 : return GetOptValueImpl(var_map, var, val, static_cast<ValueType *>(0), 70 24 : true); 71 : } 72 : 73 : } // namespace util 74 : } // namespace options 75 : 76 : #endif // BASE_OPTIONS_UTIL_H_