~smspillaz/unity/unity.6.0.fix_1036521

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2010 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Alex Launi <alex.launi@canonical.com>
 */

#include "Introspectable.h"

namespace unity
{
namespace debug
{

Introspectable::Introspectable()
{
  static guint64 unique_id=0;
  _id = unique_id++;
}

Introspectable::~Introspectable()
{
  for (auto parent : _parents)
    parent->_children.remove(this);
  for (auto child : _children)
    child->_parents.remove(this);
}

Introspectable::IntrospectableList Introspectable::GetIntrospectableChildren()
{
  return _children;
}

GVariant*
Introspectable::Introspect()
{
  GVariantBuilder  builder;
  GVariantBuilder  child_builder;
  gint             n_children = 0;

  g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
  g_variant_builder_add(&builder, "{sv}", "id", g_variant_new_uint64(_id));

  AddProperties(&builder);

  g_variant_builder_init(&child_builder, G_VARIANT_TYPE("as"));

  auto children = GetIntrospectableChildren();
  for (auto it = children.begin(); it != children.end(); it++)
  {
    if ((*it)->GetName() != "")
    {
      g_variant_builder_add(&child_builder, "s", (*it)->GetName().c_str());
      n_children++;
    }
  }

  GVariant* child_results = g_variant_builder_end(&child_builder);

  if (n_children > 0)
    g_variant_builder_add(&builder, "{sv}", GetChildsName().c_str(), child_results);
  return g_variant_builder_end(&builder);
}

void
Introspectable::AddChild(Introspectable* child)
{
  _children.push_back(child);
  child->_parents.push_back(this);
}

void
Introspectable::RemoveChild(Introspectable* child, child_destructor destr)
{
  _children.remove(child);
  child->_parents.remove(this);

  if (destr)
    (*destr)(child);
}

std::string
Introspectable::GetChildsName() const
{
  return "Children";
}

guint64 Introspectable::GetIntrospectionId() const
{
  return _id;
}

void
Introspectable::RemoveAllChildren(child_destructor destr)
{
  for (auto child : _children)
  {
    child->_parents.remove(this);
    if (destr)
      (*destr)(child);
  }
  _children.clear();
}

}
}