/inc/raws/rawnode.h

https://code.google.com/p/dwarftherapist/ · C Header · 92 lines · 54 code · 16 blank · 22 comment · 6 complexity · 095682e9dbab390f98f02031ad8d8b92 MD5 · raw file

  1. /*
  2. Dwarf Therapist
  3. Copyright (c) 2011 Justin Ehlert (Dwarf Engineer)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #ifndef RAWNODE_H
  21. #define RAWNODE_H
  22. #include <QObject>
  23. #include <QVector>
  24. #include <QSharedPointer>
  25. class RawNode;
  26. typedef QSharedPointer<RawNode> RawNodePtr;
  27. class RawNode : public QObject {
  28. Q_OBJECT
  29. friend class RawReader;
  30. public:
  31. RawNode(QObject * parent = 0) : QObject(parent) {
  32. }
  33. RawNode(QString name, QObject * parent) :
  34. QObject(parent), name(name) {
  35. }
  36. RawNode(QString name, const QVector<QString> & values, QObject * parent) :
  37. QObject(parent), name(name), values(values) {
  38. }
  39. QString get_name() const {
  40. return name;
  41. }
  42. QString get_value(int idx, QString default_value = "") const {
  43. if(values.size() > idx)
  44. return values[idx];
  45. return default_value;
  46. }
  47. const QVector<RawNodePtr> & get_children() const {
  48. return children;
  49. }
  50. QVector<RawNodePtr> get_children(QString name) const {
  51. QVector<RawNodePtr> result;
  52. foreach(RawNodePtr n, children) {
  53. if(n->name == name)
  54. result.append(n);
  55. }
  56. return result;
  57. }
  58. const QString get_value(QString name, QString default_value = "") const {
  59. QString result = default_value;
  60. foreach(RawNodePtr n, children) {
  61. if(n->name == name && !n->values.isEmpty()) {
  62. result = n->values[0];
  63. break;
  64. }
  65. }
  66. return result;
  67. }
  68. protected:
  69. QString name;
  70. QVector<QString> values;
  71. QVector<RawNodePtr> children;
  72. };
  73. #endif // RAWNODE_H