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

as_value.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_AS_VALUE_H
00020 #define GNASH_AS_VALUE_H
00021 
00022 #include "dsodefs.h"
00023 #include "CharacterProxy.h"
00024 
00025 #include <limits>
00026 #include <string>
00027 #include <boost/variant.hpp>
00028 #include <ostream> // for inlined output operator
00029 #include <boost/type_traits/is_floating_point.hpp>
00030 #include <boost/utility/enable_if.hpp>
00031 #include <boost/cstdint.hpp>
00032 
00033 #include "utility.h" // UNUSED
00034 
00035 // Forward declarations
00036 namespace gnash {
00037     class VM;
00038         class as_object;
00039         class Global_as;
00040         class fn_call;
00041         class as_function;
00042         class MovieClip;
00043         class DisplayObject;
00044     namespace amf {
00045         class Writer;
00046     }
00047 }
00048 
00049 namespace gnash {
00050 
00051 
00052 // NaN constant for use in as_value implementation
00053 static const double NaN = std::numeric_limits<double>::quiet_NaN();
00054 
00055 // The following template works just like its C counterpart, with added
00056 // type safety (i.e., they will only compile for floating point arguments).
00057 template <typename T>
00058 inline bool
00059 isNaN(const T& num, typename boost::enable_if<boost::is_floating_point<T> >::
00060         type* dummy = 0)
00061 {
00062         UNUSED(dummy);
00063         return num != num;
00064 }
00065 
00066 template <typename T>
00067 inline bool
00068 isInf(const T& num)
00069 {
00070         return isNaN(num - num);
00071 }
00072 
00073 
00075 enum primitive_types
00076 {
00077         PTYPE_STRING,
00078         PTYPE_NUMBER,
00079         PTYPE_BOOLEAN
00080 };
00081 
00083 //
00085 //
00088 //
00091 //
00097 //
00103 //
00106 class as_value
00107 {
00108 
00109 public:
00110 
00111     // The exception type should always be one greater than the normal type.
00112     enum AsType
00113     {
00114         UNDEFINED,
00115         UNDEFINED_EXCEPT,
00116         NULLTYPE,
00117         NULLTYPE_EXCEPT,
00118         BOOLEAN,
00119         BOOLEAN_EXCEPT,
00120         STRING,
00121         STRING_EXCEPT,
00122         NUMBER, 
00123         NUMBER_EXCEPT,
00124         OBJECT,
00125         OBJECT_EXCEPT,
00126         DISPLAYOBJECT,
00127         DISPLAYOBJECT_EXCEPT
00128     };
00129     
00131     DSOEXPORT as_value();
00132     
00134     DSOEXPORT as_value(const char* str);
00135     DSOEXPORT as_value(const std::string& str);
00136     
00138     template <typename T>
00139     as_value(T val, typename boost::enable_if<boost::is_same<bool, T> >::type*
00140              dummy = 0)
00141         :
00142         _type(BOOLEAN),
00143         _value(val)
00144         {
00145             UNUSED(dummy);
00146         }
00147     
00149     as_value(double val);
00150     
00152     as_value(as_object* obj);
00153     
00155     DSOEXPORT as_value(const as_value& value);
00156     
00158     const char* typeOf() const;
00159     
00161     //
00163     primitive_types ptype() const;
00164     
00166     bool is_function() const;
00167     
00169     bool is_string() const {
00170         return _type == STRING;
00171     }
00172     
00174     bool is_number() const {
00175         return _type == NUMBER;
00176     }
00177     
00179     //
00181     bool is_object() const {
00182         return _type == OBJECT || _type == DISPLAYOBJECT;
00183     }
00184     
00186     bool is_sprite() const {
00187         return _type == DISPLAYOBJECT;
00188     }
00189     
00191     //
00196     //
00198     std::string to_string(int version = 7) const;
00199     
00201     //
00203     double to_number() const;
00204     
00206     //
00208     bool to_bool() const;
00209     
00211     //
00213     //
00220     //
00224     as_object* to_object(Global_as& global) const;
00225     
00227     //
00230     //
00233     MovieClip* toMovieClip(bool skipRebinding = false) const;
00234     
00236     //
00239     //
00250     DisplayObject* toDisplayObject(bool skipRebinding = false) const;
00251 
00253     //
00256     as_function* to_function() const;
00257 
00258     // Used for operator<< to give useful information about an
00259     // as_value object.
00260     DSOEXPORT std::string toDebugString() const;
00261     
00262     AsType defaultPrimitive(int version) const;
00263     
00265     //
00267     //
00276     as_value to_primitive(AsType hint) const;
00277     
00279     void set_string(const std::string& str);
00280     
00282     void set_double(double val);
00283     
00285     void set_bool(bool val);
00286 
00288     void set_as_object(as_object* obj);
00289     
00291     void set_undefined();
00292     
00294     void set_null();
00295     
00296     DSOEXPORT void operator=(const as_value& v);
00297     
00298     bool is_undefined() const {
00299         return (_type == UNDEFINED);
00300     }
00301     
00302     bool is_null() const {
00303         return (_type == NULLTYPE);
00304     }
00305     
00306     bool is_bool() const {
00307         return (_type == BOOLEAN);
00308     }
00309     
00310     bool is_exception() const {
00311         return (_type == UNDEFINED_EXCEPT || _type == NULLTYPE_EXCEPT
00312                 || _type == BOOLEAN_EXCEPT || _type == NUMBER_EXCEPT
00313                 || _type == OBJECT_EXCEPT || _type == DISPLAYOBJECT_EXCEPT
00314                 || _type == STRING_EXCEPT);
00315     }
00316     
00317     // Flag or unflag an as_value as an exception -- this gets flagged
00318     // when an as_value is 'thrown'.
00319     void flag_exception() {
00320         if (!is_exception()) {
00321             _type = static_cast<AsType>(static_cast<int>(_type) + 1);
00322         }
00323     }
00324     
00325     void unflag_exception() {
00326         if (is_exception()) {
00327             _type = static_cast<AsType>(static_cast<int>(_type) - 1);
00328         }
00329     }
00330     
00332     //
00335     bool strictly_equals(const as_value& v) const;
00336     
00338     //
00348     bool equals(const as_value& v) const;
00349     
00351     //
00353     void setReachable() const;
00354     
00356     //
00371     bool writeAMF0(amf::Writer& w) const;
00372 
00373 private:
00374 
00376     //
00383     typedef boost::variant<boost::blank,
00384                            double,
00385                            bool,
00386                            as_object*,
00387                            CharacterProxy,
00388                            std::string>
00389     AsValueType;
00390     
00392     bool operator==(const as_value& v) const;
00393     
00395     bool operator!=(const as_value& v) const;
00396     
00398     //
00401     bool equalsSameType(const as_value& v) const;
00402     
00404     bool to_bool_v7() const;
00405     
00407     bool to_bool_v6() const;
00408     
00410     bool to_bool_v5() const;
00411     
00412     AsType _type;
00413     
00414     AsValueType _value;
00415     
00417     //
00419     as_object* getObj() const;
00420     
00422     //
00424     DisplayObject* getCharacter(bool skipRebinding = false) const;
00425 
00427     //
00429     CharacterProxy getCharacterProxy() const;
00430 
00432     //
00434     double getNum() const {
00435         assert(_type == NUMBER);
00436         return boost::get<double>(_value);
00437     }
00438     
00440     //
00442     bool getBool() const {
00443         assert(_type == BOOLEAN);
00444         return boost::get<bool>(_value);
00445     }
00446 
00448     //
00450     const std::string& getStr() const {
00451         assert(_type == STRING);
00452         return boost::get<std::string>(_value);
00453     }
00454     
00455 };
00456 
00458 as_value& convertToNumber(as_value& v, VM& vm);
00459 
00461 as_value& convertToString(as_value& v, VM& vm);
00462 
00464 as_value& convertToBoolean(as_value& v, VM& vm);
00465 
00467 as_value& convertToPrimitive(as_value& v, VM& vm);
00468 
00469 inline std::ostream& operator<< (std::ostream& os, const as_value& v) {
00470         return os << v.toDebugString();
00471 }
00472 
00474 //
00475 // Printing formats:
00476 //
00477 // If _val > 1, Print up to 15 significant digits, then switch
00478 // to scientific notation, rounding at the last place and
00479 // omitting trailing zeroes.
00480 // For values < 1, print up to 4 leading zeroes after the
00481 // decimal point, then switch to scientific notation with up
00482 // to 15 significant digits, rounding with no trailing zeroes
00483 // If the value is negative, just add a '-' to the start; this
00484 // does not affect the precision of the printed value.
00485 //
00486 // This almost corresponds to iomanip's std::setprecision(15)
00487 // format, except that iomanip switches to scientific notation
00488 // at e-05 not e-06, and always prints at least two digits for the exponent.
00489 std::string doubleToString(double val, int radix = 10);
00490 
00494 //
00504 bool parseNonDecimalInt(const std::string& s, double& d, bool whole = true);
00505 
00507 //
00511 //
00513 boost::int32_t toInt(const as_value& val);
00514 
00516 inline void
00517 setNaN(as_value& v) {
00518     v.set_double(NaN);
00519 }
00520 
00521 } // namespace gnash
00522 
00523 #endif // GNASH_AS_VALUE_H
00524 
00525 // Local Variables:
00526 // mode: C++
00527 // indent-tabs-mode: nil
00528 // End:
00529 

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