21 #ifndef mia_core_attributes_hh 22 #define mia_core_attributes_hh 34 #include <type_traits> 36 #include <boost/any.hpp> 37 #include <boost/ref.hpp> 38 #include <boost/lexical_cast.hpp> 57 std::string as_string()
const;
74 virtual const char *typedescr()
const = 0;
77 virtual int type_id()
const = 0;
79 virtual std::string do_as_string()
const = 0;
81 virtual bool do_is_equal(
const CAttribute& other)
const = 0;
83 virtual bool do_is_less(
const CAttribute& other)
const = 0;
115 template <
typename T>
124 TAttribute(typename ::boost::reference_wrapper<T>::type value);
140 const T& get_value()
const;
142 virtual std::string do_as_string()
const;
143 virtual bool do_is_equal(
const CAttribute& other)
const;
144 virtual bool do_is_less(
const CAttribute& other)
const;
158 template <
typename T>
221 static const int value = 1000;
279 const PAttribute get_attribute(
const std::string& key)
const;
284 CAttributeMap::const_iterator begin_attributes()
const;
289 CAttributeMap::const_iterator end_attributes()
const;
297 void set_attribute(
const std::string& key,
PAttribute attr);
305 void set_attributes(CAttributeMap::const_iterator begin, CAttributeMap::const_iterator end);
312 void set_attribute(
const std::string& key,
const std::string& value);
321 template <
typename T>
322 void set_attribute(
const std::string& key,
const T& value);
329 void set_attribute(
const std::string& key,
const char* value);
333 const std::string get_attribute_as_string(
const std::string& key)
const;
343 template <
typename T>
354 template <
typename T>
362 void delete_attribute(
const std::string& key);
369 bool has_attribute(
const std::string& key)
const;
375 void print(std::ostream& os)
const {
420 virtual PAttribute do_from_string(
const std::string& value)
const = 0;
428 bool do_register(
const std::string& key);
448 PAttribute to_attr(
const std::string& key,
const std::string& value)
const;
464 typedef std::map<std::string, std::shared_ptr<CAttrTranslator>> CMap;
480 template <
typename T>
483 cvdebug() <<
"add attribute " << key <<
" of type " <<
typeid(T).name() <<
" and value '" << value <<
"'\n";
491 cvdebug() <<
"add_attribute '" << key
492 <<
"' to '" << value <<
"' of type '" 493 << attributes[key]->typedescr() <<
"'\n";
508 template <
typename T>
517 static bool register_for(
const std::string& key);
519 virtual PAttribute do_from_string(
const std::string& value)
const;
525 template <
typename T>
531 template <
typename T>
537 template <
typename T>
543 template <
typename T>
546 return typeid(T).name();
549 template <
typename T>
553 "You must provide a type specialization for attribute_type<T>");
566 template <
typename T,
bool is_
floating>
567 struct __convert_to_string
569 static std::string apply(
const typename ::boost::reference_wrapper<T>::type value) {
570 return boost::lexical_cast<std::string>(value);
574 template <
typename T>
575 struct __convert_to_string<T, true> {
576 static std::string apply(
const typename ::boost::reference_wrapper<T>::type value) {
577 std::stringstream sval;
578 sval << std::setprecision(10) << value;
584 template <
typename T>
585 struct dispatch_attr_string {
586 static std::string val2string(
const typename ::boost::reference_wrapper<T>::type value) {
587 return __convert_to_string<T, std::is_floating_point<T>::value>::apply(value);
589 static T string2val(
const std::string& str) {
591 std::istringstream svalue(str);
598 template <
typename T>
599 struct dispatch_attr_string<std::vector<T> > {
600 static std::string val2string(
const std::vector<T>& value) {
601 std::stringstream sval;
602 sval << value.size();
603 for (
size_t i = 0; i < value.size(); ++i)
605 << __convert_to_string<T, std::is_floating_point<T>::value>::apply(value[i]);
608 static std::vector<T> string2val(
const std::string& str) {
610 std::istringstream svalue(str);
613 if (s > v.max_size())
614 throw create_exception<std::runtime_error>(
"string2val: try to create a vector of size ",
615 s,
" but support only size ", v.max_size());
617 for (
size_t i = 0; i < s; ++i)
620 std::stringstream msg;
621 msg <<
"string2val: unable to convert '" << str <<
"'";
622 throw std::invalid_argument(msg.str());
630 struct dispatch_attr_string<std::vector<bool> > {
631 static std::string val2string(
const std::vector<bool>& value) {
632 std::stringstream sval;
633 sval << value.size();
634 for (
size_t i = 0; i < value.size(); ++i)
635 sval <<
" " << value[i];
638 static std::vector<bool> string2val(
const std::string& str) {
640 std::istringstream svalue(str);
642 std::vector<bool> v(s);
652 for (
size_t i = 0; i < s; ++i) {
658 std::stringstream msg;
659 msg <<
"string2val: unable to convert '" << str <<
"'";
660 throw std::invalid_argument(msg.str());
667 struct dispatch_attr_string<unsigned char> {
668 static std::string val2string(
unsigned char value) {
669 std::stringstream sval;
670 sval << (
unsigned int)value;
673 static unsigned char string2val(
const std::string& str) {
675 std::istringstream svalue(str);
677 return (
unsigned char)v;
682 struct dispatch_attr_string<signed char> {
683 static std::string val2string(
signed char value) {
684 std::stringstream sval;
685 sval << (
signed int)value;
688 static signed char string2val(
const std::string& str) {
690 std::istringstream svalue(str);
692 return (
signed char)v;
697 struct dispatch_attr_string<std::string> {
698 static std::string val2string(std::string value) {
701 static std::string string2val(
const std::string& str) {
707 struct dispatch_attr_string<CAttributeMap> {
708 static std::string val2string(
const CAttributeMap& ) {
709 throw std::invalid_argument(
"Conversion of a CAttributeMap to a string not implemented");
711 static CAttributeMap string2val(
const std::string& ) {
712 throw std::invalid_argument(
"Conversion of a string to a CAttributeMap not implemented");
718 template <
typename T>
725 template <
typename T>
728 return dispatch_attr_string<T>::val2string(m_value);
731 template <
typename T>
736 cvdebug() <<
"TAttribute<T>::do_is_equal:Cast to " 741 return m_value == o->m_value;
744 template <
typename T>
749 return m_value < o->m_value;
754 template <
typename T>
761 throw create_exception<std::invalid_argument>(
"CAttributedData: no attribute '", key,
"' found");
764 template <
typename T>
769 return default_value;
770 auto attr =
dynamic_cast<const TAttribute<T> *
>(pattr.get());
772 cvwarn() <<
"Attribute '" << key <<
"'exists but is not of the expected type, returning default\n";
773 return default_value;
TAttribute< std::vector< double > > CVDoubleAttribute
a vector of doubles attribute
virtual int type_id() const
T EXPORT_CORE get_attribute_as(const CAttribute &attr)
void print(std::ostream &os) const
TTranslator< std::vector< float > > CVFloatTranslator
TTranslator< std::vector< int64_t > > CVSLTranslator
TTranslator< uint8_t > CUBTranslator
Generic string vs. attribute translator singleton.
bool operator==(const CAttribute &a, const CAttribute &b)
static CStringAttrTranslatorMap & instance()
TAttribute< std::string > CStringAttribute
a string attribute
TTranslator< unsigned int > CUITranslator
const T & get_value() const
TTranslator< int64_t > CSLTranslator
virtual const char * typedescr() const =0
static const int attr_unknown
TAttribute< CAttributeMap > CAttributeList
providing the possibility to nest attribute lists
TAttribute< int > CIntAttribute
an integer attribute
The class of all attributes of data that is considered to ve meta-data.
bool is_equal(const CAttribute &other) const
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
TAttribute< double > CDoubleAttribute
a double attribute
A collection of attributes.
TTranslator< std::vector< int8_t > > CVSBTranslator
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< uint8_t > > CVUBTranslator
const T get_attribute_as(const std::string &key) const
TTranslator< std::vector< bool > > CVBitTranslator
void set_attribute(const std::string &key, PAttribute attr)
TTranslator< std::vector< int32_t > > CVSITranslator
TAttribute< float > CFloatAttribute
a float attribute
A class to translate an attribute from a string.
TTranslator< int8_t > CSBTranslator
TTranslator< std::vector< int16_t > > CVSSTranslator
NEVER typedef TTranslator< double > CDoubleTranslator
*/
TTranslator< uint64_t > CULTranslator
TAttribute< std::vector< std::string > > CVStringAttribute
a vector of strings attribute
std::string as_string() const
returns the value as a atring
A singelton class to translate strings to attributes based on keys.
TTranslator< bool > CBitTranslator
PAttribute to_attr(const std::string &key, const std::string &value) const
TAttribute(typename ::boost::reference_wrapper< T >::type value)
TTranslator< uint16_t > CUSTranslator
vstream & cvwarn()
send warnings to this stream adapter
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
std::map< std::string, PAttribute > CAttributeMap
A name:attribute map.
TTranslator< int16_t > CSSTranslator
TTranslator< std::vector< uint64_t > > CVULTranslator
TTranslator< float > CFloatTranslator
TTranslator< std::vector< uint32_t > > CVUITranslator
TTranslator< std::vector< double > > CVDoubleTranslator
std::ostream & operator<<(std::ostream &os, const CAttribute &attr)
virtual const char * typedescr() const
std::shared_ptr< CAttributeMap > PAttributeMap
another pointer-usage easy maker
Class of an attribute that holds data of type T.
#define NS_MIA_END
conveniance define to end the mia namespace
TTranslator< std::vector< uint16_t > > CVUSTranslator
bool from_string(const char *s, T &result)