21 #ifndef mia_core_attributes_hh
22 #define mia_core_attributes_hh
34 #include <boost/any.hpp>
35 #include <boost/ref.hpp>
54 std::string as_string()
const;
71 virtual const char *typedescr()
const = 0;
73 virtual std::string do_as_string()
const = 0;
75 virtual bool do_is_equal(
const CAttribute& other)
const = 0;
77 virtual bool do_is_less(
const CAttribute& other)
const = 0;
120 template <
typename T>
129 TAttribute(typename ::boost::reference_wrapper<T>::type value);
143 const T& get_value()
const;
145 virtual std::string do_as_string()
const;
146 virtual bool do_is_equal(
const CAttribute& other)
const;
147 virtual bool do_is_less(
const CAttribute& other)
const;
161 template <
typename T>
276 const PAttribute get_attribute(
const std::string& key)
const;
281 CAttributeMap::const_iterator begin_attributes()
const;
286 CAttributeMap::const_iterator end_attributes()
const;
294 void set_attribute(
const std::string& key,
PAttribute attr);
302 void set_attributes(CAttributeMap::const_iterator begin, CAttributeMap::const_iterator end);
309 void set_attribute(
const std::string& key,
const std::string& value);
312 const std::string get_attribute_as_string(
const std::string& key)
const;
322 template <
typename T>
330 void delete_attribute(
const std::string& key);
337 bool has_attribute(
const std::string& key)
const;
377 virtual PAttribute do_from_string(
const std::string& value)
const = 0;
385 void do_register(
const std::string& key);
405 PAttribute to_attr(
const std::string& key,
const std::string& value)
const;
421 typedef std::map<std::string, const CAttrTranslator * const> CMap;
437 template <
typename T>
440 cvdebug() <<
"add attribute " << key <<
" of type " <<
typeid(T).name() <<
" and value '" << value <<
"'\n";
466 template <
typename T>
475 static void register_for(
const std::string& key);
477 virtual PAttribute do_from_string(
const std::string& value)
const;
483 template <
typename T>
489 template <
typename T>
495 template <
typename T>
501 template <
typename T>
504 return typeid(T).name();
514 template <
typename T>
515 struct dispatch_attr_string {
516 static std::string val2string(
const typename ::boost::reference_wrapper<T>::type value) {
517 std::stringstream sval;
521 static T string2val(
const std::string& str) {
523 std::istringstream svalue(str);
530 template <
typename T>
531 struct dispatch_attr_string<std::vector<T> > {
532 static std::string val2string(
const std::vector<T>& value) {
533 std::stringstream sval;
534 sval << value.size();
535 for (
size_t i = 0; i < value.size(); ++i)
536 sval <<
" " << value[i];
539 static std::vector<T> string2val(
const std::string& str) {
541 std::istringstream svalue(str);
544 for (
size_t i = 0; i < s; ++i)
547 std::stringstream msg;
548 msg <<
"string2val: unable to convert '" << str <<
"'";
549 throw std::invalid_argument(msg.str());
557 struct dispatch_attr_string<std::vector<bool> > {
558 static std::string val2string(
const std::vector<bool>& value) {
559 std::stringstream sval;
560 sval << value.size();
561 for (
size_t i = 0; i < value.size(); ++i)
562 sval <<
" " << value[i];
565 static std::vector<bool> string2val(
const std::string& str) {
567 std::istringstream svalue(str);
569 std::vector<bool> v(s);
570 for (
size_t i = 0; i < s; ++i) {
576 std::stringstream msg;
577 msg <<
"string2val: unable to convert '" << str <<
"'";
578 throw std::invalid_argument(msg.str());
585 struct dispatch_attr_string<unsigned char> {
586 static std::string val2string(
unsigned char value) {
587 std::stringstream sval;
588 sval << (
unsigned int)value;
591 static unsigned char string2val(
const std::string& str) {
593 std::istringstream svalue(str);
595 return (
unsigned char)v;
600 struct dispatch_attr_string<signed char> {
601 static std::string val2string(
signed char value) {
602 std::stringstream sval;
603 sval << (
signed int)value;
606 static signed char string2val(
const std::string& str) {
608 std::istringstream svalue(str);
610 return (
signed char)v;
615 struct dispatch_attr_string<std::string> {
616 static std::string val2string(std::string value) {
619 static std::string string2val(
const std::string& str) {
627 throw std::invalid_argument(
"Conversion of a CAttributeMap to a string not implemented");
629 return std::string(
"");
632 throw std::invalid_argument(
"Conversion of a string to a CAttributeMap not implemented");
640 template <
typename T>
643 return dispatch_attr_string<T>::val2string(m_value);
646 template <
typename T>
651 cvdebug() <<
"TAttribute<T>::do_is_equal:Cast to "
656 return m_value == o->m_value;
659 template <
typename T>
664 return m_value < o->m_value;
666 return strcmp(typedescr(), other.
typedescr()) < 0;
670 template <
typename T>
671 TVAttribute<T>::TVAttribute(
const std::vector<T>& value):
676 template <
typename T>
677 bool TVAttribute<T>::do_is_equal(
const CAttribute& other)
const
679 const TVAttribute<T>* o =
dynamic_cast<const TVAttribute<T> *
>(&other);
682 return o->
get_value().size() == this->get_value().size() &&
683 std::equal(this->get_value().begin(), this->get_value().end(), o->get_value().begin());
687 template <
typename T>
694 template <
typename T>
700 template <
typename T>
707 throw create_exception<std::invalid_argument>(
"CAttributedData: no attribute '", key,
"' found");