File: persistence.cpp

package info (click to toggle)
k3d 0.4.3.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 51,716 kB
  • ctags: 81,610
  • sloc: cpp: 283,698; ansic: 64,095; xml: 61,533; sh: 9,026; makefile: 5,282; python: 431; perl: 308; awk: 130
file content (136 lines) | stat: -rw-r--r-- 4,461 bytes parent folder | download
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// K-3D
// Copyright (c) 1995-2004, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

/** \file
		\brief Implements the k3d::container class, which automatically serializes / deserializes a group of data objects
		\author Tim Shead (tshead@k-3d.com)
*/

#include "persistence.h"
#include "result.h"
#include "utility.h"

#include <iostream>

namespace k3d
{

namespace persistence
{

/////////////////////////////////////////////////////////////////////////////
// container

container::~container()
{
	clear();
}

void container::load(sdpxml::Document& Document, sdpxml::Element& Element)
{
	// Create a mapping of elements to choose from ...
	typedef std::map<std::string, sdpxml::ElementPointer> element_map_t;
	typedef element_map_t::iterator element_iterator;
	element_map_t element_map;

	// Insert <variable> tags ... look for a container called <variables> first ...
	sdpxml::ElementPointer variables = sdpxml::FindElement(Element, sdpxml::SameName("variables"));

	/** \deprecated No <variables> tag, so for backwards compatibility, try the parent container */
	if(!variables)
		variables = &Element;

	for(sdpxml::ElementCollection::iterator element = variables->Children().begin(); element != variables->Children().end(); element++)
		{
			/** \todo Allow <object>, <shader>, and <argument> tags for backwards-compatibility with documents prior to version 0.2 ... should be removed from the 0.5 branch */
			if(element->Name() == "variable" || element->Name() == "object" || element->Name() == "shader" || element->Name() == "argument")
				{
					const std::string name = sdpxml::GetAttribute<std::string>(*element, "name", "");
					if(0 == name.size())
						continue;

					if(element_map.find(name) != element_map.end())
						std::cerr << __PRETTY_FUNCTION__ << " - duplicate <variable> [" << name << "] at " << sdpxml::FileReference(Document, *element) << " will be ignored" << std::endl;
					else
						element_map[name] = &(*element);

					// Warn the user about old data ...
					if(element->Name() != "variable")
						std::cerr << __PRETTY_FUNCTION__ << " - loading data from obsolete <" << element->Name() << "> tag ... re-save document to eliminate this warning" << std::endl;
				}
		}

	// If we didn't find anything, we're done ...
	if(0 == element_map.size())
		return;

	// For each proxy ...
	for(proxy_iterator proxy = m_proxies.begin(); proxy != m_proxies.end(); ++proxy)
		{
			// See if it has a corresponding element ...
			sdpxml::ElementPointer proxy_element = element_map[(*proxy)->name()];
			if(!proxy_element)
				continue;

			// Load it up!
			(*proxy)->load(Document, *proxy_element);
			element_map.erase((*proxy)->name());
		}
}

void container::load_complete()
{
	// Defer to our proxies ...
	std::for_each(m_proxies.begin(), m_proxies.end(), std::mem_fun(&idata_proxy::load_complete));
}

void container::save(sdpxml::Element& Element, k3d::idependencies& Dependencies)
{
	// If we don't have anything to save, we're done ...
	if(!m_proxies.size())
		return;

	// Create an XML element to contain our proxies if it doesn't already exist ...
	sdpxml::Element* variables = sdpxml::FindElement(Element, sdpxml::SameName("variables"));
	if(!variables)
		variables = &Element.Append(sdpxml::Element("variables"));

	// Save 'em ...
	for(proxy_iterator proxy = m_proxies.begin(); proxy != m_proxies.end(); ++proxy)
		(*proxy)->save(*variables, Dependencies);
}

void container::enable_serialization(std::auto_ptr<idata_proxy> Proxy)
{
	// Sanity checks ...
	return_if_fail(Proxy.get());
	m_proxies.push_back(Proxy.release());
}

void container::clear()
{
	std::for_each(m_proxies.begin(), m_proxies.end(), k3d::delete_object());
	m_proxies.clear();
}

} // namespace persistence

} // namespace k3d