/usr/include/mia-2.4/mia/core/parameter.hh is in libmia-2.4-dev 2.4.3-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 | /* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2016 Gert Wollny
*
* MIA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Pub
lic License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MIA; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef mia_core_parameters_hh
#define mia_core_parameters_hh
#include <string>
#include <map>
#include <ostream>
#include <istream>
#include <sstream>
#include <memory>
#include <mia/core/flags.hh>
#include <mia/core/dictmap.hh>
#include <mia/core/msgstream.hh>
#include <mia/core/handlerbase.hh>
#include <mia/core/factory_trait.hh>
#include <mia/core/cmdoptionflags.hh>
NS_MIA_BEGIN
/**
\ingroup cmdline
\brief The base class for parameters used in complex options
The base class for parameters that might be set based on strings.
The main use-case is to provide parameters to plug-ins.
*/
class EXPORT_CORE CParameter {
public:
/**
Base parameter type constructor
\param type a type description string that will show up in the help
\param required set to true if the parameter has to be set by the user
\param descr a short description of the parameter
*/
CParameter(const char type[], bool required, const char *descr);
/**
ensure virtual destructor and avoid warnings because we have a virtual function
*/
virtual ~CParameter();
/**
\returns the type string of the parameter
q */
const char *type() const;
/**
\returns the description string of the parameter
*/
void descr(std::ostream& os) const;
/**
Get the curent parameter value as string
\returns the current parameter value
*/
std::string get_value_as_string() const;
/**
Write the current value plus information to a stream,
\param os output stream
*/
void value(std::ostream& os) const;
/**
\returns wheather the required flag is (still) set
*/
bool required_set() const;
/**
set the parameter based on the string value
*/
bool set(const std::string& str_value);
/// @returns the help description of the parameter
const char *get_descr() const;
/**
clear the required flag and reset to default value
*/
void reset();
/**
Interface to add the plug-in handler this parameter calls to translate the input string
Only for CFactoryParameter this actually does something
\param[in,out] handler_map the map to store then pointers to handlers used by this parameter
*/
void add_dependend_handler(HandlerHelpMap& handler_map) const;
/// \returns the default value of this parameter as a string
std::string get_default_value() const;
/**
Add the help for this parameter to a given XML tree
\param root the root node to add the help entry to.
*/
void get_help_xml(CXMLElement& root) const;
/**
This command is run after the parsing has been done
in most cases it will do nothing, only for factory parameters
and the like it must set the final parameter value from the init string
*/
virtual void post_set();
protected:
/** the actual (abstract) function to write the description to a stream
that needs to be overwritten
\param os
*/
virtual void do_descr(std::ostream& os) const = 0;
/// create an error message by using the given value that raises the error
const std::string errmsg(const std::string& err_value) const;
private:
/** the actual (abstract) function to set the parameter that needs to be overwritten
\param str_value the parameter value as string
*/
virtual void do_add_dependend_handler(HandlerHelpMap& handler_map) const;
virtual bool do_set(const std::string& str_value) = 0;
virtual void do_reset() = 0;
virtual std::string do_get_default_value() const = 0;
virtual std::string do_get_value_as_string() const = 0;
virtual void do_get_help_xml(CXMLElement& self) const;
bool m_required;
bool m_is_required;
const char *m_type;
const char *m_descr;
};
/**
\ingroup cmdline
\brief Generic type of a complex paramter
The (templated) typed parameter. There needs to be defined an
\a operator << (istream& is, T& x) for the \a do_set method to work.
*/
template <typename T>
class EXPORT_CORE CTParameter : public CParameter {
public:
/** Constructor
\param value reference to the parameter handled by this parameter object
\param required set to \a true if the parameter has to be set by the user
\param descr a description of the parameter
*/
CTParameter(T& value, bool required, const char *descr);
protected:
/**
the implementation of the description-function
*/
virtual void do_descr(std::ostream& os) const;
private:
virtual bool do_set(const std::string& str_value);
virtual void do_reset();
virtual void adjust(T& value);
virtual std::string do_get_default_value() const;
virtual std::string do_get_value_as_string() const;
T& m_value;
const T m_default_value;
};
/**
\ingroup cmdline
\brief Scalar parameter with an expected value range
A scalar parameter that supports a bracketing range. If the user tries to set the parameter
to a value outside the range, the set method will throw an \a invalid_argument exception
*/
/**
\ingroup cmdline
\brief Scalar parameter with a expected value boundaries
A scalar parameter that supports specifying boundaries. These boundaries can be one-sided
or on both sides, and the boundaries can be included in the range or not.
If the user tries to set the parameter to a value outside the range,
the set method will throw an \a invalid_argument exception
*/
enum class EParameterBounds : int {
bf_min = 1,
bf_min_open = 3,
bf_min_closed = 5,
bf_min_flags = 7,
bf_max = 0x10,
bf_max_open = 0x30,
bf_max_closed = 0x50,
bf_max_flags = 0x70,
bf_closed_interval = 0x55,
bf_open_interval = 0x33
};
IMPLEMENT_FLAG_OPERATIONS(EParameterBounds);
EXPORT_CORE std::ostream& operator << (std::ostream& os, EParameterBounds flags);
template <typename T>
class EXPORT_CORE TBoundedParameter : public CTParameter<T> {
public:
/** Constructor
\param value reference to the parameter handled by this parameter object
\param flags boundary flags
\param boundaries the boundaries of the parameter. Depending on the flags
it expects one or two values. If two values are given the first value is interpreted
as the lower boundary,
\param flags boundary flags
\param required set to \a true if the parameter has to be set by the user
\param descr a description of the parameter
*/
TBoundedParameter(T& value, EParameterBounds flags, const std::vector<T>& boundaries, bool required, const char *descr);
protected:
/**
the implementation of the description-function
*/
void do_descr(std::ostream& os) const;
private:
virtual void adjust(T& value);
virtual void do_get_help_xml(CXMLElement& self) const;
T m_min;
T m_max;
EParameterBounds m_flags;
};
template <typename T>
CParameter *make_param(T& value, bool required, const char *descr)
{
return new CTParameter<T>(value, required, descr);
}
template <typename T, typename S>
CParameter *make_lo_param(T& value, S lower_bound, bool required, const char *descr)
{
return new TBoundedParameter<T>(value, EParameterBounds::bf_min_open,
{static_cast<T>(lower_bound)}, required, descr);
}
template <typename T>
CParameter *make_positive_param(T& value, bool required, const char *descr)
{
return new TBoundedParameter<T>(value, EParameterBounds::bf_min_open, {T()}, required, descr);
}
template <typename T, typename S>
CParameter *make_lc_param(T& value, S lower_bound, bool required, const char *descr)
{
return new TBoundedParameter<T>(value, EParameterBounds::bf_min_closed,
{static_cast<T>(lower_bound)}, required, descr);
}
template <typename T>
CParameter *make_nonnegative_param(T& value, bool required, const char *descr)
{
return new TBoundedParameter<T>(value, EParameterBounds::bf_min_closed, {T()}, required, descr);
}
template <typename T, typename S>
CParameter *make_uo_param(T& value, S upper_bound, bool required, const char *descr)
{
return new TBoundedParameter<T>(value, EParameterBounds::bf_min_open,
{static_cast<T>(upper_bound)}, required, descr);
}
template <typename T, typename S>
CParameter *make_uc_param(T& value, S upper_bound, bool required, const char *descr)
{
return new TBoundedParameter<T>(value, EParameterBounds::bf_min_closed,
{static_cast<T>(upper_bound)}, required, descr);
}
template <typename T, typename S1, typename S2>
CParameter *make_ci_param(T& value, S1 lower_bound, S2 upper_bound, bool required, const char *descr)
{
return new TBoundedParameter<T>(value, EParameterBounds::bf_closed_interval,
{static_cast<T>(lower_bound), static_cast<T>(upper_bound)}, required, descr);
}
template <typename T, typename S1, typename S2>
CParameter *make_oi_param(T& value, S1 lower_bound, S2 upper_bound, bool required, const char *descr)
{
return new TBoundedParameter<T>(value, EParameterBounds::bf_open_interval,
{static_cast<T>(lower_bound), static_cast<T>(upper_bound)}, required, descr);
}
template <typename T, typename S1, typename S2>
CParameter *make_coi_param(T& value, S1 lower_bound, S2 upper_bound,bool required, const char *descr)
{
return new TBoundedParameter<T>(value, EParameterBounds::bf_min_closed | EParameterBounds::bf_max_open,
{static_cast<T>(lower_bound), static_cast<T>(upper_bound)}, required, descr);
}
template <typename T, typename S1, typename S2>
CParameter *make_oci_param(T& value, S1 lower_bound, S2 upper_bound,bool required, const char *descr)
{
return new TBoundedParameter<T>(value, EParameterBounds::bf_max_closed | EParameterBounds::bf_min_open,
{static_cast<T>(lower_bound), static_cast<T>(upper_bound)}, required, descr);
}
/**
\ingroup cmdline
\brief Dictionary parameter
The (templated) parameter that takes its value froma restricted Dictionary.
\tparam the enumerate that is used by the dictionary
*/
template <typename T>
class CDictParameter : public CParameter{
public:
/** Constructor
\param value reference to the parameter handled by this parameter object
\param dict dictionary for parameter translation
\param descr a description of the parameter
\param required set if this parameter must be set by the end user
*/
CDictParameter(T& value, const TDictMap<T>& dict, const char *descr, bool required = false);
protected:
/**
the implementation of the description-function
*/
virtual void do_descr(std::ostream& os) const;
private:
virtual bool do_set(const std::string& str_value);
virtual void do_reset();
virtual std::string do_get_default_value() const;
virtual std::string do_get_value_as_string() const;
virtual void do_get_help_xml(CXMLElement& self) const;
T& m_value;
T m_default_value;
const TDictMap<T> m_dict;
};
/**
\ingroup cmdline
\brief A parameter that get's initialized by a factory to a shared or unique pointer
This parameter type is used for parameters that are created by a factory
that uses a plug-in handler for instance creation.
\tparam F the plugin handler type used to create the parameter value
*/
template <typename F>
class TFactoryParameter : public CParameter{
public:
/** Constructor if the parameter tales a shared pointer
The constructor should take an empty F::ProductPtr and add the default value as initializer string init
in order to avoid calling the plug-in hanlder for creation before the true desired value of the parameter is known.
\param value reference to the shared pointer parameter handled by this parameter object
\param init an init string used as the default parameter to create the value
\param required set to true when the parameter is required
\param descr a description of the parameter
\remark Don't call this constructor directly and use one of the make_param variants. It automatically takes care
of the type instanciation and pointer variant handiong.
*/
TFactoryParameter(typename F::ProductPtr& value, const std::string& init, bool required, const char *descr);
/** Constructor if the parameter tales a unique pointer
The constructor should take an empty F::ProductPtr and add the default value as initializer string init
in order to avoid calling the plug-in hanlder for creation before the true desired value of the parameter is known.
\param value reference to the to the unique pointer parameter handled by this parameter object
\param init an init string used as the default parameter to create the value
\param required set to true when the parameter is required
\param descr a description of the parameter
\remark Don't call this constructor directly and use one of the make_param variants. It automatically takes care
of the type instanciation and pointer variant handiong.
*/
TFactoryParameter(typename F::UniqueProduct& value, const std::string& init, bool required, const char *descr);
private:
virtual void do_descr(std::ostream& os) const;
virtual void do_add_dependend_handler(HandlerHelpMap& handler_map)const;
virtual bool do_set(const std::string& str_value);
virtual void do_reset();
virtual std::string do_get_default_value() const;
virtual std::string do_get_value_as_string() const;
virtual void do_get_help_xml(CXMLElement& self) const;
typename F::ProductPtr dummy_shared_value;
typename F::UniqueProduct dummy_unique_value;
typename F::ProductPtr& m_shared_value;
typename F::UniqueProduct& m_unique_value;
virtual void post_set();
std::string m_string_value;
std::string m_default_value;
bool m_unique;
};
/**
\ingroup cmdline
\brief A parameter that can only assume values out of a limited set
This parameter type is used for parameters that that can only assume values out of a
limited set of values. Other than the CDictParameter this parameter may handle any type
T that can be streamed.
\tparam T the value type of the parameter
*/
template <typename T>
class CSetParameter : public CParameter{
public:
/** Constructor
\param value reference to the parameter handled by this parameter object
\param valid_set dictionary for parameter translation
\param descr a description of the parameter
\param required boolean to indicate whether the user must set the parameter
*/
CSetParameter(T& value, const std::set<T>& valid_set, const char *descr, bool required = false);
protected:
/**
the implementation of the description-function
*/
virtual void do_descr(std::ostream& os) const;
private:
virtual bool do_set(const std::string& str_value);
virtual void do_reset();
virtual std::string do_get_default_value() const;
virtual std::string do_get_value_as_string() const;
void do_get_help_xml(CXMLElement& self) const;
T& m_value;
T m_default_value;
const std::set<T> m_valid_set;
};
/**
\ingroup cmdline
\brief A parameter that can assume any value of the given value type
This parameter type is used for parameters that are only restricted by the value type
T that must be streamable.
\tparam T type of the parameter value
*/
template <typename T>
class TParameter : public CParameter{
public:
/** Constructor
\param value reference to the parameter handled by this parameter object
\param required set to \a true if the parameter has to be set by the user
\param descr a description of the parameter
*/
TParameter(T& value, bool required, const char *descr);
protected:
/**
the implementation of the description-function
*/
virtual void do_descr(std::ostream& os) const;
private:
virtual void do_reset();
virtual bool do_set(const std::string& str_value);
virtual std::string do_get_default_value() const;
virtual std::string do_get_value_as_string() const;
T& m_value;
T m_default_value;
};
/// an string parameter
class EXPORT_CORE CStringParameter: public CParameter {
public:
CStringParameter(std::string& value, CCmdOptionFlags flags, const char *descr,
const CPluginHandlerBase *plugin_hint = nullptr);
private:
virtual void do_reset();
virtual bool do_set(const std::string& str_value);
virtual std::string do_get_default_value() const;
virtual std::string do_get_value_as_string() const;
virtual void do_descr(std::ostream& os) const;
virtual void do_get_help_xml(CXMLElement& self) const;
virtual void do_add_dependend_handler(HandlerHelpMap& handler_map)const;
std::string& m_value;
std::string m_default_value;
CCmdOptionFlags m_flags;
const CPluginHandlerBase *m_plugin_hint;
};
/// boolean parameter
typedef CTParameter<bool> CBoolParameter;
/// an unsigned short parameter (with possible boundaries)
typedef TBoundedParameter<unsigned short> CUSBoundedParameter;
/// an unsigned int parameter (with possible boundaries)
typedef TBoundedParameter<unsigned int> CUIBoundedParameter;
/// an unsigned long parameter (with possible boundaries)
typedef TBoundedParameter<unsigned long> CULBoundedParameter;
/// an signed short parameter (with possible boundaries)
typedef TBoundedParameter<short> CSSBoundedParameter;
/// an signed int parameter (with possible boundaries)
typedef TBoundedParameter<int> CSIBoundedParameter;
/// an signed long parameter (with possible boundaries)
typedef TBoundedParameter<long> CSLBoundedParameter;
/// an float parameter, single accuracy (with possible boundaries)
typedef TBoundedParameter<float> CFBoundedParameter;
/// an float parameter, double accuracy (with possible boundaries)
typedef TBoundedParameter<double> CDBoundedParameter;
/**
\ingroup cmdline
\brief create a factory parameter that initializes to a std::shared_ptr
Creates a TFactoryParameter accurding to the given parameters. The advantage over calling the
TactoryParameter cunstructor is, that type deduction is done automatically.
\tparam the type of the factory created parameter value
\param value the shared_ptr value to be set through this parameter. Best is to pass an empty shared pointer and
leave the initialization to the parameter handling
\param init the default initialization string for the parameter, pass "" if there is no default.
\param required set to true if the user must set this parameter
\param descr a help description of the parameter
*/
template <typename T>
CParameter *make_param(std::shared_ptr<T>& value, const std::string& init, bool required, const char *descr)
{
typedef typename FactoryTrait<T>::type F;
return new TFactoryParameter<F>(value, init, required, descr);
}
/**
\ingroup cmdline
\brief create a factory parameter that initializes to a std::unique_ptr
Creates a TFactoryParameter accurding to the given parameters. The advantage over calling the
TactoryParameter cunstructor is that type deduction is done automatically.
\tparam the type of the factory created parameter value
\param value the unique_ptr value to be set through this parameter. Best is to pass an empty unique pointer and
leave the initialization to the parameter handling
\param init the default initialization string for the parameter, pass "" if there is no default.
\param required set to true if the user must set this parameter
\param descr a help description of the parameter
*/
template <typename T>
CParameter *make_param(std::unique_ptr<T>& value, const std::string& init, bool required, const char *descr)
{
typedef typename FactoryTrait<T>::type F;
return new TFactoryParameter<F>(value, init, required, descr);
}
//// implementations
/** @cond INTERNAL
*/
template <typename T>
struct __dispatch_param_translate {
static std::string apply(T x) {
std::ostringstream s;
s << x;
return s.str();
}
};
template <>
struct __dispatch_param_translate<std::string> {
static std::string apply(const std::string& x) {
return x;
}
};
template <>
struct __dispatch_param_translate<const char *> {
static std::string apply(const char * x) {
return std::string(x);
}
};
/// @endcond
template <typename T>
CDictParameter<T>::CDictParameter(T& value, const TDictMap<T>& dict, const char *descr, bool required):
CParameter("dict", required, descr),
m_value(value),
m_default_value(value),
m_dict(dict)
{
}
template <typename T>
void CDictParameter<T>::do_descr(std::ostream& os) const
{
for (auto i = m_dict.get_help_begin(); i != m_dict.get_help_end(); ++i) {
os << "\n " << i->second.first << ": " << i->second.second;
}
}
template <typename T>
void CDictParameter<T>::do_get_help_xml(CXMLElement& self) const
{
TRACE_FUNCTION;
auto dict = self.add_child("dict");
for (auto i = m_dict.get_help_begin(); i != m_dict.get_help_end(); ++i) {
auto v = dict->add_child("value");
v->set_attribute("name", i->second.first);
v->set_child_text(i->second.second);
}
}
template <typename T>
bool CDictParameter<T>::do_set(const std::string& str_value)
{
m_value = m_dict.get_value(str_value.c_str());
return true;
}
template <typename T>
void CDictParameter<T>::do_reset()
{
m_value = m_default_value;
}
template <typename T>
std::string CDictParameter<T>::do_get_default_value() const
{
return m_dict.get_name(m_default_value);
}
template <typename T>
std::string CDictParameter<T>::do_get_value_as_string() const
{
return m_dict.get_name(m_value);
}
template <typename F>
TFactoryParameter<F>::TFactoryParameter(typename F::ProductPtr& value,
const std::string& init, bool required, const char *descr):
CParameter("factory", required, descr),
m_shared_value(value),
m_unique_value(dummy_unique_value),
m_string_value(init),
m_default_value(init),
m_unique(false)
{
}
template <typename F>
TFactoryParameter<F>::TFactoryParameter(typename F::UniqueProduct& value, const std::string& init, bool required, const char *descr):
CParameter("factory", required, descr),
m_shared_value(dummy_shared_value),
m_unique_value(value),
m_string_value(init),
m_default_value(init),
m_unique(true)
{
}
template <typename T>
void TFactoryParameter<T>::do_descr(std::ostream& os) const
{
os << "For a list of available plug-ins see run 'mia-plugin-help "
<< T::instance().get_descriptor() << "'";
}
template <typename T>
void TFactoryParameter<T>::do_get_help_xml(CXMLElement& self) const
{
auto dict = self.add_child("factory");
dict->set_attribute("name", T::instance().get_descriptor());
}
template <typename T>
bool TFactoryParameter<T>::do_set(const std::string& str_value)
{
m_string_value = str_value;
return true;
}
template <typename T>
void TFactoryParameter<T>::post_set()
{
if (!m_string_value.empty()) {
if (m_unique)
m_unique_value = T::instance().produce_unique(m_string_value);
else
m_shared_value = T::instance().produce(m_string_value);
}
}
template <typename T>
void TFactoryParameter<T>::do_reset()
{
m_string_value = m_default_value;
}
template <typename T>
void TFactoryParameter<T>::do_add_dependend_handler(HandlerHelpMap& handler_map)const
{
// add recursively all dependent handlers
if (handler_map.find(T::instance().get_descriptor()) == handler_map.end()){
handler_map[T::instance().get_descriptor()] = &T::instance();
for (auto i = T::instance().begin(); i != T::instance().end(); ++i)
i->second->add_dependend_handlers(handler_map);
}
}
template <typename T>
std::string TFactoryParameter<T>::do_get_default_value() const
{
return m_default_value;
}
template <typename T>
std::string TFactoryParameter<T>::do_get_value_as_string() const
{
if (m_unique && m_unique_value)
return m_unique_value->get_init_string();
if (!m_unique && m_shared_value)
return m_shared_value->get_init_string();
return m_string_value;
}
template <typename T>
CSetParameter<T>::CSetParameter(T& value, const std::set<T>& valid_set, const char *descr, bool required):
CParameter("set", required, descr),
m_value(value),
m_default_value(value),
m_valid_set(valid_set)
{
if (m_valid_set.empty())
throw std::invalid_argument("CSetParameter initialized with empty set");
}
template <typename T>
std::string CSetParameter<T>::do_get_default_value() const
{
return __dispatch_param_translate<T>::apply(m_default_value);
}
template <typename T>
std::string CSetParameter<T>::do_get_value_as_string() const
{
return __dispatch_param_translate<T>::apply(m_value);
}
template <typename T>
void CSetParameter<T>::do_descr(std::ostream& os) const
{
auto i = m_valid_set.begin();
auto e = m_valid_set.end();
assert ( i != e );
os << " Supported values are (" << *i;
++i;
while (i != e)
os << '|' << *i++;
os << ')';
}
template <typename T>
void CSetParameter<T>::do_get_help_xml(CXMLElement& self) const
{
auto set = self.add_child("set");
for (auto i = m_valid_set.begin(); i != m_valid_set.end(); ++i) {
auto v = set->add_child("value");
v->set_attribute("name", __dispatch_param_translate<T>::apply(*i));
}
}
template <typename T>
void CSetParameter<T>::do_reset()
{
m_value = m_default_value;
}
template <typename T>
bool CSetParameter<T>::do_set(const std::string& str_value)
{
std::stringstream s(str_value);
T val;
s >> val;
if (s.fail() || m_valid_set.find(val) == m_valid_set.end()) {
throw std::invalid_argument(errmsg(str_value));
}
m_value = val;
return true;
}
template <typename T>
TParameter<T>::TParameter(T& value, bool required, const char *descr):
CParameter("streamable", required, descr),
m_value(value),
m_default_value(value)
{
}
template <typename T>
void TParameter<T>::do_descr(std::ostream& os) const
{
os << m_value;
}
template <typename T>
bool TParameter<T>::do_set(const std::string& str_value)
{
std::stringstream s(str_value);
s >> m_value;
if (s.fail())
throw std::invalid_argument(errmsg(str_value));
return true;
}
template <typename T>
void TParameter<T>::do_reset()
{
m_value = m_default_value;
}
template <typename T>
std::string TParameter<T>::do_get_default_value() const
{
std::ostringstream s;
s << m_default_value;
auto str = s.str();
if (str.find(',') != std::string::npos) {
std::ostringstream s2;
s2 << '[' << str << ']';
str = s2.str();
}
return str;
}
template <typename T>
std::string TParameter<T>::do_get_value_as_string() const
{
return __dispatch_param_translate<T>::apply(m_value);
}
NS_MIA_END
#endif
|