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 
27  public:
28  class NLIB_VIS_PUBLIC ReallocDeleter {
29  public:
30  typedef void* (*ReallocFunc)(void* ptr, size_t n) NLIB_NOEXCEPT_FUNCPTR;
31  ReallocDeleter() NLIB_NOEXCEPT : realloc_(::nlib_realloc) {}
32  explicit ReallocDeleter(ReallocFunc reallocfunc) NLIB_NOEXCEPT : realloc_(reallocfunc) {}
33  void operator()(void* p) const NLIB_NOEXCEPT;
34  ReallocFunc GetFunc() const NLIB_NOEXCEPT { return realloc_; }
35 
36  private:
37  ReallocFunc realloc_;
38  };
39  NLIB_STATIC_ASSERT(!IsEmpty<ReallocDeleter>::value);
42  typedef void* (*ReallocFunc)(void* ptr, size_t n) NLIB_NOEXCEPT_FUNCPTR;
43 
45  : data_(NULL), realloc_func_(nlib_realloc), capacity_(0) {}
46  explicit ReallocOutputStream(ReallocFunc func) NLIB_NOEXCEPT NLIB_NONNULL
47  : data_(NULL), realloc_func_(func), capacity_(0) {}
49 
50  uint8_t* Data() NLIB_NOEXCEPT { return data_; }
51  const uint8_t* Data() const NLIB_NOEXCEPT { return data_; }
52 
53  bool Reserve(size_t n) NLIB_NOEXCEPT { return this->Expand(n) == 0; }
55  if (this->ExpandOrShrink(this->Pos()) == 0) {
56  this->ResetBuffer(data_ + capacity_, 0);
57  }
58  }
59 
60  size_t Release(UniquePtrType* ptr) NLIB_NOEXCEPT;
61  // UniquePtrType Reelase(size_t* n);
62 
63  // Appends '\0' and, returns the memory after ShrinkToFit().
64  bool ReleaseAsCstring(CharPtrType* ptr) NLIB_NOEXCEPT;
65 
66  private:
67  errno_t Expand(size_t newcap) NLIB_NOEXCEPT {
68  return (newcap > capacity_) ? this->ExpandOrShrink(newcap) : 0;
69  }
70  errno_t ExpandOrShrink(size_t newcap) NLIB_NOEXCEPT;
71  virtual bool PushBuffer_(const void* p, size_t nbytes,
72  bool do_flush) NLIB_NOEXCEPT NLIB_OVERRIDE;
73  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE;
74  virtual void* GetWorkBuffer_(size_t* nbytes) NLIB_NOEXCEPT NLIB_OVERRIDE;
75 
76  private:
77  uint8_t* data_;
78  ReallocFunc realloc_func_;
79  size_t capacity_;
81 };
82 
83 NLIB_NAMESPACE_END
84 
85 #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:228
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:163
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:109
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:89
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:99
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:229
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.
Definition: Config.h:154
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