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++);
437 TEMP const Attr* THIS::AttributeType(
void)
const {
439 FD_DC(
"TAttrMap::AttributeType(): accessing non trivial type");
445 TEMP Attr* THIS::Attributep(
const T& rElem) {
446 #ifdef FAUDES_CHECKED
447 if(!pBaseSet->Exists(rElem)) {
448 std::stringstream errstr;
449 errstr <<
"element \"" << pBaseSet->Str(rElem) <<
"\" not member of set " << pBaseSet->Name() << std::endl;
450 throw Exception(
"TAttrMap::Attributep(elem)", errstr.str(), 60);
456 Attr* attr=
dynamic_cast<Attr*
>(pBaseSet->DoAttributeExplicit(rElem));
457 FD_DC(
"TAttrMap::Attributep(): found " << attr <<
" type " <<
typeid(*attr).name() );
463 TEMP const Attr& THIS::Attribute(
const T& rElem)
const {
464 #ifdef FAUDES_CHECKED
465 if(!pBaseSet->Exists(rElem)) {
466 std::stringstream errstr;
467 errstr <<
"element \"" << pBaseSet->Str(rElem) <<
"\" not member of set " << pBaseSet->Name() << std::endl;
468 throw Exception(
"TAttrMap::Attribute(elem)", errstr.str(), 60);
472 const Attr* attr=
dynamic_cast<const Attr*
>(pBaseSet->DoAttribute(rElem));
474 if(!attr) attr=this->AttributeType();
480 TEMP void THIS::Attribute(
const T& rElem,
const Type& attr) {
481 #ifdef FAUDES_CHECKED
482 if (!pBaseSet->Exists(rElem)) {
483 std::stringstream errstr;
484 errstr <<
"element \"" << pBaseSet->Str(rElem) <<
"\" not member of set " << pBaseSet->Name() << std::endl;
485 throw Exception(
"TAttrMap::Attribute(elem,attr [t])", errstr.str(), 60);
488 if(!pBaseSet->AttributeTest(attr)) {
489 std::stringstream errstr;
490 errstr <<
"cannot cast attribute " << std::endl;
491 throw Exception(
"TAttrMap::Attribute(elem,attr [t])", errstr.str(), 63);
494 pBaseSet->DoAttribute(rElem,&attr);
498 TEMP void THIS::AttributeTry(
const T& rElem,
const Type& attr) {
499 FD_DC(
"TAttrMap::Attribute(elem,attr) [t]");
500 #ifdef FAUDES_CHECKED
501 if(!pBaseSet->Exists(rElem)) {
502 std::stringstream errstr;
503 errstr <<
"element \"" << pBaseSet->Str(rElem) <<
"\" not member of set " << pBaseSet->Name() << std::endl;
504 throw Exception(
"TAttrMap::AttributeTry(elem,attr [t])", errstr.str(), 60);
507 if(!pBaseSet->AttributeTest(attr))
return;
509 pBaseSet->DoAttribute(rElem,&attr);
513 TEMP void THIS::Attribute(
const T& rElem,
const Attr& attr) {
514 FD_DC(
"TAttrMap::Attribute(elem,attr) [a]");
515 #ifdef FAUDES_CHECKED
516 if(!pBaseSet->Exists(rElem)) {
518 std::stringstream errstr;
519 errstr <<
"element \"" << pBaseSet->Str(rElem) <<
"\" not member of set " << pBaseSet->Name() << std::endl;
520 throw Exception(
"TAttrMap::Attribute(elem,attr [a])", errstr.str(), 60);
524 pBaseSet->DoAttribute(rElem, &attr);
bool Insert(const T &rElem, const Attr &attr)
const Attr & Attribute(const T &rElem) const
Attr * Attributep(const T &rElem)
void AttributeTry(const T &rElem, const Type &attr)
bool Erase(const T &rElem)
TBaseSet< T, Cmp >::const_aiterator const_aiterator
void EraseSet(const TBaseSet< T, Cmp > &rOtherSet)
TBaseSet< T, Cmp >::Iterator Erase(const typename TBaseSet< T, Cmp >::Iterator &pos)
TBaseSet< T, Cmp >::aiterator aiterator
void Attribute(const T &rElem, const Type &attr)
void InsertSet(const TBaseSet< T, Cmp > &rOtherSet)
void RestrictSet(const TBaseSet< T, Cmp > &rOtherSet)
TAttrMap(TBaseSet< T, Cmp > *pBaseSetRef)
TBaseSet< T, Cmp >::const_iterator const_iterator
void AssignWithAttributes(const TBaseSet< T, Cmp > &rSourceSet)
TBaseSet< T, Cmp > * pBaseSet
TBaseSet< T, Cmp >::iterator iterator
bool Insert(const T &rElem)
const Attr * AttributeType(void) const
void Attribute(const T &rElem, const Attr &attr)
std::map< T, AttributeVoid * > * pAttributes
std::set< T, Cmp > * pSet
virtual const AttributeVoid * AttributeType(void) const
void DoAttribute(const T &rElem, const Type *pAttr)
void Detach(DetachMode flag=AttrIncl) const
Idx AttributesSize(void) const
virtual const AttributeVoid & Attribute(const T &rElem) const