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;
74 virtual int type_id()
const = 0;
76 virtual std::string do_as_string()
const = 0;
78 virtual bool do_is_equal(
const CAttribute& other)
const = 0;
80 virtual bool do_is_less(
const CAttribute& other)
const = 0;
112 template <
typename T>
121 TAttribute(typename ::boost::reference_wrapper<T>::type value);
137 const T& get_value()
const;
139 virtual std::string do_as_string()
const;
140 virtual bool do_is_equal(
const CAttribute& other)
const;
141 virtual bool do_is_less(
const CAttribute& other)
const;
155 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);
318 template <
typename T>
319 void set_attribute(
const std::string& key,
const T& value);
326 void set_attribute(
const std::string& key,
const char* value);
330 const std::string get_attribute_as_string(
const std::string& key)
const;
340 template <
typename T>
351 template <
typename T>
359 void delete_attribute(
const std::string& key);
366 bool has_attribute(
const std::string& key)
const;
372 void print(std::ostream& os)
const {
417 virtual PAttribute do_from_string(
const std::string& value)
const = 0;
425 bool do_register(
const std::string& key);
445 PAttribute to_attr(
const std::string& key,
const std::string& value)
const;
461 typedef std::map<std::string, std::shared_ptr<CAttrTranslator>> CMap;
477 template <
typename T>
480 cvdebug() <<
"add attribute " << key <<
" of type " <<
typeid(T).name() <<
" and value '" << value <<
"'\n";
488 cvdebug() <<
"add_attribute '" << key
489 <<
"' to '" << value <<
"' of type '"
490 << attributes[key]->typedescr() <<
"'\n";
505 template <
typename T>
514 static bool register_for(
const std::string& key);
516 virtual PAttribute do_from_string(
const std::string& value)
const;
522 template <
typename T>
528 template <
typename T>
534 template <
typename T>
540 template <
typename T>
543 return typeid(T).name();
546 template <
typename T>
550 "You must provide a type specialization for attribute_type<T>");
563 template <
typename T>
564 struct dispatch_attr_string {
565 static std::string val2string(
const typename ::boost::reference_wrapper<T>::type value) {
566 std::stringstream sval;
570 static T string2val(
const std::string& str) {
572 std::istringstream svalue(str);
579 template <
typename T>
580 struct dispatch_attr_string<
std::vector<T> > {
581 static std::string val2string(
const std::vector<T>& value) {
582 std::stringstream sval;
583 sval << value.size();
584 for (
size_t i = 0; i < value.size(); ++i)
585 sval <<
" " << value[i];
588 static std::vector<T> string2val(
const std::string& str) {
590 std::istringstream svalue(str);
593 if (s > v.max_size())
594 throw create_exception<std::runtime_error>(
"string2val: try to create a vector of size ",
595 s,
" but support only size ", v.max_size());
597 for (
size_t i = 0; i < s; ++i)
600 std::stringstream msg;
601 msg <<
"string2val: unable to convert '" << str <<
"'";
602 throw std::invalid_argument(msg.str());
610 struct dispatch_attr_string<
std::vector<bool> > {
611 static std::string val2string(
const std::vector<bool>& value) {
612 std::stringstream sval;
613 sval << value.size();
614 for (
size_t i = 0; i < value.size(); ++i)
615 sval <<
" " << value[i];
618 static std::vector<bool> string2val(
const std::string& str) {
620 std::istringstream svalue(str);
622 std::vector<bool> v(s);
623 for (
size_t i = 0; i < s; ++i) {
629 std::stringstream msg;
630 msg <<
"string2val: unable to convert '" << str <<
"'";
631 throw std::invalid_argument(msg.str());
638 struct dispatch_attr_string<unsigned char> {
639 static std::string val2string(
unsigned char value) {
640 std::stringstream sval;
641 sval << (
unsigned int)value;
644 static unsigned char string2val(
const std::string& str) {
646 std::istringstream svalue(str);
648 return (
unsigned char)v;
653 struct dispatch_attr_string<signed char> {
654 static std::string val2string(
signed char value) {
655 std::stringstream sval;
656 sval << (
signed int)value;
659 static signed char string2val(
const std::string& str) {
661 std::istringstream svalue(str);
663 return (
signed char)v;
668 struct dispatch_attr_string<
std::string> {
669 static std::string val2string(std::string value) {
672 static std::string string2val(
const std::string& str) {
680 throw std::invalid_argument(
"Conversion of a CAttributeMap to a string not implemented");
683 throw std::invalid_argument(
"Conversion of a string to a CAttributeMap not implemented");
689 template <
typename T>
696 template <
typename T>
699 return dispatch_attr_string<T>::val2string(m_value);
702 template <
typename T>
707 cvdebug() <<
"TAttribute<T>::do_is_equal:Cast to "
712 return m_value == o->m_value;
715 template <
typename T>
720 return m_value < o->m_value;
722 return strcmp(typedescr(), other.
typedescr()) < 0;
725 template <
typename T>
736 template <
typename T>
742 template <
typename T>
749 throw create_exception<std::invalid_argument>(
"CAttributedData: no attribute '", key,
"' found");
752 template <
typename T>
757 return default_value;
758 auto attr =
dynamic_cast<const TAttribute<T> *
>(pattr.get());
760 cvwarn() <<
"Attribute '" << key <<
"'exists but is not of the expected type, returning default\n";
761 return default_value;
TAttribute< std::vector< double > > CVDoubleAttribute
a vector of doubles attribute
virtual const char * typedescr() const
T EXPORT_CORE get_attribute_as(const CAttribute &attr)
TTranslator< std::vector< float > > CVFloatTranslator
TTranslator< std::vector< unsigned long > > CVULTranslator
TTranslator< unsigned long > CULTranslator
Generic string vs. attribute translator singleton.
bool operator==(const CAttribute &a, const CAttribute &b)
TTranslator< std::vector< signed int > > CVSITranslator
static CStringAttrTranslatorMap & instance()
TAttribute< std::string > CStringAttribute
a string attribute
TTranslator< unsigned int > CUITranslator
virtual const char * typedescr() const =0
static const int attr_unknown
virtual int type_id() const
TAttribute< CAttributeMap > CAttributeList
providing the possibility to nest attribute lists
TTranslator< std::vector< signed long > > CVSLTranslator
TTranslator< std::vector< unsigned char > > CVUBTranslator
TAttribute< int > CIntAttribute
an integer attribute
The class of all attributes of data that is considered to ve meta-data.
static bool register_for(const std::string &key)
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
TAttribute< double > CDoubleAttribute
a double attribute
A collection of attributes.
std::string as_string() const
returns the value as a atring
TAttribute< std::vector< int > > CVIntAttribute
a vector of integers attribute
TTranslator< signed int > CSITranslator
virtual ~CAttrTranslator()
The virtual destructor just ensures virtual destruction and silences a warning.
std::shared_ptr< CAttribute > PAttribute
define the shared pointer wrapped attribute pointer
TAttribute< std::vector< float > > CVFloatAttribute
TTranslator< std::vector< bool > > CVBitTranslator
PAttribute to_attr(const std::string &key, const std::string &value) const
TTranslator< signed short > CSSTranslator
void set_attribute(const std::string &key, PAttribute attr)
TAttribute< float > CFloatAttribute
a float attribute
A class to translate an attribute from a string.
const T get_attribute_as(const std::string &key) const
TAttribute< std::vector< std::string > > CVStringAttribute
a vector of strings attribute
TTranslator< double > CDoubleTranslator
A singelton class to translate strings to attributes based on keys.
TTranslator< bool > CBitTranslator
TTranslator< std::vector< signed char > > CVSBTranslator
const T & get_value() const
TTranslator< std::vector< unsigned short > > CVUSTranslator
TTranslator< unsigned char > CUBTranslator
vstream & cvwarn()
send warnings to this stream adapter
bool is_equal(const CAttribute &other) const
void print(std::ostream &os) const
void EXPORT_CORE add_attribute(CAttributeMap &attributes, const std::string &key, T value)
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
virtual int type_id() const =0
TTranslator< signed long > CSLTranslator
std::map< std::string, PAttribute > CAttributeMap
A name:attribute map.
const PAttribute get_attribute(const std::string &key) const
TTranslator< std::vector< unsigned int > > CVUITranslator
TTranslator< float > CFloatTranslator
TTranslator< std::vector< double > > CVDoubleTranslator
bool do_register(const std::string &key)
TTranslator< signed char > CSBTranslator
TTranslator< unsigned short > CUSTranslator
std::ostream & operator<<(std::ostream &os, const CAttribute &attr)
std::shared_ptr< CAttributeMap > PAttributeMap
another pointer-usage easy maker
TAttribute(typename::boost::reference_wrapper< T >::type value)
TTranslator< std::vector< signed short > > CVSSTranslator
Class of an attribute that holds data of type T.
#define NS_MIA_END
conveniance define to end the mia namespace
bool from_string(const char *s, T &result)