1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // basic_xml_oarchive.ipp:
4 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org for updates, documentation, and revision history.
12 #include <cstddef> // NULL
14 #if defined(BOOST_NO_STDC_NAMESPACE) && ! defined(__LIBCOMO__)
20 #include <boost/archive/basic_xml_archive.hpp>
21 #include <boost/archive/basic_xml_oarchive.hpp>
22 #include <boost/archive/xml_archive_exception.hpp>
23 #include <boost/core/no_exceptions_support.hpp>
29 template<class CharType>
31 void operator()(CharType t) const{
32 const unsigned char lookup_table[] = {
33 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
34 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
35 0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0, // -.
36 1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, // 0-9
37 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // A-
38 1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1, // -Z _
39 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // a-
40 1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, // -z
41 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
42 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
46 if(0 == lookup_table[(unsigned)t])
47 boost::serialization::throw_exception(
48 xml_archive_exception(
49 xml_archive_exception::xml_archive_tag_name_error
57 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
58 // implemenations of functions common to both types of xml output
60 template<class Archive>
61 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
62 basic_xml_oarchive<Archive>::write_attribute(
63 const char *attribute_name,
65 const char *conjunction
67 this->This()->put(' ');
68 this->This()->put(attribute_name);
69 this->This()->put(conjunction);
70 this->This()->save(t);
71 this->This()->put('"');
74 template<class Archive>
75 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
76 basic_xml_oarchive<Archive>::write_attribute(
77 const char *attribute_name,
80 this->This()->put(' ');
81 this->This()->put(attribute_name);
82 this->This()->put("=\"");
83 this->This()->save(key);
84 this->This()->put('"');
87 template<class Archive>
88 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
89 basic_xml_oarchive<Archive>::indent(){
91 for(i = depth; i-- > 0;)
92 this->This()->put('\t');
95 template<class Archive>
96 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
97 basic_xml_oarchive<Archive>::save_start(const char *name)
102 // be sure name has no invalid characters
103 std::for_each(name, name + std::strlen(name), detail::XML_name<const char>());
107 this->This()->put('\n');
111 this->This()->put('<');
112 this->This()->save(name);
113 pending_preamble = true;
117 template<class Archive>
118 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
119 basic_xml_oarchive<Archive>::save_end(const char *name)
124 // be sure name has no invalid characters
125 std::for_each(name, name + std::strlen(name), detail::XML_name<const char>());
130 this->This()->put('\n');
134 this->This()->put("</");
135 this->This()->save(name);
136 this->This()->put('>');
138 this->This()->put('\n');
141 template<class Archive>
142 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
143 basic_xml_oarchive<Archive>::end_preamble(){
144 if(pending_preamble){
145 this->This()->put('>');
146 pending_preamble = false;
150 template<class Archive>
151 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
152 basic_xml_oarchive<Archive>::save_override(const object_id_type & t)
154 int i = t.t; // extra .t is for borland
155 write_attribute(BOOST_ARCHIVE_XML_OBJECT_ID(), i, "=\"_");
157 template<class Archive>
158 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
159 basic_xml_oarchive<Archive>::save_override(
160 const object_reference_type & t,
163 int i = t.t; // extra .t is for borland
164 write_attribute(BOOST_ARCHIVE_XML_OBJECT_REFERENCE(), i, "=\"_");
166 template<class Archive>
167 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
168 basic_xml_oarchive<Archive>::save_override(const version_type & t)
170 int i = t.t; // extra .t is for borland
171 write_attribute(BOOST_ARCHIVE_XML_VERSION(), i);
175 template<class Archive>
176 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
177 basic_xml_oarchive<Archive>::save_override(const object_id_type & t)
179 // borland doesn't do conversion of STRONG_TYPEDEFs very well
180 const unsigned int i = t;
181 write_attribute(BOOST_ARCHIVE_XML_OBJECT_ID(), i, "=\"_");
183 template<class Archive>
184 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
185 basic_xml_oarchive<Archive>::save_override(
186 const object_reference_type & t
188 const unsigned int i = t;
189 write_attribute(BOOST_ARCHIVE_XML_OBJECT_REFERENCE(), i, "=\"_");
191 template<class Archive>
192 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
193 basic_xml_oarchive<Archive>::save_override(const version_type & t)
195 const unsigned int i = t;
196 write_attribute(BOOST_ARCHIVE_XML_VERSION(), i);
199 template<class Archive>
200 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
201 basic_xml_oarchive<Archive>::save_override(const class_id_type & t)
203 write_attribute(BOOST_ARCHIVE_XML_CLASS_ID(), t);
205 template<class Archive>
206 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
207 basic_xml_oarchive<Archive>::save_override(
208 const class_id_reference_type & t
210 write_attribute(BOOST_ARCHIVE_XML_CLASS_ID_REFERENCE(), t);
212 template<class Archive>
213 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
214 basic_xml_oarchive<Archive>::save_override(
215 const class_id_optional_type & t
217 write_attribute(BOOST_ARCHIVE_XML_CLASS_ID(), t);
219 template<class Archive>
220 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
221 basic_xml_oarchive<Archive>::save_override(const class_name_type & t)
223 const char * key = t;
226 write_attribute(BOOST_ARCHIVE_XML_CLASS_NAME(), key);
229 template<class Archive>
230 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
231 basic_xml_oarchive<Archive>::save_override(const tracking_type & t)
233 write_attribute(BOOST_ARCHIVE_XML_TRACKING(), t.t);
236 template<class Archive>
237 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
238 basic_xml_oarchive<Archive>::init(){
240 this->This()->put("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n");
241 this->This()->put("<!DOCTYPE boost_serialization>\n");
242 // xml document wrapper - outer root
243 this->This()->put("<boost_serialization");
244 write_attribute("signature", BOOST_ARCHIVE_SIGNATURE());
245 write_attribute("version", BOOST_ARCHIVE_VERSION());
246 this->This()->put(">\n");
249 template<class Archive>
250 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
251 basic_xml_oarchive<Archive>::windup(){
253 this->This()->put("</boost_serialization>\n");
256 template<class Archive>
257 BOOST_ARCHIVE_OR_WARCHIVE_DECL
258 basic_xml_oarchive<Archive>::basic_xml_oarchive(unsigned int flags) :
259 detail::common_oarchive<Archive>(flags),
261 pending_preamble(false),
266 template<class Archive>
267 BOOST_ARCHIVE_OR_WARCHIVE_DECL
268 basic_xml_oarchive<Archive>::~basic_xml_oarchive(){
271 } // namespace archive