1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
/**
* Copyright (c) 2014, Institute of Telematics, Karlsruhe Institute of Technology.
*
* This file is part of the PktAnon project. PktAnon is distributed under 2-clause BSD licence.
* See LICENSE file found in the top-level directory of this distribution.
*/
# ifndef PKTANON_PARAMS_H
# define PKTANON_PARAMS_H
# include <stdexcept>
# include <string>
# include <unordered_map>
namespace pktanon
{
enum class TFD_VAL {TRUE, FALSE, DEFAULT};
struct ID_VAL
{
ID_VAL() : int_val(0), is_default(true) {}
ID_VAL(unsigned long value) : int_val(value), is_default(false) {}
unsigned long int_val;
bool is_default;
};
# define THROW_MISSING_VAL(key) throw std::runtime_error(std::string("missing parameter: ") + key);
# define THROW_WRONG_VAL(key, wrong_value) throw std::runtime_error("illegal value for boolean parameter " + key + ": " + wrong_value);
/**
* wrapper arount hashtable (inspired by java.lang.Properties)
*/
class Params
{
public:
~Params() = default;
Params() :props() {};
// Params(Params&& other) : props(std::move(other.props)) {};
bool has_param(const std::string& key) const { return props.find(key) != props.end(); }
const std::string& get_param(const std::string& key) const
{
if (!has_param(key)) throw std::runtime_error(std::string("missing parameter: ") + key);
return (*props.find(key)).second;
}
const std::string& get_param(const std::string& key, const std::string& default_value) const
{
if (has_param(key)) return (*props.find(key)).second;
return default_value;
}
bool get_bool_param(const std::string& key) const
{
if (!has_param(key)) throw std::runtime_error(std::string("missing parameter: ") + key);
const auto& str_val = (*props.find(key)).second;
if (is_true_value(str_val)) { return true; }
else if (is_false_value(str_val)) { return false; }
else { THROW_WRONG_VAL(key, str_val); }
}
bool get_bool_param(const std::string& key, bool default_value) const
{
if (has_param(key)) { return get_bool_param(key); }
return default_value;
}
///param with values - true/false/default
TFD_VAL get_tfd_param(const std::string& key) const
{
if (!has_param(key)) return TFD_VAL::DEFAULT;
const auto& str_val = (*props.find(key)).second;
if (is_default_value(str_val)) {return TFD_VAL::DEFAULT;}
else if (is_true_value(str_val)) {return TFD_VAL::TRUE;}
else if (is_false_value(str_val)) {return TFD_VAL::FALSE;}
else {THROW_WRONG_VAL(key, str_val);}
}
unsigned long get_uint_param(const std::string& key) const
{
if (!has_param(key)) throw std::runtime_error(std::string("missing parameter: ") + key);
const auto& str_val = (*props.find(key)).second;
return std::stoul(str_val);
}
unsigned long get_uint_param(const std::string& key, unsigned long default_value) const
{
if (has_param(key)) { return get_uint_param(key); }
return default_value;
}
ID_VAL get_uintdefault_param(const std::string& key) const
{
if (!has_param(key)) return ID_VAL();
const auto& str_val = (*props.find(key)).second;
if (is_default_value(str_val)) {return ID_VAL();}
else { return ID_VAL(std::stoul(str_val)); }
}
bool is_missing_or_has_default_value(const std::string& key) const
{
if (!has_param(key)) return true;
const auto& str_val = (*props.find(key)).second;
if (is_default_value(str_val)) return true;
return false;
}
void add_param(const std::string&& key, const std::string&& value)
{
props.emplace(std::move(key), std::move(value));
}
private:
bool is_default_value(const std::string& value) const { return value.compare("auto") == 0 || value.compare("default") == 0;}
bool is_true_value(const std::string& value) const { return value.compare("yes") == 0 || value.compare("on") == 0 || value.compare("1") == 0;}
bool is_false_value(const std::string& value) const { return value.compare("no") == 0 || value.compare("off") == 0 || value.compare("0") == 0;}
std::unordered_map<std::string, std::string> props;
};
}
# endif
|