script_modify.h

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00017 #ifndef __SCRIPT_MODIFY_HEADER__
00018 #define __SCRIPT_MODIFY_HEADER__
00019 
00020 #include "utils.h"
00021 #include "defs.h"
00022 
00023 #include "script.h"
00024 #include "script_read.h"
00025 
00026 namespace hoa_script {
00027 
00046 class ModifyScriptDescriptor : public ReadScriptDescriptor {
00047   friend class GameScript;
00048 public:
00049   ~ModifyScriptDescriptor();
00050 
00056   bool OpenFile(const std::string& file_name);
00057   bool OpenFile();
00058   void CloseFile();
00060 
00069   void ModifyBool(const std::string& key, bool value)
00070     { _ModifyData(key, value); }
00071 
00072   void ModifyBool(int32 key, bool value)
00073     { _ModifyData(key, value); }
00074   
00075   void ModifyInt(const std::string& key, int32 value)
00076     { _ModifyData(key, value); }
00077   
00078   void ModifyInt(int32 key, int32 value)
00079     { _ModifyData(key, value); }
00080   
00081   void ModifyUInt(const std::string& key, uint32 value)
00082     { _ModifyData(key, value); }
00083 
00084   void ModifyUInt(int32 key, uint32 value)
00085     { _ModifyData(key, value); }
00086 
00087   void ModifyFloat(const std::string& key, float value)
00088     { _ModifyData(key, value); }
00089   
00090   void ModifyFloat(int32 key, float value)
00091     { _ModifyData(key, value); }
00092   
00093   void ModifyString(const std::string& key, const std::string& value)
00094     { _ModifyData(key, value); }
00095   
00096   void ModifyString(int32 key, const std::string& value)
00097     { _ModifyData(key, value); }
00099 
00110   void AddNewBool(const std::string& key, bool value)
00111     { _AddNewData(key, value); }
00112 
00113   void AddNewBool(int32 key, bool value)
00114     { _AddNewData(key, value); }
00115   
00116   void AddNewInt(const std::string& key, int32 value)
00117     { _AddNewData(key, value); }
00118   
00119   void AddNewInt(int32 key, int32 value)
00120     { _AddNewData(key, value); }
00121   
00122   void AddNewUInt(const std::string& key, uint32 value)
00123     { _AddNewData(key, value); }
00124 
00125   void AddNewUInt(int32 key, uint32 value)
00126     { _AddNewData(key, value); }
00127 
00128   void AddNewFloat(const std::string& key, float value)
00129     { _AddNewData(key, value); }
00130   
00131   void AddNewFloat(int32 key, float value)
00132     { _AddNewData(key, value); }
00133   
00134   void AddNewString(const std::string& key, const std::string& value)
00135     { _AddNewData(key, value); }
00136   
00137   void AddNewString(int32 key, const std::string& value)
00138     { _AddNewData(key, value); }
00140 
00141   // TODO: Add methods to allow the creation of new tables
00142 //  void CreateNewTable(const std::string& key);
00143 //  void CreateNewTable(uint32 key);
00144 //  void EndNewTable();
00145 
00157   void CommitChanges(bool leave_closed = false);
00158 
00159 private:
00164   void _CommitTable(WriteScriptDescriptor& file, const luabind::object& table);
00165 
00171   template <class T> void _ModifyData(const std::string& key, T value);
00172   template <class T> void _ModifyData(int32 key, T value);
00174 
00180   template <class T> void _AddNewData(const std::string& key, T value);
00181   template <class T> void _AddNewData(int32 key, T value);
00183 }; // class ModifyScriptDescriptor
00184 
00185 //-----------------------------------------------------------------------------
00186 // Template Function Definitions
00187 //-----------------------------------------------------------------------------
00188 
00189 template <class T> void ModifyScriptDescriptor::_ModifyData(const std::string& key, T value) {
00190   // A pointer to the table where the object is contained
00191   luabind::object* table = NULL;
00192 
00193   if (_open_tables.empty() == false) // Retrieve the globals table
00194     table = new luabind::object(luabind::from_stack(_lstack, LUA_GLOBALSINDEX));
00195   else // Retrieve the most recently opened table from the top of the stack
00196     table = new luabind::object(luabind::from_stack(_lstack, private_script::STACK_TOP));
00197 
00198   if (luabind::type(*table) != LUA_TTABLE) {
00199     _error_messages << "* _ModifyData() failed because it could not construct the table "
00200       << "where the data resided: " << key << std::endl;
00201     delete(table);
00202     return;
00203   }
00204 
00205   for (luabind::iterator i(*table); i != private_script::TABLE_END; ++i) {
00206     // Check to see if global value exists
00207     try {
00208       if (luabind::object_cast<std::string>(i.key()) == key) {
00209         *i = value;
00210         delete(table);
00211         return;
00212       }
00213     }
00214     catch (...)  {
00215       // A cast failure does not indicate an error, simply continue on with the next key
00216     }
00217   }
00218 
00219   // If the code reaches this far, then the variable could not be found
00220   _error_messages << "* _ModifyData() failed because in the active scope, it did not find the "
00221     << "table key: " << key << std::endl;
00222   delete(table);
00223 } // template <class T> void ModifyScriptDescriptor::_ModifyData(const std::string& key, T value) {
00224 
00225 
00226 
00227 template <class T> void ModifyScriptDescriptor::_ModifyData(int32 key, T value) {
00228   if (_open_tables.empty() == false) {
00229     _error_messages << "* _ModifyData() failed because there were no open tables when the "
00230       << "function was invoked for key: " << key << std::endl;
00231     return;
00232   }
00233 
00234   luabind::object table(luabind::from_stack(_lstack, private_script::STACK_TOP));
00235 
00236   if (luabind::type(table) != LUA_TTABLE) {
00237     _error_messages << "* _ModifyData() failed because the top of the stack was not a table "
00238       << "when trying to modify the data named: " << key << std::endl;
00239     return;
00240   }
00241 
00242   for (luabind::iterator i(table); i != private_script::TABLE_END; ++i) {
00243     // Check to see if global value exists
00244     try {
00245       if (luabind::object_cast<int32>(i.key()) == key) {
00246         *i = value;
00247         delete(table);
00248         return;
00249       }
00250     }
00251     catch (...)  {
00252       // A cast failure does not indicate an error, simply continue on with the next key
00253     }
00254   }
00255 
00256   // If the code reaches this far, then the variable could not be found
00257   _error_messages << "* _ModifyData() failed because in the active scope, it did not find the "
00258     << "table key: " << key << std::endl;
00259   delete(table);
00260 } // template <class T> void ModifyScriptDescriptor::_ModifyData(int32 key, T value) {
00261 
00262 
00263 
00264 template <class T> void ModifyScriptDescriptor::_AddNewData(const std::string& key, T value) {
00265   // A pointer to the table where the object is contained
00266   luabind::object* table = NULL;
00267 
00268   if (_open_tables.empty() == false) // Retrieve the globals table
00269     table = new luabind::object(luabind::from_stack(_lstack, LUA_GLOBALSINDEX));
00270   else // Retrieve the most recently opened table from the top of the stack
00271     table = new luabind::object(luabind::from_stack(_lstack, private_script::STACK_TOP));
00272 
00273   if (luabind::type(*table) != LUA_TTABLE) {
00274     _error_messages << "* _AddNewData() failed because the top of the stack was not a table "
00275       << "when trying to add the new data: " << key << std::endl;
00276     return;
00277   }
00278 
00279   // NOTE: If the key already exists in the table, its value will be overwritten here
00280   luabind::settable(*table, key, value);
00281 } // template <class T> void ModifyScriptDescriptor::_AddNewData(const std::string& key, T value)
00282 
00283 
00284 
00285 template <class T> void ModifyScriptDescriptor::_AddNewData(int32 key, T value) {
00286   if (_open_tables.empty() == false) {
00287     _error_messages << "* _AddNewData() failed because there were no open tables when the "
00288       << "function was invoked for key: " << key << std::endl;
00289     return;
00290   }
00291 
00292   // A pointer to the table where the object is contained
00293   luabind::object table(luabind::from_stack(_lstack, private_script::STACK_TOP));
00294 
00295   if (luabind::type(table) != LUA_TTABLE) {
00296     _error_messages << "* _AddNewData() failed because the top of the stack was not a table "
00297       << "when trying to add the new data: " << key << std::endl;
00298     return;
00299   }
00300 
00301   // NOTE: If the key already exists in the table, its value will be overwritten here
00302   luabind::settable(table, key, value);
00303 } // template <class T> void ModifyScriptDescriptor::_AddNewData(int32 key, T value)
00304 
00305 } // namespace hoa_script
00306 
00307 #endif // __SCRIPT_MODIFY_HEADER

Generated on Fri Jul 6 23:11:22 2007 for Hero of Allacrost by  doxygen 1.5.1