LCOV - code coverage report
Current view: top level - source/tools/atlas/AtlasObject - AtlasObject.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 13 14 92.9 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : // Public interface to almost all of AtlasObject.
      19             : // (See AtlasObjectText for the rest of it).
      20             : //
      21             : // Tries to include as few headers as possible, to minimise its impact
      22             : // on compile times.
      23             : 
      24             : #ifndef INCLUDED_ATLASOBJECT
      25             : #define INCLUDED_ATLASOBJECT
      26             : 
      27             : #if defined(_WIN32)
      28             : # define _CRT_SECURE_NO_WARNINGS // Disable deprecation warning in VS2008
      29             : #endif
      30             : 
      31             : #include <string>
      32             : 
      33             : //////////////////////////////////////////////////////////////////////////
      34             : // Mostly-private bits:
      35             : 
      36             : // Helper class to let us define a conversion operator only for AtSmartPtr<not const>
      37             : template<class ConstSmPtr, class T> struct ConstCastHelper { operator ConstSmPtr (); };
      38             : template<class ConstSmPtr, class T> struct ConstCastHelper<ConstSmPtr, const T> { };
      39             : 
      40             : // Simple reference-counted pointer. class T must contain a reference count,
      41             : // initialised to 0. An external implementation (in AtlasObjectImpl.cpp)
      42             : // provides the inc_ref and dec_ref methods, so that this header file doesn't
      43             : // need to know their implementations.
      44             : template<class T> class AtSmartPtr : public ConstCastHelper<AtSmartPtr<const T>, T>
      45             : {
      46             :     friend struct ConstCastHelper<AtSmartPtr<const T>, T>;
      47             : private:
      48             :     void inc_ref();
      49             :     void dec_ref();
      50             :     T* ptr;
      51             : public:
      52             :     // Constructors
      53          26 :     AtSmartPtr() : ptr(NULL) {}
      54          34 :     explicit AtSmartPtr(T* p) : ptr(p) { inc_ref(); }
      55             :     // Copy constructor
      56         108 :     AtSmartPtr(const AtSmartPtr<T>& r) : ptr(r.ptr) { inc_ref(); }
      57             :     // Assignment operators
      58          13 :     AtSmartPtr<T>& operator=(T* p) { dec_ref(); ptr = p; inc_ref(); return *this; }
      59          39 :     AtSmartPtr<T>& operator=(const AtSmartPtr<T>& r) { if (&r != this) { dec_ref(); ptr = r.ptr; inc_ref(); } return *this; }
      60             :     // Destructor
      61         114 :     ~AtSmartPtr() { dec_ref(); }
      62             :     // Allow conversion from non-const T* to const T*
      63             :     //operator AtSmartPtr<const T> () { return AtSmartPtr<const T>(ptr); } // (actually provided by ConstCastHelper)
      64             :     // Override ->
      65         108 :     T* operator->() const { return ptr; }
      66             :     // Test whether the pointer is pointing to anything
      67          60 :     explicit operator bool() const { return ptr != nullptr; }
      68             : };
      69             : 
      70             : template<class ConstSmPtr, class T>
      71          21 : ConstCastHelper<ConstSmPtr, T>::operator ConstSmPtr ()
      72             : {
      73          21 :     return ConstSmPtr(static_cast<AtSmartPtr<T>*>(this)->ptr);
      74             : }
      75             : 
      76             : // A few required declarations
      77             : class AtObj;
      78             : class AtNode;
      79             : class AtIterImpl;
      80             : 
      81             : 
      82             : //////////////////////////////////////////////////////////////////////////
      83             : // Public bits:
      84             : 
      85             : 
      86             : // AtIter is an iterator over AtObjs - use it like:
      87             : //
      88             : //     for (AtIter thing = whatever["thing"]; thing.defined(); ++thing)
      89             : //         DoStuff(thing);
      90             : //
      91             : // to handle XML data like:
      92             : //
      93             : //   <whatever>
      94             : //     <thing>Stuff 1</thing>
      95             : //     <thing>Stuff 2</thing>
      96             : //   </whatever>
      97             : 
      98           0 : class AtIter
      99             : {
     100             : public:
     101             :     // Increment the iterator; or make it undefined, if there weren't any
     102             :     // AtObjs left to iterate over
     103             :     AtIter& operator++ ();
     104             :     // Return whether this iterator has an AtObj to point to
     105             :     bool defined() const;
     106             :     // Return whether this iterator is pointing to a non-contentless AtObj
     107             :     bool hasContent() const;
     108             :     // Return the number of AtObjs that will be iterated over (including the current one)
     109             :     size_t count() const;
     110             : 
     111             :     // Return an iterator to the children matching 'key'. (That is, children
     112             :     // of the AtObj currently pointed to by this iterator)
     113             :     const AtIter operator[] (const char* key) const;
     114             : 
     115             :     // Return the AtObj currently pointed to by this iterator
     116             :     const AtObj operator* () const;
     117             : 
     118             :     // Return the string value of the AtObj currently pointed to by this iterator
     119             :     operator const char* () const;
     120             : 
     121             :     // Private implementation. (But not 'private:', because it's a waste of time
     122             :     // adding loads of friend functions)
     123             :     AtSmartPtr<AtIterImpl> m_Impl;
     124             : };
     125             : 
     126             : 
     127          26 : class AtObj
     128             : {
     129             : public:
     130          26 :     AtObj() {}
     131          13 :     AtObj(const AtObj& r) : m_Node(r.m_Node) {}
     132             :     AtObj& operator=(const AtObj& r)
     133             :     {
     134             :         m_Node = r.m_Node;
     135             :         return *this;
     136             :     }
     137             : 
     138             :     // Return an iterator to the children matching 'key'
     139             :     const AtIter operator[] (const char* key) const;
     140             : 
     141             :     // Return the string value of this object
     142             :     operator const char* () const;
     143             : 
     144             :     // Return the floating point value of this object
     145             :     double getDouble() const;
     146             : 
     147             :     // Return the integer value of this object
     148             :     int getInt() const;
     149             :     long getLong() const;
     150             : 
     151             :     // Check whether the object contains anything (even if those things are empty)
     152             :     bool defined() const { return static_cast<bool>(m_Node); }
     153             : 
     154             :     // Check recursively whether there's actually any non-empty data in the object
     155             :     bool hasContent() const;
     156             : 
     157             :     // Add or set a child. The char* version creates a new AtObj with
     158             :     // the appropriate string value, then uses that as the child.
     159             :     //
     160             :     // These alter the AtObj's internal pointer, and the pointed-to data is
     161             :     // never actually altered. Copies of this AtObj (including copies stored
     162             :     // inside other AtObjs) will not be affected.
     163             :     void add(const char* key, const char* value);
     164             :     void add(const char* key, AtObj& data);
     165             :     void set(const char* key, const char* value);
     166             :     void set(const char* key, AtObj& data);
     167             :     void unset(const char* key);
     168             :     void setBool(const char* key, bool value);
     169             :     void setDouble(const char* key, double value);
     170             :     void setInt(const char* key, int value);
     171             :     void setString(const char* value);
     172             :     void addOverlay(AtObj& data);
     173             : 
     174             :     AtSmartPtr<const AtNode> m_Node;
     175             : };
     176             : 
     177             : 
     178             : // Miscellaneous utility functions:
     179             : namespace AtlasObject
     180             : {
     181             :     // Returns AtObj() on failure - test with AtObj::defined()
     182             :     AtObj LoadFromXML(const std::string& xml);
     183             : 
     184             :     // Returns AtObj() on failure - test with AtObj::defined()
     185             :     AtObj LoadFromJSON(const std::string& json);
     186             : 
     187             :     // Returns UTF-8-encoded XML document string.
     188             :     // Returns empty string on failure.
     189             :     std::string SaveToXML(AtObj& obj);
     190             : 
     191             :     // Returns UTF-8-encoded JSON string.
     192             :     // Returns empty string on failure.
     193             :     std::string SaveToJSON(AtObj& obj);
     194             : 
     195             :     AtObj TrimEmptyChildren(AtObj& obj);
     196             : }
     197             : 
     198             : #endif // INCLUDED_ATLASOBJECT

Generated by: LCOV version 1.16