home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / scripted_plugin.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-03-30  |  2.5 KB  |  91 lines

  1. #ifndef K3DSDK_SCRIPTED_PLUGIN_H
  2. #define K3DSDK_SCRIPTED_PLUGIN_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2008, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /** \file
  24.         \author Tim Shead (tshead@k-3d.com)
  25. */
  26.  
  27. #include "data.h"
  28. #include "k3d-i18n-config.h"
  29. #include "plugins.h"
  30. #include "scripting.h"
  31.  
  32. #include <boost/scoped_ptr.hpp>
  33.  
  34. namespace k3d
  35. {
  36.  
  37. /// Convenience class for application plugins that need to call scripts
  38. template<typename base_t>
  39. class scripted_plugin :
  40.     public base_t
  41. {
  42. public:
  43.     scripted_plugin() :
  44.         m_script(init_owner(*this) + init_name("script") + init_label(_("Script")) + init_description(_("Script source code")) + init_value<string_t>(""))
  45.     {
  46.         m_script.changed_signal().connect(sigc::mem_fun(*this, &scripted_plugin::on_script_changed));
  47.     }
  48.  
  49. protected:
  50.     void set_script(const string_t& Script)
  51.     {
  52.         m_script.set_value(Script);
  53.     }
  54.  
  55.     bool execute_script(iscript_engine::context_t& Context)
  56.     {
  57.         const script::code code(m_script.pipeline_value());
  58.  
  59.         if(!m_script_engine)
  60.         {
  61.             // Examine the script to determine the correct language ...
  62.             const script::language language(code);
  63.  
  64.             return_val_if_fail(language.factory(), false);
  65.  
  66.             // Create our script engine as-needed ...
  67.             m_script_engine.reset(plugin::create<iscript_engine>(language.factory()->factory_id()));
  68.  
  69.             // No script engine?  We're outta here ...
  70.             return_val_if_fail(m_script_engine, false);
  71.         }
  72.  
  73.         // Execute that bad-boy!
  74.         return m_script_engine->execute("Script", code.source(), Context);
  75.     }
  76.  
  77. private:
  78.     void on_script_changed(iunknown* hint)
  79.     {
  80.         m_script_engine.reset();
  81.     }
  82.  
  83.     k3d_data(string_t, immutable_name, change_signal, no_undo, local_storage, no_constraint, script_property, no_serialization) m_script;
  84.     boost::scoped_ptr<iscript_engine> m_script_engine;
  85. };
  86.  
  87. } // namespace k3d
  88.  
  89. #endif // !K3DSDK_SCRIPTED_PLUGIN_H
  90.  
  91.