00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef FLC_VARDEF_H
00017 #define FLC_VARDEF_H
00018
00019 #include <falcon/setup.h>
00020 #include <falcon/types.h>
00021 #include <falcon/common.h>
00022 #include <falcon/reflectfunc.h>
00023
00024 namespace Falcon {
00025
00026 class Symbol;
00027 class Stream;
00028
00045 class FALCON_DYN_CLASS VarDef: public BaseAlloc
00046 {
00047 public:
00048 typedef enum {
00049 t_nil,
00050 t_int,
00051 t_bool,
00052 t_num,
00053 t_string,
00054 t_symbol,
00055 t_base,
00056 t_reference,
00057 t_reflective,
00058 t_reflectFunc
00059 } t_type;
00060 private:
00061 t_type m_val_type;
00062 bool m_bReadOnly;
00063
00064 union {
00065 bool val_bool;
00066 uint64 val_int;
00067 numeric val_num;
00068
00069 struct {
00070 t_reflection mode;
00071 uint32 offset;
00072 } val_reflect;
00073
00074 struct {
00075 reflectionFunc from;
00076 reflectionFunc to;
00077 void *data;
00078 } val_rfunc;
00079
00080 const String *val_str;
00081 Symbol *val_sym;
00082 } m_value;
00083
00084 public:
00085
00086 VarDef():
00087 m_val_type(t_nil),
00088 m_bReadOnly( false )
00089 {}
00090
00091 explicit VarDef( bool val ):
00092 m_val_type(t_bool),
00093 m_bReadOnly( false )
00094 {
00095 m_value.val_bool = val;
00096 }
00097
00098 VarDef( int64 val ):
00099 m_val_type(t_int),
00100 m_bReadOnly( false )
00101 {
00102 m_value.val_int = val;
00103 }
00104
00105 VarDef( numeric val ):
00106 m_val_type(t_num),
00107 m_bReadOnly( false )
00108 {
00109 m_value.val_num = val;
00110 }
00111
00112 VarDef( const String *str ):
00113 m_val_type(t_string),
00114 m_bReadOnly( false )
00115 {
00116 m_value.val_str = str;
00117 }
00118
00119 VarDef( Symbol *sym ):
00120 m_val_type( t_symbol ),
00121 m_bReadOnly( false )
00122 {
00123 m_value.val_sym = sym;
00124 }
00125
00126 VarDef( t_type t, Symbol *sym ):
00127 m_val_type( t ),
00128 m_bReadOnly( false )
00129 {
00130 m_value.val_sym = sym;
00131 }
00132
00133 VarDef( t_type t, int64 iv ):
00134 m_val_type(t),
00135 m_bReadOnly( false )
00136 {
00137 m_value.val_int = iv;
00138 }
00139
00140 VarDef( reflectionFunc rfrom, reflectionFunc rto=0 ):
00141 m_val_type( t_reflectFunc ),
00142 m_bReadOnly( rto==0 )
00143 {
00144 m_value.val_rfunc.from = rfrom;
00145 m_value.val_rfunc.to = rto;
00146 }
00147
00148 VarDef( t_reflection mode, uint32 offset ):
00149 m_val_type( t_reflective ),
00150 m_bReadOnly( false )
00151 {
00152 m_value.val_reflect.mode = mode;
00153 m_value.val_reflect.offset = offset;
00154 }
00155
00156 t_type type() const { return m_val_type; }
00157
00161 VarDef& setNil() { m_val_type = t_nil; return *this; }
00162 VarDef& setBool( bool val ) { m_val_type = t_bool; m_value.val_bool = val; return *this;}
00163 VarDef& setInteger( int64 val ) { m_val_type = t_int; m_value.val_int = val; return *this;}
00164 VarDef& setString( const String *str ) { m_val_type = t_string; m_value.val_str = str; return *this; }
00165 VarDef& setSymbol( Symbol *sym ) { m_val_type = t_symbol; m_value.val_sym = sym; return *this;}
00166 VarDef& setNumeric( numeric val ) { m_val_type = t_num; m_value.val_num = val; return *this;}
00167 VarDef& setBaseClass( Symbol *sym ) { m_val_type = t_base; m_value.val_sym = sym; return *this;}
00168 VarDef& setReference( Symbol *sym ) { m_val_type = t_reference; m_value.val_sym = sym; return *this;}
00169
00185 VarDef &setReflectFunc( reflectionFunc rfrom, reflectionFunc rto=0, void *reflect_data = 0 ) {
00186 m_val_type = t_reflectFunc;
00187 m_bReadOnly = rto == 0;
00188 m_value.val_rfunc.from = rfrom;
00189 m_value.val_rfunc.to = rto;
00190 m_value.val_rfunc.data = reflect_data;
00191 return *this;
00192 }
00193
00197 VarDef &setReflective( t_reflection mode, uint32 offset )
00198 {
00199 m_val_type = t_reflective;
00200 m_value.val_reflect.mode = mode;
00201 m_value.val_reflect.offset = offset;
00202 return *this;
00203 }
00204
00209 VarDef& setReflective( t_reflection mode, void *base, void *position )
00210 {
00211 return setReflective( mode, static_cast<uint32>(
00212 static_cast<char *>(position) - static_cast<char *>(base)) );
00213 }
00214
00218 VarDef& setReadOnly( bool ro ) {
00219 m_bReadOnly = ro;
00220 return *this;
00221 }
00222
00223
00224 bool asBool() const { return m_value.val_bool; }
00225 int64 asInteger() const { return m_value.val_int; }
00226 const String *asString() const { return m_value.val_str; }
00227 Symbol *asSymbol() const { return m_value.val_sym; }
00228 numeric asNumeric() const { return m_value.val_num; }
00229 reflectionFunc asReflectFuncFrom() const { return m_value.val_rfunc.from; }
00230 reflectionFunc asReflectFuncTo() const { return m_value.val_rfunc.to; }
00231 void* asReflectFuncData() const { return m_value.val_rfunc.data; }
00232 t_reflection asReflecMode() const { return m_value.val_reflect.mode; }
00233 uint32 asReflecOffset() const { return m_value.val_reflect.offset; }
00234
00235 bool isNil() const { return m_val_type == t_nil; }
00236 bool isBool() const { return m_val_type == t_bool; }
00237 bool isInteger() const { return m_val_type == t_int; }
00238 bool isString() const { return m_val_type == t_string; }
00239 bool isNumeric() const { return m_val_type == t_num; }
00240 bool isSymbol() const { return m_val_type == t_symbol || m_val_type == t_base; }
00241 bool isBaseClass() const { return m_val_type == t_base; }
00242 bool isReference() const { return m_val_type == t_reference; }
00243 bool isReflective() const { return m_val_type == t_reflective; }
00244 bool isReflectFunc() const { return m_val_type == t_reflectFunc; }
00245 bool isReadOnly() const { return m_bReadOnly; }
00246
00247 bool save( Stream *out ) const;
00248 bool load( Module *mod, Stream *in );
00249 };
00250
00251 }
00252
00253 #endif
00254
00255