21 #ifndef mia_core_parameters_hh
22 #define mia_core_parameters_hh
56 CParameter(
const char type[],
bool required,
const char *descr);
66 const char *type()
const;
70 void descr(std::ostream& os)
const;
77 std::string get_value_as_string()
const;
83 void value(std::ostream& os)
const;
88 bool required_set()
const;
93 bool set(
const std::string& str_value);
96 const char *get_descr()
const;
111 std::string get_default_value()
const;
117 void get_help_xml(xmlpp::Element& root)
const;
125 virtual void post_set();
133 virtual void do_descr(std::ostream& os)
const = 0;
136 const std::string errmsg(
const std::string& err_value)
const;
141 virtual void do_add_dependend_handler(
HandlerHelpMap& handler_map)
const;
142 virtual bool do_set(
const std::string& str_value) = 0;
143 virtual void do_reset() = 0;
144 virtual std::string do_get_default_value()
const = 0;
145 virtual std::string do_get_value_as_string()
const = 0;
146 virtual void do_get_help_xml(xmlpp::Element&
self)
const;
162 template <
typename T>
171 CTParameter(T& value,
bool required,
const char *descr);
177 virtual void do_descr(std::ostream& os)
const;
179 virtual bool do_set(
const std::string& str_value);
180 virtual void do_reset();
181 virtual void adjust(T& value);
182 virtual std::string do_get_default_value()
const;
183 virtual std::string do_get_value_as_string()
const;
185 const T m_default_value;
196 template <
typename T>
207 TRangeParameter(T& value, T min, T max,
bool required,
const char *descr);
212 void do_descr(std::ostream& os)
const;
214 virtual void adjust(T& value);
215 virtual void do_get_help_xml(xmlpp::Element&
self)
const;
230 template <
typename T>
245 virtual void do_descr(std::ostream& os)
const;
247 virtual bool do_set(
const std::string& str_value);
248 virtual void do_reset();
249 virtual std::string do_get_default_value()
const;
250 virtual std::string do_get_value_as_string()
const;
251 virtual void do_get_help_xml(xmlpp::Element&
self)
const;
267 template <
typename F>
295 TFactoryParameter(
typename F::UniqueProduct& value,
const std::string& init,
bool required,
const char *descr);
297 virtual void do_descr(std::ostream& os)
const;
298 virtual void do_add_dependend_handler(
HandlerHelpMap& handler_map)
const;
299 virtual bool do_set(
const std::string& str_value);
300 virtual void do_reset();
301 virtual std::string do_get_default_value()
const;
302 virtual std::string do_get_value_as_string()
const;
303 virtual void do_get_help_xml(xmlpp::Element&
self)
const;
305 typename F::ProductPtr dummy_shared_value;
306 typename F::UniqueProduct dummy_unique_value;
308 typename F::ProductPtr& m_shared_value;
309 typename F::UniqueProduct& m_unique_value;
311 virtual void post_set();
313 std::string m_string_value;
314 std::string m_default_value;
332 template <
typename T>
347 virtual void do_descr(std::ostream& os)
const;
349 virtual bool do_set(
const std::string& str_value);
350 virtual void do_reset();
351 virtual std::string do_get_default_value()
const;
352 virtual std::string do_get_value_as_string()
const;
353 void do_get_help_xml(xmlpp::Element&
self)
const;
356 const std::set<T> m_valid_set;
369 template <
typename T>
383 virtual void do_descr(std::ostream& os)
const;
385 virtual void do_reset();
386 virtual bool do_set(
const std::string& str_value);
387 virtual std::string do_get_default_value()
const;
388 virtual std::string do_get_value_as_string()
const;
402 virtual void do_reset();
403 virtual bool do_set(
const std::string& str_value);
404 virtual std::string do_get_default_value()
const;
405 virtual std::string do_get_value_as_string()
const;
407 virtual void do_descr(std::ostream& os)
const;
408 virtual void do_get_help_xml(xmlpp::Element&
self)
const;
409 virtual void do_add_dependend_handler(
HandlerHelpMap& handler_map)
const;
412 std::string& m_value;
413 std::string m_default_value;
444 template <
typename T>
447 typedef typename FactoryTrait<T>::type F;
466 template <
typename T>
469 typedef typename FactoryTrait<T>::type F;
475 template <
typename T>
488 template <
typename T>
489 struct __dispatch_param_translate {
490 static std::string apply(T x) {
491 std::ostringstream s;
498 struct __dispatch_param_translate<std::string> {
499 static std::string apply(
const std::string& x) {
505 struct __dispatch_param_translate<const char *> {
506 static std::string apply(
const char * x) {
507 return std::string(x);
513 template <
typename T>
517 m_default_value(value),
522 template <
typename T>
525 for (
auto i = m_dict.get_help_begin(); i != m_dict.get_help_end(); ++i) {
526 os <<
"\n " << i->second.first <<
": " << i->second.second;
530 template <
typename T>
534 auto dict =
self.add_child(
"dict");
535 for (
auto i = m_dict.get_help_begin(); i != m_dict.get_help_end(); ++i) {
536 auto v = dict->add_child(
"value");
537 v->set_attribute(
"name", i->second.first);
538 v->set_child_text(i->second.second);
542 template <
typename T>
545 m_value = m_dict.get_value(str_value.c_str());
549 template <
typename T>
552 m_value = m_default_value;
555 template <
typename T>
558 return m_dict.get_name(m_default_value);
561 template <
typename T>
564 return m_dict.get_name(m_value);
567 template <
typename F>
569 const std::string& init,
bool required,
const char *descr):
571 m_shared_value(value),
572 m_unique_value(dummy_unique_value),
573 m_string_value(init),
574 m_default_value(init),
579 template <
typename F>
582 m_shared_value(dummy_shared_value),
583 m_unique_value(value),
584 m_string_value(init),
585 m_default_value(init),
592 template <
typename T>
595 os <<
"For a list of available plug-ins see run 'mia-plugin-help "
596 << T::instance().get_descriptor() <<
"'";
599 template <
typename T>
602 auto dict =
self.add_child(
"factory");
603 dict->set_attribute(
"name", T::instance().get_descriptor());
606 template <
typename T>
609 m_string_value = str_value;
613 template <
typename T>
616 if (!m_string_value.empty()) {
618 m_unique_value = T::instance().produce_unique(m_string_value);
620 m_shared_value = T::instance().produce(m_string_value);
624 template <
typename T>
627 m_string_value = m_default_value;
630 template <
typename T>
634 if (handler_map.find(T::instance().get_descriptor()) == handler_map.end()){
635 handler_map[T::instance().get_descriptor()] = &T::instance();
636 for (
auto i = T::instance().begin(); i != T::instance().end(); ++i)
637 i->second->add_dependend_handlers(handler_map);
641 template <
typename T>
644 return m_default_value;
647 template <
typename T>
650 if (m_unique && m_unique_value)
651 return m_unique_value->get_init_string();
652 if (!m_unique && m_shared_value)
653 return m_shared_value->get_init_string();
654 return m_string_value;
657 template <
typename T>
661 m_default_value(value),
662 m_valid_set(valid_set)
664 if (m_valid_set.empty())
665 throw std::invalid_argument(
"CSetParameter initialized with empty set");
669 template <
typename T>
672 return __dispatch_param_translate<T>::apply(m_default_value);
675 template <
typename T>
678 return __dispatch_param_translate<T>::apply(m_value);
681 template <
typename T>
684 auto i = m_valid_set.begin();
685 auto e = m_valid_set.end();
689 os <<
" Supported values are (" << *i;
697 template <
typename T>
700 auto set =
self.add_child(
"set");
701 for (
auto i = m_valid_set.begin(); i != m_valid_set.end(); ++i) {
702 auto v = set->add_child(
"value");
703 v->set_attribute(
"name", __dispatch_param_translate<T>::apply(*i));
707 template <
typename T>
710 m_value = m_default_value;
713 template <
typename T>
716 std::stringstream s(str_value);
719 if (s.fail() || m_valid_set.find(val) == m_valid_set.end()) {
720 throw std::invalid_argument(errmsg(str_value));
728 template <
typename T>
732 m_default_value(value)
738 template <
typename T>
744 template <
typename T>
747 std::stringstream s(str_value);
750 throw std::invalid_argument(errmsg(str_value));
754 template <
typename T>
757 m_value = m_default_value;
760 template <
typename T>
763 std::ostringstream s;
764 s << m_default_value;
766 if (str.find(
',') != std::string::npos) {
767 std::ostringstream s2;
768 s2 <<
'[' << str <<
']';
774 template <
typename T>
777 return __dispatch_param_translate<T>::apply(m_value);