nlib
ReallocOutputStream.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_REALLOCOUTPUTSTREAM_H_
4 #define INCLUDE_NN_NLIB_REALLOCOUTPUTSTREAM_H_
5 
6 #include <stdlib.h>
7 
8 #include "nn/nlib/UniquePtr.h"
9 #include "nn/nlib/OutputStream.h"
10 
11 NLIB_NAMESPACE_BEGIN
12 
13 template <class T>
14 class FreeDeleter {
15  public:
16  typedef void (*FreeFunc)(void* ptr);
17  FreeDeleter() NLIB_NOEXCEPT : m_Free(& ::nlib_free) {}
18  explicit FreeDeleter(FreeFunc freefunc) NLIB_NOEXCEPT : m_Free(freefunc) {}
19  template <class U>
20  FreeDeleter(const FreeDeleter<U>& rhs) NLIB_NOEXCEPT {
21  m_Free = rhs.m_Free;
22  }
23  void operator()(T* p) const NLIB_NOEXCEPT {
24  if (p && m_Free) {
25  (*m_Free)(p);
26  }
27  }
28  FreeFunc GetFunc() const NLIB_NOEXCEPT { return m_Free; }
29 
30  private:
31  FreeFunc m_Free;
32 };
33 
34 template <class T>
35 class ReallocDeleter {
36  public:
37  typedef void* (*ReallocFunc)(void* ptr, size_t n);
38  ReallocDeleter() NLIB_NOEXCEPT : m_Realloc(::nlib_realloc) {}
39  explicit ReallocDeleter(ReallocFunc reallocfunc) NLIB_NOEXCEPT : m_Realloc(reallocfunc) {}
40  template <class U>
41  ReallocDeleter(const ReallocDeleter<U>& rhs) NLIB_NOEXCEPT {
42  m_Realloc = rhs.m_Realloc;
43  }
44  void operator()(T* p) const NLIB_NOEXCEPT {
45  if (p && m_Realloc) {
46  (*m_Realloc)(p, 0);
47  }
48  }
49  ReallocFunc GetFunc() const NLIB_NOEXCEPT { return m_Realloc; }
50 
51  private:
52  ReallocFunc m_Realloc;
53 };
54 
55 NLIB_STATIC_ASSERT(!IsEmpty<FreeDeleter<int> >::value && !IsEmpty<ReallocDeleter<int> >::value);
56 
57 #ifdef _MSC_VER
58 class ReallocOutputStream NLIB_FINAL : public OutputStream {
59 #else
61 #endif
62 
63  public:
66  typedef void* (*ReallocFunc)(void* ptr, size_t n);
67 
68  ReallocOutputStream() NLIB_NOEXCEPT : m_Data(), m_Capacity(0) {}
71 
72  uint8_t* Data() NLIB_NOEXCEPT { return m_Data.get(); }
73  const uint8_t* Data() const NLIB_NOEXCEPT { return m_Data.get(); }
74 
75  bool Reserve(size_t n) NLIB_NOEXCEPT { return this->Expand(n) == 0; }
77  if (this->ExpandOrShrink(this->Pos()) == 0) {
78  this->ResetBuffer(m_Data.get() + m_Capacity, 0);
79  }
80  }
81 
82  NLIB_VIS_PUBLIC size_t Release(UniquePtrType* ptr) NLIB_NOEXCEPT;
83  // UniquePtrType Reelase(size_t* n);
84 
85  // Appends '\0' and, returns the memory after ShrinkToFit().
86  NLIB_VIS_PUBLIC bool ReleaseAsCstring(CharPtrType* ptr) NLIB_NOEXCEPT;
87 
88  private:
89  errno_t Expand(size_t newCap) NLIB_NOEXCEPT {
90  return (newCap > m_Capacity) ? this->ExpandOrShrink(newCap) : 0;
91  }
92  NLIB_VIS_PUBLIC errno_t ExpandOrShrink(size_t newCap) NLIB_NOEXCEPT;
93  NLIB_VIS_PUBLIC virtual bool PushBuffer_(const void* p, size_t nBytes,
94  bool doFlush) NLIB_NOEXCEPT NLIB_OVERRIDE;
95  NLIB_VIS_PUBLIC virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE;
96  NLIB_VIS_PUBLIC virtual void* GetWorkBuffer_(size_t* nBytes) NLIB_NOEXCEPT NLIB_OVERRIDE;
97 
98  private:
99  UniquePtrType m_Data;
100  size_t m_Capacity;
101  NLIB_DISALLOW_COPY_AND_ASSIGN(ReallocOutputStream);
102 };
103 
104 NLIB_NAMESPACE_END
105 
106 #endif // INCLUDE_NN_NLIB_REALLOCOUTPUTSTREAM_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
ReallocOutputStream() noexcept
Instantiates the object with default parameters (default constructor). Configures the object to expan...
UniquePtr< char[], ReallocDeleter< char > > CharPtrType
The typedefed UniquePtr to a char array.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:66
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:126
The class for output streams written to memory expanded using nlib_realloc or another realloc functio...
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
Definition: UniquePtr.h:96
Defines that class that is corresponding to std::unique_ptr.
void ShrinkToFit() noexcept
Makes the buffer size fit to the current write position.
UniquePtr< uint8_t[], ReallocDeleter< uint8_t > > UniquePtrType
The typedefed UniquePtr to a uint8_t array.
NLIB_CHECK_RESULT void * nlib_realloc(void *ptr, size_t size)
A weak function that calls the C standard function realloc. nlib calls realloc via this function...
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.
Definition: Config.h:117
void nlib_free(void *ptr)
A weak function that calls the C standard function free. nlib calls free via this function...
bool Reserve(size_t n) noexcept
Allocates a buffer of the specified size for writing to.
Defines the base class for output streams.
The base class for output streams. This class cannot be instantiated.
Definition: OutputStream.h:17
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24