diff --git a/pvDataApp/misc/byteBuffer.h b/pvDataApp/misc/byteBuffer.h index 61e488c..8e48e5d 100644 --- a/pvDataApp/misc/byteBuffer.h +++ b/pvDataApp/misc/byteBuffer.h @@ -11,6 +11,8 @@ #include #include +#include "epicsException.h" + namespace epics { namespace pvData { @@ -305,6 +307,21 @@ namespace epics { return _buffer; } + /** + * Sets this buffer's position. If the new position is invalid in + * regards to the invariant then it is discarded. + * + * @param[in] newPosition The new position value; must be + * non-negative and no larger than the current limit. + * @throws EpicsException - If the preconditions on + * {@code newPosition} do not hold + */ + inline void setPosition(int newPosition) { + if(newPosition<0||newPosition>_limit) throw EpicsException( + "invalid limit"); + _position = newPosition; + } + // TODO must define arrays private: diff --git a/testApp/misc/testByteBuffer.cpp b/testApp/misc/testByteBuffer.cpp index 7de8607..40c68b0 100644 --- a/testApp/misc/testByteBuffer.cpp +++ b/testApp/misc/testByteBuffer.cpp @@ -93,6 +93,21 @@ void testBasicOperations() { assert(buff->getLimit()==32); assert(buff->getRemaining()==32); + buff->setPosition(4); + assert(buff->getPosition()==4); + assert(buff->getLimit()==32); + assert(buff->getRemaining()==(32-4)); + + buff->setPosition(13); + assert(buff->getPosition()==13); + assert(buff->getLimit()==32); + assert(buff->getRemaining()==(32-13)); + + buff->clear(); + assert(buff->getPosition()==0); + assert(buff->getLimit()==32); + assert(buff->getRemaining()==32); + char src[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm' }; char dst[] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',