• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

fn_call.h

Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
00003 //   Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 #ifndef GNASH_FN_CALL_H
00020 #define GNASH_FN_CALL_H
00021 
00022 #include "as_environment.h" 
00023 #include "as_object.h"
00024 #include "as_value.h"
00025 #include "VM.h"
00026 #include "GnashException.h"
00027 
00028 #include <string>
00029 #include <vector>
00030 #include <cassert> 
00031 #include <ostream>
00032 #include <sstream>
00033 #include <algorithm>
00034 
00035 // Forward declarations
00036 namespace gnash {
00037     class as_environment;
00038     class as_function;
00039     class movie_definition;
00040 }
00041 
00042 namespace gnash {
00043 
00045 //
00047 //
00049 //
00052 //
00056 template<typename T>
00057 class FunctionArgs
00058 {
00059 public:
00060 
00061     typedef typename std::vector<T>::size_type size_type;
00062     typedef std::vector<T> container_type;
00063     typedef T value_type;
00064 
00065     FunctionArgs() {}
00066 
00068     FunctionArgs(const FunctionArgs& other)
00069         :
00070         _v(other._v)
00071     {}
00072 
00073     FunctionArgs& operator+=(const T& t) {
00074         _v.push_back(t);
00075         return *this;
00076     }
00077 
00078     FunctionArgs& operator,(const T& t) {
00079         _v.push_back(t);
00080         return *this;
00081     }
00082 
00084     //
00087     void setReachable() const {
00088         std::for_each(_v.begin(), _v.end(),
00089                       std::mem_fun_ref(&as_value::setReachable));
00090     }
00091 
00092     void swap(std::vector<T>& to) {
00093         std::swap(_v, to);
00094     }
00095 
00096     size_type size() const {
00097         return _v.size();
00098     }
00099 
00100 private:
00101     std::vector<T> _v;
00102 };
00103 
00104 
00108 class fn_call
00109 {
00110 public:
00111 
00112     typedef FunctionArgs<as_value> Args;
00113 
00115     //
00122     fn_call(as_object* this_in, const as_environment& env_in,
00123             Args& args, as_object* sup = 0, bool isNew = false)
00124         :
00125         this_ptr(this_in),
00126         super(sup),
00127         nargs(args.size()),
00128         callerDef(0),
00129         _env(env_in),
00130         _new(isNew)
00131     {
00132         args.swap(_args);
00133     }
00134     
00135     fn_call(as_object* this_in, const as_environment& env_in)
00136         :
00137         this_ptr(this_in),
00138         super(0),
00139         nargs(0),
00140         callerDef(0),
00141         _env(env_in),
00142         _new(false)
00143         {
00144         }
00145     
00147     fn_call(const fn_call& fn)
00148         :
00149         this_ptr(fn.this_ptr),
00150         super(fn.super),
00151         nargs(fn.nargs),
00152         callerDef(fn.callerDef),
00153         _env(fn._env),
00154         _args(fn._args),
00155         _new(false)
00156         {
00157         }
00158     
00161     as_object* this_ptr;
00162     
00164     //
00166     as_object* super;
00167     
00169     Args::size_type nargs;
00170     
00172     const movie_definition* callerDef;
00173     
00175     VM& getVM() const {
00176         return _env.getVM();
00177     }
00178     
00180     bool isInstantiation() const {
00181         return _new;
00182         }
00183     
00185     const Args::value_type& arg(unsigned int n) const {
00186         assert(n < nargs);
00187         return _args[n]; 
00188         }
00189     
00190     const Args::container_type& getArgs() const {
00191         return _args;
00192     }
00193     
00194     void drop_bottom() {
00195         assert(!_args.empty());
00196         _args.erase(_args.begin());
00197         --nargs;
00198         }
00199     
00200     const as_environment& env() const {
00201         return _env;
00202         }
00203     
00205     void dump_args(std::ostream& os) const {
00206         for (size_t i = 0; i < nargs; ++i) {
00207             if ( i ) os << ", ";
00208             os << arg(i).toDebugString();
00209         }
00210         }
00211     
00212     void resetArgs() {
00213         nargs = 0;
00214         _args.clear();
00215         }
00216     
00217     void pushArg(const Args::value_type& arg) {
00218         ++nargs;
00219         _args.push_back(arg);
00220         }
00221     
00222 private:
00223 
00226     const as_environment& _env;
00227     
00229     Args::container_type _args;
00230     
00231     bool _new;
00232     
00233 };
00234 
00235 
00237 //
00239 template<typename T>
00240 struct ThisIs
00241 {
00242     typedef T value_type;
00243     value_type* operator()(as_object* o) const {
00244         return dynamic_cast<value_type*>(o);
00245     }
00246 };
00247 
00248 
00250 //
00252 template<typename T>
00253 struct ThisIsNative
00254 {
00255     typedef T value_type;
00256     value_type* operator()(as_object* o) const {
00257         return dynamic_cast<value_type*>(o->relay());
00258     }
00259 };
00260 
00262 //
00264 template<typename T = DisplayObject>
00265 struct IsDisplayObject
00266 {
00267     typedef T value_type;
00268     value_type* operator()(as_object* o) const {
00269         if (!o) return 0;
00270         return dynamic_cast<T*>(o->displayObject());
00271     }
00272 };
00273 
00275 struct ValidThis
00276 {
00277     typedef as_object value_type;
00278     value_type* operator()(as_object* o) const {
00279         return o;
00280     }
00281 };
00282 
00284 //
00288 //
00293 //
00301 template<typename T>
00302 typename T::value_type*
00303 ensure(const fn_call& fn)
00304 {
00305     as_object* obj = fn.this_ptr;
00306     if (!obj) throw ActionTypeError();
00307     
00308     typename T::value_type* ret = T()(obj);
00309     
00310     if (!ret) {
00311         std::string target = typeName(ret);
00312         std::string source = typeName(obj);
00313 
00314         std::string msg = "Function requiring " + target + " as 'this' "
00315             "called from " + source + " instance.";
00316 
00317         throw ActionTypeError(msg);
00318     }
00319     return ret;
00320 }
00321 
00322 inline string_table&
00323 getStringTable(const fn_call& fn)
00324 {
00325     return fn.getVM().getStringTable();
00326 }
00327 
00328 inline movie_root&
00329 getRoot(const fn_call& fn)
00330 {
00331     return fn.getVM().getRoot();
00332 }
00333 
00334 inline int
00335 getSWFVersion(const fn_call& fn)
00336 {
00337     return fn.getVM().getSWFVersion();
00338 }
00339 
00340 inline VM&
00341 getVM(const fn_call& fn)
00342 {
00343     return fn.getVM();
00344 }
00345 
00346 inline Global_as&
00347 getGlobal(const fn_call& fn)
00348 {
00349     return *fn.getVM().getGlobal();
00350 }
00351 
00352 } // namespace gnash
00353 
00354 
00355 #endif 
00356 
00357 
00358 // Local Variables:
00359 // mode: C++
00360 // indent-tabs-mode: nil
00361 // End:

Generated on Thu Sep 30 2010 14:34:58 for Gnash by  doxygen 1.7.1