OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /* |
| 6 This file contains the declaration for CppVariant, a type used by C++ classes |
| 7 that are to be bound to JavaScript objects. |
| 8 |
| 9 CppVariant exists primarily as an interface between C++ callers and the |
| 10 corresponding NPVariant type. CppVariant also provides a number of |
| 11 convenience constructors and accessors, so that the NPVariantType values |
| 12 don't need to be exposed, and a destructor to free any memory allocated for |
| 13 string values. |
| 14 */ |
| 15 |
| 16 #ifndef CppVariant_h |
| 17 #define CppVariant_h |
| 18 |
| 19 #include <string> |
| 20 #include <vector> |
| 21 |
| 22 #include "third_party/WebKit/public/web/WebBindings.h" |
| 23 |
| 24 namespace WebTestRunner { |
| 25 |
| 26 class CppVariant : public NPVariant { |
| 27 public: |
| 28 CppVariant(); |
| 29 ~CppVariant(); |
| 30 void setNull(); |
| 31 void set(bool); |
| 32 void set(int32_t); |
| 33 void set(double); |
| 34 |
| 35 // Note that setting a CppVariant to a string value involves copying the |
| 36 // string data, which must be freed with a call to freeData() when the |
| 37 // CppVariant is set to a different value or is no longer needed. Normally |
| 38 // this is handled by the other set() methods and by the destructor. |
| 39 void set(const char*); // Must be a null-terminated string. |
| 40 void set(const std::string&); |
| 41 void set(const NPString&); |
| 42 void set(const NPVariant&); |
| 43 |
| 44 // Note that setting a CppVariant to an NPObject involves ref-counting |
| 45 // the actual object. freeData() should only be called if the CppVariant |
| 46 // is no longer needed. The other set() methods handle this internally. |
| 47 // Also, the object's NPClass is expected to be a static object: neither |
| 48 // the NP runtime nor CppVariant will ever free it. |
| 49 void set(NPObject*_value); |
| 50 |
| 51 // These three methods all perform deep copies of any string data. This |
| 52 // allows local CppVariants to be released by the destructor without |
| 53 // corrupting their sources. In performance-critical code, or when strings |
| 54 // are very long, avoid creating new CppVariants. |
| 55 // In case of NPObject as the data, the copying involves ref-counting |
| 56 // as opposed to deep-copying. The ref-counting ensures that sources don't |
| 57 // get corrupted when the copies get destroyed. |
| 58 void copyToNPVariant(NPVariant* result) const; |
| 59 CppVariant& operator=(const CppVariant& original); |
| 60 CppVariant(const CppVariant& original); |
| 61 |
| 62 // Calls NPN_ReleaseVariantValue, which frees any string data |
| 63 // held by the object and sets its type to null. |
| 64 // In case of NPObject, the NPN_ReleaseVariantValue decrements |
| 65 // the ref-count (releases when ref-count becomes 0) |
| 66 void freeData(); |
| 67 |
| 68 // Compares this CppVariant's type and value to another's. They must be |
| 69 // identical in both type and value to be considered equal. For string and |
| 70 // object types, a deep comparison is performed; that is, the contents of th
e |
| 71 // strings, or the classes and refcounts of the objects, must be the same, |
| 72 // but they need not be the same pointers. |
| 73 bool isEqual(const CppVariant&) const; |
| 74 |
| 75 // The value of a CppVariant may be read directly from its NPVariant (but |
| 76 // should only be set using one of the set() methods above). Although the |
| 77 // type of a CppVariant is likewise public, it can be accessed through these |
| 78 // functions rather than directly if a caller wishes to avoid dependence on |
| 79 // the NPVariantType values. |
| 80 bool isBool() const { return (type == NPVariantType_Bool); } |
| 81 bool isInt32() const { return (type == NPVariantType_Int32); } |
| 82 bool isDouble() const { return (type == NPVariantType_Double); } |
| 83 bool isNumber() const { return (isInt32() || isDouble()); } |
| 84 bool isString() const { return (type == NPVariantType_String); } |
| 85 bool isVoid() const { return (type == NPVariantType_Void); } |
| 86 bool isNull() const { return (type == NPVariantType_Null); } |
| 87 bool isEmpty() const { return (isVoid() || isNull()); } |
| 88 bool isObject() const { return (type == NPVariantType_Object); } |
| 89 |
| 90 // Converters. The CppVariant must be of a type convertible to these values. |
| 91 // For example, toInt32() works only if isNumber() is true. |
| 92 std::string toString() const; |
| 93 int32_t toInt32() const; |
| 94 double toDouble() const; |
| 95 bool toBoolean() const; |
| 96 // Returns a vector of strings for the specified argument. This is useful |
| 97 // for converting a JavaScript array of strings into a vector of strings. |
| 98 std::vector<std::string> toStringVector() const; |
| 99 |
| 100 // Invoke method of the given name on an object with the supplied arguments. |
| 101 // The first argument should be the object on which the method is to be |
| 102 // invoked. Returns whether the method was successfully invoked. If the |
| 103 // method was invoked successfully, any return value is stored in the |
| 104 // CppVariant specified by result. |
| 105 bool invoke(const std::string&, const CppVariant* arguments, |
| 106 uint32_t argumentCount, CppVariant& result) const; |
| 107 |
| 108 // Invoke an object's default method with the supplied arguments. |
| 109 // The first argument should be the object on which the method is to be |
| 110 // invoked. Returns whether the method was successfully invoked. If the |
| 111 // method was invoked successfully, any return value is stored in the |
| 112 // CppVariant specified by result. |
| 113 bool invokeDefault(const CppVariant* arguments, |
| 114 uint32_t argumentCount, CppVariant& result) const; |
| 115 }; |
| 116 |
| 117 } |
| 118 |
| 119 #endif // CppVariant_h |
OLD | NEW |