openshot-audio  0.1.4
juce_ArrayAllocationBase.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_ARRAYALLOCATIONBASE_H_INCLUDED
30 #define JUCE_ARRAYALLOCATIONBASE_H_INCLUDED
31 
32 
33 //==============================================================================
45 template <class ElementType, class TypeOfCriticalSectionToUse>
46 class ArrayAllocationBase : public TypeOfCriticalSectionToUse
47 {
48 public:
49  //==============================================================================
52  : numAllocated (0)
53  {
54  }
55 
58  {
59  }
60 
61  #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
63  : elements (static_cast <HeapBlock <ElementType>&&> (other.elements)),
64  numAllocated (other.numAllocated)
65  {
66  }
67 
69  {
70  elements = static_cast <HeapBlock <ElementType>&&> (other.elements);
71  numAllocated = other.numAllocated;
72  return *this;
73  }
74  #endif
75 
76  //==============================================================================
84  void setAllocatedSize (const int numElements)
85  {
86  if (numAllocated != numElements)
87  {
88  if (numElements > 0)
89  elements.realloc ((size_t) numElements);
90  else
91  elements.free();
92 
93  numAllocated = numElements;
94  }
95  }
96 
105  void ensureAllocatedSize (const int minNumElements)
106  {
107  if (minNumElements > numAllocated)
108  setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
109 
110  jassert (numAllocated <= 0 || elements != nullptr);
111  }
112 
116  void shrinkToNoMoreThan (const int maxNumElements)
117  {
118  if (maxNumElements < numAllocated)
119  setAllocatedSize (maxNumElements);
120  }
121 
124  {
125  elements.swapWith (other.elements);
126  std::swap (numAllocated, other.numAllocated);
127  }
128 
129  //==============================================================================
132 
133 private:
135 };
136 
137 
138 #endif // JUCE_ARRAYALLOCATIONBASE_H_INCLUDED
void setAllocatedSize(const int numElements)
Definition: juce_ArrayAllocationBase.h:84
#define noexcept
Definition: juce_CompilerSupport.h:141
Definition: juce_ArrayAllocationBase.h:46
void swapWith(HeapBlock< ElementType, otherBlockThrows > &other) noexcept
Definition: juce_HeapBlock.h:275
void realloc(const size_t newNumElements, const size_t elementSize=sizeof(ElementType))
Definition: juce_HeapBlock.h:255
void shrinkToNoMoreThan(const int maxNumElements)
Definition: juce_ArrayAllocationBase.h:116
ArrayAllocationBase() noexcept
Definition: juce_ArrayAllocationBase.h:51
~ArrayAllocationBase() noexcept
Definition: juce_ArrayAllocationBase.h:57
HeapBlock< ElementType > elements
Definition: juce_ArrayAllocationBase.h:130
#define jassert(a)
Definition: juce_PlatformDefs.h:146
#define JUCE_DECLARE_NON_COPYABLE(className)
Definition: juce_PlatformDefs.h:191
void swapWith(ArrayAllocationBase< ElementType, TypeOfCriticalSectionToUse > &other) noexcept
Definition: juce_ArrayAllocationBase.h:123
void ensureAllocatedSize(const int minNumElements)
Definition: juce_ArrayAllocationBase.h:105
void free() noexcept
Definition: juce_HeapBlock.h:265
int numAllocated
Definition: juce_ArrayAllocationBase.h:131