reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

xml.hpp (2154B)


      1 /* ReaPack: Package manager for REAPER
      2  * Copyright (C) 2015-2025  Christian Fillion
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU Lesser General Public License as published by
      6  * the Free Software Foundation, either version 3 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program 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 Lesser General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Lesser General Public License
     15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 #ifndef REAPACK_XML_HPP
     19 #define REAPACK_XML_HPP
     20 
     21 #include <istream>
     22 #include <memory>
     23 
     24 class XmlNode;
     25 class XmlString;
     26 
     27 class XmlDocument {
     28   struct Impl;
     29   using ImplPtr = std::unique_ptr<Impl>;
     30 
     31 public:
     32   XmlDocument(std::istream &);
     33   XmlDocument(const XmlDocument &) = delete;
     34   XmlDocument(XmlDocument &&);
     35   ~XmlDocument();
     36 
     37   operator bool() const;
     38   const char *error() const;
     39 
     40   XmlNode root() const;
     41 
     42 private:
     43   ImplPtr m_impl;
     44 };
     45 
     46 class XmlNode {
     47   friend XmlDocument;
     48   struct Impl;
     49   using ImplPtr = std::unique_ptr<Impl>;
     50 
     51 public:
     52   XmlNode(void *);
     53   XmlNode(const XmlNode &);
     54   ~XmlNode();
     55 
     56   XmlNode &operator=(const XmlNode &);
     57   operator bool() const;
     58 
     59   const char *name() const;
     60   XmlString attribute(const char *name) const;
     61   bool attribute(const char *name, int *value) const;
     62   XmlString text() const;
     63 
     64   XmlNode firstChild(const char *element = nullptr) const;
     65   XmlNode nextSibling(const char *element = nullptr) const;
     66 
     67 private:
     68   ImplPtr m_impl;
     69 };
     70 
     71 class XmlString {
     72 public:
     73   XmlString(const void *);
     74   XmlString(const XmlString &) = delete;
     75   XmlString(XmlString &&) = default;
     76   ~XmlString();
     77 
     78   operator bool() const { return m_str != nullptr; }
     79   const char *operator *() const { return reinterpret_cast<const char *>(m_str); }
     80   const char *value_or(const char *fallback) const { return m_str ? **this : fallback; }
     81 
     82 private:
     83   const void *m_str;
     84 };
     85 
     86 #endif