Advertisement
Guest User

Untitled

a guest
May 1st, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #ifndef COMPONENTS_H
  2. #define COMPONENTS_H
  3.  
  4. #include <vector>
  5. #include <string>
  6. #include <memory>
  7. #include <QVariant>
  8.  
  9. class Components
  10. {
  11. public:
  12.     enum Types {
  13.         kString,
  14.         knumber,
  15.         kDictionary,
  16.         kArray
  17.     };
  18.  
  19.     Components(Types type);
  20.     Components(Types type, std::shared_ptr<Components> parent, std::string name);
  21.     void setValue(std::string value);
  22.  
  23.     void addChild(std::shared_ptr<Components> child);
  24.     Components *child(int row) const;
  25.     Components *parent() const;
  26.     int childCount() const;
  27.     int columnCount() const;
  28.     int row() const;
  29.     QVariant data(int column) const;
  30.  
  31. private:
  32.     std::shared_ptr<Components> m_parent;
  33.     enum Types m_type;
  34.  
  35.     QVariant m_name; // "key" in dictionary terms
  36.     QVariant m_value;
  37.     bool m_isValuePresent;
  38.  
  39.     std::vector<std::shared_ptr<Components>> m_children;
  40. };
  41.  
  42. #endif
  43.  
  44.  
  45.  
  46. // components.cpp
  47.  
  48. #include "components.h"
  49. #include <stdexcept>
  50. #include <QString>
  51.  
  52. Components::Components(Types type) {
  53.     m_type = type;
  54.     m_name = "root";
  55.     m_isValuePresent = false;
  56. }
  57.  
  58. Components::Components(Types type, std::shared_ptr<Components> parent, std::string name) {
  59.     m_type = type;
  60.     m_parent = parent;
  61.     m_name = QVariant(QString::fromStdString(name));;
  62.  
  63.     m_isValuePresent = false;
  64. }
  65.  
  66. void Components::setValue(std::string value) {
  67.     if (m_children.size() != 0)
  68.         throw std::invalid_argument("Value can't be assigned if children are present");
  69.  
  70.     m_value = QVariant(QString::fromStdString(value));
  71.     m_isValuePresent = true;
  72. }
  73.  
  74. void Components::addChild(std::shared_ptr<Components> child) {
  75.     if (m_isValuePresent)
  76.         throw std::invalid_argument("Can't add child if value is present");
  77.     m_children.push_back(child);
  78.     return;
  79. }
  80.  
  81. Components* Components::child(int row) const {
  82.     if (row >= m_children.size())
  83.         return nullptr;
  84.  
  85.     return m_children[row].get();
  86. }
  87.  
  88. Components* Components::parent() const {
  89.     //if (m_parent.expired()) {
  90.     //    throw new std::invalid_argument("Parent pointer doesn't exist");
  91.     //}
  92.     return m_parent.get();
  93. }
  94.  
  95. int Components::childCount() const {
  96.     return m_children.size();
  97. }
  98.  
  99. int Components::columnCount() const {
  100.     // Apparently this goes for all children
  101.     return 2;
  102. }
  103.  
  104. QVariant Components::data(int column) const {
  105.     if (column == 0)
  106.         return m_name;
  107.     if (column == 1 && m_isValuePresent)
  108.         return m_value;
  109.     return QVariant();
  110. }
  111.  
  112. int Components::row() const {
  113.     //if (m_parent.expired())
  114.     //    return 0;
  115.     Components *parent = this->parent();
  116.     for (int i = 0; i < parent->childCount(); ++i) {
  117.         Components *child = parent->m_children[i].get();
  118.         if (child == this)
  119.             return i;
  120.     }
  121.     // Shouldn't happen
  122.     return 0;
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement