Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using a boost::flyweight class with std::unique_ptr

18 views
Skip to first unread message

bitrex

unread,
Oct 14, 2016, 1:08:26 PM10/14/16
to
I'm trying to write some code to manage large numbers of sprites on the
screen using Allegro 5 as the graphics engine.

As many of the sprites will be using the same underlying bitmap, I'd
like to use the boost::flyweight library to template the bitmap type, so
bitmaps which are associated with the same object will not be repeatedly
loaded into memory.

The underlying ALLEGRO_BITMAP straight-C type is wrapped up in a
structure allegro_bitmap_t with appropriate user-defined conversions so
it can be used directly as the underlying type when necessary. I'm
trying to set up the flyweight type so that it can be dereferenced from
the unique_ptr the same as the "regular" type without calling the "get"
method, but it's not working. Here's the code (the types are wrapped in
a namespace):

using allegro_bitmap_t = struct B
{
public:
B(int bitmap_w, int bitmap_h) : current_id(id())
{
_bitmap_ptr = al_create_bitmap(bitmap_w, bitmap_h);
if (_bitmap_ptr == nullptr) throw std::bad_alloc();
}

B(const char* filename) : current_id(id())
{
_bitmap_ptr = al_load_bitmap(filename);
if (_bitmap_ptr == nullptr) throw std::bad_alloc();
}

~B() = default;

operator ALLEGRO_BITMAP*() const
{
return _bitmap_ptr;
}

const ALLEGRO_BITMAP& operator* () const
{
return *_bitmap_ptr;
}

const int current_id;

private:
static int id()
{
static int _id = 0;
return _id++;
}

ALLEGRO_BITMAP* _bitmap_ptr;
};

using allegro_flyweight_bitmap_t = struct FB : public
boost::flyweights::flyweight<allegro_bitmap_t>
{
using super = boost::flyweights::flyweight<allegro_bitmap_t>;
using super::super;
using allegro_bitmap_t = super::value_type::allegro_bitmap_t;

const allegro_bitmap_t& operator*() const{return *(this->get());}
const allegro_bitmap_t* operator->()const{return &*(this->get());}
};

inline bool operator==(const B& l, const B& r)
{
return l.current_id == r.current_id;
}

inline std::size_t hash_value(B const& b)
{
boost::hash<int> hasher;
return hasher(b.current_id);
}



Compile errors i am getting:

||=== Build: Debug in GraphicsTest (compiler: GNU GCC Compiler) ===|
include/GraphicsTypes.h|73|error: ‘allegro_bitmap_t’ in
‘boost::flyweights::flyweight<GraphicsTypes::B>::value_type {aka struct
GraphicsTypes::B}’ does not name a type|

include/GraphicsTypes.h||In member function ‘const allegro_bitmap_t&
GraphicsTypes::FB::operator*() const’:|

include/GraphicsTypes.h|75|error: invalid initialization of reference of
type ‘const allegro_bitmap_t& {aka const GraphicsTypes::B&}’ from
expression of type ‘const ALLEGRO_BITMAP’|

include/GraphicsTypes.h||In member function ‘const allegro_bitmap_t*
GraphicsTypes::FB::operator->() const’:|

include/GraphicsTypes.h|76|error: cannot convert ‘const ALLEGRO_BITMAP*’
to ‘const allegro_bitmap_t* {aka const GraphicsTypes::B*}’ in return|

/usr/include/allegro5/bitmap.h|12|note: class type ‘const
ALLEGRO_BITMAP’ is incomplete|

bitrex

unread,
Oct 14, 2016, 1:16:20 PM10/14/16
to
On 10/14/2016 01:08 PM, bitrex wrote:

> bitmaps which are associated with the same object will not be repeatedly
> loaded into memory.
>

Same type of object I should say
0 new messages