useval.h

Go to the documentation of this file.
00001 /*
00002  *  useval.h - Values used in Usecode interpreter.
00003  *
00004  *  Copyright (C) 1999  Jeffrey S. Freedman
00005  *  Copyright (C) 2000-2001  The Exult Team
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020  */
00021 
00022 #ifndef USEVAL_H
00023 #define USEVAL_H  1
00024 
00025 #include <cassert>
00026 #include <iostream>
00027 
00028 #include <vector> // STL container
00029 #include <deque>  // STL container
00030 #include <string> // STL string
00031 
00032 class Game_object;
00033 
00034 /*
00035  *  A value that we store can be an integer, string, or array.
00036  */
00037 class Usecode_value
00038   {
00039 public:
00040   enum Val_type     // The types:
00041     {
00042     int_type = 0,
00043     string_type = 1,  // Allocated string.
00044     array_type = 2,
00045     end_of_array_type = 3,  // Marks end of array.
00046     pointer_type = 4
00047     };
00048 private:
00049   Val_type type;    // Type stored here.
00050   union
00051     {
00052     long intval;
00053     char *str;
00054     Usecode_value *array;
00055     Game_object *ptr;
00056     } value;
00057 
00058   bool undefined;
00059           // Count array elements.
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           // Create array with 1st element.
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           // Copy ctor.
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           // Comparator.
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    // Get size of array.
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  // Get integer value.
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  // Get pointer value.
00111     { return ((type == pointer_type) ? value.ptr : 0); }
00112           // Get string value.
00113   const char *get_str_value() const
00114     { return ((type == string_type) ? value.str : 0); }
00115   long need_int_value() const
00116     {
00117           // Convert strings.
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           // Pointer = ref.
00123       : (type == pointer_type ? (value.intval&0x7ffffff)
00124           : get_int_value()));
00125     }
00126           // Add array element. (No checking!)
00127   void put_elem(int i, Usecode_value& val)
00128     { value.array[i] = val; }
00129           // Get an array element.
00130   inline Usecode_value& get_elem(int i) const
00131     {
00132     static Usecode_value zval(0);
00133 //    assert(type == array_type);//+++++Testing.
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           // Get array elem. 0, or this.
00142   inline Usecode_value& get_elem0()
00143     { return (type == array_type) ? value.array[0] : *this; }
00144   inline bool is_false() const  // Represents a FALSE value?
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); // Resize array.
00165           // Look in array for given value.
00166   int find_elem(const Usecode_value& val);
00167           // Concat. to end of this array.
00168   Usecode_value& concat(Usecode_value& val2);
00169   void append(int *vals, int cnt);// Append integer values.
00170           // Add value(s) to an array.
00171   int add_values(int index, Usecode_value& val2);
00172   void print(std::ostream& out, bool shortformat=false); // Print in ASCII.
00173           // Save/restore.
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

Generated on Mon Jul 9 14:42:52 2007 for ExultEngine by  doxygen 1.5.1