|
/* |
|
* Copyright (c) 2018 easyaspi314 |
|
* |
|
* Permission is hereby granted, free of charge, to any person |
|
* obtaining a copy of this software and associated documentation |
|
* files (the "Software"), to deal in the Software without |
|
* restriction, including without limitation the rights to use, |
|
* copy, modify, merge, publish, distribute, sublicense, and/or |
|
* sell copies of the Software, and to permit persons to whom the |
|
* Software is furnished to do so, subject to the following |
|
* conditions: |
|
* |
|
* The above copyright notice and this permission notice shall be |
|
* included in all copies or substantial portions of the Software. |
|
* |
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
* OTHER DEALINGS IN THE SOFTWARE. |
|
*/ |
|
|
|
#ifndef VOID_PTR_H |
|
#define VOID_PTR_H 1 |
|
|
|
#include <stdlib.h> |
|
|
|
#ifdef __cplusplus |
|
#include <utility> |
|
|
|
class VoidPtr |
|
{ |
|
void *ptr; |
|
public: |
|
/* use like malloc */ |
|
inline explicit VoidPtr(size_t count) : ptr(malloc(count)) {} |
|
|
|
/* Wrapping a pointer */ |
|
template <typename T> |
|
inline constexpr VoidPtr(T *data) : ptr(data) {} |
|
|
|
inline constexpr VoidPtr(std::nullptr_t ) : ptr(nullptr) {} |
|
|
|
/* The real magic. This lets it silently convert to any pointer you wish. */ |
|
template <typename T> |
|
inline constexpr operator T *() const { |
|
return reinterpret_cast<T *>(ptr); |
|
} |
|
|
|
/* Comparing to other pointers */ |
|
template <typename T> |
|
inline constexpr bool operator==(T *other) const { return other == reinterpret_cast<T *>(ptr); } |
|
template <typename T> |
|
inline constexpr bool operator!=(T *other) const { return other != reinterpret_cast<T *>(ptr); } |
|
|
|
/* basic comparisons */ |
|
inline constexpr bool operator==(std::nullptr_t) const { return ptr == nullptr; } |
|
inline constexpr bool operator!=(std::nullptr_t) const { return ptr != nullptr; } |
|
inline constexpr bool operator==(const VoidPtr &other) const { return ptr == other.ptr; } |
|
inline constexpr bool operator!=(const VoidPtr &other) const { return ptr != other.ptr; } |
|
inline bool operator==(size_t addr) const { return (size_t)ptr == addr; } |
|
inline bool operator!=(size_t addr) const { return (size_t)ptr != addr; } |
|
|
|
/* if (ptr) */ |
|
inline constexpr operator bool() const { return ptr != nullptr; } |
|
|
|
inline VoidPtr &reallocate(size_t newlen) { |
|
ptr = realloc(ptr, newlen); |
|
return *this; |
|
} |
|
}; |
|
|
|
inline VoidPtr &&c_malloc(size_t count) { |
|
return std::move(VoidPtr(count)); |
|
} |
|
inline VoidPtr &&c_realloc(VoidPtr ptr, size_t newsize) { |
|
return std::move(ptr.reallocate(newsize)); |
|
} |
|
inline VoidPtr &&c_calloc(size_t nitems, size_t count) { |
|
return std::move(VoidPtr(calloc(nitems, count))); |
|
} |
|
inline void c_free(VoidPtr ptr) { |
|
if (ptr) |
|
delete ptr; |
|
} |
|
/* new */ |
|
template <typename T> |
|
inline T *c_allocate() { |
|
return new T(); |
|
} |
|
|
|
#define malloc c_malloc |
|
#define calloc c_calloc |
|
#define realloc c_realloc |
|
#define free c_free |
|
#define allocate c_allocate |
|
|
|
#undef NULL |
|
#define NULL nullptr |
|
|
|
#else /* C code */ |
|
|
|
/* |
|
* Not really a typedef, a typedef would make const VoidPtr |
|
* void *const, which isn't really what you would want. |
|
*/ |
|
#undef VoidPtr |
|
#define VoidPtr void * |
|
|
|
#define c_malloc malloc |
|
#define c_calloc calloc |
|
#define c_realloc realloc |
|
#define c_free free |
|
|
|
#define c_allocate(x) malloc(sizeof(x)) |
|
#define allocate c_allocate |
|
|
|
#endif /* __cplusplus */ |
|
|
|
#endif /* VOID_PTR_H */ |
THANK YOU!!!!