Back to index

unity  6.0.0
Introspectable.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2010 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Alex Launi <alex.launi@canonical.com>
00018  */
00019 
00020 #include "Introspectable.h"
00021 
00022 namespace unity
00023 {
00024 namespace debug
00025 {
00026 
00027 Introspectable::Introspectable()
00028 {
00029   static guint64 unique_id=0;
00030   _id = unique_id++;
00031 }
00032 
00033 Introspectable::~Introspectable()
00034 {
00035   for (auto parent : _parents)
00036     parent->_children.remove(this);
00037   for (auto child : _children)
00038     child->_parents.remove(this);
00039 }
00040 
00041 Introspectable::IntrospectableList Introspectable::GetIntrospectableChildren()
00042 {
00043   return _children;
00044 }
00045 
00046 GVariant*
00047 Introspectable::Introspect()
00048 {
00049   GVariantBuilder  builder;
00050   GVariantBuilder  child_builder;
00051   gint             n_children = 0;
00052 
00053   g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
00054   g_variant_builder_add(&builder, "{sv}", "id", g_variant_new_uint64(_id));
00055 
00056   AddProperties(&builder);
00057 
00058   g_variant_builder_init(&child_builder, G_VARIANT_TYPE("as"));
00059 
00060   auto children = GetIntrospectableChildren();
00061   for (auto it = children.begin(); it != children.end(); it++)
00062   {
00063     if ((*it)->GetName() != "")
00064     {
00065       g_variant_builder_add(&child_builder, "s", (*it)->GetName().c_str());
00066       n_children++;
00067     }
00068   }
00069 
00070   GVariant* child_results = g_variant_builder_end(&child_builder);
00071 
00072   if (n_children > 0)
00073     g_variant_builder_add(&builder, "{sv}", GetChildsName().c_str(), child_results);
00074   return g_variant_builder_end(&builder);
00075 }
00076 
00077 void
00078 Introspectable::AddChild(Introspectable* child)
00079 {
00080   _children.push_back(child);
00081   child->_parents.push_back(this);
00082 }
00083 
00084 void
00085 Introspectable::RemoveChild(Introspectable* child)
00086 {
00087   _children.remove(child);
00088   child->_parents.remove(this);
00089 }
00090 
00091 std::string
00092 Introspectable::GetChildsName() const
00093 {
00094   return "Children";
00095 }
00096 
00097 guint64 Introspectable::GetIntrospectionId() const
00098 {
00099   return _id;
00100 }
00101 
00102 }
00103 }
00104