/*====================================================================
Copyright (C) 2016-2016 Ruler. All rights reserved.
Author: Ruler
Address: Nan'an District,Chongqing,China
Contact: 26105499@qq.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY RULER ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
====================================================================*/
#pragma once
#ifndef __CORE_SAMPLE_ALLOCATOR_H__
#define __CORE_SAMPLE_ALLOCATOR_H__
#include <memory>
#include <exception>
namespace core
{
template <class T> class sample_allocator;
// Specialize for void
template <>
class sample_allocator<void>
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef void value_type;
typedef void* pointer;
typedef const void* const_pointer;
template <class U> struct rebind
{
typedef sample_allocator<U> other;
};
};
// Template class sample_allocator
template <class T>
class sample_allocator
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
template <class U> struct rebind
{
typedef sample_allocator<U> other;
};
typedef ::std::true_type propagate_on_container_move_assignment;
typedef ::std::true_type is_always_equal;
public:
// Construct default sample_allocator
sample_allocator(void) noexcept
{
}
// Construct by copying
sample_allocator(const sample_allocator&) noexcept
{
}
// Construct by moving
sample_allocator(const sample_allocator&&) noexcept
{
}
// Construct from a related sample_allocator
template<class U>
sample_allocator(const sample_allocator<U>&) noexcept
{
}
// Return address of mutable x
pointer address(reference x) const noexcept
{
return (::std::addressof(x));
}
// Return address of nonmutable x
const_pointer address(const_reference x) const noexcept
{
return (::std::addressof(x));
}
// Allocate array of n elements
pointer allocate(size_type n, typename sample_allocator<void>::const_pointer hint = nullptr)
{
return (static_cast<pointer>(malloc(n * sizeof(value_type))));
}
// Deallocate object at p
void deallocate(pointer p, size_type n)
{
free(p);
}
// Estimate maximum array size
size_type max_size(void) const noexcept
{
return (static_cast<size_t>(-1) / sizeof(value_type));
}
// Construct U(Args...) at p
template<class U, class... Args>
void construct(U* p, Args&&... args)
{
::new ((void*)p) U(::std::forward<Args>(args)...);
}
// Destroy object at p
template <class U>
void destroy(U* p)
{
p->~U();
}
};
template <class T1, class T2>
inline bool operator==(const sample_allocator<T1>&, const sample_allocator<T2>&)
{
return true;
}
template <class T1, class T2>
inline bool operator!=(const sample_allocator<T1>&, const sample_allocator<T2>&)
{
return false;
}
} // namespace core
#endif