libcruft-util/alloc/raw/null.hpp
Danny Robson f6056153e3 rename root namespace from util to cruft
This places, at long last, the core library code into the same namespace
as the extended library code.
2018-08-05 14:42:02 +10:00

71 lines
1.7 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_ALLOC_RAW_NULL_HPP
#define CRUFT_UTIL_ALLOC_RAW_NULL_HPP
#include "../../view.hpp"
#include <cstddef>
namespace cruft::alloc::raw {
// allocator that always fails, throwing bad_alloc. deallocate will
// succeed with nullptr as with delete, but is undefined with other values
// (it is likely to at least assert).
class null {
public:
null () = default;
null (const null&) = delete;
null& operator= (const null&) = delete;
template <typename T>
cruft::view<T*>
allocate (size_t count)
{
return allocate<T> (count, alignof (T));
}
template <typename T>
cruft::view<T*>
allocate (size_t count, size_t align)
{
(void)count;
(void)align;
throw std::bad_alloc ();
}
template <typename T>
void
deallocate (cruft::view<T*> ptr)
{
(void)ptr;
CHECK_EQ (ptr.data (), static_cast<const void*> (nullptr));
}
cruft::view<std::byte*> data (void);
cruft::view<const std::byte*> data (void) const;
std::byte* begin (void);
const std::byte* begin (void) const;
std::byte* end (void);
const std::byte* end (void) const;
size_t offset (const void*) const;
void reset (void);
size_t capacity (void) const;
size_t used (void) const;
size_t remain (void) const;
};
}
#endif