From 151a1f0d1b0da19605af787c326f8461b37c6ac3 Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Mon, 4 Aug 2014 14:43:56 +0200 Subject: [PATCH] Table: Add clone constructor (and bind in MRI) Fixes #50. --- binding-mri/table-binding.cpp | 2 ++ src/table.cpp | 8 ++++++++ src/table.h | 2 ++ 3 files changed, 12 insertions(+) diff --git a/binding-mri/table-binding.cpp b/binding-mri/table-binding.cpp index f0aa818..9a29079 100644 --- a/binding-mri/table-binding.cpp +++ b/binding-mri/table-binding.cpp @@ -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); diff --git a/src/table.cpp b/src/table.cpp index 22a9e7d..bab6cbd 100644 --- a/src/table.cpp +++ b/src/table.cpp @@ -36,6 +36,14 @@ Table::Table(int x, int y /*= 1*/, int z /*= 1*/) data = static_cast(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(malloc(size)); + memcpy(data, other.data, size); +} + Table::~Table() { free(data); diff --git a/src/table.h b/src/table.h index 3b8079a..5269329 100644 --- a/src/table.h +++ b/src/table.h @@ -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; }