// Copyright 2008, Google Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. /* This file contains the declaration for CppVariant, a type used by C++ classes that are to be bound to JavaScript objects. CppVariant exists primarily as an interface between C++ callers and the corresponding NPVariant type. CppVariant also provides a number of convenience constructors and accessors, so that the NPVariantType values don't need to be exposed, and a destructor to free any memory allocated for string values. For a usage example, see cpp_binding_example.{h|cc}. */ #ifndef WEBKIT_GLUE_CPP_VARIANT_H__ #define WEBKIT_GLUE_CPP_VARIANT_H__ #include #include #include "base/basictypes.h" #include "third_party/npapi/bindings/npruntime.h" class CppVariant : public NPVariant { public: CppVariant(); ~CppVariant(); void SetNull(); void Set(bool value); void Set(int32_t value); void Set(double value); // Note that setting a CppVariant to a string value involves copying the // string data, which must be freed with a call to FreeData() when the // CppVariant is set to a different value or is no longer needed. Normally // this is handled by the other Set() methods and by the destructor. void Set(const char* value); // Must be a null-terminated string. void Set(const std::string& value); void Set(const NPString& new_value); void Set(const NPVariant& new_value); // Note that setting a CppVariant to an NPObject involves ref-counting // the actual object. FreeData() should only be called if the CppVariant // is no longer needed. The other Set() methods handle this internally. // Also, the object's NPClass is expected to be a static object: neither // the NP runtime nor CPPVariant will ever free it. void Set(NPObject* new_value); // These three methods all perform deep copies of any string data. This // allows local CppVariants to be released by the destructor without // corrupting their sources. In performance-critical code, or when strings // are very long, avoid creating new CppVariants. // In case of NPObject as the data, the copying involves ref-counting // as opposed to deep-copying. The ref-counting ensures that sources don't // get corrupted when the copies get destroyed. void CopyToNPVariant(NPVariant* result) const; CppVariant& operator=(const CppVariant& original); CppVariant(const CppVariant& original); // Calls NPN_ReleaseVariantValue, which frees any string data // held by the object and sets its type to null. // In case of NPObject, the NPN_ReleaseVariantValue decrements // the ref-count (releases when ref-count becomes 0) void FreeData(); // Compares this CppVariant's type and value to another's. They must be // identical in both type and value to be considered equal. For string and // object types, a deep comparison is performed; that is, the contents of the // strings, or the classes and refcounts of the objects, must be the same, // but they need not be the same pointers. bool isEqual(const CppVariant& other) const; // The value of a CppVariant may be read directly from its NPVariant (but // should only be set using one of the Set() methods above). Although the // type of a CppVariant is likewise public, it can be accessed through these // functions rather than directly if a caller wishes to avoid dependence on // the NPVariantType values. bool isBool() const { return (type == NPVariantType_Bool); } bool isInt32() const { return (type == NPVariantType_Int32); } bool isDouble() const { return (type == NPVariantType_Double); } bool isNumber() const { return (isInt32() || isDouble()); } bool isString() const { return (type == NPVariantType_String); } bool isVoid() const { return (type == NPVariantType_Void); } bool isNull() const { return (type == NPVariantType_Null); } bool isEmpty() const { return (isVoid() || isNull()); } bool isObject() const { return (type == NPVariantType_Object); } // Converters. The CppVariant must be of a type convertible to these values. // For example, ToInteger() works only if isNumber() is true. std::string ToString() const; int32_t ToInt32() const; double ToDouble() const; bool ToBoolean() const; // Returns a vector of strings for the specified argument. This is useful // for converting a JavaScript array of strings into a vector of strings. std::vector ToStringVector() const; }; #endif // WEBKIT_GLUE_CPP_VARIANT_H__