Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Unified Diff: third_party/WebKit/Source/platform/JSONValues.h

Issue 2204383003: Make blink::JSONValue (and subclasses) use unique_ptr rather than RefPtrs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/platform/JSONValues.h
diff --git a/third_party/WebKit/Source/platform/JSONValues.h b/third_party/WebKit/Source/platform/JSONValues.h
index 1789d4225525204b56fd66741fa42c3930c09947..1425c46aeaf5861bd4acd29cbd581c66d72c0525 100644
--- a/third_party/WebKit/Source/platform/JSONValues.h
+++ b/third_party/WebKit/Source/platform/JSONValues.h
@@ -35,12 +35,14 @@
#include "wtf/Allocator.h"
#include "wtf/Forward.h"
#include "wtf/HashMap.h"
-#include "wtf/RefCounted.h"
+#include "wtf/Noncopyable.h"
#include "wtf/TypeTraits.h"
#include "wtf/Vector.h"
#include "wtf/text/StringHash.h"
#include "wtf/text/WTFString.h"
+#include <memory>
+
namespace blink {
class JSONValue;
@@ -52,15 +54,16 @@ namespace blink {
class JSONArray;
class JSONObject;
-class PLATFORM_EXPORT JSONValue : public RefCounted<JSONValue> {
+class PLATFORM_EXPORT JSONValue {
+ WTF_MAKE_NONCOPYABLE(JSONValue);
jbroman 2016/08/04 15:17:30 You may also want USING_FAST_MALLOC(JSONValue), as
public:
static const int maxDepth = 1000;
virtual ~JSONValue() { }
- static PassRefPtr<JSONValue> null()
+ static std::unique_ptr<JSONValue> null()
{
- return adoptRef(new JSONValue());
+ return wrapUnique(new JSONValue());
}
typedef enum {
@@ -106,19 +109,19 @@ private:
class PLATFORM_EXPORT JSONBasicValue : public JSONValue {
public:
- static PassRefPtr<JSONBasicValue> create(bool value)
+ static std::unique_ptr<JSONBasicValue> create(bool value)
{
- return adoptRef(new JSONBasicValue(value));
+ return wrapUnique(new JSONBasicValue(value));
}
- static PassRefPtr<JSONBasicValue> create(int value)
+ static std::unique_ptr<JSONBasicValue> create(int value)
{
- return adoptRef(new JSONBasicValue(value));
+ return wrapUnique(new JSONBasicValue(value));
}
- static PassRefPtr<JSONBasicValue> create(double value)
+ static std::unique_ptr<JSONBasicValue> create(double value)
{
- return adoptRef(new JSONBasicValue(value));
+ return wrapUnique(new JSONBasicValue(value));
}
bool asBoolean(bool* output) const override;
@@ -143,14 +146,14 @@ private:
class PLATFORM_EXPORT JSONString : public JSONValue {
public:
- static PassRefPtr<JSONString> create(const String& value)
+ static std::unique_ptr<JSONString> create(const String& value)
{
- return adoptRef(new JSONString(value));
+ return wrapUnique(new JSONString(value));
}
- static PassRefPtr<JSONString> create(const char* value)
+ static std::unique_ptr<JSONString> create(const char* value)
{
- return adoptRef(new JSONString(value));
+ return wrapUnique(new JSONString(value));
}
bool asString(String* output) const override;
@@ -166,22 +169,27 @@ private:
class PLATFORM_EXPORT JSONObject : public JSONValue {
private:
- typedef HashMap<String, RefPtr<JSONValue>> Dictionary;
+ typedef HashMap<String, std::unique_ptr<JSONValue>> Dictionary;
public:
typedef Dictionary::iterator iterator;
typedef Dictionary::const_iterator const_iterator;
- static PassRefPtr<JSONObject> create()
+ static std::unique_ptr<JSONObject> create()
{
- return adoptRef(new JSONObject());
+ return wrapUnique(new JSONObject());
}
- static PassRefPtr<JSONObject> cast(PassRefPtr<JSONValue> value)
+ static JSONObject* cast(JSONValue* value)
{
if (!value || value->getType() != TypeObject)
return nullptr;
- return adoptRef(static_cast<JSONObject*>(value.leakRef()));
+ return static_cast<JSONObject*>(value);
+ }
+
+ static std::unique_ptr<JSONObject> cast(std::unique_ptr<JSONValue> value)
+ {
+ return wrapUnique(JSONObject::cast(value.release()));
}
void writeJSON(StringBuilder* output) const override;
@@ -191,33 +199,29 @@ public:
void setBoolean(const String& name, bool);
void setNumber(const String& name, double);
void setString(const String& name, const String&);
- void setValue(const String& name, PassRefPtr<JSONValue>);
- void setObject(const String& name, PassRefPtr<JSONObject>);
- void setArray(const String& name, PassRefPtr<JSONArray>);
+ void setValue(const String& name, std::unique_ptr<JSONValue>);
+ void setObject(const String& name, std::unique_ptr<JSONObject>);
+ void setArray(const String& name, std::unique_ptr<JSONArray>);
iterator find(const String& name);
const_iterator find(const String& name) const;
bool getBoolean(const String& name, bool* output) const;
template<class T> bool getNumber(const String& name, T* output) const
{
- RefPtr<JSONValue> value = get(name);
+ JSONValue* value = get(name);
if (!value)
return false;
return value->asNumber(output);
}
bool getString(const String& name, String* output) const;
- PassRefPtr<JSONObject> getObject(const String& name) const;
- PassRefPtr<JSONArray> getArray(const String& name) const;
- PassRefPtr<JSONValue> get(const String& name) const;
+ JSONObject* getObject(const String& name) const;
+ JSONArray* getArray(const String& name) const;
+ JSONValue* get(const String& name) const;
bool booleanProperty(const String& name, bool defaultValue) const;
void remove(const String& name);
- iterator begin() { return m_data.begin(); }
- iterator end() { return m_data.end(); }
- const_iterator begin() const { return m_data.begin(); }
- const_iterator end() const { return m_data.end(); }
~JSONObject() override;
protected:
@@ -232,19 +236,21 @@ private:
class PLATFORM_EXPORT JSONArray : public JSONValue {
public:
- typedef Vector<RefPtr<JSONValue>>::iterator iterator;
- typedef Vector<RefPtr<JSONValue>>::const_iterator const_iterator;
-
- static PassRefPtr<JSONArray> create()
+ static std::unique_ptr<JSONArray> create()
{
- return adoptRef(new JSONArray());
+ return wrapUnique(new JSONArray());
}
- static PassRefPtr<JSONArray> cast(PassRefPtr<JSONValue> value)
+ static JSONArray* cast(JSONValue* value)
{
if (!value || value->getType() != TypeArray)
return nullptr;
- return adoptRef(static_cast<JSONArray*>(value.leakRef()));
+ return static_cast<JSONArray*>(value);
+ }
+
+ static std::unique_ptr<JSONArray> cast(std::unique_ptr<JSONValue> value)
+ {
+ return wrapUnique(JSONArray::cast(value.release()));
}
~JSONArray() override;
@@ -255,24 +261,19 @@ public:
void pushInt(int);
void pushNumber(double);
void pushString(const String&);
- void pushValue(PassRefPtr<JSONValue>);
- void pushObject(PassRefPtr<JSONObject>);
- void pushArray(PassRefPtr<JSONArray>);
+ void pushValue(std::unique_ptr<JSONValue>);
+ void pushObject(std::unique_ptr<JSONObject>);
+ void pushArray(std::unique_ptr<JSONArray>);
- PassRefPtr<JSONValue> get(size_t index);
+ JSONValue* get(size_t index) const;
unsigned length() const { return m_data.size(); }
- iterator begin() { return m_data.begin(); }
- iterator end() { return m_data.end(); }
- const_iterator begin() const { return m_data.begin(); }
- const_iterator end() const { return m_data.end(); }
-
protected:
void prettyWriteJSONInternal(StringBuilder* output, int depth) const override;
private:
JSONArray();
- Vector<RefPtr<JSONValue>> m_data;
+ Vector<std::unique_ptr<JSONValue>> m_data;
};
PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*);

Powered by Google App Engine
This is Rietveld 408576698