[haiku-commits] Re: BRANCH midar-github.master - headers/os/support

  • From: Stephan Aßmus <superstippi@xxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 20 Nov 2012 16:15:06 +0100

Am 20.11.2012 um 15:15 schrieb midar-github.master <community@xxxxxxxxxxxx>:

> added 1 changeset to branch 'refs/remotes/midar-github/master'
> old head: e9f3fef7c135d49dc8587a1cd92225fbacd37fd1
> new head: 12d7885725d7a83341021e4f9823bbb98889f65a
> overview: https://github.com/Midar/haiku/compare/e9f3fef...12d7885
> 
> ----------------------------------------------------------------------------
> 
> 12d7885: Add class BStackOrHeapArray.
> 
>                                     [ Jonathan Schleifer <js@xxxxxxxxxxx> ]
> 
> ----------------------------------------------------------------------------
> 
> Commit:      12d7885725d7a83341021e4f9823bbb98889f65a
> Author:      Jonathan Schleifer <js@xxxxxxxxxxx>
> Date:        Tue Nov 20 14:14:40 2012 UTC
> 
> ----------------------------------------------------------------------------
> 
> 1 file changed, 43 insertions(+)
> headers/os/support/StackOrHeapArray.h | 43 +++++++++++++++++++++++++++++++
> 
> ----------------------------------------------------------------------------
> 
> diff --git a/headers/os/support/StackOrHeapArray.h 
> b/headers/os/support/StackOrHeapArray.h
> new file mode 100644
> index 0000000..81cc8b8
> --- /dev/null
> +++ b/headers/os/support/StackOrHeapArray.h
> @@ -0,0 +1,43 @@
> +/*
> + * Copyright 2012, Jonathan Schleifer <js@xxxxxxxxxxx>. All Rights Reserved.
> + * Distributed under the terms of the MIT License.
> + */
> +#ifndef      _SUPPORT_STACKORHEAPARRAY_H
> +#define      _SUPPORT_STACKORHEAPARRAY_H
> +
> +template <typename T, int C>

Maybe "Type" and "StackSize"?

> +class BStackOrHeapArray
> +{

Would need to check, but I think 

> +private:
> +     T stackData[C];
> +     T *data;

Should be fStackData and fData or perhaps even fStackOrHeapData.

> +public:
> +     BStackOrHeapArray(unsigned long count)
> +     {
> +             if (count >= C)
> +                     data = new(std::nothrow) T[count];
> +             else
> +                     data = stackData;
> +     }

If you use (std::nothrow), then new is supposed to return NULL in case of no 
memory. So a method "bool IsValid() const { return fData != NULL; }" could be 
added, like in my initial proposal.

> +     ~BStackOrHeapArray()
> +     {
> +             if (data != stackData)
> +                     delete[] data;
> +     }
> +
> +     T&
> +     operator [](unsigned long index)
> +     {
> +             return data[index];
> +     }
> +
> +     T*
> +     operator ()()
> +     {
> +             return data;
> +     }
> +};
> 

Probably more convenient to use than my proposed T* Array() method, but I sort 
of avoid that stuff, since it hides a bit more and may leave someone wondering. 
At least those self-taught types like yours truly. :-)

Thanks for making use of my proposal so quickly!

Best regards,
-Stephan


Other related posts: