parameter.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2014 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef mia_core_parameters_hh
22 #define mia_core_parameters_hh
23 
24 #include <string>
25 #include <map>
26 #include <ostream>
27 #include <istream>
28 #include <sstream>
29 #include <mia/core/dictmap.hh>
30 #include <mia/core/msgstream.hh>
31 #include <mia/core/handlerbase.hh>
34 
35 namespace xmlpp {
36  class Element;
37 }
38 
40 
50 public:
57  CParameter(const char type[], bool required, const char *descr);
58 
62  virtual ~CParameter();
63 
67  const char *type() const;
71  void descr(std::ostream& os) const;
72 
73 
78  std::string get_value_as_string() const;
79 
84  void value(std::ostream& os) const;
85 
89  bool required_set() const;
90 
94  bool set(const std::string& str_value);
95 
97  const char *get_descr() const;
98 
102  void reset();
103 
109  void add_dependend_handler(HandlerHelpMap& handler_map) const;
110 
112  std::string get_default_value() const;
113 
118  void get_help_xml(xmlpp::Element& root) const;
119 
120 
126  virtual void post_set();
127 
128 protected:
129 
134  virtual void do_descr(std::ostream& os) const = 0;
135 
137  const std::string errmsg(const std::string& err_value) const;
138 private:
142  virtual void do_add_dependend_handler(HandlerHelpMap& handler_map) const;
143  virtual bool do_set(const std::string& str_value) = 0;
144  virtual void do_reset() = 0;
145  virtual std::string do_get_default_value() const = 0;
146  virtual std::string do_get_value_as_string() const = 0;
147  virtual void do_get_help_xml(xmlpp::Element& self) const;
148  bool m_required;
149  bool m_is_required;
150  const char *m_type;
151  const char *m_descr;
152 };
153 
154 
163 template <typename T>
165 
166 public:
172  CTParameter(T& value, bool required, const char *descr);
173 
174 protected:
178  virtual void do_descr(std::ostream& os) const;
179 private:
180  virtual bool do_set(const std::string& str_value);
181  virtual void do_reset();
182  virtual void adjust(T& value);
183  virtual std::string do_get_default_value() const;
184  virtual std::string do_get_value_as_string() const;
185  T& m_value;
186  const T m_default_value;
187 };
188 
197 template <typename T>
199 
200 public:
208  TRangeParameter(T& value, T min, T max, bool required, const char *descr);
209 protected:
213  void do_descr(std::ostream& os) const;
214 private:
215  virtual void adjust(T& value);
216  virtual void do_get_help_xml(xmlpp::Element& self) const;
217  T m_min;
218  T m_max;
219 
220 };
221 
222 
231 template <typename T>
232 class CDictParameter : public CParameter{
233 
234 public:
241  CDictParameter(T& value, const TDictMap<T>& dict, const char *descr, bool required = false);
242 protected:
246  virtual void do_descr(std::ostream& os) const;
247 private:
248  virtual bool do_set(const std::string& str_value);
249  virtual void do_reset();
250  virtual std::string do_get_default_value() const;
251  virtual std::string do_get_value_as_string() const;
252  virtual void do_get_help_xml(xmlpp::Element& self) const;
253  T& m_value;
254  T m_default_value;
255  const TDictMap<T> m_dict;
256 
257 };
258 
259 
268 template <typename F>
270 
271 public:
283  TFactoryParameter(typename F::ProductPtr& value, const std::string& init, bool required, const char *descr);
284 
296  TFactoryParameter(typename F::UniqueProduct& value, const std::string& init, bool required, const char *descr);
297 private:
298  virtual void do_descr(std::ostream& os) const;
299  virtual void do_add_dependend_handler(HandlerHelpMap& handler_map)const;
300  virtual bool do_set(const std::string& str_value);
301  virtual void do_reset();
302  virtual std::string do_get_default_value() const;
303  virtual std::string do_get_value_as_string() const;
304  virtual void do_get_help_xml(xmlpp::Element& self) const;
305 
306  typename F::ProductPtr dummy_shared_value;
307  typename F::UniqueProduct dummy_unique_value;
308 
309  typename F::ProductPtr& m_shared_value;
310  typename F::UniqueProduct& m_unique_value;
311 
312  virtual void post_set();
313 
314  std::string m_string_value;
315  std::string m_default_value;
316  bool m_unique;
317 
318 
319 };
320 
321 
322 
333 template <typename T>
334 class CSetParameter : public CParameter{
335 
336 public:
343  CSetParameter(T& value, const std::set<T>& valid_set, const char *descr, bool required = false);
344 protected:
348  virtual void do_descr(std::ostream& os) const;
349 private:
350  virtual bool do_set(const std::string& str_value);
351  virtual void do_reset();
352  virtual std::string do_get_default_value() const;
353  virtual std::string do_get_value_as_string() const;
354  void do_get_help_xml(xmlpp::Element& self) const;
355  T& m_value;
356  T m_default_value;
357  const std::set<T> m_valid_set;
358 
359 };
360 
370 template <typename T>
371 class TParameter : public CParameter{
372 
373 public:
379  TParameter(T& value, bool required, const char *descr);
380 protected:
384  virtual void do_descr(std::ostream& os) const;
385 private:
386  virtual void do_reset();
387  virtual bool do_set(const std::string& str_value);
388  virtual std::string do_get_default_value() const;
389  virtual std::string do_get_value_as_string() const;
390 
391  T& m_value;
392  T m_default_value;
393 };
394 
395 
398 public:
399  CStringParameter(std::string& value, CCmdOptionFlags flags, const char *descr,
400  const CPluginHandlerBase *plugin_hint = nullptr);
401 
402 private:
403  virtual void do_reset();
404  virtual bool do_set(const std::string& str_value);
405  virtual std::string do_get_default_value() const;
406  virtual std::string do_get_value_as_string() const;
407 
408  virtual void do_descr(std::ostream& os) const;
409  virtual void do_get_help_xml(xmlpp::Element& self) const;
410  virtual void do_add_dependend_handler(HandlerHelpMap& handler_map)const;
411 
412 
413  std::string& m_value;
414  std::string m_default_value;
415  CCmdOptionFlags m_flags;
416  const CPluginHandlerBase *m_plugin_hint;
417 };
418 
419 
430 
446 template <typename T>
447 CParameter *make_param(std::shared_ptr<T>& value, const std::string& init, bool required, const char *descr)
448 {
449  typedef typename FactoryTrait<T>::type F;
450  return new TFactoryParameter<F>(value, init, required, descr);
451 
452 }
453 
468 template <typename T>
469 CParameter *make_param(std::unique_ptr<T>& value, const std::string& init, bool required, const char *descr)
470 {
471  typedef typename FactoryTrait<T>::type F;
472  return new TFactoryParameter<F>(value, init, required, descr);
473 
474 }
475 
476 
477 template <typename T>
478 CParameter *make_param(T& value, bool required, const char *descr)
479 {
480  return new TParameter<T>(value, required, descr);
481 }
482 
483 
484 
486 
490 template <typename T>
491 struct __dispatch_param_translate {
492  static std::string apply(T x) {
493  std::ostringstream s;
494  s << x;
495  return s.str();
496  }
497 };
498 
499 template <>
500 struct __dispatch_param_translate<std::string> {
501  static std::string apply(const std::string& x) {
502  return x;
503  }
504 };
505 
506 template <>
507 struct __dispatch_param_translate<const char *> {
508  static std::string apply(const char * x) {
509  return std::string(x);
510  }
511 };
512 
514 
515 template <typename T>
516 CDictParameter<T>::CDictParameter(T& value, const TDictMap<T>& dict, const char *descr, bool required):
517  CParameter("dict", required, descr),
518  m_value(value),
519  m_default_value(value),
520  m_dict(dict)
521 {
522 }
523 
524 template <typename T>
525 void CDictParameter<T>::do_descr(std::ostream& os) const
526 {
527  for (auto i = m_dict.get_help_begin(); i != m_dict.get_help_end(); ++i) {
528  os << "\n " << i->second.first << ": " << i->second.second;
529  }
530 }
531 
532 template <typename T>
533 void CDictParameter<T>::do_get_help_xml(xmlpp::Element& self) const
534 {
536  auto dict = self.add_child("dict");
537  for (auto i = m_dict.get_help_begin(); i != m_dict.get_help_end(); ++i) {
538  auto v = dict->add_child("value");
539  v->set_attribute("name", i->second.first);
540  v->set_child_text(i->second.second);
541  }
542 }
543 
544 template <typename T>
545 bool CDictParameter<T>::do_set(const std::string& str_value)
546 {
547  m_value = m_dict.get_value(str_value.c_str());
548  return true;
549 }
550 
551 template <typename T>
553 {
554  m_value = m_default_value;
555 }
556 
557 template <typename T>
558 std::string CDictParameter<T>::do_get_default_value() const
559 {
560  return m_dict.get_name(m_default_value);
561 }
562 
563 template <typename T>
565 {
566  return m_dict.get_name(m_value);
567 }
568 
569 template <typename F>
570 TFactoryParameter<F>::TFactoryParameter(typename F::ProductPtr& value,
571  const std::string& init, bool required, const char *descr):
572  CParameter("factory", required, descr),
573  m_shared_value(value),
574  m_unique_value(dummy_unique_value),
575  m_string_value(init),
576  m_default_value(init),
577  m_unique(false)
578 {
579 }
580 
581 template <typename F>
582 TFactoryParameter<F>::TFactoryParameter(typename F::UniqueProduct& value, const std::string& init, bool required, const char *descr):
583  CParameter("factory", required, descr),
584  m_shared_value(dummy_shared_value),
585  m_unique_value(value),
586  m_string_value(init),
587  m_default_value(init),
588  m_unique(true)
589 {
590 }
591 
592 
593 
594 template <typename T>
595 void TFactoryParameter<T>::do_descr(std::ostream& os) const
596 {
597  os << "For a list of available plug-ins see run 'mia-plugin-help "
598  << T::instance().get_descriptor() << "'";
599 }
600 
601 template <typename T>
602 void TFactoryParameter<T>::do_get_help_xml(xmlpp::Element& self) const
603 {
604  auto dict = self.add_child("factory");
605  dict->set_attribute("name", T::instance().get_descriptor());
606 }
607 
608 template <typename T>
609 bool TFactoryParameter<T>::do_set(const std::string& str_value)
610 {
611  m_string_value = str_value;
612  return true;
613 }
614 
615 template <typename T>
617 {
618  if (!m_string_value.empty()) {
619  if (m_unique)
620  m_unique_value = T::instance().produce_unique(m_string_value);
621  else
622  m_shared_value = T::instance().produce(m_string_value);
623  }
624 }
625 
626 template <typename T>
628 {
629  m_string_value = m_default_value;
630 }
631 
632 template <typename T>
634 {
635  // add recursively all dependent handlers
636  if (handler_map.find(T::instance().get_descriptor()) == handler_map.end()){
637  handler_map[T::instance().get_descriptor()] = &T::instance();
638  for (auto i = T::instance().begin(); i != T::instance().end(); ++i)
639  i->second->add_dependend_handlers(handler_map);
640  }
641 }
642 
643 template <typename T>
645 {
646  return m_default_value;
647 }
648 
649 template <typename T>
651 {
652  if (m_unique && m_unique_value)
653  return m_unique_value->get_init_string();
654  if (!m_unique && m_shared_value)
655  return m_shared_value->get_init_string();
656  return m_string_value;
657 }
658 
659 template <typename T>
660 CSetParameter<T>::CSetParameter(T& value, const std::set<T>& valid_set, const char *descr, bool required):
661  CParameter("set", required, descr),
662  m_value(value),
663  m_default_value(value),
664  m_valid_set(valid_set)
665 {
666  if (m_valid_set.empty())
667  throw std::invalid_argument("CSetParameter initialized with empty set");
668 }
669 
670 
671 template <typename T>
672 std::string CSetParameter<T>::do_get_default_value() const
673 {
674  return __dispatch_param_translate<T>::apply(m_default_value);
675 }
676 
677 template <typename T>
679 {
680  return __dispatch_param_translate<T>::apply(m_value);
681 }
682 
683 template <typename T>
684 void CSetParameter<T>::do_descr(std::ostream& os) const
685 {
686  auto i = m_valid_set.begin();
687  auto e = m_valid_set.end();
688 
689  assert ( i != e );
690 
691  os << " Supported values are (" << *i;
692  ++i;
693 
694  while (i != e)
695  os << '|' << *i++;
696  os << ')';
697 }
698 
699 template <typename T>
700 void CSetParameter<T>::do_get_help_xml(xmlpp::Element& self) const
701 {
702  auto set = self.add_child("set");
703  for (auto i = m_valid_set.begin(); i != m_valid_set.end(); ++i) {
704  auto v = set->add_child("value");
705  v->set_attribute("name", __dispatch_param_translate<T>::apply(*i));
706  }
707 }
708 
709 template <typename T>
711 {
712  m_value = m_default_value;
713 }
714 
715 template <typename T>
716 bool CSetParameter<T>::do_set(const std::string& str_value)
717 {
718  std::stringstream s(str_value);
719  T val;
720  s >> val;
721  if (s.fail() || m_valid_set.find(val) == m_valid_set.end()) {
722  throw std::invalid_argument(errmsg(str_value));
723  }
724  m_value = val;
725  return true;
726 }
727 
728 
729 
730 template <typename T>
731 TParameter<T>::TParameter(T& value, bool required, const char *descr):
732  CParameter("streamable", required, descr),
733  m_value(value),
734  m_default_value(value)
735 {
736 }
737 
738 
739 
740 template <typename T>
741 void TParameter<T>::do_descr(std::ostream& os) const
742 {
743  os << m_value;
744 }
745 
746 template <typename T>
747 bool TParameter<T>::do_set(const std::string& str_value)
748 {
749  std::stringstream s(str_value);
750  s >> m_value;
751  if (s.fail())
752  throw std::invalid_argument(errmsg(str_value));
753  return true;
754 }
755 
756 template <typename T>
758 {
759  m_value = m_default_value;
760 }
761 
762 template <typename T>
763 std::string TParameter<T>::do_get_default_value() const
764 {
765  std::ostringstream s;
766  s << m_default_value;
767  auto str = s.str();
768  if (str.find(',') != std::string::npos) {
769  std::ostringstream s2;
770  s2 << '[' << str << ']';
771  str = s2.str();
772  }
773  return str;
774 }
775 
776 template <typename T>
777 std::string TParameter<T>::do_get_value_as_string() const
778 {
779  return __dispatch_param_translate<T>::apply(m_value);
780 }
781 
783 
784 #endif
CSetParameter(T &value, const std::set< T > &valid_set, const char *descr, bool required=false)
Definition: parameter.hh:660
TRangeParameter< float > CFloatParameter
a float parameter (with range)
Definition: parameter.hh:425
virtual void do_descr(std::ostream &os) const
Definition: parameter.hh:525
TRangeParameter< double > CDoubleParameter
a double parameter (with range)
Definition: parameter.hh:427
virtual void do_descr(std::ostream &os) const
virtual void do_descr(std::ostream &os) const
Definition: parameter.hh:741
A parameter that get's initialized by a factory to a shared or unique pointer.
Definition: parameter.hh:269
void value(std::ostream &os) const
A parameter that can only assume values out of a limited set.
Definition: parameter.hh:334
CTParameter< bool > CBoolParameter
boolean parameter
Definition: parameter.hh:429
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:43
TFactoryParameter(typename F::ProductPtr &value, const std::string &init, bool required, const char *descr)
Definition: parameter.hh:570
The base class for parameters used in complex options.
Definition: parameter.hh:49
std::map< std::string, const CPluginHandlerBase * > HandlerHelpMap
A map that is used to collect the plug-in handlers used in a program.
Definition: handlerbase.hh:36
The base class for all plugin handlers.
Definition: handlerbase.hh:57
Generic type of a complex paramter.
Definition: parameter.hh:164
virtual void do_descr(std::ostream &os) const =0
A parameter that can assume any value of the given value type.
Definition: parameter.hh:371
#define TRACE_FUNCTION
Definition: msgstream.hh:202
TRangeParameter< int > CIntParameter
an integer parameter (with range)
Definition: parameter.hh:421
CCmdOptionFlags
CParameter * make_param(std::shared_ptr< T > &value, const std::string &init, bool required, const char *descr)
create a factory parameter that initializes to a std::shared_ptr
Definition: parameter.hh:447
void descr(std::ostream &os) const
TRangeParameter< unsigned int > CUIntParameter
an unsigned integer parameter (with range)
Definition: parameter.hh:423
virtual void do_descr(std::ostream &os) const
Definition: parameter.hh:684
Scalar parameter with an expected value range.
Definition: parameter.hh:198
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition: defines.hh:110
an string parameter
Definition: parameter.hh:397
Dictionary paramater.
Definition: parameter.hh:232
TParameter(T &value, bool required, const char *descr)
Definition: parameter.hh:731
CDictParameter(T &value, const TDictMap< T > &dict, const char *descr, bool required=false)
Definition: parameter.hh:516
A mapper from emums to string values. - usefull for names flags.
Definition: dictmap.hh:45
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:46