Table: Add clone constructor (and bind in MRI)

Fixes #50.
This commit is contained in:
Jonas Kulla 2014-08-04 14:43:56 +02:00
parent 59aed37d3e
commit 151a1f0d1b
3 changed files with 12 additions and 0 deletions

View File

@ -142,6 +142,7 @@ RB_METHOD(tableSetAt)
}
MARSH_LOAD_FUN(Table)
INITCOPY_FUN(Table)
void
tableBindingInit()
@ -156,6 +157,7 @@ tableBindingInit()
rb_define_class_method(klass, "_load", TableLoad);
_rb_define_method(klass, "initialize", tableInitialize);
_rb_define_method(klass, "initialize_copy", TableInitializeCopy);
_rb_define_method(klass, "resize", tableResize);
_rb_define_method(klass, "xsize", tableXSize);
_rb_define_method(klass, "ysize", tableYSize);

View File

@ -36,6 +36,14 @@ Table::Table(int x, int y /*= 1*/, int z /*= 1*/)
data = static_cast<int16_t*>(calloc(x * y * z, sizeof(int16_t)));
}
Table::Table(const Table &other)
:m_x(other.m_x), m_y(other.m_y), m_z(other.m_z)
{
const size_t size = m_x * m_y * m_z * sizeof(int16_t);;
data = static_cast<int16_t*>(malloc(size));
memcpy(data, other.data, size);
}
Table::~Table()
{
free(data);

View File

@ -31,6 +31,8 @@ class Table : public Serializable
{
public:
Table(int x, int y = 1, int z = 1);
/* Clone constructor */
Table(const Table &other);
virtual ~Table();
int xSize() const { return m_x; }