21 #ifndef mia_core_attributes_hh 22 #define mia_core_attributes_hh 34 #include <boost/any.hpp> 35 #include <boost/ref.hpp> 36 #include <boost/lexical_cast.hpp> 55 std::string as_string()
const;
72 virtual const char *typedescr()
const = 0;
75 virtual int type_id()
const = 0;
77 virtual std::string do_as_string()
const = 0;
79 virtual bool do_is_equal(
const CAttribute& other)
const = 0;
81 virtual bool do_is_less(
const CAttribute& other)
const = 0;
113 template <
typename T>
122 TAttribute(typename ::boost::reference_wrapper<T>::type value);
138 const T& get_value()
const;
140 virtual std::string do_as_string()
const;
141 virtual bool do_is_equal(
const CAttribute& other)
const;
142 virtual bool do_is_less(
const CAttribute& other)
const;
156 template <
typename T>
219 static const int value = 1000;
277 const PAttribute get_attribute(
const std::string& key)
const;
282 CAttributeMap::const_iterator begin_attributes()
const;
287 CAttributeMap::const_iterator end_attributes()
const;
295 void set_attribute(
const std::string& key,
PAttribute attr);
303 void set_attributes(CAttributeMap::const_iterator begin, CAttributeMap::const_iterator end);
310 void set_attribute(
const std::string& key,
const std::string& value);
319 template <
typename T>
320 void set_attribute(
const std::string& key,
const T& value);
327 void set_attribute(
const std::string& key,
const char* value);
331 const std::string get_attribute_as_string(
const std::string& key)
const;
341 template <
typename T>
352 template <
typename T>
360 void delete_attribute(
const std::string& key);
367 bool has_attribute(
const std::string& key)
const;
373 void print(std::ostream& os)
const {
418 virtual PAttribute do_from_string(
const std::string& value)
const = 0;
426 bool do_register(
const std::string& key);
446 PAttribute to_attr(
const std::string& key,
const std::string& value)
const;
462 typedef std::map<std::string, std::shared_ptr<CAttrTranslator>> CMap;
478 template <
typename T>
481 cvdebug() <<
"add attribute " << key <<
" of type " <<
typeid(T).name() <<
" and value '" << value <<
"'\n";
489 cvdebug() <<
"add_attribute '" << key
490 <<
"' to '" << value <<
"' of type '" 491 << attributes[key]->typedescr() <<
"'\n";
506 template <
typename T>
515 static bool register_for(
const std::string& key);
517 virtual PAttribute do_from_string(
const std::string& value)
const;
523 template <
typename T>
529 template <
typename T>
535 template <
typename T>
541 template <
typename T>
544 return typeid(T).name();
547 template <
typename T>
551 "You must provide a type specialization for attribute_type<T>");
565 template <
typename T>
566 struct dispatch_attr_string {
567 static std::string val2string(
const typename ::boost::reference_wrapper<T>::type value) {
568 std::stringstream sval;
569 sval << boost::lexical_cast<std::string>(value);
572 static T string2val(
const std::string& str) {
574 std::istringstream svalue(str);
581 template <
typename T>
582 struct dispatch_attr_string<std::vector<T> > {
583 static std::string val2string(
const std::vector<T>& value) {
584 std::stringstream sval;
585 sval << value.size();
586 for (
size_t i = 0; i < value.size(); ++i)
587 sval <<
" " << boost::lexical_cast<std::string>(value[i]);
590 static std::vector<T> string2val(
const std::string& str) {
592 std::istringstream svalue(str);
595 if (s > v.max_size())
596 throw create_exception<std::runtime_error>(
"string2val: try to create a vector of size ",
597 s,
" but support only size ", v.max_size());
599 for (
size_t i = 0; i < s; ++i)
602 std::stringstream msg;
603 msg <<
"string2val: unable to convert '" << str <<
"'";
604 throw std::invalid_argument(msg.str());
612 struct dispatch_attr_string<std::vector<bool> > {
613 static std::string val2string(
const std::vector<bool>& value) {
614 std::stringstream sval;
615 sval << value.size();
616 for (
size_t i = 0; i < value.size(); ++i)
617 sval <<
" " << value[i];
620 static std::vector<bool> string2val(
const std::string& str) {
622 std::istringstream svalue(str);
624 std::vector<bool> v(s);
625 for (
size_t i = 0; i < s; ++i) {
631 std::stringstream msg;
632 msg <<
"string2val: unable to convert '" << str <<
"'";
633 throw std::invalid_argument(msg.str());
640 struct dispatch_attr_string<unsigned char> {
641 static std::string val2string(
unsigned char value) {
642 std::stringstream sval;
643 sval << (
unsigned int)value;
646 static unsigned char string2val(
const std::string& str) {
648 std::istringstream svalue(str);
650 return (
unsigned char)v;
655 struct dispatch_attr_string<signed char> {
656 static std::string val2string(
signed char value) {
657 std::stringstream sval;
658 sval << (
signed int)value;
661 static signed char string2val(
const std::string& str) {
663 std::istringstream svalue(str);
665 return (
signed char)v;
670 struct dispatch_attr_string<std::string> {
671 static std::string val2string(std::string value) {
674 static std::string string2val(
const std::string& str) {
680 struct dispatch_attr_string<CAttributeMap> {
681 static std::string val2string(
const CAttributeMap& ) {
682 throw std::invalid_argument(
"Conversion of a CAttributeMap to a string not implemented");
684 static CAttributeMap string2val(
const std::string& ) {
685 throw std::invalid_argument(
"Conversion of a string to a CAttributeMap not implemented");
691 template <
typename T>
698 template <
typename T>
701 return dispatch_attr_string<T>::val2string(m_value);
704 template <
typename T>
709 cvdebug() <<
"TAttribute<T>::do_is_equal:Cast to " 714 return m_value == o->m_value;
717 template <
typename T>
722 return m_value < o->m_value;
727 template <
typename T>
734 throw create_exception<std::invalid_argument>(
"CAttributedData: no attribute '", key,
"' found");
737 template <
typename T>
742 return default_value;
743 auto attr =
dynamic_cast<const TAttribute<T> *
>(pattr.get());
745 cvwarn() <<
"Attribute '" << key <<
"'exists but is not of the expected type, returning default\n";
746 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.
#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.
TTranslator< std::vector< unsigned int > > CVUITranslator
TTranslator< float > CFloatTranslator
TTranslator< std::vector< double > > CVDoubleTranslator
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)