|
|
Home » Developing U++ » U++ Developers corner » Initialization for Buffer<T>
Initialization for Buffer<T> [message #45090] |
Sat, 29 August 2015 18:41 |
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 |
|
mirek
Messages: 14039 Registered: November 2005
|
Ultimate Member |
|
|
Mindtraveller wrote on Sat, 29 August 2015 18:41Buffer<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 #45221 is a reply to message #45217] |
Fri, 09 October 2015 17:34 |
|
mirek
Messages: 14039 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 |
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
|
|
|
|
Goto Forum:
Current Time: Fri Sep 20 11:20:39 CEST 2024
Total time taken to generate the page: 0.07913 seconds
|
|
|