31.01.2014 Views

Version 5.0 The LEDA User Manual

Version 5.0 The LEDA User Manual

Version 5.0 The LEDA User Manual

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4. Example<br />

First two simple observer classes. <strong>The</strong> first one is a dummy class, which ignores all<br />

notifications. <strong>The</strong> second one merely counts the number of calls to operator++ for all<br />

iterators that share the same observer object through copy construction or assignment<br />

(of course, a real implementation should apply some kind of reference counting or other<br />

garbage collection).<br />

In this example, the counter variable count of class SimpleCountObserver will be initialized<br />

with the counter variable count of class DummyObserver, i.e. the variable is created<br />

only once.<br />

template <br />

class DummyObserver {<br />

int* _count;<br />

public:<br />

DummyObserver() : _count(new int(0)) { }<br />

void notify_constructor(const Iter& ) { }<br />

void notify_forward(const Iter& ) { }<br />

void notify_update(const Iter& ) { }<br />

int counter() const { return *_count; }<br />

int* counter_ptr() const { return _count; }<br />

bool operator==(const DummyObserver& D) const {<br />

return _count==D._count; }<br />

};<br />

template <br />

class SimpleCountObserver {<br />

int* _count;<br />

public:<br />

SimpleCountObserver() : _count(new int(0)) { }<br />

SimpleCountObserver(Observer& obs) :<br />

_count(obs.counter_ptr()) { }<br />

void notify_constructor(const Iter& ) { }<br />

void notify_forward(const Iter& ) { ++(*_count); }<br />

void notify_update(const Iter& ) { }<br />

int counter() const { return *_count; }<br />

int* counter_ptr() const { return _count; }<br />

bool operator==(const SimpleCountObserver& S) const {<br />

return _count==S._count; }<br />

};<br />

Next an exemplary application, which counts the number of calls to operator++ of all<br />

adjacency iterator objects inside dummy algorithm. Here the dummy observer class is

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!