Skip to content
Snippets Groups Projects
Commit 23a211a5 authored by Sebastian Eibl's avatar Sebastian Eibl
Browse files

extended SendBuffer with low level memory functions

parent 997814a8
Branches
Tags
No related merge requests found
...@@ -140,11 +140,13 @@ public: ...@@ -140,11 +140,13 @@ public:
//**Utility functions************************************************************************************************ //**Utility functions************************************************************************************************
/*!\name Utility functions */ /*!\name Utility functions */
//@{ //@{
inline T* ptr () const; inline T* ptr () const;
inline void reserve( size_t newCapacity ); inline std::ptrdiff_t getOffset() const;
inline void clear (); inline T* getMemoryLocation( const std::ptrdiff_t offset);
inline void reset (); inline void reserve( size_t newCapacity );
inline void addDebugMarker( const char * marker ); inline void clear ();
inline void reset ();
inline void addDebugMarker( const char * marker );
//@} //@}
//******************************************************************************************************************* //*******************************************************************************************************************
...@@ -490,6 +492,39 @@ inline T* GenericSendBuffer<T,G>::ptr() const ...@@ -490,6 +492,39 @@ inline T* GenericSendBuffer<T,G>::ptr() const
} }
//********************************************************************************************************************** //**********************************************************************************************************************
/**
* Returns the offset from the beginning to the current position inside the buffer in bytes.
*
* Example:
* \snippet BufferTest.cpp SendBuffer Overwrite Test
* The buffer now contains 3, 2, 3
* \attention This is a low level function. Use with care!
* \see getMemoryLocation()
*/
template< typename T // Element type
, typename G > // Growth policy
inline std::ptrdiff_t GenericSendBuffer<T,G>::getOffset() const
{
return cur_ - begin_;
}
/**
* Returns the memory address corresponding to the offset. Offset is measured in bytes from the beginning of the buffer.
*
* Example:
* \snippet BufferTest.cpp SendBuffer Overwrite Test
* The buffer now contains 3, 2, 3
* \attention This is a low level function. Use with care!
* \see getOffset()
*/
template< typename T // Element type
, typename G > // Growth policy
inline T* GenericSendBuffer<T,G>::getMemoryLocation( const std::ptrdiff_t offset)
{
return begin_ + offset;
}
//********************************************************************************************************************** //**********************************************************************************************************************
/*!\brief Setting the minimum capacity of the send buffer. /*!\brief Setting the minimum capacity of the send buffer.
......
...@@ -299,6 +299,32 @@ void bufferTestUInt8() ...@@ -299,6 +299,32 @@ void bufferTestUInt8()
} }
void bufferOverwriteTest()
{
//! [SendBuffer Overwrite Test]
int a = 1;
int b = 2;
int c = 3;
SendBuffer sb;
auto offset = sb.getOffset();
sb << a << b << c;
*reinterpret_cast<int*>(sb.getMemoryLocation(offset)) = c;
//! [SendBuffer Overwrite Test]
// Copying
RecvBuffer rb( sb );
int recv;
rb >> recv;
WALBERLA_CHECK_EQUAL(c, recv);
rb >> recv;
WALBERLA_CHECK_EQUAL(b, recv);
rb >> recv;
WALBERLA_CHECK_EQUAL(c, recv);
}
int main() int main()
{ {
debug::enterTestMode(); debug::enterTestMode();
...@@ -313,6 +339,8 @@ int main() ...@@ -313,6 +339,8 @@ int main()
// than the type that is stored // than the type that is stored
//bufferTest<double>(); //bufferTest<double>();
bufferOverwriteTest();
return 0; return 0;
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment