Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » PROPOSAL: small / usefull Stream iface extension
icon3.gif  PROPOSAL: small / usefull Stream iface extension [message #27952] Wed, 11 August 2010 23:39 Go to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
hi all,

when implementing own, nontrivial protocols using stream, one sometimes (like i am now) in trouble, needing to access the underlying buffer directly, without always triggering a complete copy.. and also beeing able to skip read/write things.
thus here some very small but usefull functions. it would be really cool to have them there. otherwise please comment why not and how could i circumvent this need, without copying over and over.. (imagine having to put packets there that have some data, over which to calculate crc..etc).

Stream.h:85
	byte *    Base()                 { return buffer; }
	byte *    Head()                 { return ptr; }
	void      SkipRead(dword size = 1) { ptr += min((dword)(uintptr_t)(rdlim - ptr), size); }
	void      SkipWrite(dword size = 1){ ptr += min((dword)(uintptr_t)(wrlim - ptr), size); }


Head is the most important for me..
SkipRead makes sense as well (ignoring stuff, without need of dummy-copying just to advance ptr)
SkipWrite is kind of just for symetrical completeness, but might be usefull somewhere..

[Updated on: Wed, 11 August 2010 23:47]

Report message to a moderator

Re: PROPOSAL: small / usefull Stream iface extension [message #27953 is a reply to message #27952] Thu, 12 August 2010 00:14 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
and some 2 more..


	void      PutReserve(byte *& p, dword size = 1) { if(ptr + size <= wrlim) { p = ptr; ptr += size; } else p = NULL; }
	dword     GetReserve(byte *& p, dword size = 1) { if(ptr + size <= rdlim) { p = ptr; ptr += size; return size; } 
														else { dword s = dword((uintptr_t)(rdlim - ptr)); return ((s>0)?(p=ptr,s):(0)); } }



could maybe be named PutShadow, GetShadow, or something, to indicate that the buffer is refered only, no memcpy..
this is usefull to modify the data underneeth directly.
Re: PROPOSAL: small / usefull Stream iface extension [message #27961 is a reply to message #27953] Thu, 12 August 2010 08:36 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
the use case of this is the following:

when coding or especially decoding some nontrivial protocols from a Stream (beeing serialized by another device actually, coming in serially) one needs to 'interpret' the data being made available in the stream, instead of consuming it. only if the paket is 'valid' in total, it may be consumed. if not, one may drop one byte and start 'interprete' again. if not all data is available yet one may postpone processing..without copying over and over again..

all this is not possible without accessing the data directly. otherwise a rebuffering would be nesseccary, which is not very well.

trying to avoid copying / reallocating stuff..(will probably run on an embedded system, whithout MPU / MMU (blackfin), which doesnt like a lot of new / delete)
Re: PROPOSAL: small / usefull Stream iface extension [message #28002 is a reply to message #27961] Fri, 13 August 2010 09:57 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Sorry, but this is poorly defined...

IMO, you should trust more to Seek. I believe that if you reimplement your code with Seek and regular stream interface, I believe it will be as effective as using proposed methods. Well, maybe just almost as effective..

Well, MAYBE we could add something like:

const byte *Peek(int size)

which would return NULL if size is not available...

In any case, SkipRead and SkipWrite do not offer any advantage over Seek IMO.
Re: PROPOSAL: small / usefull Stream iface extension [message #28004 is a reply to message #28002] Fri, 13 August 2010 10:07 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
Quote:


Sorry, but this is poorly defined...


thats why i'm asking people who know better Smile

would Peek return the underlying buffer? at ptr position?
then i'm fine with it and can tweak around with Seek ofcorse. indeed, Peek is quite a usefull function, anyway. in fact i prefer it, it keeps the API understandable, not too implementation bound, but usage bound. good idea.

SkipRead / SkipWrite are only helpers, and are not that much nessesary actually, neither my second post. one could would workaround it sth like
Seek(GetPos() + count);


Peek would need to deferentiate wrlim and rdlim right?
why const btw?
const byte * Peek(int size);


my point is solely driven by avoiding unwanted memcpy, thats why accessing buffer directly to perform tweaked read /write (some special communication protocol)

[Updated on: Fri, 13 August 2010 10:16]

Report message to a moderator

Re: PROPOSAL: small / usefull Stream iface extension [message #28006 is a reply to message #28004] Fri, 13 August 2010 10:23 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
another proposal, maybe this one's better...

	const byte * Peek(int size = 1)  { if(size <= 0) return NULL; if(ptr + size <= rdlim) return ptr; return NULL; }
	byte *    Head(int size = 1)     { if(size <= 0) return NULL; if(ptr + size <= wrlim) return ptr; return NULL; }
Re: PROPOSAL: small / usefull Stream iface extension [message #28008 is a reply to message #28004] Fri, 13 August 2010 10:29 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
kohait00 wrote on Fri, 13 August 2010 04:07

Quote:


Sorry, but this is poorly defined...


thats why i'm asking people who know better Smile

would Peek return the underlying buffer? at ptr position?
then i'm fine with it and can tweak around with Seek ofcorse. indeed, Peek is quite a usefull function, anyway. in fact i prefer it, it keeps the API understandable, not too implementation bound, but usage bound. good idea.

SkipRead / SkipWrite are only helpers, and are not that much nessesary actually, neither my second post. one could would workaround it sth like
Seek(GetPos() + count);




Which is in the interface as SeekCur.

Quote:


Peek would need to deferentiate wrlim and rdlim right?
why const btw?
const byte * Peek(int size);




Sure, only read variant.
Re: PROPOSAL: small / usefull Stream iface extension [message #28009 is a reply to message #28006] Fri, 13 August 2010 10:30 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
kohait00 wrote on Fri, 13 August 2010 04:23

another proposal, maybe this one's better...

	const byte * Peek(int size = 1)  { if(size <= 0) return NULL; if(ptr + size <= rdlim) return ptr; return NULL; }
	byte *    Head(int size = 1)     { if(size <= 0) return NULL; if(ptr + size <= wrlim) return ptr; return NULL; }



For some reasons, write variant sounds somewhat dangerous to me...

Have to think about this one...

Well, in any case, it should advance the ptr...

[Updated on: Fri, 13 August 2010 10:31]

Report message to a moderator

Re: PROPOSAL: small / usefull Stream iface extension [message #28011 is a reply to message #28009] Fri, 13 August 2010 10:46 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
OK, here is what we got:

	const byte *Peek(int size = 1)   { ASSERT(size > 0); return ptr + size <= rdlim ? ptr : NULL; }
	byte       *PutPtr(int size = 1) { ASSERT(size > 0); if(ptr + size <= wrlim) { byte *p = ptr; ptr += size; return p; }; return NULL; }

Re: PROPOSAL: small / usefull Stream iface extension [message #28012 is a reply to message #28009] Fri, 13 August 2010 10:47 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
another one...
	const byte * Peek(int size = 1)  { if(size <= 0 || (ptr + size > rdlim)) return NULL; return ptr; }
	byte *    PutReserve(int size)   { if(size <= 0 || (ptr + size > wrlim)) return NULL; byte * p = ptr; ptr+= size; return p; }


concerning Head():
if anyone really wants to break things its even possible with Peek then (brutal cast away const). those who really use these options are aware of it and use it with caution.

Peek shouldnt advance ptr. Peek only peeks Smile advancing if needed should be done with Seek(GetPos() + size) afterwards, if desired.

advancing ptr in Head is a protection though, that same buffer section is not returned twice.. but the name is maybe irritating.

[Updated on: Fri, 13 August 2010 10:49]

Report message to a moderator

Re: PROPOSAL: small / usefull Stream iface extension [message #28014 is a reply to message #28012] Fri, 13 August 2010 10:50 Go to previous message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
this was work in paralell Very Happy
i like your option..

my above version were buggy btw Smile p+=size...

[Updated on: Fri, 13 August 2010 10:52]

Report message to a moderator

Previous Topic: NEW: Tree<T> container
Next Topic: better Stream manual....more info needed
Goto Forum:
  


Current Time: Mon Apr 29 13:10:35 CEST 2024

Total time taken to generate the page: 0.02844 seconds