nlib
ReallocOutputStream.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_REALLOCOUTPUTSTREAM_H_
17 #define INCLUDE_NN_NLIB_REALLOCOUTPUTSTREAM_H_
18 
19 #include <stdlib.h>
20 
21 #include "nn/nlib/UniquePtr.h"
22 #include "nn/nlib/OutputStream.h"
23 
24 NLIB_NAMESPACE_BEGIN
25 
26 class NLIB_VIS_PUBLIC ReallocDeleter {
27  public:
28  typedef void* (*ReallocFunc)(void* ptr, size_t n)NLIB_NOEXCEPT_FUNCPTR;
29  ReallocDeleter() NLIB_NOEXCEPT : realloc_(::nlib_realloc) {}
30  explicit ReallocDeleter(ReallocFunc reallocfunc) NLIB_NOEXCEPT : realloc_(reallocfunc) {}
31  void operator()(void* p) const NLIB_NOEXCEPT;
32  ReallocFunc GetFunc() const NLIB_NOEXCEPT { return realloc_; }
33 
34  private:
35  ReallocFunc realloc_;
36 };
37 
39  public:
42  typedef void* (*ReallocFunc)(void* ptr, size_t n)NLIB_NOEXCEPT_FUNCPTR;
43 
44  ReallocOutputStream() NLIB_NOEXCEPT : data_(nullptr),
45  realloc_func_(nlib_realloc),
46  capacity_(0) {
47  NLIB_STATIC_ASSERT(!IsEmpty<ReallocDeleter>::value);
48  }
49  explicit ReallocOutputStream(ReallocFunc func) NLIB_NOEXCEPT NLIB_NONNULL : data_(nullptr),
50  realloc_func_(func),
51  capacity_(0) {}
53 
54  uint8_t* Data() NLIB_NOEXCEPT { return data_; }
55  const uint8_t* Data() const NLIB_NOEXCEPT { return data_; }
56 
57  bool Reserve(size_t n) NLIB_NOEXCEPT { return this->Expand(n) == 0; }
59  if (this->ExpandOrShrink(this->Pos()) == 0) {
60  this->ResetBuffer(data_ + capacity_, 0);
61  }
62  }
63 
64  size_t Release(UniquePtrType* ptr) NLIB_NOEXCEPT;
65 #ifdef __cpp_rvalue_references
66  std::pair<UniquePtrType, size_t> Release() NLIB_NOEXCEPT;
67 #endif
68 
69  // Appends '\0' and, returns the memory after ShrinkToFit().
70  bool ReleaseAsCstring(CharPtrType* ptr) NLIB_NOEXCEPT;
71 #ifdef __cpp_rvalue_references
72  CharPtrType ReleaseAsCstring() NLIB_NOEXCEPT;
73 #endif
74 
75  private:
76  errno_t Expand(size_t newcap) NLIB_NOEXCEPT {
77  return (newcap > capacity_) ? this->ExpandOrShrink(newcap) : 0;
78  }
79  errno_t ExpandOrShrink(size_t newcap) NLIB_NOEXCEPT;
80  virtual bool
81  PushBuffer_(const void* p, size_t nbytes, bool do_flush) NLIB_NOEXCEPT NLIB_OVERRIDE;
82  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE;
83  virtual void* GetWorkBuffer_(size_t* nbytes) NLIB_NOEXCEPT NLIB_OVERRIDE;
84 
85  private:
86  uint8_t* data_;
87  ReallocFunc realloc_func_;
88  size_t capacity_;
89  NLIB_DISALLOW_COPY_AND_ASSIGN(ReallocOutputStream);
90 };
91 
92 NLIB_NAMESPACE_END
93 
94 #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:249
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:183
The class for output streams written to memory expanded using nlib_realloc or another realloc functio...
size_t Pos() const noexcept
Returns the current position in the stream.
Definition: OutputStream.h:51
In the C++11 environment (which supports alias templates), std::unique_ptr is made an alias template...
Definition: UniquePtr.h:108
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:87
const uint8_t * Data() const noexcept
The const decoration version of the above function.
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:109
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.
void ResetBuffer(void *p, size_t nbytes) noexcept
Sets the buffer held by OutputStream.
Definition: OutputStream.h:107
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:250
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.
Definition: Config.h:174
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.
The base class for output streams. This class cannot be instantiated.
Definition: OutputStream.h:30
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37