2 Copyright (C) 2011 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <boost/variant/static_visitor.hpp>
26 #include "gui_object.h"
31 const string
GUIObjectState::xml_node_name (X_("GUIObjectState"));
33 GUIObjectState::~GUIObjectState ()
39 GUIObjectState::clear_maps ()
41 _property_maps
.clear ();
44 class gos_string_vistor
: public boost::static_visitor
<> {
46 gos_string_vistor (std::ostream
& o
)
49 void operator() (const int64_t& i
) {
53 void operator() (const std::string
& s
) {
62 GUIObjectState::get_string (const std::string
& id
, const std::string
& prop_name
, bool* empty
)
64 StringPropertyMap::iterator i
= _property_maps
.find (id
);
66 if (i
== _property_maps
.end()) {
73 const PropertyMap
& pmap (i
->second
);
74 PropertyMap::const_iterator p
= pmap
.find (prop_name
);
76 if (p
== pmap
.end()) {
84 gos_string_vistor
gsv (ss
);
86 boost::apply_visitor (gsv
, p
->second
);
96 GUIObjectState::get_state () const
98 XMLNode
* root
= new XMLNode (xml_node_name
);
100 for (StringPropertyMap::const_iterator i
= _property_maps
.begin(); i
!= _property_maps
.end(); ++i
) {
102 const PropertyMap
& pmap (i
->second
);
103 XMLNode
* id_node
= new XMLNode (X_("Object"));
105 id_node
->add_property ("id", i
->first
);
107 for (PropertyMap::const_iterator p
= pmap
.begin(); p
!= pmap
.end(); ++p
) {
108 std::stringstream ss
;
109 gos_string_vistor
gsv (ss
);
110 boost::apply_visitor (gsv
, p
->second
);
111 id_node
->add_property (p
->first
.c_str(), ss
.str());
114 root
->add_child_nocopy (*id_node
);
122 GUIObjectState::set_state (const XMLNode
& node
)
124 if (node
.name() != xml_node_name
) {
130 for (XMLNodeList::const_iterator i
= node
.children().begin(); i
!= node
.children().end(); ++i
) {
131 if ((*i
)->name() == X_("Object")) {
133 XMLNode
* child
= (*i
);
134 const XMLProperty
* idp
= child
->property (X_("id"));
140 string
id (idp
->value());
142 for (XMLPropertyList::const_iterator p
= child
->properties().begin(); p
!= child
->properties().end(); ++p
) {
143 /* note that this always sets the property with
144 a string value, and so is not equivalent to
145 a call made by the program that passed a
148 if ((*p
)->name() != X_("id")) {
149 set (id
, (*p
)->name(), (*p
)->value());
160 GUIObjectState::load (const XMLNode
& node
)
162 (void) set_state (node
);
166 GUIObjectState::operator= (const GUIObjectState
& other
)
168 _property_maps
= other
._property_maps
;