00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef USEVAL_H
00023 #define USEVAL_H 1
00024
00025 #include <cassert>
00026 #include <iostream>
00027
00028 #include <vector>
00029 #include <deque>
00030 #include <string>
00031
00032 class Game_object;
00033
00034
00035
00036
00037 class Usecode_value
00038 {
00039 public:
00040 enum Val_type
00041 {
00042 int_type = 0,
00043 string_type = 1,
00044 array_type = 2,
00045 end_of_array_type = 3,
00046 pointer_type = 4
00047 };
00048 private:
00049 Val_type type;
00050 union
00051 {
00052 long intval;
00053 char *str;
00054 Usecode_value *array;
00055 Game_object *ptr;
00056 } value;
00057
00058 bool undefined;
00059
00060 static int count_array(const Usecode_value& val);
00061 public:
00062 inline Usecode_value() : type(int_type), undefined(true)
00063 { value.intval = 0; }
00064 inline Usecode_value(int ival) : type(int_type), undefined(false)
00065 { value.intval = ival; }
00066 Usecode_value(const char *s);
00067
00068 Usecode_value(int size, Usecode_value *elem0)
00069 : type(array_type), undefined(false)
00070 {
00071 value.array = new Usecode_value[size + 1];
00072 value.array[size].type = end_of_array_type;
00073 if (elem0)
00074 value.array[0] = *elem0;
00075 }
00076 Usecode_value(Game_object *ptr) : type(pointer_type), undefined(false)
00077 { value.ptr = ptr; }
00078 ~Usecode_value();
00079 Usecode_value& operator=(const Usecode_value& v2);
00080 Usecode_value& operator=(const char *str);
00081
00082 inline Usecode_value(const Usecode_value& v2)
00083 : type(int_type)
00084 { *this = v2; }
00085
00086 Usecode_value operator+(const Usecode_value& v2);
00087
00088 void push_back(int);
00089 bool operator==(const Usecode_value& v2) const;
00090 bool operator!=(const Usecode_value& v2) const { return !(*this == v2); }
00091
00092 inline Val_type get_type() const
00093 { return type; }
00094 int get_array_size() const
00095 { return (type == array_type) ? count_array(*this) : 0; }
00096 bool is_array() const
00097 { return (type == array_type); }
00098 bool is_int() const
00099 { return (type == int_type); }
00100 bool is_ptr() const
00101 { return (type == pointer_type); }
00102 long get_int_value() const
00103 {
00104 #ifdef DEBUG
00105 if (type == pointer_type || (type == int_type && (value.intval > 0x10000 || value.intval < -0x10000)))
00106 std::cerr << "Probable attempt at getting int value of pointer!!" << std::endl;
00107 #endif
00108 return ((type == int_type) ? value.intval : 0);
00109 }
00110 Game_object* get_ptr_value() const
00111 { return ((type == pointer_type) ? value.ptr : 0); }
00112
00113 const char *get_str_value() const
00114 { return ((type == string_type) ? value.str : 0); }
00115 long need_int_value() const
00116 {
00117
00118 const char *str = get_str_value();
00119 return str ? std::atoi(str)
00120 : ((type == array_type && get_array_size())
00121 ? value.array[0].need_int_value()
00122
00123 : (type == pointer_type ? (value.intval&0x7ffffff)
00124 : get_int_value()));
00125 }
00126
00127 void put_elem(int i, Usecode_value& val)
00128 { value.array[i] = val; }
00129
00130 inline Usecode_value& get_elem(int i) const
00131 {
00132 static Usecode_value zval(0);
00133
00134 return (type == array_type) ? value.array[i] : zval;
00135 }
00136 inline Usecode_value& operator[](int i)
00137 {
00138 assert(type == array_type);
00139 return value.array[i];
00140 }
00141
00142 inline Usecode_value& get_elem0()
00143 { return (type == array_type) ? value.array[0] : *this; }
00144 inline bool is_false() const
00145 {
00146 switch(type)
00147 {
00148 case int_type:
00149 return value.intval == 0;
00150 case pointer_type:
00151 return value.ptr == NULL;
00152 case array_type:
00153 return value.array[0].type == end_of_array_type;
00154 default:
00155 return false;
00156 }
00157 }
00158 inline bool is_true() const
00159 { return !is_false(); }
00160
00161 inline bool is_undefined() const
00162 { return undefined; }
00163
00164 int resize(int new_size);
00165
00166 int find_elem(const Usecode_value& val);
00167
00168 Usecode_value& concat(Usecode_value& val2);
00169 void append(int *vals, int cnt);
00170
00171 int add_values(int index, Usecode_value& val2);
00172 void print(std::ostream& out, bool shortformat=false);
00173
00174 int save(unsigned char *buf, int len);
00175 bool restore(unsigned char *& ptr, int len);
00176 };
00177
00178
00179 std::ostream& operator<<(std::ostream& out, Usecode_value& val);
00180
00181 #endif