Eina-Cxx: Added native type constructors and examples.

Summary:
This patch allows us to construct efl::eina:: types from
their respective C types.

Reviewers: cedric, felipealmeida, q66

Reviewed By: q66

CC: smohanty, woohyun, raster, cedric

Differential Revision: https://phab.enlightenment.org/D1068
This commit is contained in:
Savio Sena 2014-06-20 01:00:13 +01:00 committed by Daniel Kolesa
parent ee24425ab5
commit 656d280e6a
7 changed files with 134 additions and 7 deletions

View File

@ -363,7 +363,7 @@ struct _inarray_common_base
* @warning It is important to note that the created array object
* gains ownership of the handle, deallocating it at destruction time.
*/
explicit _inarray_common_base(Eina_Inarray* array)
explicit _inarray_common_base(native_handle_type array)
: _array(array) {}
/**
@ -439,7 +439,7 @@ struct _inarray_common_base
* @internal
* Member variable that holds the native @c Eina_Inarray handle.
*/
Eina_Inarray* _array;
native_handle_type _array;
private:
/** Disabled copy constructor. */
_inarray_common_base(_inarray_common_base const& other);
@ -503,7 +503,7 @@ public:
* @warning It is important to note that the created object gains
* ownership of the handle, deallocating it at destruction time.
*/
_pod_inarray(Eina_Inarray* array)
_pod_inarray(native_handle_type array)
: _base_type(array) {}
/**
@ -607,6 +607,7 @@ public:
void push_back(T const& value)
{
size_type s = size();
static_cast<void>(s);
eina_inarray_push(_array, &value);
assert(size() != s);
assert(size() == s + 1u);
@ -1019,7 +1020,7 @@ public:
* @warning It is important to take care when using it, since the
* handle will be automatically release upon object destruction.
*/
Eina_Inarray* native_handle()
native_handle_type native_handle()
{
return this->_array;
}
@ -1033,7 +1034,7 @@ public:
*
* @see native_handle()
*/
Eina_Inarray const* native_handle() const
const_native_handle_type native_handle() const
{
return this->_array;
}
@ -1286,6 +1287,7 @@ public:
for(size_type j = 0;j != n;++j)
new (&*first++) T(t);
std::size_t diff = last - first;
static_cast<void>(diff);
assert(diff == _array->len - index - n);
static_cast<void>(diff);
while(first != last)

View File

@ -418,6 +418,7 @@ struct _inlist_common_base
typedef typename Allocator::template rebind<_inlist_node<T> >::other node_allocator_type; /**< Type for the allocator of the node. */
typedef Allocator allocator_type; /**< Type for the allocator. */
typedef _inlist_node<T> node_type; /**< Type for the list node. */
typedef Eina_Inlist* native_handle_type; /**< Native type. */
/**
* @brief Creates a list with the given allocator.
@ -426,6 +427,13 @@ struct _inlist_common_base
_inlist_common_base(Allocator allocator)
: _impl(allocator) {}
/**
* @brief Wraps the native object.
* @param inlist The native inlist object (Eina_Inlist*).
*/
_inlist_common_base(native_handle_type inlist)
: _impl(inlist) {}
/**
* @brief Creates an empty inline list.
*/
@ -477,9 +485,11 @@ struct _inlist_common_base
_inlist_impl(Allocator allocator)
: node_allocator_type(allocator), _list(0)
{}
explicit _inlist_impl(native_handle_type list)
: _list(list)
{}
_inlist_impl() : _list(0) {}
Eina_Inlist* _list;
native_handle_type _list;
};
_inlist_impl _impl; /**< @internal */
@ -518,6 +528,7 @@ public:
typedef typename allocator_type::const_pointer const_pointer; /**< Type for a constant pointer for an element. */
typedef std::size_t size_type; /**< Type for size information. */
typedef std::ptrdiff_t difference_type; /**< Type to represent the distance between two iterators. */
typedef typename _base_type::native_handle_type native_handle_type; /**< The native handle type. */
typedef std::reverse_iterator<iterator> reverse_iterator; /**< Type for reverse iterator for this kind of container. */
typedef std::reverse_iterator<const_iterator> const_reverse_iterator; /**< Type for constant reverse iterator for this kind of container. */
@ -529,6 +540,14 @@ public:
*/
inlist() {}
/**
* @brief Construct an inlist from a native object.
* @param list The native object.
*/
inlist(native_handle_type list)
: _inlist_common_base<T, Allocator>(list)
{}
/**
* @brief Construct an inline list object with @p n copies of @p t.
* @param n Number of elements.

View File

@ -6,6 +6,7 @@
#include <eina_lists_auxiliary.hh>
#include <eina_type_traits.hh>
#include <eina_accessor.hh>
#include <eina_iterator.hh>
#include <memory>
#include <iterator>

View File

@ -419,6 +419,27 @@ START_TEST(eina_cxx_range_inarray)
}
END_TEST
START_TEST(eina_cxx_inarray_from_c)
{
efl::eina::eina_init eina_init;
Eina_Inarray *c_array = nullptr;
int values[] = { 11, 22, 33 };
c_array = ::eina_inarray_new(sizeof(int), sizeof(values)/sizeof(int));
ck_assert(!!c_array);
eina_inarray_push(c_array, &values[0]);
eina_inarray_push(c_array, &values[1]);
eina_inarray_push(c_array, &values[2]);
{
efl::eina::range_inarray<int> range_array(c_array);
}
ck_assert(eina_inarray_count(c_array) == 3);
efl::eina::inarray<int> array(c_array);
}
END_TEST
void
eina_test_inarray(TCase *tc)
{
@ -433,4 +454,5 @@ eina_test_inarray(TCase *tc)
tcase_add_test(tc, eina_cxx_inarray_nonpod_erase);
tcase_add_test(tc, eina_cxx_inarray_nonpod_constructors);
tcase_add_test(tc, eina_cxx_range_inarray);
tcase_add_test(tc, eina_cxx_inarray_from_c);
}

View File

@ -6,6 +6,12 @@
#include <check.h>
struct Eina_Test_Inlist
{
int i;
EINA_INLIST;
};
START_TEST(eina_cxx_inlist_push_back)
{
efl::eina::eina_init eina_init;
@ -242,6 +248,31 @@ START_TEST(eina_cxx_inlist_range)
}
END_TEST
START_TEST(eina_cxx_inlist_from_c)
{
efl::eina::eina_init eina_init;
Eina_Inlist *c_list = nullptr;
Eina_Test_Inlist arr[3] = { {11}, {22}, {33} };
c_list = eina_inlist_append(c_list, EINA_INLIST_GET(&arr[0]));
ck_assert(!!c_list);
c_list = eina_inlist_append(c_list, EINA_INLIST_GET(&arr[1]));
ck_assert(!!c_list);
c_list = eina_inlist_append(c_list, EINA_INLIST_GET(&arr[2]));
ck_assert(!!c_list);
efl::eina::range_inlist<int const> const_range_inlist(c_list);
efl::eina::range_inlist<int> range_inlist(c_list);
c_list = eina_inlist_first(c_list);
while (c_list)
c_list = eina_inlist_remove(c_list, c_list);
}
END_TEST
void
eina_test_inlist(TCase *tc)
{
@ -253,4 +284,5 @@ eina_test_inlist(TCase *tc)
tcase_add_test(tc, eina_cxx_inlist_erase);
tcase_add_test(tc, eina_cxx_inlist_constructors);
tcase_add_test(tc, eina_cxx_inlist_range);
tcase_add_test(tc, eina_cxx_inlist_from_c);
}

View File

@ -212,6 +212,18 @@ START_TEST(eina_cxx_ptrarray_range)
}
END_TEST
START_TEST(eina_cxx_ptrarray_from_c)
{
efl::eina::eina_init eina_init;
Eina_Array *c_array = eina_array_new(3);
ck_assert(!!c_array);
efl::eina::range_ptr_array<int> range_array(c_array);
eina_array_free(c_array);
}
END_TEST
void
eina_test_ptrarray(TCase* tc)
{
@ -221,4 +233,5 @@ eina_test_ptrarray(TCase* tc)
tcase_add_test(tc, eina_cxx_ptrarray_constructors);
tcase_add_test(tc, eina_cxx_ptrarray_erase);
tcase_add_test(tc, eina_cxx_ptrarray_range);
tcase_add_test(tc, eina_cxx_ptrarray_from_c);
}

View File

@ -255,6 +255,43 @@ START_TEST(eina_cxx_ptrlist_range)
}
END_TEST
START_TEST(eina_cxx_ptrlist_from_c)
{
efl::eina::eina_init eina_init;
Eina_List *c_list = nullptr;
int values[] = { 11, 22, 33 };
c_list = ::eina_list_append(c_list, &values[0]);
ck_assert(!!c_list);
c_list = ::eina_list_append(c_list, &values[1]);
ck_assert(!!c_list);
c_list = ::eina_list_append(c_list, &values[2]);
ck_assert(!!c_list);
const Eina_List* const_c_list = c_list;
const Eina_List* const_c_list_const_ptr = c_list;
efl::eina::range_ptr_list<int> r0(c_list);
const efl::eina::range_ptr_list<int> r1(c_list);
efl::eina::range_ptr_list<int const> r2(c_list);
efl::eina::range_ptr_list<int const> r3(const_c_list);
efl::eina::range_ptr_list<int const> r4(const_c_list_const_ptr);
const efl::eina::range_ptr_list<int const> r5(c_list);
const efl::eina::range_ptr_list<int const> r6(const_c_list);
const efl::eina::range_ptr_list<int const> r7(const_c_list_const_ptr);
const efl::eina::range_ptr_list<int> r8(c_list);
c_list = ::eina_list_free(c_list);
ck_assert(!c_list);
}
END_TEST
void
eina_test_ptrlist(TCase* tc)
{
@ -267,4 +304,5 @@ eina_test_ptrlist(TCase* tc)
tcase_add_test(tc, eina_cxx_ptrlist_erase);
tcase_add_test(tc, eina_cxx_ptrlist_range);
tcase_add_test(tc, eina_cxx_ptrlist_malloc_clone_allocator);
tcase_add_test(tc, eina_cxx_ptrlist_from_c);
}