src/lib/TransactionalBuffer.h
branchv_0
changeset 14 02725d301010
parent 13 d5e2cb9e31f1
equal deleted inserted replaced
13:d5e2cb9e31f1 14:02725d301010
    34 	bool hasData = false;
    34 	bool hasData = false;
    35 	size_t writePosition = 0;
    35 	size_t writePosition = 0;
    36 	// size_t writePositionCommited = 0; // TODO: transactions also on the writing side?
    36 	// size_t writePositionCommited = 0; // TODO: transactions also on the writing side?
    37 	size_t readPosition = 0;
    37 	size_t readPosition = 0;
    38 	size_t readPositionCommited = 0;
    38 	size_t readPositionCommited = 0;
       
    39 
       
    40 	size_t bytesRead = 0;
       
    41 	size_t bytesReadCommited = 0;
    39 
    42 
    40 	size_t availableForReading() {
    43 	size_t availableForReading() {
    41 		if (readPosition < writePosition) return writePosition - readPosition;
    44 		if (readPosition < writePosition) return writePosition - readPosition;
    42 		else if (readPosition > writePosition) return bufferSize - readPosition + writePosition;
    45 		else if (readPosition > writePosition) return bufferSize - readPosition + writePosition;
    43 		else if (hasData) return bufferSize;
    46 		else if (hasData) return bufferSize;
    97 		if (length == 0) return;
   100 		if (length == 0) return;
    98 
   101 
    99 		peek(outputBuffer, length);
   102 		peek(outputBuffer, length);
   100 		readPosition = (readPosition + length) % bufferSize;
   103 		readPosition = (readPosition + length) % bufferSize;
   101 		hasData = readPosition != writePosition;
   104 		hasData = readPosition != writePosition;
       
   105 		bytesRead += length;
   102 	}
   106 	}
   103 
   107 
   104 	void peek(uint8_t* outputBuffer, const size_t length) {
   108 	void peek(uint8_t* outputBuffer, const size_t length) {
   105 		if (length == 0) return;
   109 		if (length == 0) return;
   106 		if (length > availableForReading()) throw ReadBufferUnderflowException();
   110 		if (length > availableForReading()) throw ReadBufferUnderflowException();
   112 		memcpy(outputBuffer + a, buffer, b);
   116 		memcpy(outputBuffer + a, buffer, b);
   113 	}
   117 	}
   114 
   118 
   115 	void commitRead() {
   119 	void commitRead() {
   116 		readPositionCommited = readPosition;
   120 		readPositionCommited = readPosition;
       
   121 		bytesReadCommited = bytesRead;
   117 	}
   122 	}
   118 
   123 
   119 	void rollbackRead() {
   124 	void rollbackRead() {
   120 		readPosition = readPositionCommited;
   125 		readPosition = readPositionCommited;
       
   126 		bytesRead = bytesReadCommited;
   121 	}
   127 	}
       
   128 
       
   129 	size_t getBytesRead() {
       
   130 		return bytesRead;
       
   131 	}
       
   132 
   122 };
   133 };
   123 
   134 
   124 
   135 
   125 }
   136 }
   126 }
   137 }