mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-07-11 22:03:01 -06:00
bazaar: BufferStream, a Vector<byte> buffer based, self growing Stream
git-svn-id: svn://ultimatepp.org/upp/trunk@2607 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
c761602615
commit
f51955968a
9 changed files with 224 additions and 0 deletions
52
bazaar/BufferStream/BufferStream.cpp
Normal file
52
bazaar/BufferStream/BufferStream.cpp
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include "BufferStream.h"
|
||||
|
||||
|
||||
void BufferStream::_Put(const void *data, dword size)
|
||||
{
|
||||
if(size > (dword)(uintptr_t)(wrlim - ptr)) {
|
||||
Reserve(size + 128);
|
||||
}
|
||||
memcpy(ptr, data, size);
|
||||
ptr += size;
|
||||
}
|
||||
|
||||
void BufferStream::SetSize(int64 asize)
|
||||
{
|
||||
dword size = (dword)asize;
|
||||
dword p = (dword)(uintptr_t)(ptr - buffer);
|
||||
data.SetCount(size);
|
||||
Open(data);
|
||||
SetStoring();
|
||||
Seek(min(p, size));
|
||||
}
|
||||
|
||||
void BufferStream::Seek(int64 pos)
|
||||
{
|
||||
dword size = (dword)GetSize();
|
||||
if(pos > size) {
|
||||
size = (dword)pos;
|
||||
SetSize(size + 100);
|
||||
}
|
||||
ptr = buffer + min(pos, int64(rdlim - buffer));
|
||||
}
|
||||
|
||||
void BufferStream::Open(Vector<byte> & d)
|
||||
{
|
||||
if(&data != &d) data = d; //pick
|
||||
MemStream::Create((byte*)data, data.GetCount());
|
||||
}
|
||||
|
||||
void BufferStream::Create()
|
||||
{
|
||||
data.Clear();
|
||||
Open(data);
|
||||
SetStoring();
|
||||
}
|
||||
|
||||
Vector<byte> BufferStream::GetResult()
|
||||
{
|
||||
data.SetCount((dword)(uintptr_t)(ptr - buffer));
|
||||
Vector<byte> d = data; //pick
|
||||
Create();
|
||||
return d;
|
||||
}
|
||||
34
bazaar/BufferStream/BufferStream.h
Normal file
34
bazaar/BufferStream/BufferStream.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef _BufferStream_BufferStream_h
|
||||
#define _BufferStream_BufferStream_h
|
||||
|
||||
#include <Core/Core.h>
|
||||
|
||||
using namespace Upp;
|
||||
|
||||
class BufferStream : public MemStream {
|
||||
protected:
|
||||
virtual void _Put(int w) { byte h = w; _Put(&h, 1); }
|
||||
virtual void _Put(const void *data, dword size);
|
||||
|
||||
public:
|
||||
virtual void Seek(int64 pos);
|
||||
virtual void SetSize(int64 asize);
|
||||
|
||||
protected:
|
||||
Vector<byte> data;
|
||||
|
||||
public:
|
||||
void Open(Vector<byte> & d);
|
||||
void Create();
|
||||
void Reserve(int n) { SetSize((int)GetSize() + n); }
|
||||
|
||||
Vector<byte> GetResult();
|
||||
operator Vector<byte>() { return GetResult(); }
|
||||
|
||||
BufferStream() { Create(); }
|
||||
BufferStream(Vector<byte>& d) { Open(d); }
|
||||
};
|
||||
|
||||
typedef BufferStream VectorStream;
|
||||
|
||||
#endif
|
||||
11
bazaar/BufferStream/BufferStream.upp
Normal file
11
bazaar/BufferStream/BufferStream.upp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
uses
|
||||
Core;
|
||||
|
||||
file
|
||||
BufferStream.h,
|
||||
BufferStream.cpp,
|
||||
src.tpp;
|
||||
|
||||
mainconfig
|
||||
"" = "";
|
||||
|
||||
4
bazaar/BufferStream/init
Normal file
4
bazaar/BufferStream/init
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
#ifndef _BufferStream_icpp_init_stub
|
||||
#define _BufferStream_icpp_init_stub
|
||||
#include "Core/init"
|
||||
#endif
|
||||
50
bazaar/BufferStream/src.tpp/BufferStream$en-us.tpp
Normal file
50
bazaar/BufferStream/src.tpp/BufferStream$en-us.tpp
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
topic "class BufferStream : public MemStream";
|
||||
[ $$0,0#00000000000000000000000000000000:Default]
|
||||
[i448;a25;kKO9; $$1,0#37138531426314131252341829483380:structitem]
|
||||
[l288;2 $$2,0#27521748481378242620020725143825:desc]
|
||||
[0 $$3,0#96390100711032703541132217272105:end]
|
||||
[H6;0 $$4,0#05600065144404261032431302351956:begin]
|
||||
[i448;a25;kKO9;2 $$5,0#37138531426314131252341829483370:codeitem]
|
||||
[{_}
|
||||
[s1;:BufferStream`:`:class: [@(0.0.255) class]_[* BufferStream]_:_[@(0.0.255) public]_[*@3 Mem
|
||||
Stream]&]
|
||||
[s2;%% A Stream based on a Vector<byte> buffer, beeing able to open
|
||||
such a Vector<byte> to Loading or directly serializing to such
|
||||
a Vector.It automatically increases the underlying Vector. It
|
||||
uses `_pick semantics to open a Vector<byte> and returns the
|
||||
underlying Vector<byte> as `_pick (leaving a new instance under
|
||||
hood, so new buffering is possible as well)&]
|
||||
[s3; &]
|
||||
[s4; &]
|
||||
[s5;:BufferStream`:`:Open`(Vector`<byte`>`&`): [@(0.0.255) void]_[* Open]([_^Vector^ Vector
|
||||
]<[_^byte^ byte]>_`&_[*@3 d])&]
|
||||
[s2;%% Picks the Vector [%-*@3 d].as underlying buffer. doesnt change
|
||||
the current Stream mode, only updates the pointers od the Stream,
|
||||
resetting read position.&]
|
||||
[s3;%% &]
|
||||
[s4; &]
|
||||
[s5;:BufferStream`:`:Create`(`): [@(0.0.255) void]_[* Create]()&]
|
||||
[s2;%% resets underlying buffer and sets Stream to Storing mode.
|
||||
rewinds current ptr to beginning.&]
|
||||
[s3; &]
|
||||
[s4; &]
|
||||
[s5;:BufferStream`:`:Reserve`(int`): [@(0.0.255) void]_[* Reserve]([@(0.0.255) int]_[*@3 n])&]
|
||||
[s2;%% reserves additional [%-*@3 n].bytes in the Stream. sets Storing
|
||||
mode.can speed up things if you know how much is to come.&]
|
||||
[s3;%% &]
|
||||
[s4; &]
|
||||
[s5;:BufferStream`:`:GetResult`(`): [_^Vector^ Vector]<[_^byte^ byte]>_[* GetResult]()&]
|
||||
[s2;%% picks internal Vector, leaving an initialized Vector for new
|
||||
operations.&]
|
||||
[s3; &]
|
||||
[s4; &]
|
||||
[s5;:BufferStream`:`:BufferStream`(`): [* BufferStream]()&]
|
||||
[s2;%% creates a BufferStream in Storing Mode&]
|
||||
[s3; &]
|
||||
[s4; &]
|
||||
[s5;:BufferStream`:`:BufferStream`(Vector`<byte`>`&`): [* BufferStream]([_^Vector^ Vector
|
||||
]<[_^byte^ byte]>`&_[*@3 d])&]
|
||||
[s2;%% Creates a BufferStream in Loading mode. Loads Vector [%-*@3 d].picking
|
||||
it.&]
|
||||
[s3;%% &]
|
||||
[s0; ]
|
||||
51
bazaar/BufferStreamTest/BufferStreamTest.cpp
Normal file
51
bazaar/BufferStreamTest/BufferStreamTest.cpp
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#include "BufferStreamTest.h"
|
||||
|
||||
|
||||
|
||||
CONSOLE_APP_MAIN
|
||||
{
|
||||
Vector<int> vi;
|
||||
Array<int> ai;
|
||||
|
||||
VectorMap<unsigned, int> vmi;
|
||||
ArrayMap<unsigned, int> ami;
|
||||
|
||||
Index<int> vii;
|
||||
ArrayIndex<int> aii;
|
||||
|
||||
int & iv = vi.Add(123);
|
||||
int & ia = ai.Add(123);
|
||||
|
||||
int & imv = vmi.Add(0, 123);
|
||||
int & ima = ami.Add(0, 123);
|
||||
|
||||
int & iiv = vii.Add(123);
|
||||
int & iia = aii.Add(123);
|
||||
|
||||
|
||||
BufferStream vs;
|
||||
|
||||
vs % String("Hallo");
|
||||
for(int i = 0; i < 1000; i++)
|
||||
vs % i;
|
||||
|
||||
Vector<byte> vb;
|
||||
vb = vs.GetResult();
|
||||
|
||||
BufferStream vsd(vb);
|
||||
|
||||
String s;
|
||||
vsd % s;
|
||||
ASSERT(s == "Hallo");
|
||||
|
||||
for(int i = 0; i < 1000; i++)
|
||||
{
|
||||
int ii;
|
||||
vsd % ii;
|
||||
ASSERT(ii == i);
|
||||
int a = 0;
|
||||
}
|
||||
|
||||
int abc = 123;
|
||||
}
|
||||
|
||||
6
bazaar/BufferStreamTest/BufferStreamTest.h
Normal file
6
bazaar/BufferStreamTest/BufferStreamTest.h
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef _ContTest_ContTest_h
|
||||
#define _ContTest_ContTest_h
|
||||
|
||||
#include <BufferStream/BufferStream.h>
|
||||
|
||||
#endif
|
||||
11
bazaar/BufferStreamTest/BufferStreamTest.upp
Normal file
11
bazaar/BufferStreamTest/BufferStreamTest.upp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
uses
|
||||
Core,
|
||||
BufferStream;
|
||||
|
||||
file
|
||||
BufferStreamTest.h,
|
||||
BufferStreamTest.cpp;
|
||||
|
||||
mainconfig
|
||||
"" = "MT";
|
||||
|
||||
5
bazaar/BufferStreamTest/init
Normal file
5
bazaar/BufferStreamTest/init
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#ifndef _BufferStreamTest_icpp_init_stub
|
||||
#define _BufferStreamTest_icpp_init_stub
|
||||
#include "Core/init"
|
||||
#include "BufferStream/init"
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue