Go to the documentation of this file.
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>
80 virtual std::string do_as_string()
const = 0;
82 virtual bool do_is_equal(
const CAttribute& other)
const = 0;
84 virtual bool do_is_less(
const CAttribute& other)
const = 0;
117 template <
typename T>
127 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>
310 void set_attributes(CAttributeMap::const_iterator begin, CAttributeMap::const_iterator end);
326 template <
typename T>
327 void set_attribute(
const std::string& key,
const T& value);
348 template <
typename T>
359 template <
typename T>
427 virtual PAttribute do_from_string(
const std::string& value)
const = 0;
472 typedef std::map<std::string, std::shared_ptr<CAttrTranslator>> CMap;
488 template <
typename T>
491 cvdebug() <<
"add attribute " << key <<
" of type " <<
typeid(T).name() <<
" and value '" << value <<
"'\n";
499 cvdebug() <<
"add_attribute '" << key
500 <<
"' to '" << value <<
"' of type '"
501 << attributes[key]->typedescr() <<
"'\n";
516 template <
typename T>
528 virtual PAttribute do_from_string(
const std::string& value)
const;
534 template <
typename T>
540 template <
typename T>
546 template <
typename T>
552 template <
typename T>
555 return typeid(T).name();
558 template <
typename T>
562 "You must provide a type specialization for attribute_type<T>");
574 template <
typename T,
bool is_
floating>
575 struct __convert_to_string {
576 static std::string apply(
const typename ::boost::reference_wrapper<T>::type value)
578 return boost::lexical_cast<std::string>(value);
582 template <
typename T>
583 struct __convert_to_string<T, true> {
584 static std::string apply(
const typename ::boost::reference_wrapper<T>::type value)
586 std::stringstream sval;
587 sval << std::setprecision(10) << value;
593 template <
typename T>
594 struct dispatch_attr_string {
595 static std::string val2string(
const typename ::boost::reference_wrapper<T>::type value)
597 return __convert_to_string<T, std::is_floating_point<T>::value>::apply(value);
599 static T string2val(
const std::string& str)
602 std::istringstream svalue(str);
609 template <
typename T>
610 struct dispatch_attr_string<
std::vector<T>> {
611 static std::string val2string(
const std::vector<T>& value)
613 std::stringstream sval;
614 sval << value.size();
616 for (
size_t i = 0; i < value.size(); ++i)
618 << __convert_to_string<T, std::is_floating_point<T>::value>::apply(value[i]);
622 static std::vector<T> string2val(
const std::string& str)
625 std::istringstream svalue(str);
629 if (s > v.max_size())
630 throw create_exception<std::runtime_error>(
"string2val: try to create a vector of size ",
631 s,
" but support only size ", v.max_size());
635 for (
size_t i = 0; i < s; ++i)
639 std::stringstream msg;
640 msg <<
"string2val: unable to convert '" << str <<
"'";
641 throw std::invalid_argument(msg.str());
650 struct dispatch_attr_string<
std::vector<bool>> {
651 static std::string val2string(
const std::vector<bool>& value)
653 std::stringstream sval;
654 sval << value.size();
656 for (
size_t i = 0; i < value.size(); ++i)
657 sval <<
" " << value[i];
661 static std::vector<bool> string2val(
const std::string& str)
664 std::istringstream svalue(str);
666 std::vector<bool> v(s);
676 for (
size_t i = 0; i < s; ++i) {
683 std::stringstream msg;
684 msg <<
"string2val: unable to convert '" << str <<
"'";
685 throw std::invalid_argument(msg.str());
693 struct dispatch_attr_string<unsigned char> {
694 static std::string val2string(
unsigned char value)
696 std::stringstream sval;
697 sval << (
unsigned int)value;
700 static unsigned char string2val(
const std::string& str)
703 std::istringstream svalue(str);
705 return (
unsigned char)v;
710 struct dispatch_attr_string<signed char> {
711 static std::string val2string(
signed char value)
713 std::stringstream sval;
714 sval << (
signed int)value;
717 static signed char string2val(
const std::string& str)
720 std::istringstream svalue(str);
722 return (
signed char)v;
727 struct dispatch_attr_string<
std::string> {
728 static std::string val2string(std::string value)
732 static std::string string2val(
const std::string& str)
742 throw std::invalid_argument(
"Conversion of a CAttributeMap to a string not implemented");
746 throw std::invalid_argument(
"Conversion of a string to a CAttributeMap not implemented");
752 template <
typename T>
759 template <
typename T>
762 return dispatch_attr_string<T>::val2string(m_value);
765 template <
typename T>
771 cvdebug() <<
"TAttribute<T>::do_is_equal:Cast to "
777 return m_value == o->m_value;
780 template <
typename T>
786 return m_value < o->m_value;
788 return strcmp(typedescr(), other.
typedescr()) < 0;
791 template <
typename T>
799 throw create_exception<std::invalid_argument>(
"CAttributedData: no attribute '", key,
"' found");
802 template <
typename T>
808 return default_value;
810 auto attr =
dynamic_cast<const TAttribute<T> *
>(pattr.get());
813 cvwarn() <<
"Attribute '" << key <<
"'exists but is not of the expected type, returning default\n";
814 return default_value;
TTranslator< std::vector< uint8_t > > CVUBTranslator
TTranslator< std::vector< int64_t > > CVSLTranslator
std::ostream & operator<<(std::ostream &os, const CAttribute &attr)
static bool register_for(const std::string &key)
virtual int type_id() const =0
CAttributeMap::const_iterator begin_attributes() const
TTranslator< std::vector< uint32_t > > CVUITranslator
TTranslator< unsigned int > CUITranslator
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
T EXPORT_CORE get_attribute_as(const CAttribute &attr)
TTranslator< uint64_t > CULTranslator
TAttribute< CAttributeMap > CAttributeList
providing the possibility to nest attribute lists
bool do_register(const std::string &key)
TTranslator< std::vector< int8_t > > CVSBTranslator
virtual ~CAttrTranslator()
The virtual destructor just ensures virtual destruction and silences a warning.
void set_attributes(CAttributeMap::const_iterator begin, CAttributeMap::const_iterator end)
bool is_less(const CAttribute &other) const
TAttribute< std::string > CStringAttribute
a string attribute
CAttributedData(PAttributeMap attr)
A singelton class to translate strings to attributes based on keys.
TAttribute(typename ::boost::reference_wrapper< T >::type value)
static CStringAttrTranslatorMap & instance()
#define NS_MIA_END
conveniance define to end the mia namespace
TAttribute< std::vector< float > > CVFloatAttribute
TAttribute< double > CDoubleAttribute
a double attribute
NEVER typedef TTranslator< double > CDoubleTranslator
*/
TTranslator< int64_t > CSLTranslator
virtual const char * typedescr() const
TAttribute< int > CIntAttribute
an integer attribute
A class to translate an attribute from a string.
TAttribute< std::vector< int > > CVIntAttribute
a vector of integers attribute
TTranslator< std::vector< uint64_t > > CVULTranslator
TTranslator< int16_t > CSSTranslator
virtual ~CAttribute()
virtual destructor since derived classes may define dynamic objetcs
void set_attribute(const std::string &key, const std::string &value)
Class of an attribute that holds data of type T.
const std::string get_attribute_as_string(const std::string &key) const
returns the requested attribute as string, returns an empty string if attribute doesn't exist
A collection of attributes.
TAttribute< float > CFloatAttribute
a float attribute
TTranslator< std::vector< bool > > CVBitTranslator
TTranslator< std::vector< uint16_t > > CVUSTranslator
vstream & cvwarn()
send warnings to this stream adapter
TTranslator< bool > CBitTranslator
TTranslator< int8_t > CSBTranslator
TTranslator< std::vector< int32_t > > CVSITranslator
TTranslator< std::vector< double > > CVDoubleTranslator
virtual int type_id() const
void delete_attribute(const std::string &key)
bool operator==(const CAttribute &a, const CAttribute &b)
TTranslator< std::vector< int16_t > > CVSSTranslator
virtual const char * typedescr() const =0
const T get_attribute_as(const std::string &key) const
CAttributedData(const CAttributedData &org)
void EXPORT_CORE add_attribute(CAttributeMap &attributes, const std::string &key, T value)
bool has_attribute(const std::string &key) const
TTranslator< std::vector< float > > CVFloatTranslator
static const int attr_unknown
TTranslator< uint16_t > CUSTranslator
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
void set_attribute(const std::string &key, PAttribute attr)
TAttribute< std::vector< std::string > > CVStringAttribute
a vector of strings attribute
std::shared_ptr< CAttribute > PAttribute
define the shared pointer wrapped attribute pointer
bool is_equal(const CAttribute &other) const
std::string as_string() const
returns the value as a atring
const T & get_value() const
TTranslator< float > CFloatTranslator
PAttribute to_attr(const std::string &key, const std::string &value) const
PAttribute from_string(const std::string &value) const
void print(std::ostream &os) const
TTranslator< signed int > CSITranslator
Generic string vs. attribute translator singleton.
void set_attribute(const std::string &key, const char *value)
CAttributeMap::const_iterator end_attributes() const
The class of all attributes of data that is considered to ve meta-data.
const PAttribute get_attribute(const std::string &key) const
TTranslator< uint8_t > CUBTranslator
std::map< std::string, PAttribute > CAttributeMap
A name:attribute map.
std::shared_ptr< CAttributeMap > PAttributeMap
another pointer-usage easy maker
TAttribute< std::vector< double > > CVDoubleAttribute
a vector of doubles attribute