@ -25,6 +25,7 @@ distribution.
# pragma once
# include <string>
# include <sstream>
# include <vector>
# include <map>
@ -36,6 +37,10 @@ distribution.
# undef interface
# endif
/*
* Definitions of DFHack namespace structs used by generated headers .
*/
namespace DFHack
{
class virtual_class { } ;
@ -153,6 +158,10 @@ inline int linear_index(const DFHack::enum_list_attr<const char*> &lst, const st
return - 1 ;
}
/*
* Definitions of df namespace structs used by generated headers .
*/
namespace df
{
using DFHack : : virtual_ptr ;
@ -213,8 +222,20 @@ namespace df
namespace enums { }
}
/*
* Templates for access to enum and bitfield traits .
*/
DFHACK_EXPORT std : : string join_strings ( const std : : string & separator , const std : : vector < std : : string > & items ) ;
namespace DFHack {
// Enums
/*
* Enum trait tools .
*/
/**
* Return the next item in the enum , wrapping to the first one at the end .
*/
template < class T >
inline typename df : : enum_traits < T > : : enum_type next_enum_item ( T v ) {
typedef df : : enum_traits < T > traits ;
@ -223,55 +244,185 @@ namespace DFHack {
return ( iv < traits : : last_item_value ) ? T ( iv + 1 ) : traits : : first_item ;
}
/**
* Check if the value is valid for its enum type .
*/
template < class T >
inline bool is_valid_enum_item ( T v ) {
return df : : enum_traits < T > : : is_valid ( v ) ;
}
/**
* Return the enum item key string pointer , or NULL if none .
*/
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 ;
}
/**
* Return the enum item key string pointer , or " ? " if none .
*/
template < class T >
inline const char * enum_item_key_str ( T val ) {
return ifnull ( enum_item_raw_key ( val ) , " ? " ) ;
}
DFHACK_EXPORT int findEnumItem_ ( const std : : string & name , int size , const char * const * items ) ;
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 ( ) ;
}
/**
* 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 .
*/
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 ;
int idx = findEnumItem_ ( name , size , traits : : key_table ) ;
int idx = findEnumItem ( name , size , traits : : key_table ) ;
if ( idx < 0 ) return false ;
* var = T ( traits : : first_item_value + idx ) ;
return true ;
}
DFHACK_EXPORT int findBitfieldField_ ( const std : : string & name , int size , const bitfield_item_info * items ) ;
/*
* 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 ) ;
/**
* Find a bitfield item by key string . Returns success code .
*/
template < class T >
inline int findBitfieldField ( const std : : string & name ) {
inline bool find_bitfield_field ( unsigned * idx , const std : : string & name , const T * = NULL ) {
typedef df : : bitfield_traits < T > traits ;
return findBitfieldField_ ( name , traits : : bit_count , traits : : bits ) ;
return findBitfieldField ( & idx , name , traits : : bit_count , traits : : bits ) ;
}
DFHACK_EXPORT std : : string bitfieldToString ( const void * p , int size , const bitfield_item_info * items ) ;
/**
* 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 ;
}
/**
* Find a bitfield item by key and retrieve its value . Returns success code .
*/
template < class T >
inline std : : string bitfieldToString ( const T & val ) {
inline bool get_bitfield_field ( int * value , const T & bitfield , const std : : string & name )
{
typedef df : : bitfield_traits < T > traits ;
return bitfieldToString ( & val . whole , traits : : bit_count , traits : : bits ) ;
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 ) ;
}
/**
* 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 >
inline bool find_bitfield_field ( unsigned * idx , const std : : string & name , const BitArray < T > * ) {
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 >
inline bool set_bitfield_field ( BitArray < T > * bitfield , const std : : string & name , int value )
{
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 >
inline bool get_bitfield_field ( int * value , const BitArray < T > & bitfield , const std : : string & name )
{
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 >
inline void bitfield_to_string ( std : : vector < std : : string > * pvec , const BitArray < T > & val ) {
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 ) ;
}
}
# define ENUM_ATTR(enum,attr,val) (df::enum_traits<df::enum>::attrs(val).attr)
# define ENUM_ATTR_STR(enum,attr,val) DFHack::ifnull(ENUM_ATTR(enum,attr,val),"?")
# define ENUM_KEY_STR(enum,val) (DFHack::enum_item_key_str<df::enum>(val))
# define ENUM_KEY_STR(enum,val) (DFHack::enum_item_key <df::enum>(val))
# define ENUM_FIRST_ITEM(enum) (df::enum_traits<df::enum>::first_item)
# define ENUM_LAST_ITEM(enum) (df::enum_traits<df::enum>::last_item)
@ -280,6 +431,10 @@ namespace DFHack {
# define FOR_ENUM_ITEMS(enum,iter) \
for ( df : : enum iter = ENUM_FIRST_ITEM ( enum ) ; iter < = ENUM_LAST_ITEM ( enum ) ; iter = df : : enum ( 1 + int ( iter ) ) )
/*
* Include mandatory generated headers .
*/
// Global object pointers
# include "df/global_objects.h"