24 #ifndef FAUDES_ATTRMAP_H
25 #define FAUDES_ATTRMAP_H
51 template<
class T,
class Attr,
class Cmp=std::less<T> >
175 bool Insert(
const T& rElem,
const Attr& attr);
273 #define THIS TAttrMap<T,Attr,Cmp>
274 #define TEMP template<class T, class Attr, class Cmp>
278 pBaseSet(pBaseSetRef)
280 FD_DC(
"TAttrMap(" <<
this <<
")::TAttrMap(): set at " <<
pBaseSet);
286 FD_DC(
"TAttrMap(" <<
this <<
")::~TAttrMap()");
293 FD_DC(
"TAttrMap(" << pBaseSet <<
")::Assign(..): dst map type " <<
typeid(*this).name());
294 FD_DC(
"TAttrMap(" << pBaseSet <<
")::Assign(..): src type " <<
typeid(rSourceSet).name());
296 pBaseSet->DoAssign(rSourceSet);
298 if(pBaseSet->AttributesSize()==rSourceSet.
AttributesSize())
return;
300 FD_DC(
"TAttrMap(" <<
this <<
")::Assign(..): dst attribute type " <<
typeid(*AttributeType()).name());
301 if(
typeid(*AttributeType()) ==
typeid(
const AttributeVoid) )
return;
303 FD_DC(
"TAttrMap(" <<
this <<
")::Assign(..): src attribute type " <<
typeid(*rSourceSet.
AttributeType()).name());
306 FD_DC(
"TAttrMap(" <<
this <<
")::Assign(..): try attribute cast");
307 if(!AttributeType()->Cast(rSourceSet.
AttributeType()))
return;
310 FD_DC(
"TAttrMap(" <<
this <<
")::Assign(..): mind attributes");
313 Attr* attr=
new Attr;
314 attr->Assign(*ait->second);
315 (*pBaseSet->pAttributes)[ait->first]=attr;
318 FD_DC(
"TAttrMap(" << pBaseSet <<
")::Assign(..): done");
323 TEMP bool THIS::Insert(
const T& rElem) {
324 FD_DC(
"TAttrMap(" << pBaseSet <<
")::Insert(elem)");
326 bool ret=pBaseSet->pSet->insert(rElem).second;
331 TEMP bool THIS::Insert(
const T& rElem,
const Attr& rAttr) {
332 FD_DC(
"TAttrMap(" << pBaseSet <<
")::Insert(elem,attr)");
334 bool ret=pBaseSet->pSet->insert(rElem).second;
335 pBaseSet->DoAttribute(rElem,&rAttr);
341 bool doattr=pBaseSet->AttributeTest(*rOtherSet.
AttributeType());
342 FD_DC(
"TAttrMap(" << pBaseSet <<
")::InsertSet( [v] " << &rOtherSet <<
"): doattr=" << doattr);
344 iterator it1 = pBaseSet->pSet->begin();
346 while ((it1 != pBaseSet->pSet->end()) && (it2 != rOtherSet.
pSet->end())) {
350 else if (*it1 == *it2) {
355 pBaseSet->pSet->insert(*it2);
356 if(doattr) pBaseSet->DoAttribute(*it2,&rOtherSet.
Attribute(*it2));
360 while(it2 != rOtherSet.
pSet->end()) {
361 pBaseSet->pSet->insert(*it2);
362 if(doattr) pBaseSet->DoAttribute(*it2,&rOtherSet.
Attribute(*it2));
365 FD_DC(
"TAttrMap(" << pBaseSet <<
")::InsertSet( [v] " << &rOtherSet <<
"): done");
369 TEMP bool THIS::Erase(
const T& rElem) {
372 return (pBaseSet->pSet->erase(rElem)!=0);
380 pBaseSet->pSet->erase(del++);
381 return pBaseSet->ThisIterator(del);
387 FD_DC(
"TAttrMap(" << pBaseSet <<
")::EraseSet(" << &rOtherSet <<
")");
390 iterator it = pBaseSet->pSet->begin();
392 while ((it != pBaseSet->pSet->end()) && (oit != rOtherSet.
pSet->end())) {
394 it=pBaseSet->pSet->lower_bound(*oit);
396 else if (*it == *oit) {
399 pBaseSet->pSet->erase(it++);
402 oit=rOtherSet.
pSet->lower_bound(*it);
410 FD_DC(
"TAttrMap(" << pBaseSet <<
")::RestrictSet(" << &rOtherSet <<
")");
413 iterator it = pBaseSet->pSet->begin();
415 while ((it != pBaseSet->pSet->end()) && (oit != rOtherSet.
pSet->end())) {
418 pBaseSet->pSet->erase(it++);
420 else if (*it == *oit) {
428 while(it != pBaseSet->pSet->end()) {
430 pBaseSet->pSet->erase(it++);
438 TEMP const Attr* THIS::AttributeType(
void)
const {
440 FD_DC(
"TAttrMap::AttributeType(): accessing non trivial type");
446 TEMP Attr* THIS::Attributep(
const T& rElem) {
447 #ifdef FAUDES_CHECKED
448 if(!pBaseSet->Exists(rElem)) {
449 std::stringstream errstr;
450 errstr <<
"element \"" << pBaseSet->Str(rElem) <<
"\" not member of set " << pBaseSet->Name() << std::endl;
451 throw Exception(
"TAttrMap::Attributep(elem)", errstr.str(), 60);
457 Attr* attr=
dynamic_cast<Attr*
>(pBaseSet->DoAttributeExplicit(rElem));
458 FD_DC(
"TAttrMap::Attributep(): found " << attr <<
" type " <<
typeid(*attr).name() );
464 TEMP const Attr& THIS::Attribute(
const T& rElem)
const {
465 #ifdef FAUDES_CHECKED
466 if(!pBaseSet->Exists(rElem)) {
467 std::stringstream errstr;
468 errstr <<
"element \"" << pBaseSet->Str(rElem) <<
"\" not member of set " << pBaseSet->Name() << std::endl;
469 throw Exception(
"TAttrMap::Attribute(elem)", errstr.str(), 60);
473 const Attr* attr=
dynamic_cast<const Attr*
>(pBaseSet->DoAttribute(rElem));
475 if(!attr) attr=this->AttributeType();
481 TEMP void THIS::Attribute(
const T& rElem,
const Type& attr) {
482 #ifdef FAUDES_CHECKED
483 if (!pBaseSet->Exists(rElem)) {
484 std::stringstream errstr;
485 errstr <<
"element \"" << pBaseSet->Str(rElem) <<
"\" not member of set " << pBaseSet->Name() << std::endl;
486 throw Exception(
"TAttrMap::Attribute(elem,attr [t])", errstr.str(), 60);
489 if(!pBaseSet->AttributeTest(attr)) {
490 std::stringstream errstr;
491 errstr <<
"cannot cast attribute " << std::endl;
492 throw Exception(
"TAttrMap::Attribute(elem,attr [t])", errstr.str(), 63);
495 pBaseSet->DoAttribute(rElem,&attr);
499 TEMP void THIS::AttributeTry(
const T& rElem,
const Type& attr) {
500 FD_DC(
"TAttrMap::Attribute(elem,attr) [t]");
501 #ifdef FAUDES_CHECKED
502 if(!pBaseSet->Exists(rElem)) {
503 std::stringstream errstr;
504 errstr <<
"element \"" << pBaseSet->Str(rElem) <<
"\" not member of set " << pBaseSet->Name() << std::endl;
505 throw Exception(
"TAttrMap::AttributeTry(elem,attr [t])", errstr.str(), 60);
508 if(!pBaseSet->AttributeTest(attr))
return;
510 pBaseSet->DoAttribute(rElem,&attr);
514 TEMP void THIS::Attribute(
const T& rElem,
const Attr& attr) {
515 FD_DC(
"TAttrMap::Attribute(elem,attr) [a]");
516 #ifdef FAUDES_CHECKED
517 if(!pBaseSet->Exists(rElem)) {
519 std::stringstream errstr;
520 errstr <<
"element \"" << pBaseSet->Str(rElem) <<
"\" not member of set " << pBaseSet->Name() << std::endl;
521 throw Exception(
"TAttrMap::Attribute(elem,attr [a])", errstr.str(), 60);
525 pBaseSet->DoAttribute(rElem, &attr);
#define FD_DC(message)
Debug: optional report on container operations.
Attribute interface for TBaseSet.
bool Insert(const T &rElem, const Attr &attr)
Insert element with attribute.
const Attr & Attribute(const T &rElem) const
Get attribute by element.
Attr * Attributep(const T &rElem)
Get attribute reference by element.
void AttributeTry(const T &rElem, const Type &attr)
Set attribute.
bool Erase(const T &rElem)
Erase Element (incl its attribute)
TBaseSet< T, Cmp >::const_aiterator const_aiterator
use TBaseSet STL iterators
void EraseSet(const TBaseSet< T, Cmp > &rOtherSet)
Erase elements given by other set.
TBaseSet< T, Cmp >::Iterator Erase(const typename TBaseSet< T, Cmp >::Iterator &pos)
Erase element by iterator (incl attribute)
TBaseSet< T, Cmp >::aiterator aiterator
use TBaseSet STL iterators
void Attribute(const T &rElem, const Type &attr)
Set attribute.
void InsertSet(const TBaseSet< T, Cmp > &rOtherSet)
Inserts elements from rOtherSet.
void RestrictSet(const TBaseSet< T, Cmp > &rOtherSet)
Restrict to specified subset.
TAttrMap(TBaseSet< T, Cmp > *pBaseSetRef)
Constructor.
virtual ~TAttrMap(void)
Virtual destructor.
TBaseSet< T, Cmp >::const_iterator const_iterator
use TBaseSet STL iterators
void AssignWithAttributes(const TBaseSet< T, Cmp > &rSourceSet)
Copy from a TBaseSet to the associated TBaseSet, with attributes, provided that they can be casted ac...
TBaseSet< T, Cmp > * pBaseSet
reference to associated TBaseSet
TBaseSet< T, Cmp >::iterator iterator
use TBaseSet STL iterators
bool Insert(const T &rElem)
Insert element.
const Attr * AttributeType(void) const
Attribute typeinfo.
void Attribute(const T &rElem, const Attr &attr)
Set attribute.
std::map< T, AttributeVoid * > * pAttributes
Pointer to attribute map to operate on.
std::set< T, Cmp > * pSet
Pointer on STL set to operate on.
Base class of all libFAUDES objects that participate in the run-time interface.
virtual const AttributeVoid * AttributeType(void) const
Attribute typeinfo.
void DoAttribute(const T &rElem, const Type *pAttr)
set attribute in map (assume elem exists in set, NULL <=> set to default)
void Detach(DetachMode flag=AttrIncl) const
Idx AttributesSize(void) const
Attribute access.
virtual const AttributeVoid & Attribute(const T &rElem) const
Attribute access.
libFAUDES resides within the namespace faudes.