FactoryParams.hh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #if !defined(h_3e645482_ae6a_43e5_8f81_abbc4200212d)
  2. #define h_3e645482_ae6a_43e5_8f81_abbc4200212d
  3. #include "Base/GCException.h"
  4. #include <map>
  5. #include <string>
  6. #include <sstream>
  7. #include <stdexcept>
  8. #include "Portability.hh"
  9. LOG4CPP_NS_BEGIN
  10. class FactoryParams;
  11. namespace details
  12. {
  13. class base_validator_data
  14. {
  15. public:
  16. base_validator_data(const char* tag, const FactoryParams* params) : tag_(tag), params_(params){}
  17. protected:
  18. const char* tag_;
  19. const FactoryParams* params_;
  20. template<typename T>
  21. void assign(const std::string& param_value, T& value) const
  22. {
  23. assign_impl(param_value, value);
  24. }
  25. template<typename T>
  26. void assign_impl(const std::string& param_value, T& value) const
  27. {
  28. std::stringstream s;
  29. s << param_value;
  30. s >> value;
  31. }
  32. void assign_impl(const std::string& param_value, std::string& value) const
  33. {
  34. value = param_value;
  35. }
  36. void throw_error(const char* param_name) const
  37. {
  38. std::stringstream s;
  39. s << "Property '" << param_name << "' required to configure " << tag_;
  40. //throw std::runtime_error(s.str());
  41. throw RUNTIME_EXCEPTION("%s", s.str().c_str());
  42. }
  43. };
  44. class optional_params_validator;
  45. class required_params_validator : public base_validator_data
  46. {
  47. public:
  48. required_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
  49. template<typename T>
  50. optional_params_validator optional(const char* param, T& value) const;
  51. template<typename T>
  52. const required_params_validator& operator()(const char* param, T& value) const;
  53. };
  54. class optional_params_validator : public base_validator_data
  55. {
  56. public:
  57. optional_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
  58. template<typename T>
  59. required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
  60. template<typename T>
  61. const optional_params_validator& operator()(const char* param, T& value) const;
  62. };
  63. template<typename T>
  64. optional_params_validator required_params_validator::optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
  65. class parameter_validator : public base_validator_data
  66. {
  67. public:
  68. parameter_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
  69. template<typename T>
  70. required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
  71. template<typename T>
  72. optional_params_validator optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
  73. };
  74. }
  75. class LOG4CPP_EXPORT FactoryParams
  76. {
  77. typedef std::map<std::string, std::string> storage_t;
  78. storage_t storage_;
  79. public:
  80. typedef storage_t::const_iterator const_iterator;
  81. const std::string& operator[](const std::string& v) const;
  82. std::string& operator[](const std::string& v) { return storage_[v]; }
  83. details::parameter_validator get_for(const char* tag) const { return details::parameter_validator(tag, this); }
  84. const_iterator find(const std::string& t) const;
  85. const_iterator begin() const { return storage_.begin(); }
  86. const_iterator end() const { return storage_.end(); }
  87. private:
  88. /*typedef std::map<std::string, std::string> storage_t;
  89. storage_t storage_; */
  90. };
  91. namespace details
  92. {
  93. template<typename T>
  94. const required_params_validator& required_params_validator::operator()(const char* param, T& value) const
  95. {
  96. FactoryParams::const_iterator i = params_->find(param);
  97. if (i != params_->end())
  98. assign(i->second, value);
  99. else
  100. throw_error(param);
  101. return *this;
  102. }
  103. template<typename T>
  104. const optional_params_validator& optional_params_validator::operator()(const char* param, T& value) const
  105. {
  106. FactoryParams::const_iterator i = params_->find(param);
  107. if (i != params_->end())
  108. assign(i->second, value);
  109. return *this;
  110. }
  111. }
  112. LOG4CPP_NS_END
  113. #endif // h_3e645482_ae6a_43e5_8f81_abbc4200212d