From 6046d686393244bd1cdeecaabf60da9816d66c5c Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Wed, 24 Sep 2014 22:47:04 +0400 Subject: [PATCH] Support std::fstream in data structures and add file_compressorst to xml. One tricky thing is that fstream does not have operator=, so the new file_compressorst requires a manually implemented one that skips it. --- library/DataDefs.cpp | 5 +---- library/DataStaticsFields.cpp | 8 ++++++++ library/LuaTypes.cpp | 7 +++++-- library/include/DataDefs.h | 11 ++++++----- library/include/DataIdentity.h | 17 +++++++++++++++++ .../df/custom/file_compressorst.methods.inc | 12 ++++++++++++ library/xml | 2 +- 7 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 library/include/df/custom/file_compressorst.methods.inc diff --git a/library/DataDefs.cpp b/library/DataDefs.cpp index 93e75e280..f9187ff09 100644 --- a/library/DataDefs.cpp +++ b/library/DataDefs.cpp @@ -67,10 +67,7 @@ void *type_identity::allocate() { bool type_identity::copy(void *tgt, const void *src) { if (can_allocate() && tgt && src) - { - do_copy(tgt, src); - return true; - } + return do_copy(tgt, src); else return false; } diff --git a/library/DataStaticsFields.cpp b/library/DataStaticsFields.cpp index 79aa3bcf1..4d8decbed 100644 --- a/library/DataStaticsFields.cpp +++ b/library/DataStaticsFields.cpp @@ -39,6 +39,14 @@ namespace df { stl_bit_vector_identity identity_traits >::identity; bit_array_identity identity_traits >::identity; + static void *fstream_allocator_fn(void *out, const void *in) { + if (out) { /* *(T*)out = *(const T*)in;*/ return NULL; } + else if (in) { delete (std::fstream*)in; return (std::fstream*)in; } + else return new std::fstream(); + } + opaque_identity identity_traits::identity( + sizeof(std::fstream), fstream_allocator_fn, "fstream"); + buffer_container_identity buffer_container_identity::base_instance; #undef NUMBER_IDENTITY_TRAITS diff --git a/library/LuaTypes.cpp b/library/LuaTypes.cpp index 877609e56..410c25f88 100644 --- a/library/LuaTypes.cpp +++ b/library/LuaTypes.cpp @@ -1256,8 +1256,11 @@ static void MakePrimitiveMetatable(lua_State *state, type_identity *type) // Index the fields lua_newtable(state); - EnableMetaField(state, base+2, "value", type); - AssociateId(state, base+3, 1, "value"); + if (type->type() != IDTYPE_OPAQUE) + { + EnableMetaField(state, base+2, "value", type); + AssociateId(state, base+3, 1, "value"); + } // Add the iteration metamethods PushStructMethod(state, base+1, base+3, meta_struct_next); diff --git a/library/include/DataDefs.h b/library/include/DataDefs.h index 3591be045..3bb62a2c1 100644 --- a/library/include/DataDefs.h +++ b/library/include/DataDefs.h @@ -61,7 +61,8 @@ namespace DFHack IDTYPE_STRUCT, IDTYPE_CLASS, IDTYPE_BUFFER, - IDTYPE_STL_PTR_VECTOR + IDTYPE_STL_PTR_VECTOR, + IDTYPE_OPAQUE }; typedef void *(*TAllocateFn)(void*,const void*); @@ -78,7 +79,7 @@ namespace DFHack virtual bool can_allocate() { return true; } virtual void *do_allocate() { return do_allocate_pod(); } - virtual void do_copy(void *tgt, const void *src) { do_copy_pod(tgt, src); } + virtual bool do_copy(void *tgt, const void *src) { do_copy_pod(tgt, src); return true; } virtual bool do_destroy(void *obj) { return do_destroy_pod(obj); } public: @@ -116,7 +117,7 @@ namespace DFHack virtual bool can_allocate() { return (allocator != NULL); } virtual void *do_allocate() { return allocator(NULL,NULL); } - virtual void do_copy(void *tgt, const void *src) { allocator(tgt,src); } + virtual bool do_copy(void *tgt, const void *src) { return allocator(tgt,src) == tgt; } virtual bool do_destroy(void *obj) { return allocator(NULL,obj) == obj; } public: virtual bool isPrimitive() { return false; } @@ -166,7 +167,7 @@ namespace DFHack protected: virtual bool can_allocate() { return true; } virtual void *do_allocate() { return do_allocate_pod(); } - virtual void do_copy(void *tgt, const void *src) { do_copy_pod(tgt, src); } + virtual bool do_copy(void *tgt, const void *src) { do_copy_pod(tgt, src); return true; } virtual bool do_destroy(void *obj) { return do_destroy_pod(obj); } public: @@ -199,7 +200,7 @@ namespace DFHack protected: virtual bool can_allocate() { return true; } virtual void *do_allocate(); - virtual void do_copy(void *tgt, const void *src) { do_copy_pod(tgt, src); } + virtual bool do_copy(void *tgt, const void *src) { do_copy_pod(tgt, src); return true; } virtual bool do_destroy(void *obj) { return do_destroy_pod(obj); } public: diff --git a/library/include/DataIdentity.h b/library/include/DataIdentity.h index e684c6abd..e6dfc6a18 100644 --- a/library/include/DataIdentity.h +++ b/library/include/DataIdentity.h @@ -65,6 +65,17 @@ namespace DFHack virtual identity_type type() { return IDTYPE_PRIMITIVE; } }; + class DFHACK_EXPORT opaque_identity : public constructed_identity { + std::string name; + + public: + opaque_identity(size_t size, TAllocateFn alloc, const std::string &name) + : constructed_identity(size, alloc), name(name) {}; + + virtual std::string getFullName() { return name; } + virtual identity_type type() { return IDTYPE_OPAQUE; } + }; + class DFHACK_EXPORT pointer_identity : public primitive_identity { type_identity *target; @@ -170,6 +181,7 @@ namespace df { using DFHack::function_identity_base; using DFHack::primitive_identity; + using DFHack::opaque_identity; using DFHack::pointer_identity; using DFHack::container_identity; using DFHack::ptr_container_identity; @@ -488,6 +500,11 @@ namespace df static stl_string_identity *get() { return &identity; } }; + template<> struct DFHACK_EXPORT identity_traits { + static opaque_identity identity; + static opaque_identity *get() { return &identity; } + }; + template<> struct DFHACK_EXPORT identity_traits { static ptr_string_identity identity; static ptr_string_identity *get() { return &identity; } diff --git a/library/include/df/custom/file_compressorst.methods.inc b/library/include/df/custom/file_compressorst.methods.inc new file mode 100644 index 000000000..ad61c7bc7 --- /dev/null +++ b/library/include/df/custom/file_compressorst.methods.inc @@ -0,0 +1,12 @@ +file_compressorst& operator=(const file_compressorst &in) { + compressed = in.compressed; + /* fstream cannot be assigned */ + in_buffer = in.in_buffer; + in_buffersize = in.in_buffersize; + in_buffer_amount_loaded = in.in_buffer_amount_loaded; + in_buffer_position = in.in_buffer_position; + out_buffer = in.out_buffer; + out_buffersize = in.out_buffersize; + out_buffer_amount_written = in.out_buffer_amount_written; + return *this; +} diff --git a/library/xml b/library/xml index 93606b34b..5541b66ce 160000 --- a/library/xml +++ b/library/xml @@ -1 +1 @@ -Subproject commit 93606b34b78a94b201704adb5c9ae28af304cdb1 +Subproject commit 5541b66cef283e57c32551f69cc3fd87cff966d2