Allow to compile without C++ library

Provide a suitable allocator using GLib

Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
This commit is contained in:
Frediano Ziglio 2020-03-06 04:13:00 +00:00 committed by Frediano Ziglio
parent fe0298a290
commit 767a9caded

View File

@ -25,16 +25,49 @@
#include <memory>
#include <forward_list>
#include <algorithm>
#include <glib.h>
#include "push-visibility.h"
namespace red {
template <class T>
struct Mallocator
{
typedef T value_type;
Mallocator() = default;
template <class U>
constexpr Mallocator(const Mallocator<U>&) noexcept
{
}
T* allocate(size_t n)
{
return static_cast<T*>(g_malloc_n(n, sizeof(T)));
}
void deallocate(T* p, size_t) noexcept
{
g_free(p);
}
};
template <class T, class U>
bool operator==(const Mallocator<T>&, const Mallocator<U>&)
{
return true;
}
template <class T, class U>
bool operator!=(const Mallocator<T>&, const Mallocator<U>&)
{
return false;
}
template <typename T>
class safe_list
{
typedef typename std::forward_list<T> wrapped;
typename std::forward_list<T> list;
typename std::forward_list<T,Mallocator<T>> list;
class iterator;
public:
void push_front(const T& v)
@ -74,7 +107,7 @@ public:
template <typename T>
class safe_list<T>::iterator: public std::iterator<std::forward_iterator_tag, T>
{
typedef typename std::forward_list<T>::iterator wrapped;
typedef typename std::forward_list<T,Mallocator<T>>::iterator wrapped;
wrapped curr, next;
public:
iterator(wrapped curr) :