Add a new DfArray class to match generated headers.

Also, add working assignment and copy constructor to BitArray, and
change the unsafe conversion to int from an operator to a method.
develop
Alexander Gavrilov 2012-02-12 16:44:35 +04:00
parent ed53ee7031
commit c04a140713
4 changed files with 66 additions and 17 deletions

@ -27,6 +27,7 @@ distribution.
#include "Export.h" #include "Export.h"
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <sstream> #include <sstream>
//#include <ostream> //#include <ostream>
namespace DFHack namespace DFHack
@ -35,33 +36,42 @@ namespace DFHack
class BitArray class BitArray
{ {
public: public:
BitArray() BitArray() : bits(NULL), size(0) {}
BitArray(const BitArray<T> &other) : bits(NULL), size(0)
{ {
bits = NULL; *this = other;
size = 0;
} }
~BitArray() ~BitArray()
{ {
if(bits) free(bits);
delete [] bits;
} }
void clear_all ( void ) void clear_all ( void )
{ {
if(bits) if(bits)
memset(bits, 0, size); memset(bits, 0, size);
} }
void extend (T index) void resize (unsigned newsize)
{ {
uint32_t newsize = (index / 8) + 1; if (newsize == size)
if (newsize <= size)
return; return;
uint8_t *newbits = new uint8_t[newsize]; bits = (uint8_t*)realloc(bits, newsize);
memset(newbits, 0, newsize); if (newsize > size)
memcpy(newbits, bits, size); memset(bits+size, 0, newsize-size);
delete[] bits;
bits = newbits;
size = newsize; size = newsize;
} }
BitArray<T> &operator= (const BitArray<T> &other)
{
resize(other.size);
memcpy(bits, other.bits, size);
return *this;
}
void extend (T index)
{
unsigned newsize = (index / 8) + 1;
if (newsize > size)
resize(newsize);
}
void set (T index, bool value = true) void set (T index, bool value = true)
{ {
if(!value) if(!value)
@ -107,7 +117,7 @@ namespace DFHack
else return false; else return false;
} }
/// WARNING: this can truncate long bit arrays /// WARNING: this can truncate long bit arrays
operator uint32_t () uint32_t as_int ()
{ {
if(!bits) if(!bits)
return 0; return 0;
@ -147,4 +157,42 @@ namespace DFHack
uint8_t * bits; uint8_t * bits;
uint32_t size; uint32_t size;
}; };
template <typename T = int>
class DfArray
{
T *m_data;
unsigned short m_size;
public:
DfArray() : m_data(NULL), m_size(0) {}
~DfArray() { free(m_data); }
DfArray(const DfArray<T> &other) : m_data(NULL), m_size(0) {
*this = other;
}
T *data() { return m_data; }
const T *data() const { return m_data; }
unsigned size() const { return m_size; }
T& operator[] (unsigned i) { return m_data[i]; }
const T& operator[] (unsigned i) const { return m_data[i]; }
void resize(unsigned new_size)
{
if (new_size == m_size)
return;
m_data = (T*)realloc(m_data, sizeof(T)*new_size);
if (new_size > m_size)
memset(m_data+sizeof(T)*m_size, 0, sizeof(T)*(new_size - m_size));
m_size = new_size;
}
DfArray &operator= (const DfArray<T> &other)
{
resize(other.size());
memcpy(data(), other.data(), sizeof(T)*size());
return *this;
}
};
} }

@ -181,6 +181,7 @@ namespace df
using DFHack::bitfield_item_info; using DFHack::bitfield_item_info;
using DFHack::enum_list_attr; using DFHack::enum_list_attr;
using DFHack::BitArray; using DFHack::BitArray;
using DFHack::DfArray;
template<class T> template<class T>
class class_virtual_identity : public virtual_identity { class class_virtual_identity : public virtual_identity {

@ -145,7 +145,7 @@ bool Maps::ReadBlock40d(uint32_t x, uint32_t y, uint32_t z, mapblock40d * buffer
buffer->local_feature = block->local_feature; buffer->local_feature = block->local_feature;
buffer->mystery = block->unk2; buffer->mystery = block->unk2;
buffer->origin = block; buffer->origin = block;
buffer->blockflags.whole = block->flags; buffer->blockflags.whole = block->flags.as_int();
return true; return true;
} }
return false; return false;
@ -211,7 +211,7 @@ bool Maps::ReadBlockFlags(uint32_t x, uint32_t y, uint32_t z, t_blockflags &bloc
df::map_block *block = getBlock(x,y,z); df::map_block *block = getBlock(x,y,z);
if (block) if (block)
{ {
blockflags.whole = block->flags; blockflags.whole = block->flags.as_int();
return true; return true;
} }
return false; return false;

@ -1 +1 @@
Subproject commit b3534e853f99129b1664766f09f461d54be6cbfe Subproject commit 2444f376e32783c9f7a7bf103b209e256e64ed80