Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/tools/atlas/AtlasObject/AtlasObject.h
/* Copyright (C) 2015 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | public: | ||||
AtSmartPtr<T>& operator=(const AtSmartPtr<T>& r) { if (&r != this) { dec_ref(); ptr = r.ptr; inc_ref(); } return *this; } | AtSmartPtr<T>& operator=(const AtSmartPtr<T>& r) { if (&r != this) { dec_ref(); ptr = r.ptr; inc_ref(); } return *this; } | ||||
// Destructor | // Destructor | ||||
~AtSmartPtr() { dec_ref(); } | ~AtSmartPtr() { dec_ref(); } | ||||
// Allow conversion from non-const T* to const T* | // Allow conversion from non-const T* to const T* | ||||
//operator AtSmartPtr<const T> () { return AtSmartPtr<const T>(ptr); } // (actually provided by ConstCastHelper) | //operator AtSmartPtr<const T> () { return AtSmartPtr<const T>(ptr); } // (actually provided by ConstCastHelper) | ||||
// Override -> | // Override -> | ||||
T* operator->() const { return ptr; } | T* operator->() const { return ptr; } | ||||
// Test whether the pointer is pointing to anything | // Test whether the pointer is pointing to anything | ||||
explicit operator bool() const { return ptr != NULL; } | explicit operator bool() const { return ptr != nullptr; } | ||||
}; | }; | ||||
template<class ConstSmPtr, class T> | template<class ConstSmPtr, class T> | ||||
ConstCastHelper<ConstSmPtr, T>::operator ConstSmPtr () | ConstCastHelper<ConstSmPtr, T>::operator ConstSmPtr () | ||||
{ | { | ||||
return ConstSmPtr(static_cast<AtSmartPtr<T>*>(this)->ptr); | return ConstSmPtr(static_cast<AtSmartPtr<T>*>(this)->ptr); | ||||
} | } | ||||
Show All 39 Lines | public: | ||||
// Return the AtObj currently pointed to by this iterator | // Return the AtObj currently pointed to by this iterator | ||||
const AtObj operator* () const; | const AtObj operator* () const; | ||||
// Return the string value of the AtObj currently pointed to by this iterator | // Return the string value of the AtObj currently pointed to by this iterator | ||||
operator const wchar_t* () const; | operator const wchar_t* () const; | ||||
// Private implementation. (But not 'private:', because it's a waste of time | // Private implementation. (But not 'private:', because it's a waste of time | ||||
// adding loads of friend functions) | // adding loads of friend functions) | ||||
AtSmartPtr<AtIterImpl> p; | AtSmartPtr<AtIterImpl> m_Impl; | ||||
}; | }; | ||||
class AtObj | class AtObj | ||||
{ | { | ||||
public: | public: | ||||
AtObj() {} | AtObj() {} | ||||
AtObj(const AtObj& r) : p(r.p) {} | AtObj(const AtObj& r) : m_Node(r.m_Node) {} | ||||
// Return an iterator to the children matching 'key' | // Return an iterator to the children matching 'key' | ||||
const AtIter operator[] (const char* key) const; | const AtIter operator[] (const char* key) const; | ||||
// Return the string value of this object | // Return the string value of this object | ||||
operator const wchar_t* () const; | operator const wchar_t* () const; | ||||
// Return the floating point value of this object | // Return the floating point value of this object | ||||
double getDouble() const; | double getDouble() const; | ||||
// Return the integer value of this object | // Return the integer value of this object | ||||
int getInt() const; | int getInt() const; | ||||
// Check whether the object contains anything (even if those things are empty) | // Check whether the object contains anything (even if those things are empty) | ||||
bool defined() const { return (bool)p; } | bool defined() const { return static_cast<bool>(m_Node); } | ||||
// Check recursively whether there's actually any non-empty data in the object | // Check recursively whether there's actually any non-empty data in the object | ||||
bool hasContent() const; | bool hasContent() const; | ||||
// Add or set a child. The wchar_t* and wxString& versions create a new | // Add or set a child. The wchar_t* and wxString& versions create a new | ||||
// AtObj with the appropriate string value, then use that as the child. | // AtObj with the appropriate string value, then use that as the child. | ||||
// | // | ||||
// These alter the AtObj's internal pointer, and the pointed-to data is | // These alter the AtObj's internal pointer, and the pointed-to data is | ||||
// never actually altered. Copies of this AtObj (including copies stored | // never actually altered. Copies of this AtObj (including copies stored | ||||
// inside other AtObjs) will not be affected. | // inside other AtObjs) will not be affected. | ||||
void add(const char* key, const wchar_t* value); | void add(const char* key, const wchar_t* value); | ||||
void add(const char* key, const wxString& value); | void add(const char* key, const wxString& value); | ||||
void add(const char* key, AtObj& data); | void add(const char* key, AtObj& data); | ||||
void set(const char* key, const wchar_t* value); | void set(const char* key, const wchar_t* value); | ||||
void set(const char* key, const wxString& value); | void set(const char* key, const wxString& value); | ||||
void set(const char* key, AtObj& data); | void set(const char* key, AtObj& data); | ||||
void setBool(const char* key, bool value); | void setBool(const char* key, bool value); | ||||
void setDouble(const char* key, double value); | void setDouble(const char* key, double value); | ||||
void setInt(const char* key, int value); | void setInt(const char* key, int value); | ||||
void setString(const wchar_t* value); | void setString(const wchar_t* value); | ||||
void addOverlay(AtObj& data); | void addOverlay(AtObj& data); | ||||
AtSmartPtr<const AtNode> p; | AtSmartPtr<const AtNode> m_Node; | ||||
}; | }; | ||||
// Miscellaneous utility functions: | // Miscellaneous utility functions: | ||||
namespace AtlasObject | namespace AtlasObject | ||||
{ | { | ||||
// Returns AtObj() on failure - test with AtObj::defined() | // Returns AtObj() on failure - test with AtObj::defined() | ||||
AtObj LoadFromXML(const std::string& xml); | AtObj LoadFromXML(const std::string& xml); | ||||
Show All 16 Lines |
Wildfire Games · Phabricator