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 
14  public:
15  class NLIB_VIS_PUBLIC ReallocDeleter {
16  public:
17  typedef void* (*ReallocFunc)(void* ptr, size_t n);
18  ReallocDeleter() NLIB_NOEXCEPT : realloc_(::nlib_realloc) {}
19  explicit ReallocDeleter(ReallocFunc reallocfunc) NLIB_NOEXCEPT : realloc_(reallocfunc) {}
20  void operator()(void* p) const NLIB_NOEXCEPT;
21  ReallocFunc GetFunc() const NLIB_NOEXCEPT { return realloc_; }
22 
23  private:
24  ReallocFunc realloc_;
25  };
26  NLIB_STATIC_ASSERT(!IsEmpty<ReallocDeleter>::value);
29  typedef void* (*ReallocFunc)(void* ptr, size_t n);
30 
32  : data_(NULL), realloc_func_(nlib_realloc), capacity_(0) {}
33  explicit ReallocOutputStream(ReallocFunc func) NLIB_NOEXCEPT NLIB_NONNULL
34  : data_(NULL), realloc_func_(func), capacity_(0) {}
36 
37  uint8_t* Data() NLIB_NOEXCEPT { return data_; }
38  const uint8_t* Data() const NLIB_NOEXCEPT { return data_; }
39 
40  bool Reserve(size_t n) NLIB_NOEXCEPT { return this->Expand(n) == 0; }
42  if (this->ExpandOrShrink(this->Pos()) == 0) {
43  this->ResetBuffer(data_ + capacity_, 0);
44  }
45  }
46 
47  size_t Release(UniquePtrType* ptr) NLIB_NOEXCEPT;
48  // UniquePtrType Reelase(size_t* n);
49 
50  // Appends '\0' and, returns the memory after ShrinkToFit().
51  bool ReleaseAsCstring(CharPtrType* ptr) NLIB_NOEXCEPT;
52 
53  private:
54  errno_t Expand(size_t newcap) NLIB_NOEXCEPT {
55  return (newcap > capacity_) ? this->ExpandOrShrink(newcap) : 0;
56  }
57  errno_t ExpandOrShrink(size_t newcap) NLIB_NOEXCEPT;
58  virtual bool PushBuffer_(const void* p, size_t nbytes,
59  bool do_flush) NLIB_NOEXCEPT NLIB_OVERRIDE;
60  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE;
61  virtual void* GetWorkBuffer_(size_t* nbytes) NLIB_NOEXCEPT NLIB_OVERRIDE;
62 
63  private:
64  uint8_t* data_;
65  ReallocFunc realloc_func_;
66  size_t capacity_;
68 };
69 
70 NLIB_NAMESPACE_END
71 
72 #endif // INCLUDE_NN_NLIB_REALLOCOUTPUTSTREAM_H_
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
Definition: Config.h:210
ReallocOutputStream() noexcept
Instantiates the object with default parameters (default constructor). Configures the object to expan...
UniquePtr< char[], ReallocDeleter > CharPtrType
The typedefed UniquePtr to a char array.
#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:145
The class for output streams written to memory expanded using nlib_realloc or another realloc functio...
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.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
UniquePtr< uint8_t[], ReallocDeleter > UniquePtrType
The typedefed UniquePtr to a uint8_t array.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
uint8_t * Data() noexcept
Gets the pointer to the start of the data that is in the process of being written.
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...
ReallocOutputStream(ReallocFunc func) noexcept
Specifies a realloc function and instantiates an object.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.
Definition: Config.h:136
bool Reserve(size_t n) noexcept
Allocates a buffer of the specified size for writing to.
Defines the base class for output streams.
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:76
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