/usr/include/mia-2.0/mia/core/attributes.hh is in libmia-2.0-dev 2.0.13-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 | /* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2013 Gert Wollny
*
* MIA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MIA; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef mia_core_attributes_hh
#define mia_core_attributes_hh
#include <mia/core/msgstream.hh>
#include <mia/core/errormacro.hh>
#include <map>
#include <memory>
#include <string>
#include <cstring>
#include <vector>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <boost/any.hpp>
#include <boost/ref.hpp>
#include <mia/core/defines.hh>
NS_MIA_BEGIN
/**
\ingroup basic
\brief The class of all attributes of data that is considered to ve meta-data.
CAttribute is the base class used for generric attributes of images and similar
entities. This class is abstract and most likely you want to use one of the
provided specializations of the class TAttribute or create your own specializations.
*/
class EXPORT_CORE CAttribute {
public:
/// virtual destructor since derived classes may define dynamic objetcs
virtual ~CAttribute();
/// returns the value as a atring
std::string as_string() const;
/** compare this attribute to another one
\param other other attribute to compare to
\returns true if the attributes are of the same type and hold the same value, false otherwiese
*/
bool is_equal(const CAttribute& other) const;
/** compare this attribute to another one for a strict ordering
\param other other attribute to compare to
\returns a result dependend on the actual implementation of the private virtual method
\a do_is_less
*/
bool is_less(const CAttribute& other) const;
/// \returns a descriptive name of the type
virtual const char *typedescr() const = 0;
private:
virtual std::string do_as_string() const = 0;
virtual bool do_is_equal(const CAttribute& other) const = 0;
virtual bool do_is_less(const CAttribute& other) const = 0;
};
inline
std::ostream& operator << (std::ostream& os, const CAttribute& attr) {
os << attr.as_string();
return os;
};
inline bool operator == (const CAttribute& a, const CAttribute& b)
{
return a.is_equal(b);
}
inline bool operator < (const CAttribute& a, const CAttribute& b)
{
return a.is_less(b);
}
/// define the shared pointer wrapped attribute pointer
typedef std::shared_ptr<CAttribute > PAttribute;
struct pattr_less {
bool operator () (PAttribute const& a, PAttribute const& b)
{
return *a < *b;
}
};
/**
\ingroup basic
\brief Class of an attribute that holds data of type \a T
This class is the templated derivative of CAttribute that provides the container
for most attributes you will come across. The value is stored read-only.
It implements implements the abstract methods typedescr, do_as_string, do_is_equal, do_is_less
For this type, is_equal returns true if the \a other attribute has the same type and holds the
same value.
is_less returns true of either the type is equal and the value is less, or with different types
if a string-compare strcmp between the type descriptions returns -1.
*/
template <typename T>
class EXPORT_CORE TAttribute : public CAttribute {
public:
//! \name Constructors
//@{
/**
Construct the attribute by setting its value to
\param value
*/
TAttribute(typename ::boost::reference_wrapper<T>::type value);
//@}
/**
provide a transparent conversion to the content type
\remark should we really use this?
*/
operator T()const;
/// \returns typeid(T).name(), and is, therefore, dependend on the compiler
virtual const char *typedescr() const;
protected:
/// @returns the value of the attribute
const T& get_value() const;
private:
virtual std::string do_as_string() const;
virtual bool do_is_equal(const CAttribute& other) const;
virtual bool do_is_less(const CAttribute& other) const;
T m_value;
};
/**
\ingroup basic
Helper function to get the value of an attribute. Thr function throws a bad_cast exception,
if the attribute doesn't hold a value ofthe requested type T
\tparam T target type
\param attr attribute to be read
*/
template <typename T>
T EXPORT_CORE get_attribute_as(const CAttribute& attr) {
const TAttribute<T>& a = dynamic_cast<const TAttribute<T>&>(attr);
return a;
}
/**
\ingroup basic
\brief an integer attribute
*/
typedef TAttribute<int> CIntAttribute;
/**
\ingroup basic
\brief a vector of integers attribute
*/
typedef TAttribute<std::vector<int> > CVIntAttribute;
/**
\ingroup basic
\brief a float attribute
*/
typedef TAttribute<float> CFloatAttribute;
/**
\ingroup basic
brief a vector of floats attribute
*/
typedef TAttribute<std::vector<float> > CVFloatAttribute;
/**
\ingroup basic
\brief a double attribute
*/
typedef TAttribute<double> CDoubleAttribute;
/**
\ingroup basic
\brief a vector of doubles attribute
*/
typedef TAttribute<std::vector<double> > CVDoubleAttribute;
/**
\ingroup basic
\brief a string attribute
*/
typedef TAttribute<std::string> CStringAttribute;
/**
\ingroup basic
\brief a vector of strings attribute
*/
typedef TAttribute<std::vector<std::string> > CVStringAttribute;
/**
\ingroup basic
\brief A name:attribute map
*/
typedef std::map<std::string, PAttribute> CAttributeMap;
/**
\ingroup basic
\brief providing the possibility to nest attribute lists
*/
typedef TAttribute<CAttributeMap> CAttributeList;
/**
\ingroup basic
\brief another pointer-usage easy maker
*/
typedef std::shared_ptr<CAttributeMap > PAttributeMap;
/**
\ingroup basic
\brief Facility to write an attribute map to a stream
\param os output stream
\param data map of values
\returns a reference to the output stream
*/
EXPORT_CORE std::ostream& operator << (std::ostream& os, const CAttributeMap& data);
/**
\ingroup basic
\brief A collection of attributes
This is the base class for all data that uses attributes. It provides all the needed functions to store
and retrive attributes.
*/
class EXPORT_CORE CAttributedData {
public:
//! \name Constructors
//@{
CAttributedData();
CAttributedData(const CAttributedData& org);
/**
Constructor that initialises with a certain attribute map
\param attr the map to be initialised with
*/
CAttributedData(PAttributeMap attr);
//@}
/// Assignemt operator
CAttributedData& operator =(const CAttributedData& org);
/** \param key
\returns the attribute with name \a name or \a NULL (wrapped as shared pointer)
if the attribute with \a name is not found
*/
const PAttribute get_attribute(const std::string& key) const;
/**
\returns the begin iterator to the attributes
*/
CAttributeMap::const_iterator begin_attributes() const;
/**
\returns the end iterator to the attributes
*/
CAttributeMap::const_iterator end_attributes() const;
/**
Sets the attribute \a name to value \a attr. If \a attr is \a NULL, then
the attribute is removed from the list (or not added)
\param key
\param attr
*/
void set_attribute(const std::string& key, PAttribute attr);
/**
Insersts or overwrites the attributes given in the input range
\param begin
\param end
*/
void set_attributes(CAttributeMap::const_iterator begin, CAttributeMap::const_iterator end);
/**
Set an attribute using one of the defined translators
\param key
\param value
*/
void set_attribute(const std::string& key, const std::string& value);
/// returns the requested attribute as string, returns an empty string if attribute doesn't exist
const std::string get_attribute_as_string(const std::string& key)const;
/**
Look for a certain attribute and try to cast it to the output type.
If the attribute is not found, a std::invalid_argument exception is thrown.
If the cast fails then std::bad_cast exception will be thrown.
@param key the key of the attribute to look up.
@returns the value of the attribute
*/
template <typename T>
const T get_attribute_as(const std::string& key)const;
/**
Delete the attribute with a given key from the list
@param key
*/
void delete_attribute(const std::string& key);
/**
See if a certain attribute exists
@param key
@returns true if attribute exists, false otherwise
*/
bool has_attribute(const std::string& key)const;
/// @cond FRIENDSDOC
friend EXPORT_CORE bool operator == (const CAttributedData& a, const CAttributedData& b);
/// @endcond
private:
PAttributeMap m_attr;
};
/**
\ingroup basic
\brief Compare two attribute data instances \a a and \a b
\returns \a true, if \a a and \a b hold the same set of attributes with the same values,
\a false otherwise
*/
EXPORT_CORE bool operator == (const CAttributeMap& am, const CAttributeMap& bm);
/**
\ingroup basic
@brief A class to translate an attribute from a string.
This class is the base class to translate attributes from their typed value to a string and back.
*/
class EXPORT_CORE CAttrTranslator {
public:
/// The virtual destructor just ensures virtual destruction and silences a warning
virtual ~CAttrTranslator() {};
/**
\param value the string value of the object
\returns a shared pointer to the newly created attribute
*/
PAttribute from_string(const std::string& value) const;
private:
virtual PAttribute do_from_string(const std::string& value) const = 0;
protected:
CAttrTranslator();
/**
Register this translator to handle attributes with the given key
@param key
*/
void do_register(const std::string& key);
};
/**
\ingroup basic
\brief A singelton class to translate strings to attributes based on keys.
This class provides a singleton to translate strings to attributes. For the translation to take
place for each attribute key a CAttrTranslator needs to be registered first.
*/
class EXPORT_CORE CStringAttrTranslatorMap {
public:
/**
Converts the input string \a value to a PAttribute by using the translator that was registered for the
given \a key.
\param key
\param value
\returns the respective attribute
*/
PAttribute to_attr(const std::string& key, const std::string& value) const;
/// \returns an instance to the translator map singleton
static CStringAttrTranslatorMap& instance();
private:
friend class CAttrTranslator;
CStringAttrTranslatorMap();
/**
Add a new translator to the map. If there is already another translator registered for the given \a key, then
throw an \a invalid_argument exception.
\param key a key that is used to pick the translator
\param t the translator object
*/
void add(const std::string& key, const CAttrTranslator * const t);
typedef std::map<std::string, const CAttrTranslator * const> CMap;
CMap m_translators;
};
/**
\ingroup basic
convenience function to set an attribute in an attribute map:
\remark review its use
\tparam type of the attribute value to be added
\param attributes map to set the value in
\param key
\param value
*/
template <typename T>
void EXPORT_CORE add_attribute(CAttributeMap& attributes, const std::string& key, T value)
{
cvdebug() << "add attribute " << key << " of type " << typeid(T).name() << " and value '" << value << "'\n";
attributes[key] = PAttribute(new TAttribute<T>(value));
}
/**
\ingroup basic
convenience function to set an string attribute from a C-string in an attribute map:
\tparam type of the attribute value to be added
\param attributes map to set the value in
\param key
\param value
*/
template <>
void EXPORT_CORE add_attribute(CAttributeMap& attributes, const std::string& key, const char * value);
/**
\ingroup basic
\brief Generic string vs. attribute translator singleton
This class defines a generic translator between strings and a specific attribute type.
All translaters are registered to a global map of type CStringAttrTranslatorMap
that selects the conversion based on a key. The global map is implemented as a singleton
can be accessed via CStringAttrTranslatorMap::instance()
*/
template <typename T>
class EXPORT_CORE TTranslator: public CAttrTranslator {
public:
/**
Register this translator for attributed for the given \a key. The translator is handled as a
singleton and is stateless.
Any translator type can be registered multiple times but keys must be different
if the target translation type is different.
*/
static void register_for(const std::string& key);
private:
virtual PAttribute do_from_string(const std::string& value) const;
};
// template implementation
template <typename T>
TAttribute<T>::TAttribute(typename ::boost::reference_wrapper<T>::type value):
m_value(value)
{
}
template <typename T>
TAttribute<T>::operator T() const
{
return m_value;
}
template <typename T>
const T& TAttribute<T>::get_value() const
{
return m_value;
}
template <typename T>
const char *TAttribute<T>::typedescr() const
{
return typeid(T).name();
}
/**
@cond INTERNAL
@ingroup traits
A trait to translate between a string and a value
\remark this should replace the parameter translation methods
*/
template <typename T>
struct dispatch_attr_string {
static std::string val2string(const typename ::boost::reference_wrapper<T>::type value) {
std::stringstream sval;
sval << value;
return sval.str();
}
static T string2val(const std::string& str) {
T v;
std::istringstream svalue(str);
svalue >> v;
return v;
}
};
template <typename T>
struct dispatch_attr_string<std::vector<T> > {
static std::string val2string(const std::vector<T>& value) {
std::stringstream sval;
sval << value.size();
for (size_t i = 0; i < value.size(); ++i)
sval << " " << value[i];
return sval.str();
}
static std::vector<T> string2val(const std::string& str) {
size_t s;
std::istringstream svalue(str);
std::vector<T> v;
svalue >> s;
if (s > v.max_size())
throw create_exception<std::runtime_error>("string2val: try to create a vector of size ",
s, " but support only size ", v.max_size());
v.resize(s);
for (size_t i = 0; i < s; ++i)
svalue >> v[i];
if (svalue.fail()) {
std::stringstream msg;
msg << "string2val: unable to convert '" << str << "'";
throw std::invalid_argument(msg.str());
}
return v;
}
};
template <>
struct dispatch_attr_string<std::vector<bool> > {
static std::string val2string(const std::vector<bool>& value) {
std::stringstream sval;
sval << value.size();
for (size_t i = 0; i < value.size(); ++i)
sval << " " << value[i];
return sval.str();
}
static std::vector<bool> string2val(const std::string& str) {
size_t s;
std::istringstream svalue(str);
svalue >> s;
std::vector<bool> v(s);
for (size_t i = 0; i < s; ++i) {
bool value;
svalue >> value;
v[i] = value;
}
if (svalue.fail()) {
std::stringstream msg;
msg << "string2val: unable to convert '" << str << "'";
throw std::invalid_argument(msg.str());
}
return v;
}
};
template <>
struct dispatch_attr_string<unsigned char> {
static std::string val2string(unsigned char value) {
std::stringstream sval;
sval << (unsigned int)value;
return sval.str();
}
static unsigned char string2val(const std::string& str) {
unsigned int v;
std::istringstream svalue(str);
svalue >> v;
return (unsigned char)v;
}
};
template <>
struct dispatch_attr_string<signed char> {
static std::string val2string(signed char value) {
std::stringstream sval;
sval << (signed int)value;
return sval.str();
}
static signed char string2val(const std::string& str) {
int v;
std::istringstream svalue(str);
svalue >> v;
return (signed char)v;
}
};
template <>
struct dispatch_attr_string<std::string> {
static std::string val2string(std::string value) {
return value;
}
static std::string string2val(const std::string& str) {
return str;
}
};
template <>
struct dispatch_attr_string<CAttributeMap> {
static std::string val2string(const CAttributeMap& /*value*/) {
throw std::invalid_argument("Conversion of a CAttributeMap to a string not implemented");
}
static CAttributeMap string2val(const std::string& /*str*/) {
throw std::invalid_argument("Conversion of a string to a CAttributeMap not implemented");
}
};
/// @endcond
template <typename T>
std::string TAttribute<T>::do_as_string() const
{
return dispatch_attr_string<T>::val2string(m_value);
}
template <typename T>
bool TAttribute<T>::do_is_equal(const CAttribute& other) const
{
const TAttribute<T>* o = dynamic_cast<const TAttribute<T> *>(&other);
if (!o) {
cvdebug() << "TAttribute<T>::do_is_equal:Cast to "
<< typeid(const TAttribute<T>*).name()
<< "failed\n";
return false;
}
return m_value == o->m_value;
}
template <typename T>
bool TAttribute<T>::do_is_less(const CAttribute& other) const
{
const TAttribute<T>* o = dynamic_cast<const TAttribute<T> *>(&other);
if (o)
return m_value < o->m_value;
return strcmp(typedescr(), other.typedescr()) < 0;
}
#if 0
template <typename T>
TVAttribute<T>::TVAttribute(const std::vector<T>& value):
TAttribute<std::vector<T> >(value)
{
}
template <typename T>
bool TVAttribute<T>::do_is_equal(const CAttribute& other) const
{
const TVAttribute<T>* o = dynamic_cast<const TVAttribute<T> *>(&other);
if (!o)
return false;
return o->get_value().size() == this->get_value().size() &&
std::equal(this->get_value().begin(), this->get_value().end(), o->get_value().begin());
};
#endif
template <typename T>
void TTranslator<T>::register_for(const std::string& key)
{
static TTranslator<T> me;
me.do_register(key);
}
template <typename T>
PAttribute TTranslator<T>::do_from_string(const std::string& value) const
{
return PAttribute(new TAttribute<T>(dispatch_attr_string<T>::string2val(value)));
}
template <typename T>
const T CAttributedData::get_attribute_as(const std::string& key)const
{
PAttribute attr = get_attribute(key);
if (attr)
return dynamic_cast<const TAttribute<T>&>(*attr);
else
throw create_exception<std::invalid_argument>("CAttributedData: no attribute '", key, "' found");
}
typedef TTranslator<double> CDoubleTranslator;
typedef TTranslator<std::vector<double> > CVDoubleTranslator;
typedef TTranslator<float> CFloatTranslator;
typedef TTranslator<std::vector<float> > CVFloatTranslator;
#ifdef LONG_64BIT
typedef TTranslator<unsigned long> CULTranslator;
typedef TTranslator<std::vector<unsigned long> > CVULTranslator;
typedef TTranslator<signed long> CSLTranslator;
typedef TTranslator<std::vector<signed long> > CVSLTranslator;
#endif
typedef TTranslator<unsigned int> CUITranslator;
typedef TTranslator<std::vector<unsigned int> > CVUITranslator;
typedef TTranslator<signed int> CSITranslator;
typedef TTranslator<std::vector<signed int> > CVSITranslator;
typedef TTranslator<unsigned short> CUSTranslator;
typedef TTranslator<std::vector<unsigned short> > CVUSTranslator;
typedef TTranslator<signed short> CSSTranslator;
typedef TTranslator<std::vector<signed short> > CVSSTranslator;
typedef TTranslator<unsigned char> CUBTranslator;
typedef TTranslator<std::vector<unsigned char> > CVUBTranslator;
typedef TTranslator<signed char> CSBTranslator;
typedef TTranslator<std::vector<signed char> > CVSBTranslator;
typedef TTranslator<bool> CBitTranslator;
typedef TTranslator<std::vector<bool> > CVBitTranslator;
NS_MIA_END
#endif
|