2011-12-24 03:51:58 -07:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
2012-03-17 02:52:22 -06:00
|
|
|
#include <sstream>
|
2011-12-24 03:51:58 -07:00
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2012-08-18 04:34:20 -06:00
|
|
|
#include <set>
|
2011-12-24 03:51:58 -07:00
|
|
|
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Core.h"
|
|
|
|
#include "BitArray.h"
|
2011-12-24 03:51:58 -07:00
|
|
|
|
2011-12-24 06:27:35 -07:00
|
|
|
// Stop some MS stupidity
|
|
|
|
#ifdef interface
|
|
|
|
#undef interface
|
|
|
|
#endif
|
|
|
|
|
2012-03-19 10:12:27 -06:00
|
|
|
typedef struct lua_State lua_State;
|
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/*
|
|
|
|
* Definitions of DFHack namespace structs used by generated headers.
|
|
|
|
*/
|
|
|
|
|
2011-12-24 03:51:58 -07:00
|
|
|
namespace DFHack
|
|
|
|
{
|
|
|
|
class virtual_class {};
|
|
|
|
|
2012-03-19 06:59:11 -06:00
|
|
|
enum identity_type {
|
|
|
|
IDTYPE_GLOBAL,
|
2012-03-25 04:06:05 -06:00
|
|
|
IDTYPE_FUNCTION,
|
2012-03-19 06:59:11 -06:00
|
|
|
IDTYPE_PRIMITIVE,
|
2012-03-20 03:56:29 -06:00
|
|
|
IDTYPE_POINTER,
|
2012-03-19 06:59:11 -06:00
|
|
|
IDTYPE_CONTAINER,
|
2012-03-21 03:26:53 -06:00
|
|
|
IDTYPE_PTR_CONTAINER,
|
|
|
|
IDTYPE_BIT_CONTAINER,
|
2012-03-19 06:59:11 -06:00
|
|
|
IDTYPE_BITFIELD,
|
|
|
|
IDTYPE_ENUM,
|
|
|
|
IDTYPE_STRUCT,
|
2012-03-20 03:56:29 -06:00
|
|
|
IDTYPE_CLASS,
|
2012-03-24 02:43:53 -06:00
|
|
|
IDTYPE_BUFFER,
|
2012-03-20 03:56:29 -06:00
|
|
|
IDTYPE_STL_PTR_VECTOR
|
2012-03-19 06:59:11 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef void *(*TAllocateFn)(void*,const void*);
|
|
|
|
|
|
|
|
class DFHACK_EXPORT type_identity {
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
type_identity(size_t size) : size(size) {};
|
|
|
|
|
2012-03-22 00:56:32 -06:00
|
|
|
void *do_allocate_pod();
|
|
|
|
void do_copy_pod(void *tgt, const void *src);
|
2012-03-25 05:48:18 -06:00
|
|
|
bool do_destroy_pod(void *obj);
|
2012-03-22 00:56:32 -06:00
|
|
|
|
|
|
|
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); }
|
2012-03-25 05:48:18 -06:00
|
|
|
virtual bool do_destroy(void *obj) { return do_destroy_pod(obj); }
|
2012-03-19 06:59:11 -06:00
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~type_identity() {}
|
|
|
|
|
2012-04-20 03:04:03 -06:00
|
|
|
virtual size_t byte_size() { return size; }
|
2012-03-19 06:59:11 -06:00
|
|
|
|
|
|
|
virtual identity_type type() = 0;
|
2012-03-20 03:56:29 -06:00
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
virtual std::string getFullName() = 0;
|
|
|
|
|
|
|
|
// For internal use in the lua wrapper
|
2012-03-22 23:38:49 -06:00
|
|
|
virtual void lua_read(lua_State *state, int fname_idx, void *ptr) = 0;
|
2012-03-20 03:56:29 -06:00
|
|
|
virtual void lua_write(lua_State *state, int fname_idx, void *ptr, int val_index) = 0;
|
2012-03-21 03:26:53 -06:00
|
|
|
virtual void build_metatable(lua_State *state);
|
|
|
|
|
2012-03-24 06:28:53 -06:00
|
|
|
// lua_read doesn't just return a reference to the object
|
|
|
|
virtual bool isPrimitive() { return true; }
|
|
|
|
// needs constructor/destructor
|
|
|
|
virtual bool isConstructed() { return false; }
|
|
|
|
// inherits from container_identity
|
2012-03-21 03:26:53 -06:00
|
|
|
virtual bool isContainer() { return false; }
|
2012-03-22 00:56:32 -06:00
|
|
|
|
|
|
|
void *allocate();
|
|
|
|
bool copy(void *tgt, const void *src);
|
2012-03-25 05:48:18 -06:00
|
|
|
bool destroy(void *obj);
|
2012-03-19 06:59:11 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
class DFHACK_EXPORT constructed_identity : public type_identity {
|
|
|
|
TAllocateFn allocator;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
constructed_identity(size_t size, TAllocateFn alloc)
|
|
|
|
: type_identity(size), allocator(alloc) {};
|
|
|
|
|
2012-03-22 00:56:32 -06:00
|
|
|
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); }
|
2012-03-25 05:48:18 -06:00
|
|
|
virtual bool do_destroy(void *obj) { return allocator(NULL,obj) == obj; }
|
2012-03-25 04:06:05 -06:00
|
|
|
public:
|
|
|
|
virtual bool isPrimitive() { return false; }
|
|
|
|
virtual bool isConstructed() { return true; }
|
2012-03-20 03:56:29 -06:00
|
|
|
|
2012-03-22 23:38:49 -06:00
|
|
|
virtual void lua_read(lua_State *state, int fname_idx, void *ptr);
|
2012-03-20 03:56:29 -06:00
|
|
|
virtual void lua_write(lua_State *state, int fname_idx, void *ptr, int val_index);
|
2012-03-19 06:59:11 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
class DFHACK_EXPORT compound_identity : public constructed_identity {
|
|
|
|
static compound_identity *list;
|
|
|
|
compound_identity *next;
|
|
|
|
|
|
|
|
const char *dfhack_name;
|
|
|
|
compound_identity *scope_parent;
|
|
|
|
std::vector<compound_identity*> scope_children;
|
|
|
|
static std::vector<compound_identity*> top_scope;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
compound_identity(size_t size, TAllocateFn alloc,
|
|
|
|
compound_identity *scope_parent, const char *dfhack_name);
|
|
|
|
|
|
|
|
virtual void doInit(Core *core);
|
|
|
|
|
|
|
|
public:
|
|
|
|
const char *getName() { return dfhack_name; }
|
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
virtual std::string getFullName();
|
|
|
|
|
2012-03-19 06:59:11 -06:00
|
|
|
compound_identity *getScopeParent() { return scope_parent; }
|
|
|
|
const std::vector<compound_identity*> &getScopeChildren() { return scope_children; }
|
|
|
|
static const std::vector<compound_identity*> &getTopScope() { return top_scope; }
|
|
|
|
|
|
|
|
static void Init(Core *core);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Bitfields
|
|
|
|
struct bitfield_item_info {
|
|
|
|
const char *name;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DFHACK_EXPORT bitfield_identity : public compound_identity {
|
|
|
|
const bitfield_item_info *bits;
|
|
|
|
int num_bits;
|
|
|
|
|
2012-03-22 00:56:32 -06:00
|
|
|
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); }
|
2012-03-25 05:48:18 -06:00
|
|
|
virtual bool do_destroy(void *obj) { return do_destroy_pod(obj); }
|
2012-03-22 00:56:32 -06:00
|
|
|
|
2012-03-19 06:59:11 -06:00
|
|
|
public:
|
2012-03-22 00:56:32 -06:00
|
|
|
bitfield_identity(size_t size,
|
2012-03-19 06:59:11 -06:00
|
|
|
compound_identity *scope_parent, const char *dfhack_name,
|
|
|
|
int num_bits, const bitfield_item_info *bits);
|
|
|
|
|
|
|
|
virtual identity_type type() { return IDTYPE_BITFIELD; }
|
|
|
|
|
2012-03-24 06:28:53 -06:00
|
|
|
virtual bool isConstructed() { return false; }
|
|
|
|
|
2012-03-19 06:59:11 -06:00
|
|
|
int getNumBits() { return num_bits; }
|
|
|
|
const bitfield_item_info *getBits() { return bits; }
|
2012-03-21 10:04:37 -06:00
|
|
|
|
|
|
|
virtual void build_metatable(lua_State *state);
|
2012-03-19 06:59:11 -06:00
|
|
|
};
|
|
|
|
|
2012-03-25 09:12:59 -06:00
|
|
|
class struct_identity;
|
|
|
|
|
2012-03-19 06:59:11 -06:00
|
|
|
class DFHACK_EXPORT enum_identity : public compound_identity {
|
|
|
|
const char *const *keys;
|
|
|
|
int64_t first_item_value;
|
|
|
|
int64_t last_item_value;
|
|
|
|
|
2012-03-20 03:56:29 -06:00
|
|
|
type_identity *base_type;
|
|
|
|
|
2012-03-25 09:12:59 -06:00
|
|
|
const void *attrs;
|
|
|
|
struct_identity *attr_type;
|
|
|
|
|
2012-03-22 00:56:32 -06:00
|
|
|
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); }
|
2012-03-25 05:48:18 -06:00
|
|
|
virtual bool do_destroy(void *obj) { return do_destroy_pod(obj); }
|
2012-03-22 00:56:32 -06:00
|
|
|
|
2012-03-19 06:59:11 -06:00
|
|
|
public:
|
2012-03-22 00:56:32 -06:00
|
|
|
enum_identity(size_t size,
|
2012-03-19 06:59:11 -06:00
|
|
|
compound_identity *scope_parent, const char *dfhack_name,
|
2012-03-20 03:56:29 -06:00
|
|
|
type_identity *base_type,
|
2012-03-19 06:59:11 -06:00
|
|
|
int64_t first_item_value, int64_t last_item_value,
|
2012-03-25 09:12:59 -06:00
|
|
|
const char *const *keys,
|
|
|
|
const void *attrs, struct_identity *attr_type);
|
2012-03-19 06:59:11 -06:00
|
|
|
|
|
|
|
virtual identity_type type() { return IDTYPE_ENUM; }
|
|
|
|
|
2012-03-19 10:12:27 -06:00
|
|
|
int64_t getFirstItem() { return first_item_value; }
|
|
|
|
int64_t getLastItem() { return last_item_value; }
|
2012-03-19 06:59:11 -06:00
|
|
|
int getCount() { return int(last_item_value-first_item_value+1); }
|
|
|
|
const char *const *getKeys() { return keys; }
|
2012-03-20 03:56:29 -06:00
|
|
|
|
|
|
|
type_identity *getBaseType() { return base_type; }
|
2012-03-25 09:12:59 -06:00
|
|
|
const void *getAttrs() { return attrs; }
|
|
|
|
struct_identity *getAttrType() { return attr_type; }
|
2012-03-20 03:56:29 -06:00
|
|
|
|
2012-03-24 06:28:53 -06:00
|
|
|
virtual bool isPrimitive() { return true; }
|
|
|
|
virtual bool isConstructed() { return false; }
|
|
|
|
|
2012-03-22 23:38:49 -06:00
|
|
|
virtual void lua_read(lua_State *state, int fname_idx, void *ptr);
|
2012-03-20 03:56:29 -06:00
|
|
|
virtual void lua_write(lua_State *state, int fname_idx, void *ptr, int val_index);
|
2012-03-19 06:59:11 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
struct struct_field_info {
|
|
|
|
enum Mode {
|
|
|
|
END,
|
|
|
|
PRIMITIVE,
|
|
|
|
STATIC_STRING,
|
|
|
|
POINTER,
|
|
|
|
STATIC_ARRAY,
|
|
|
|
SUBSTRUCT,
|
|
|
|
CONTAINER,
|
2012-03-25 04:06:05 -06:00
|
|
|
STL_VECTOR_PTR,
|
|
|
|
OBJ_METHOD,
|
|
|
|
CLASS_METHOD
|
2012-03-19 06:59:11 -06:00
|
|
|
};
|
|
|
|
Mode mode;
|
|
|
|
const char *name;
|
|
|
|
size_t offset;
|
|
|
|
type_identity *type;
|
|
|
|
size_t count;
|
|
|
|
enum_identity *eid;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DFHACK_EXPORT struct_identity : public compound_identity {
|
|
|
|
struct_identity *parent;
|
|
|
|
std::vector<struct_identity*> children;
|
|
|
|
bool has_children;
|
|
|
|
|
2012-03-20 03:56:29 -06:00
|
|
|
const struct_field_info *fields;
|
|
|
|
|
2012-03-19 06:59:11 -06:00
|
|
|
protected:
|
|
|
|
virtual void doInit(Core *core);
|
|
|
|
|
|
|
|
public:
|
|
|
|
struct_identity(size_t size, TAllocateFn alloc,
|
|
|
|
compound_identity *scope_parent, const char *dfhack_name,
|
|
|
|
struct_identity *parent, const struct_field_info *fields);
|
|
|
|
|
|
|
|
virtual identity_type type() { return IDTYPE_STRUCT; }
|
|
|
|
|
|
|
|
struct_identity *getParent() { return parent; }
|
|
|
|
const std::vector<struct_identity*> &getChildren() { return children; }
|
|
|
|
bool hasChildren() { return has_children; }
|
|
|
|
|
2012-03-20 03:56:29 -06:00
|
|
|
const struct_field_info *getFields() { return fields; }
|
|
|
|
|
2012-03-19 06:59:11 -06:00
|
|
|
bool is_subclass(struct_identity *subtype);
|
2012-03-21 03:26:53 -06:00
|
|
|
|
|
|
|
virtual void build_metatable(lua_State *state);
|
2012-03-19 06:59:11 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
class DFHACK_EXPORT global_identity : public struct_identity {
|
|
|
|
public:
|
|
|
|
global_identity(const struct_field_info *fields)
|
|
|
|
: struct_identity(0,NULL,NULL,"global",NULL,fields) {}
|
|
|
|
|
|
|
|
virtual identity_type type() { return IDTYPE_GLOBAL; }
|
2012-03-21 03:26:53 -06:00
|
|
|
|
|
|
|
virtual void build_metatable(lua_State *state);
|
2012-03-19 06:59:11 -06:00
|
|
|
};
|
|
|
|
|
2011-12-24 06:17:01 -07:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
typedef void *virtual_ptr;
|
|
|
|
#else
|
|
|
|
typedef virtual_class *virtual_ptr;
|
|
|
|
#endif
|
|
|
|
|
2012-08-17 12:40:53 -06:00
|
|
|
class DFHACK_EXPORT VMethodInterposeLinkBase;
|
|
|
|
|
2012-03-19 06:59:11 -06:00
|
|
|
class DFHACK_EXPORT virtual_identity : public struct_identity {
|
2011-12-24 03:51:58 -07:00
|
|
|
static std::map<void*, virtual_identity*> known;
|
2012-03-19 06:59:11 -06:00
|
|
|
|
2011-12-24 03:51:58 -07:00
|
|
|
const char *original_name;
|
2012-03-19 06:59:11 -06:00
|
|
|
|
2011-12-24 03:51:58 -07:00
|
|
|
void *vtable_ptr;
|
|
|
|
|
2012-08-17 12:40:53 -06:00
|
|
|
friend class VMethodInterposeLinkBase;
|
2012-08-26 03:23:59 -06:00
|
|
|
std::map<int,VMethodInterposeLinkBase*> interpose_list;
|
2012-08-17 12:40:53 -06:00
|
|
|
|
2011-12-24 03:51:58 -07:00
|
|
|
protected:
|
2012-03-19 06:59:11 -06:00
|
|
|
virtual void doInit(Core *core);
|
2011-12-24 03:51:58 -07:00
|
|
|
|
2011-12-24 06:17:01 -07:00
|
|
|
static void *get_vtable(virtual_ptr instance_ptr) { return *(void**)instance_ptr; }
|
2011-12-24 03:51:58 -07:00
|
|
|
|
2012-03-22 00:56:32 -06:00
|
|
|
bool can_allocate() { return struct_identity::can_allocate() && (vtable_ptr != NULL); }
|
|
|
|
|
2012-08-17 12:40:53 -06:00
|
|
|
void *get_vmethod_ptr(int index);
|
|
|
|
bool set_vmethod_ptr(int index, void *ptr);
|
|
|
|
|
2011-12-24 03:51:58 -07:00
|
|
|
public:
|
2012-03-19 06:59:11 -06:00
|
|
|
virtual_identity(size_t size, TAllocateFn alloc,
|
|
|
|
const char *dfhack_name, const char *original_name,
|
|
|
|
virtual_identity *parent, const struct_field_info *fields);
|
2012-08-17 12:40:53 -06:00
|
|
|
~virtual_identity();
|
2012-03-19 06:59:11 -06:00
|
|
|
|
|
|
|
virtual identity_type type() { return IDTYPE_CLASS; }
|
2011-12-24 03:51:58 -07:00
|
|
|
|
2012-03-19 06:59:11 -06:00
|
|
|
const char *getOriginalName() { return original_name ? original_name : getName(); }
|
2011-12-24 03:51:58 -07:00
|
|
|
|
2011-12-29 05:30:55 -07:00
|
|
|
public:
|
2011-12-24 06:17:01 -07:00
|
|
|
static virtual_identity *get(virtual_ptr instance_ptr);
|
2012-03-19 06:59:11 -06:00
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
static virtual_identity *find(void *vtable);
|
|
|
|
static virtual_identity *find(const std::string &name);
|
|
|
|
|
2011-12-24 06:17:01 -07:00
|
|
|
bool is_instance(virtual_ptr instance_ptr) {
|
2011-12-24 03:51:58 -07:00
|
|
|
if (!instance_ptr) return false;
|
|
|
|
if (vtable_ptr) {
|
|
|
|
void *vtable = get_vtable(instance_ptr);
|
|
|
|
if (vtable == vtable_ptr) return true;
|
2012-03-19 06:59:11 -06:00
|
|
|
if (!hasChildren()) return false;
|
2011-12-24 03:51:58 -07:00
|
|
|
}
|
2011-12-29 05:30:55 -07:00
|
|
|
return is_subclass(get(instance_ptr));
|
2011-12-24 03:51:58 -07:00
|
|
|
}
|
|
|
|
|
2011-12-24 06:17:01 -07:00
|
|
|
bool is_direct_instance(virtual_ptr instance_ptr) {
|
2011-12-24 03:51:58 -07:00
|
|
|
if (!instance_ptr) return false;
|
|
|
|
return vtable_ptr ? (vtable_ptr == get_vtable(instance_ptr))
|
2011-12-29 05:30:55 -07:00
|
|
|
: (this == get(instance_ptr));
|
2011-12-24 03:51:58 -07:00
|
|
|
}
|
|
|
|
|
2012-08-17 12:40:53 -06:00
|
|
|
template<class P> static P get_vmethod_ptr(P selector);
|
|
|
|
|
2011-12-29 05:30:55 -07:00
|
|
|
public:
|
2012-03-22 00:56:32 -06:00
|
|
|
bool can_instantiate() { return can_allocate(); }
|
|
|
|
virtual_ptr instantiate() { return can_instantiate() ? (virtual_ptr)do_allocate() : NULL; }
|
2011-12-29 05:30:55 -07:00
|
|
|
static virtual_ptr clone(virtual_ptr obj);
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Strictly for use in virtual class constructors
|
|
|
|
void adjust_vtable(virtual_ptr obj, virtual_identity *main);
|
2011-12-24 03:51:58 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
2011-12-24 06:17:01 -07:00
|
|
|
inline T *virtual_cast(virtual_ptr ptr) {
|
2011-12-24 03:51:58 -07:00
|
|
|
return T::_identity.is_instance(ptr) ? static_cast<T*>(ptr) : NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-02 07:46:24 -07:00
|
|
|
#define VIRTUAL_CAST_VAR(var,type,input) type *var = virtual_cast<type>(input)
|
|
|
|
|
2011-12-24 03:51:58 -07:00
|
|
|
template<class T>
|
2011-12-24 06:17:01 -07:00
|
|
|
inline T *strict_virtual_cast(virtual_ptr ptr) {
|
2011-12-24 03:51:58 -07:00
|
|
|
return T::_identity.is_direct_instance(ptr) ? static_cast<T*>(ptr) : NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-02 07:46:24 -07:00
|
|
|
#define STRICT_VIRTUAL_CAST_VAR(var,type,input) type *var = strict_virtual_cast<type>(input)
|
|
|
|
|
2011-12-24 03:51:58 -07:00
|
|
|
void InitDataDefGlobals(Core *core);
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T *ifnull(T *a, T *b) { return a ? a : b; }
|
2012-01-03 08:25:55 -07:00
|
|
|
|
2012-01-06 11:08:09 -07:00
|
|
|
template<class T>
|
|
|
|
struct enum_list_attr {
|
2012-03-14 02:09:02 -06:00
|
|
|
size_t size;
|
2012-01-06 11:08:09 -07:00
|
|
|
const T *items;
|
|
|
|
};
|
2011-12-24 03:51:58 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 05:20:17 -07:00
|
|
|
template<class T>
|
|
|
|
int linear_index(const DFHack::enum_list_attr<T> &lst, T val) {
|
2012-04-20 03:30:37 -06:00
|
|
|
for (size_t i = 0; i < lst.size; i++)
|
2012-01-09 05:20:17 -07:00
|
|
|
if (lst.items[i] == val)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int linear_index(const DFHack::enum_list_attr<const char*> &lst, const std::string &val) {
|
2012-04-20 03:30:37 -06:00
|
|
|
for (size_t i = 0; i < lst.size; i++)
|
2012-01-09 05:20:17 -07:00
|
|
|
if (lst.items[i] == val)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/*
|
|
|
|
* Definitions of df namespace structs used by generated headers.
|
|
|
|
*/
|
|
|
|
|
2011-12-24 03:51:58 -07:00
|
|
|
namespace df
|
|
|
|
{
|
2012-03-19 06:59:11 -06:00
|
|
|
using DFHack::type_identity;
|
|
|
|
using DFHack::compound_identity;
|
2011-12-29 05:30:55 -07:00
|
|
|
using DFHack::virtual_ptr;
|
|
|
|
using DFHack::virtual_identity;
|
2011-12-24 03:51:58 -07:00
|
|
|
using DFHack::virtual_class;
|
2012-03-19 06:59:11 -06:00
|
|
|
using DFHack::global_identity;
|
|
|
|
using DFHack::struct_identity;
|
|
|
|
using DFHack::struct_field_info;
|
2012-01-03 11:56:05 -07:00
|
|
|
using DFHack::bitfield_item_info;
|
2012-03-19 06:59:11 -06:00
|
|
|
using DFHack::bitfield_identity;
|
|
|
|
using DFHack::enum_identity;
|
2012-01-06 11:08:09 -07:00
|
|
|
using DFHack::enum_list_attr;
|
2011-12-24 03:51:58 -07:00
|
|
|
using DFHack::BitArray;
|
2012-02-12 05:44:35 -07:00
|
|
|
using DFHack::DfArray;
|
2011-12-24 03:51:58 -07:00
|
|
|
|
2012-03-15 10:46:08 -06:00
|
|
|
template<class T>
|
2012-03-19 06:59:11 -06:00
|
|
|
void *allocator_fn(void *out, const void *in) {
|
|
|
|
if (out) { *(T*)out = *(const T*)in; return out; }
|
2012-03-25 04:06:05 -06:00
|
|
|
else if (in) { delete (T*)in; return (T*)in; }
|
|
|
|
else return new T();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void *allocator_nodel_fn(void *out, const void *in) {
|
|
|
|
if (out) { *(T*)out = *(const T*)in; return out; }
|
|
|
|
else if (in) { return NULL; }
|
2012-03-19 06:59:11 -06:00
|
|
|
else return new T();
|
|
|
|
}
|
2012-03-15 10:46:08 -06:00
|
|
|
|
|
|
|
template<class T>
|
2012-03-19 06:59:11 -06:00
|
|
|
struct identity_traits {
|
|
|
|
static compound_identity *get() { return &T::_identity; }
|
|
|
|
};
|
2012-03-15 10:46:08 -06:00
|
|
|
|
2012-04-11 10:10:31 -06:00
|
|
|
template<class T>
|
|
|
|
inline T* allocate() { return (T*)identity_traits<T>::get()->allocate(); }
|
|
|
|
|
2011-12-24 03:51:58 -07:00
|
|
|
template<class T>
|
2012-03-19 06:59:11 -06:00
|
|
|
struct enum_traits {};
|
2011-12-29 05:30:55 -07:00
|
|
|
|
2012-03-19 06:59:11 -06:00
|
|
|
template<class T>
|
|
|
|
struct bitfield_traits {};
|
2011-12-24 03:51:58 -07:00
|
|
|
|
|
|
|
template<class EnumType, class IntType = int32_t>
|
|
|
|
struct enum_field {
|
|
|
|
IntType value;
|
|
|
|
|
|
|
|
enum_field() {}
|
|
|
|
enum_field(EnumType ev) : value(IntType(ev)) {}
|
|
|
|
template<class T>
|
|
|
|
enum_field(enum_field<EnumType,T> ev) : value(IntType(ev.value)) {}
|
|
|
|
|
|
|
|
operator EnumType () { return EnumType(value); }
|
|
|
|
enum_field<EnumType,IntType> &operator=(EnumType ev) {
|
|
|
|
value = IntType(ev); return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
template<class ET, class IT>
|
|
|
|
struct enum_traits<enum_field<ET, IT> > : public enum_traits<ET> {};
|
|
|
|
|
2012-01-07 08:21:07 -07:00
|
|
|
template<class EnumType, class IntType1, class IntType2>
|
|
|
|
inline bool operator== (enum_field<EnumType,IntType1> a, enum_field<EnumType,IntType2> b)
|
|
|
|
{
|
|
|
|
return EnumType(a) == EnumType(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class EnumType, class IntType1, class IntType2>
|
|
|
|
inline bool operator!= (enum_field<EnumType,IntType1> a, enum_field<EnumType,IntType2> b)
|
|
|
|
{
|
|
|
|
return EnumType(a) != EnumType(b);
|
|
|
|
}
|
|
|
|
|
2011-12-24 03:51:58 -07:00
|
|
|
namespace enums {}
|
|
|
|
}
|
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/*
|
|
|
|
* Templates for access to enum and bitfield traits.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DFHACK_EXPORT std::string join_strings(const std::string &separator, const std::vector<std::string> &items);
|
|
|
|
|
2012-03-15 10:46:08 -06:00
|
|
|
namespace DFHack {
|
2012-03-17 02:52:22 -06:00
|
|
|
/*
|
|
|
|
* Enum trait tools.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the next item in the enum, wrapping to the first one at the end.
|
|
|
|
*/
|
2012-03-15 10:46:08 -06:00
|
|
|
template<class T>
|
|
|
|
inline typename df::enum_traits<T>::enum_type next_enum_item(T v) {
|
|
|
|
typedef df::enum_traits<T> traits;
|
|
|
|
typedef typename traits::base_type base_type;
|
|
|
|
base_type iv = base_type(v);
|
|
|
|
return (iv < traits::last_item_value) ? T(iv+1) : traits::first_item;
|
|
|
|
}
|
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/**
|
|
|
|
* Check if the value is valid for its enum type.
|
|
|
|
*/
|
2012-03-15 10:46:08 -06:00
|
|
|
template<class T>
|
|
|
|
inline bool is_valid_enum_item(T v) {
|
|
|
|
return df::enum_traits<T>::is_valid(v);
|
|
|
|
}
|
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/**
|
|
|
|
* Return the enum item key string pointer, or NULL if none.
|
|
|
|
*/
|
2012-03-15 10:46:08 -06:00
|
|
|
template<class T>
|
|
|
|
inline const char *enum_item_raw_key(T val) {
|
|
|
|
typedef df::enum_traits<T> traits;
|
|
|
|
return traits::is_valid(val) ? traits::key_table[val - traits::first_item_value] : NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/**
|
|
|
|
* Return the enum item key string pointer, or "?" if none.
|
|
|
|
*/
|
2012-03-15 10:46:08 -06:00
|
|
|
template<class T>
|
|
|
|
inline const char *enum_item_key_str(T val) {
|
|
|
|
return ifnull(enum_item_raw_key(val), "?");
|
|
|
|
}
|
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
template<class BaseType>
|
|
|
|
std::string format_key(const char *keyname, BaseType val) {
|
|
|
|
if (keyname) return std::string(keyname);
|
|
|
|
std::stringstream ss; ss << "?" << val << "?"; return ss.str();
|
|
|
|
}
|
2012-03-15 10:46:08 -06:00
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/**
|
|
|
|
* Return the enum item key string, or ?123? (using the numeric value) if unknown.
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
inline std::string enum_item_key(T val) {
|
|
|
|
typedef typename df::enum_traits<T>::base_type base_type;
|
|
|
|
return format_key<base_type>(enum_item_raw_key(val), base_type(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
DFHACK_EXPORT int findEnumItem(const std::string &name, int size, const char *const *items);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find an enum item by key string. Returns success code.
|
|
|
|
*/
|
2012-03-15 10:46:08 -06:00
|
|
|
template<class T>
|
|
|
|
inline bool find_enum_item(T *var, const std::string &name) {
|
|
|
|
typedef df::enum_traits<T> traits;
|
|
|
|
int size = traits::last_item_value-traits::first_item_value+1;
|
2012-03-17 02:52:22 -06:00
|
|
|
int idx = findEnumItem(name, size, traits::key_table);
|
2012-03-15 10:46:08 -06:00
|
|
|
if (idx < 0) return false;
|
|
|
|
*var = T(traits::first_item_value+idx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/*
|
|
|
|
* Bitfield tools.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DFHACK_EXPORT bool findBitfieldField(unsigned *idx, const std::string &name,
|
|
|
|
unsigned size, const bitfield_item_info *items);
|
|
|
|
DFHACK_EXPORT void setBitfieldField(void *p, unsigned idx, unsigned size, int value);
|
|
|
|
DFHACK_EXPORT int getBitfieldField(const void *p, unsigned idx, unsigned size);
|
2012-03-15 10:46:08 -06:00
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/**
|
|
|
|
* Find a bitfield item by key string. Returns success code.
|
|
|
|
*/
|
2012-03-15 10:46:08 -06:00
|
|
|
template<class T>
|
2012-03-17 02:52:22 -06:00
|
|
|
inline bool find_bitfield_field(unsigned *idx, const std::string &name, const T* = NULL) {
|
2012-03-15 10:46:08 -06:00
|
|
|
typedef df::bitfield_traits<T> traits;
|
2012-03-17 02:52:22 -06:00
|
|
|
return findBitfieldField(&idx, name, traits::bit_count, traits::bits);
|
2012-03-15 10:46:08 -06:00
|
|
|
}
|
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/**
|
|
|
|
* Find a bitfield item by key and set its value. Returns success code.
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
inline bool set_bitfield_field(T *bitfield, const std::string &name, int value)
|
|
|
|
{
|
|
|
|
typedef df::bitfield_traits<T> traits;
|
|
|
|
unsigned idx;
|
|
|
|
if (!findBitfieldField(&idx, name, traits::bit_count, traits::bits)) return false;
|
|
|
|
setBitfieldField(&bitfield->whole, idx, traits::bits[idx].size, value);
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-15 10:46:08 -06:00
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/**
|
|
|
|
* Find a bitfield item by key and retrieve its value. Returns success code.
|
|
|
|
*/
|
2012-03-15 10:46:08 -06:00
|
|
|
template<class T>
|
2012-03-17 02:52:22 -06:00
|
|
|
inline bool get_bitfield_field(int *value, const T &bitfield, const std::string &name)
|
|
|
|
{
|
2012-03-15 10:46:08 -06:00
|
|
|
typedef df::bitfield_traits<T> traits;
|
2012-03-17 02:52:22 -06:00
|
|
|
unsigned idx;
|
|
|
|
if (!findBitfieldField(&idx, name, traits::bit_count, traits::bits)) return false;
|
|
|
|
*value = getBitfieldField(&bitfield.whole, idx, traits::bits[idx].size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFHACK_EXPORT void bitfieldToString(std::vector<std::string> *pvec, const void *p,
|
|
|
|
unsigned size, const bitfield_item_info *items);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represent bitfield bits as strings in a vector.
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
inline void bitfield_to_string(std::vector<std::string> *pvec, const T &val) {
|
|
|
|
typedef df::bitfield_traits<T> traits;
|
|
|
|
bitfieldToString(pvec, &val.whole, traits::bit_count, traits::bits);
|
2012-03-15 10:46:08 -06:00
|
|
|
}
|
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/**
|
|
|
|
* Represent bitfield bits as a string, using sep as join separator.
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
inline std::string bitfield_to_string(const T &val, const std::string &sep = " ") {
|
|
|
|
std::vector<std::string> tmp;
|
|
|
|
bitfield_to_string<T>(&tmp, val);
|
|
|
|
return join_strings(sep, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BitArray tools
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a flag array item by key string. Returns success code.
|
|
|
|
*/
|
|
|
|
template<class T>
|
2012-03-17 05:36:42 -06:00
|
|
|
inline bool find_flagarray_field(unsigned *idx, const std::string &name, const BitArray<T>*) {
|
2012-03-17 02:52:22 -06:00
|
|
|
T tmp;
|
|
|
|
if (!find_enum_item(&tmp, name) || tmp < 0) return false;
|
|
|
|
*idx = unsigned(tmp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a flag array item by key and set its value. Returns success code.
|
|
|
|
*/
|
|
|
|
template<class T>
|
2012-03-17 05:36:42 -06:00
|
|
|
inline bool set_flagarray_field(BitArray<T> *bitfield, const std::string &name, int value)
|
2012-03-17 02:52:22 -06:00
|
|
|
{
|
|
|
|
T tmp;
|
|
|
|
if (!find_enum_item(&tmp, name) || tmp < 0) return false;
|
|
|
|
bitfield->set(tmp, value!=0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a flag array item by key and retrieve its value. Returns success code.
|
|
|
|
*/
|
|
|
|
template<class T>
|
2012-03-17 05:36:42 -06:00
|
|
|
inline bool get_flagarray_field(int *value, const BitArray<T> &bitfield, const std::string &name)
|
2012-03-17 02:52:22 -06:00
|
|
|
{
|
|
|
|
T tmp;
|
|
|
|
if (!find_enum_item(&tmp, name) || tmp < 0) return false;
|
|
|
|
*value = (bitfield->is_set(tmp) ? 1 : 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFHACK_EXPORT void flagarrayToString(std::vector<std::string> *pvec, const void *p,
|
|
|
|
int bytes, int base, int size, const char *const *items);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represent flag array bits as strings in a vector.
|
|
|
|
*/
|
|
|
|
template<class T>
|
2012-03-17 05:36:42 -06:00
|
|
|
inline void flagarray_to_string(std::vector<std::string> *pvec, const BitArray<T> &val) {
|
2012-03-17 02:52:22 -06:00
|
|
|
typedef df::enum_traits<T> traits;
|
|
|
|
int size = traits::last_item_value-traits::first_item_value+1;
|
|
|
|
flagarrayToString(pvec, val.bits, val.size,
|
|
|
|
(int)traits::first_item_value, size, traits::key_table);
|
|
|
|
}
|
|
|
|
|
2012-03-17 05:36:42 -06:00
|
|
|
/**
|
|
|
|
* Represent flag array bits as a string, using sep as join separator.
|
|
|
|
*/
|
|
|
|
template<class T>
|
2012-03-24 02:43:53 -06:00
|
|
|
inline std::string flagarray_to_string(const BitArray<T> &val, const std::string &sep = " ") {
|
2012-03-17 05:36:42 -06:00
|
|
|
std::vector<std::string> tmp;
|
|
|
|
flagarray_to_string<T>(&tmp, val);
|
|
|
|
return join_strings(sep, tmp);
|
|
|
|
}
|
2012-03-17 02:52:22 -06:00
|
|
|
}
|
2012-03-15 10:46:08 -06:00
|
|
|
|
|
|
|
#define ENUM_ATTR(enum,attr,val) (df::enum_traits<df::enum>::attrs(val).attr)
|
2011-12-24 03:51:58 -07:00
|
|
|
#define ENUM_ATTR_STR(enum,attr,val) DFHack::ifnull(ENUM_ATTR(enum,attr,val),"?")
|
2012-03-17 02:52:22 -06:00
|
|
|
#define ENUM_KEY_STR(enum,val) (DFHack::enum_item_key<df::enum>(val))
|
2012-03-15 10:46:08 -06:00
|
|
|
#define ENUM_FIRST_ITEM(enum) (df::enum_traits<df::enum>::first_item)
|
|
|
|
#define ENUM_LAST_ITEM(enum) (df::enum_traits<df::enum>::last_item)
|
2011-12-24 03:51:58 -07:00
|
|
|
|
2012-01-03 08:25:55 -07:00
|
|
|
#define ENUM_NEXT_ITEM(enum,val) \
|
2012-03-15 10:46:08 -06:00
|
|
|
(DFHack::next_enum_item<df::enum>(val))
|
2012-01-03 08:25:55 -07:00
|
|
|
#define FOR_ENUM_ITEMS(enum,iter) \
|
2012-01-21 07:50:44 -07:00
|
|
|
for(df::enum iter = ENUM_FIRST_ITEM(enum); iter <= ENUM_LAST_ITEM(enum); iter = df::enum(1+int(iter)))
|
2012-01-03 08:25:55 -07:00
|
|
|
|
2012-03-17 02:52:22 -06:00
|
|
|
/*
|
|
|
|
* Include mandatory generated headers.
|
|
|
|
*/
|
|
|
|
|
2012-02-11 05:27:12 -07:00
|
|
|
// Global object pointers
|
|
|
|
#include "df/global_objects.h"
|
2012-01-19 03:30:22 -07:00
|
|
|
|
|
|
|
// A couple of headers that have to be included at once
|
|
|
|
#include "df/coord2d.h"
|
|
|
|
#include "df/coord.h"
|