Line data Source code
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> 20 374 : bool GetOptValueImpl( 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 386 : if (var_map.count(val) && (!if_not_defaulted || 26 12 : (if_not_defaulted && !var_map[val].defaulted()))) { 27 358 : std::vector<ElementType> tmp( 28 358 : var_map[val].as<std::vector<ElementType> >()); 29 : // Now split the individual elements 30 358 : for (typename std::vector<ElementType>::const_iterator it = 31 716 : tmp.begin(); 32 803 : it != tmp.end(); it++) { 33 445 : std::stringstream ss(*it); 34 890 : std::copy(std::istream_iterator<ElementType>(ss), 35 890 : std::istream_iterator<ElementType>(), 36 : std::back_inserter(var)); 37 : } 38 358 : return true; 39 358 : } 40 16 : return false; 41 : } 42 : 43 : template <typename ValueType> 44 4330 : bool GetOptValueImpl( 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 4338 : if (var_map.count(val) && (!if_not_defaulted || 50 8 : (if_not_defaulted && !var_map[val].defaulted()))) { 51 3960 : var = var_map[val].as<ValueType>(); 52 3960 : return true; 53 : } 54 370 : return false; 55 : } 56 : 57 : template <typename ValueType> 58 4672 : bool GetOptValue(const boost::program_options::variables_map &var_map, 59 : ValueType &var, const std::string &val) { 60 4672 : return GetOptValueImpl(var_map, var, val, static_cast<ValueType *>(0), 61 4672 : false); 62 : } 63 : 64 : template <typename ValueType> 65 32 : bool GetOptValueIfNotDefaulted( 66 : const boost::program_options::variables_map &var_map, 67 : ValueType &var, const std::string &val) { 68 32 : return GetOptValueImpl(var_map, var, val, static_cast<ValueType *>(0), 69 32 : true); 70 : } 71 : 72 : } // namespace util 73 : } // namespace options 74 : 75 : #endif // BASE_OPTIONS_UTIL_H_