Skip to content

Commit

Permalink
better comments for the new iterator class
Browse files Browse the repository at this point in the history
  • Loading branch information
andysworkshop committed Oct 31, 2015
1 parent 550d627 commit ee20900
Showing 1 changed file with 64 additions and 4 deletions.
68 changes: 64 additions & 4 deletions lib/include/gpio/GpioIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,23 @@
namespace stm32plus {


/*
/**
* STL compatible forward iterator that can be used to iterate over GpioPinRef
* instances in a port
* instances in a port. Instances of this iterator should be obtained from the
* GpioPort object using the begin() and end() methods. Basic usage example:
*
* GpioC<DefaultDigitalInputFeature<1,7,13>,DefaultDigitalOutputFeature<8,9,15>> pc;
*
* for(auto it=pc.begin();it!=pc.end();it++) {
* if(it->getMode()==Gpio::OUTPUT) {
* // do something
* }
* }
*
* This iterator is compatible with algorithms in the <algorithm> and <util/StdExt.h> header
* that take a forward iterator. In common with the STL iterators you are expected to
* be digilent in checking the start and end position using begin() and end() because
* the increment and decrement operators will not check for you.
*/

struct GpioIterator {
Expand Down Expand Up @@ -59,7 +73,7 @@ namespace stm32plus {

/**
* Dereference operator
* @return reference to Gpio object at position
* @return reference to GpioPinRef object at position
*/

GpioPinRef& operator*() {
Expand All @@ -72,51 +86,97 @@ namespace stm32plus {

/**
* Pointer operator
* @return
* @return address of the current object at the index
*/

GpioPinRef *operator->() {
return &(operator*());
}


/**
* Increment the iterator
* @return self reference
*/

GpioIterator& operator++() {
increment();
return *this;
}


/**
* Increment the iterator
* @return self reference
*/

GpioIterator& operator++(int) {
GpioIterator& tmp=*this;
increment();
return tmp;
}


/**
* Decrement the iterator
* @return self reference
*/

GpioIterator& operator--() {
decrement();
return *this;
}


/**
* Decrement the iterator
* @return self reference
*/

GpioIterator& operator--(int) {
GpioIterator& tmp=*this;
increment();
return tmp;
}


/**
* Equality comparison
* @param rhs the other iterator
* @return true if the iterators are logically equivalent
*/

bool operator==(const GpioIterator& rhs) const {
return _peripheralAddress==rhs._peripheralAddress && _index==rhs._index;
}


/**
* Inequality comparison
* @param rhs the other iterator
* @return true if the iterators are not logically equivalent
*/

bool operator!=(const GpioIterator& rhs) const {
return _peripheralAddress!=rhs._peripheralAddress || _index!=rhs._index;
}


/**
* Increment the iterator to the next active pin
*/

void increment() {
do {
_index++;
} while(_pinHandlers[_index]==nullptr && _index!=16);
}


/**
* Decrement the iterator to the next active pin
*/

void decrement() {
do {
_index--;
Expand Down

0 comments on commit ee20900

Please sign in to comment.