Skip to content

Commit

Permalink
Add forward declaration header and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
glenfe committed Jun 29, 2014
1 parent 1bf8903 commit 4dd9b81
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 20 deletions.
4 changes: 2 additions & 2 deletions include/boost/align/alignment_of.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/

#include <boost/config.hpp>
#include <boost/align/alignment_of_forward.hpp>
#include <boost/align/detail/type_traits.hpp>

#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
#include <boost/align/detail/alignment_of_cxx11.hpp>
Expand All @@ -38,8 +40,6 @@
#include <boost/align/detail/alignment_of.hpp>
#endif

#include <boost/align/detail/type_traits.hpp>

/**
Boost namespace.
*/
Expand Down
36 changes: 36 additions & 0 deletions include/boost/align/alignment_of_forward.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright (c) 2014 Glen Joseph Fernandes
glenfe at live dot com
Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt
or copy at http://boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_ALIGN_ALIGNMENT_OF_FORWARD_HPP
#define BOOST_ALIGN_ALIGNMENT_OF_FORWARD_HPP

/**
Class template alignment_of
forward declaration.
@note This header provides a forward declaration
for the `alignment_of` class template.
@file
@author Glen Fernandes
*/

/**
@cond
*/
namespace boost {
namespace alignment {
template<class T>
struct alignment_of;
}
}
/**
@endcond
*/

#endif
1 change: 1 addition & 0 deletions test/align_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <boost/align/align.hpp>
#include <boost/align/is_aligned.hpp>
#include <boost/core/lightweight_test.hpp>
#include <cstddef>

template<std::size_t Alignment>
void test()
Expand Down
1 change: 1 addition & 0 deletions test/aligned_alloc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <boost/align/aligned_alloc.hpp>
#include <boost/align/is_aligned.hpp>
#include <boost/core/lightweight_test.hpp>
#include <cstddef>
#include <cstring>

void test(std::size_t alignment)
Expand Down
78 changes: 60 additions & 18 deletions test/aligned_allocator_adaptor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@
#include <boost/align/aligned_allocator_adaptor.hpp>
#include <boost/align/is_aligned.hpp>
#include <boost/core/lightweight_test.hpp>
#include <new>
#include <cstddef>
#include <cstring>

template<class T>
class Allocator
: public std::allocator<T> {
class Allocator {
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef void* void_pointer;
typedef const void* const_void_pointer;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T& reference;
typedef const T& const_reference;

template<class U>
struct rebind {
typedef Allocator<U> other;
Expand All @@ -29,6 +40,25 @@ class Allocator
: state(other.state) {
}

pointer allocate(size_type size, const_void_pointer = 0) {
void* p = ::operator new(sizeof(T) * size);
return static_cast<T*>(p);
}

void deallocate(pointer ptr, size_type) {
::operator delete(ptr);
}

void construct(pointer ptr, const_reference value) {
void* p = ptr;
::new(p) T(value);
}

void destroy(pointer ptr) {
(void)ptr;
ptr->~T();
}

int state;
};

Expand All @@ -44,25 +74,23 @@ bool operator!=(const Allocator<T1>& a,
return !(a == b);
}

template<class T, std::size_t Alignment = 1>
struct adaptor {
typedef boost::alignment::
aligned_allocator_adaptor<Allocator<T>, Alignment> type;
};

template<std::size_t Alignment>
void test_allocate()
{
{
typename adaptor<int, Alignment>::type a(5);
typename boost::alignment::
aligned_allocator_adaptor<Allocator<int>,
Alignment> a(5);
int* p = a.allocate(1);
BOOST_TEST(p != 0);
BOOST_TEST(boost::alignment::is_aligned(Alignment, p));
std::memset(p, 0, 1);
a.deallocate(p, 1);
}
{
typename adaptor<int, Alignment>::type a(5);
typename boost::alignment::
aligned_allocator_adaptor<Allocator<int>,
Alignment> a(5);
int* p1 = a.allocate(1);
int* p2 = a.allocate(1, p1);
BOOST_TEST(p2 != 0);
Expand All @@ -72,7 +100,9 @@ void test_allocate()
a.deallocate(p1, 1);
}
{
typename adaptor<int, Alignment>::type a(5);
typename boost::alignment::
aligned_allocator_adaptor<Allocator<int>,
Alignment> a(5);
int* p = a.allocate(0);
a.deallocate(p, 0);
}
Expand All @@ -81,7 +111,9 @@ void test_allocate()
template<std::size_t Alignment>
void test_construct()
{
typename adaptor<int, Alignment>::type a(5);
typename boost::alignment::
aligned_allocator_adaptor<Allocator<int>,
Alignment> a(5);
int* p = a.allocate(1);
a.construct(p, 1);
BOOST_TEST(*p == 1);
Expand All @@ -93,23 +125,33 @@ template<std::size_t Alignment>
void test_constructor()
{
{
typename adaptor<char, Alignment>::type a1(5);
typename adaptor<int, Alignment>::type a2(a1);
typename boost::alignment::
aligned_allocator_adaptor<Allocator<char>,
Alignment> a1(5);
typename boost::alignment::
aligned_allocator_adaptor<Allocator<int>,
Alignment> a2(a1);
BOOST_TEST(a2 == a1);
}
{
Allocator<int> a1(5);
typename adaptor<int, Alignment>::type a2(a1);
typename boost::alignment::
aligned_allocator_adaptor<Allocator<int>,
Alignment> a2(a1);
BOOST_TEST(a2.base() == a1);
}
}

template<std::size_t Alignment>
void test_rebind()
{
typename adaptor<char, Alignment>::type a1(5);
typename adaptor<char, Alignment>::
type::template rebind<int>::other a2(a1);
typename boost::alignment::
aligned_allocator_adaptor<Allocator<char>,
Alignment> a1(5);
typename boost::alignment::
aligned_allocator_adaptor<Allocator<int>,
Alignment>::template
rebind<int>::other a2(a1);
BOOST_TEST(a2 == a1);
}

Expand Down
1 change: 1 addition & 0 deletions test/aligned_allocator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <boost/align/aligned_allocator.hpp>
#include <boost/align/is_aligned.hpp>
#include <boost/core/lightweight_test.hpp>
#include <cstddef>
#include <cstring>

template<std::size_t Alignment>
Expand Down
1 change: 1 addition & 0 deletions test/aligned_delete_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <boost/align/alignment_of.hpp>
#include <boost/core/lightweight_test.hpp>
#include <new>
#include <cstddef>

class type {
public:
Expand Down
1 change: 1 addition & 0 deletions test/is_aligned_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
#include <boost/align/is_aligned.hpp>
#include <boost/core/lightweight_test.hpp>
#include <cstddef>

template<std::size_t Alignment>
void test()
Expand Down

0 comments on commit 4dd9b81

Please sign in to comment.