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 » Developing U++ » U++ Developers corner » Initialization for Buffer<T>
Initialization for Buffer<T> [message #45090] Sat, 29 August 2015 18:41 Go to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Buffer<T> is known as replacement for C-style arrays. So I guess it needs some way to fill it as easy as possible (like we initialize arrays in C). So here is my proposal with little additions:
template <class T>
class Buffer : Moveable< Buffer<T> > {
	size_t size;
	mutable T *ptr;
	int insertI;

public:
	operator T*()                        { return ptr; }
	operator const T*() const            { return ptr; }
	T *operator~()                       { return ptr; }
	const T *operator~() const           { return ptr; }

	Buffer<T> & Alloc(size_t _size)              { Clear(); ptr = new T[_size]; size = _size; return *this; }
	Buffer<T> & Alloc(size_t _size, const T& in) { Clear(); ptr = new T[_size]; size = _size; Fill(ptr, ptr + size, in); return *this; }

	void Clear()                         { if(ptr) delete[] ptr; ptr = NULL; insertI = 0; size = 0; }
	size_t GetCount()                    { return size; }

	Buffer()                             { Init(); ptr = NULL; }
	Buffer(size_t size)                  { Init(); ptr = new T[size]; }
	Buffer(size_t size, const T& init)   { Init(); ptr = new T[size]; Fill(ptr, ptr + size, init); }
	~Buffer()                            { if(ptr) delete[] ptr; }
	
	void Init()                         { size = 0; insertI = 0; }
	void operator=(Buffer rval_ v)      { if(ptr) delete[] ptr; ptr = v.ptr; v.ptr = NULL; }
	Buffer<T> & operator<< (const T &in){ ptr[insertI++] = in; return *this; }
	Buffer(Buffer rval_ v)              { ptr = v.ptr; v.ptr = NULL; size = v.size; v.size=0; insertI = 0; }
};

// Usage:
// buffer.Alloc(3) << v1 << v2 << v3;

[Updated on: Sat, 29 August 2015 23:10]

Report message to a moderator

Re: Initialization for Buffer<T> [message #45093 is a reply to message #45090] Sun, 30 August 2015 07:38 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Mindtraveller wrote on Sat, 29 August 2015 18:41
Buffer<T> is known as replacement for C-style arrays. So I guess it needs some way to fill it as easy as possible (like we initialize arrays in C). So here is my proposal with little additions:
template <class T>
class Buffer : Moveable< Buffer<T> > {
	size_t size;
	mutable T *ptr;
	int insertI;

public:
	operator T*()                        { return ptr; }
	operator const T*() const            { return ptr; }
	T *operator~()                       { return ptr; }
	const T *operator~() const           { return ptr; }

	Buffer<T> & Alloc(size_t _size)              { Clear(); ptr = new T[_size]; size = _size; return *this; }
	Buffer<T> & Alloc(size_t _size, const T& in) { Clear(); ptr = new T[_size]; size = _size; Fill(ptr, ptr + size, in); return *this; }

	void Clear()                         { if(ptr) delete[] ptr; ptr = NULL; insertI = 0; size = 0; }
	size_t GetCount()                    { return size; }

	Buffer()                             { Init(); ptr = NULL; }
	Buffer(size_t size)                  { Init(); ptr = new T[size]; }
	Buffer(size_t size, const T& init)   { Init(); ptr = new T[size]; Fill(ptr, ptr + size, init); }
	~Buffer()                            { if(ptr) delete[] ptr; }
	
	void Init()                         { size = 0; insertI = 0; }
	void operator=(Buffer rval_ v)      { if(ptr) delete[] ptr; ptr = v.ptr; v.ptr = NULL; }
	Buffer<T> & operator<< (const T &in){ ptr[insertI++] = in; return *this; }
	Buffer(Buffer rval_ v)              { ptr = v.ptr; v.ptr = NULL; size = v.size; v.size=0; insertI = 0; }
};

// Usage:
// buffer.Alloc(3) << v1 << v2 << v3;


Adding 2 new member variable seems quite wasteful.

However, I am now in process of adding C++11 std::initializer_list to all containers. Without your post, I would probably forgot about Buffer. With C++11, we can now have

Buffer<int> x{ v1, v2, v3 };

x = { v4, v5, v6 };

Re: Initialization for Buffer<T> [message #45094 is a reply to message #45093] Sun, 30 August 2015 11:10 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

This is of corse more convenient than "classic" U++ approach with operator<<(), but what should one do if he's developing i.e. for embedded system with no C++11 support in compiler?
Re: Initialization for Buffer<T> [message #45095 is a reply to message #45094] Sun, 30 August 2015 11:20 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Mindtraveller wrote on Sun, 30 August 2015 11:10
This is of corse more convenient than "classic" U++ approach with operator<<(), but what should one do if he's developing i.e. for embedded system with no C++11 support in compiler?


Actually, the only problem for C++11 was windows. It is now solved (with VS2015).

Second problem can be some old linux distro - still can be solved by compiling new GCC on it.

Embedded systems would worry me least - all of them are using GCC, so they are C++11 for quite a long time now...

For upcoming release, C++11 will not be required, but supported. I think this issue is exactly what should be resolved with C++11 features intended for it... Smile

Mirek
Re: Initialization for Buffer<T> [message #45209 is a reply to message #45095] Tue, 06 October 2015 12:30 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

After some experience with Buffer, I still propose adding GetCount() member. It would be convenient.
Re: Initialization for Buffer<T> [message #45216 is a reply to message #45209] Thu, 08 October 2015 09:19 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
If you need GetCount, why not use Vector?
Re: Initialization for Buffer<T> [message #45217 is a reply to message #45216] Thu, 08 October 2015 14:01 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

I can not guarantee that elements are situated in Vector just like in c-style array. Event if it current;y implemented this way, noone will guarantee it will be implemented this way in the future. So I thought you implemented Buffer specially for cases we need c-style array.
Re: Initialization for Buffer<T> [message #45221 is a reply to message #45217] Fri, 09 October 2015 17:34 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Actually, it is guaranteed.

typedef T *Iterator

Iterator type. Iterator is guaranteed to be of T* type.

operator T*()
Returns non-constant pointer to elements.


operator const T*() const
Returns constant pointer to elements.

I agree that more clearly worded guarantee should be given here (this is sort of implicit). Updating docs now...

Buffer indeed is replacement of situation

byte *buffer = new byte[N];
...
delete[] buffer;

but adding GetCount() would mean it is less efficient that this raw code. In the situation when you need GetCount, overhead of Vector is so small that you should perhaps use it instead (it only stores 'alloc' in addition to 'count').

[Updated on: Fri, 09 October 2015 17:38]

Report message to a moderator

Re: Initialization for Buffer<T> [message #45226 is a reply to message #45221] Sat, 10 October 2015 22:26 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Thank you for detailed answer. Still after these years of using U++, I suppose I have to start making sime kind of guide or manual for U++. Because there are so many features and factors which are almsot undocumented. It would be good to have them documented. I see it as more structured version of your Help Tutorials (Core, GUI, etc.)

[Updated on: Sat, 10 October 2015 22:27]

Report message to a moderator

Re: Initialization for Buffer<T> [message #45227 is a reply to message #45226] Sun, 11 October 2015 12:56 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Mindtraveller wrote on Sat, 10 October 2015 22:26
Thank you for detailed answer. Still after these years of using U++, I suppose I have to start making sime kind of guide or manual for U++. Because there are so many features and factors which are almsot undocumented. It would be good to have them documented. I see it as more structured version of your Help Tutorials (Core, GUI, etc.)


Well, my fault. While I think reference docs is quite complete these days, some basic ideas are hidden in details. Plus, some issues are evolving over time, so some of docs can be misleading these days...
Previous Topic: [Solved] template.h errors when porting from 2010 source to 2012
Next Topic: How to fix this strong behaviore of MSC compiler & temporary value ?
Goto Forum:
  


Current Time: Thu Mar 28 22:36:42 CET 2024

Total time taken to generate the page: 0.01649 seconds