replaced std::auto_ptr by new std::unique_ptr

master
Marc Wäckerlin 9 years ago
parent 2f73e3bdff
commit 7991de3432
  1. 2
      examples/address.cxx
  2. 2
      examples/node_macros.cxx
  3. 42
      src/xml-cxx/xml.hxx
  4. 34
      src/xml.cxx
  5. 8
      test/xml_test.cxx

@ -45,7 +45,7 @@ int main(int, char**) try {
<url>http://swissign.com</url>\ <url>http://swissign.com</url>\
<url>http://swissign.ch</url>\ <url>http://swissign.ch</url>\
</address>"); </address>");
std::auto_ptr<xml::Node> author(addrTpl.read(ss)); std::unique_ptr<xml::Node> author(addrTpl.read(ss));
// write to stdout // write to stdout
std::cout<<"Successfully read:"<<std::endl std::cout<<"Successfully read:"<<std::endl
<<"------------------------------"<<std::endl <<"------------------------------"<<std::endl

@ -22,7 +22,7 @@ int main(int, char**) {
" <element>Hello</element>\n" " <element>Hello</element>\n"
" </child>\n" " </child>\n"
"</base>"); "</base>");
std::auto_ptr<xml::Node> file(test.read(ss)); std::unique_ptr<xml::Node> file(test.read(ss));
std::cout<<"The text in element is: " std::cout<<"The text in element is: "
<<(*file)[xml::name::child][xml::name::element].text() <<(*file)[xml::name::child][xml::name::element].text()
<<std::endl; <<std::endl;

@ -306,7 +306,7 @@ class MethodTrace {
" <element>Hello</element>\n" " <element>Hello</element>\n"
" </child>\n" " </child>\n"
"</base>"); "</base>");
std::auto_ptr<xml::Node> file(test.read(ss)); std::unique_ptr<xml::Node> file(test.read(ss));
std::cout<<"The element is: " std::cout<<"The element is: "
<<(*file)["child"]["element"] <<(*file)["child"]["element"]
<<std::endl; <<std::endl;
@ -432,7 +432,7 @@ class MethodTrace {
.attr("id", xml::mandatory))))); .attr("id", xml::mandatory)))));
[...] [...]
try { try {
std::auto_ptr<xml::Node> persons(test.read(std::ifstream("file.xml))); std::unique_ptr<xml::Node> persons(test.read(std::ifstream("file.xml)));
// Here we can be sure, that our structure is valid, // Here we can be sure, that our structure is valid,
// but we must check optional elements before access, otherwise // but we must check optional elements before access, otherwise
// we get an exception. // we get an exception.
@ -784,7 +784,7 @@ namespace xml {
Node(const Node& o) throw(); Node(const Node& o) throw();
virtual ~Node() throw(); virtual ~Node() throw();
virtual Node& operator=(const Node& o) throw(); virtual Node& operator=(const Node& o) throw();
virtual std::auto_ptr<Node> clone() const throw(); virtual std::unique_ptr<Node> clone() const throw();
virtual std::ostream& out(std::ostream& o, unsigned int level=0) const virtual std::ostream& out(std::ostream& o, unsigned int level=0) const
throw(); throw();
virtual std::string text() const throw(); virtual std::string text() const throw();
@ -836,7 +836,7 @@ namespace xml {
Attributes _attributes; Attributes _attributes;
private: private:
Node* find(const std::string& child) const throw(); Node* find(const std::string& child) const throw();
virtual std::auto_ptr<Node> clone(Node* p) const throw(); virtual std::unique_ptr<Node> clone(Node* p) const throw();
Node(); // not implemented Node(); // not implemented
Contents _contents; Contents _contents;
std::string _name; std::string _name;
@ -854,7 +854,7 @@ namespace xml {
String(std::string name, const std::string& text, String(std::string name, const std::string& text,
Node::size_type min=0, Node::size_type max=0) throw(); Node::size_type min=0, Node::size_type max=0) throw();
virtual ~String() throw() {} virtual ~String() throw() {}
virtual std::auto_ptr<Node> clone() const throw(); virtual std::unique_ptr<Node> clone() const throw();
virtual std::string text() const throw(); virtual std::string text() const throw();
virtual String& text(const std::string& txt) throw(tag_expected, virtual String& text(const std::string& txt) throw(tag_expected,
type_mismatch); type_mismatch);
@ -885,7 +885,7 @@ namespace xml {
public: public:
UnsignedInteger(std::string name, unsigned long i=0, UnsignedInteger(std::string name, unsigned long i=0,
size_type min=0, size_type max=0) throw(); size_type min=0, size_type max=0) throw();
virtual std::auto_ptr<Node> clone() const throw(); virtual std::unique_ptr<Node> clone() const throw();
virtual ~UnsignedInteger() throw() {} virtual ~UnsignedInteger() throw() {}
virtual UnsignedInteger& text(const std::string& txt) virtual UnsignedInteger& text(const std::string& txt)
throw(tag_expected, type_mismatch); throw(tag_expected, type_mismatch);
@ -954,7 +954,7 @@ namespace xml {
throw(factory_not_valid); throw(factory_not_valid);
static std::ostream& print(std::ostream& os, const Node& node, static std::ostream& print(std::ostream& os, const Node& node,
unsigned int level=0) throw(); unsigned int level=0) throw();
std::auto_ptr<Node> read(std::istream& is) std::unique_ptr<Node> read(std::istream& is)
throw(wrong_end_tag, wrong_start_tag, tag_expected, type_mismatch, throw(wrong_end_tag, wrong_start_tag, tag_expected, type_mismatch,
second_slash_in_tag, character_after_slash, second_slash_in_tag, character_after_slash,
missing_end_tag, attribute_value_not_quoted, access_error, missing_end_tag, attribute_value_not_quoted, access_error,
@ -972,7 +972,7 @@ namespace xml {
Node& operator*() throw(factory_not_valid); Node& operator*() throw(factory_not_valid);
Node*const operator->() throw(factory_not_valid); Node*const operator->() throw(factory_not_valid);
bool ws(char c) throw(); bool ws(char c) throw();
std::auto_ptr<Node> read(std::istream& is, const Node& position) std::unique_ptr<Node> read(std::istream& is, const Node& position)
throw(wrong_end_tag, wrong_start_tag, tag_expected, type_mismatch, throw(wrong_end_tag, wrong_start_tag, tag_expected, type_mismatch,
second_slash_in_tag, character_after_slash, second_slash_in_tag, character_after_slash,
missing_end_tag, missing_end_tag,
@ -980,8 +980,8 @@ namespace xml {
attributes_in_end_tag, attributes_in_end_tag,
illegal_attribute, mandatory_attribute_missing, illegal_attribute, mandatory_attribute_missing,
wrong_node_number); wrong_node_number);
std::auto_ptr<Node> checkChildren(const xml::Node& tpl, std::unique_ptr<Node> checkChildren(const xml::Node& tpl,
std::auto_ptr<Node> node, std::unique_ptr<Node> node,
std::istream& is) const std::istream& is) const
throw(wrong_node_number); throw(wrong_node_number);
Tag tag(std::istream& is, const Node& position) Tag tag(std::istream& is, const Node& position)
@ -1302,7 +1302,7 @@ namespace xml {
checkInit(); checkInit();
xml::Factory factory(_xmlFactory); xml::Factory factory(_xmlFactory);
if (name.size()) factory->name(name); if (name.size()) factory->name(name);
std::auto_ptr<xml::Node> node(factory.read(is)); std::unique_ptr<xml::Node> node(factory.read(is));
CONTAINER_TYPE::clear(); CONTAINER_TYPE::clear();
for (xml::Node::size_type i(0); i<node->children(); ++i) { for (xml::Node::size_type i(0); i<node->children(); ++i) {
typename CONTAINER_TYPE::value_type tmp; typename CONTAINER_TYPE::value_type tmp;
@ -1317,13 +1317,13 @@ namespace xml {
checkInit(); checkInit();
xml::Node node(*_xmlFactory); xml::Node node(*_xmlFactory);
if (name.size()) node.name(name); if (name.size()) node.name(name);
std::auto_ptr<xml::Node> tpl(node[0].clone()); std::unique_ptr<xml::Node> tpl(node[0].clone());
node.clear(); node.clear();
for (typename Container::const_iterator it = this->begin(); for (typename Container::const_iterator it = this->begin();
it!=this->end(); ++it) { it!=this->end(); ++it) {
typename CONTAINER_TYPE::value_type tmp; typename CONTAINER_TYPE::value_type tmp;
tmp = *it; tmp = *it;
std::auto_ptr<xml::Node> item(tpl->clone()); std::unique_ptr<xml::Node> item(tpl->clone());
Serialize::toNode(&tmp, *item); Serialize::toNode(&tmp, *item);
node<<*item; node<<*item;
} }
@ -1366,7 +1366,7 @@ namespace xml {
checkInit(); checkInit();
xml::Factory factory(_xmlFactory); xml::Factory factory(_xmlFactory);
if (name.size()) factory->name(name); if (name.size()) factory->name(name);
std::auto_ptr<xml::Node> node(factory.read(is)); std::unique_ptr<xml::Node> node(factory.read(is));
CONTAINER_TYPE::clear(); CONTAINER_TYPE::clear();
for (xml::Node::size_type i(0); i<node->children(); ++i) { for (xml::Node::size_type i(0); i<node->children(); ++i) {
typename CONTAINER_TYPE::value_type tmp; typename CONTAINER_TYPE::value_type tmp;
@ -1381,13 +1381,13 @@ namespace xml {
checkInit(); checkInit();
xml::Node node(*_xmlFactory); xml::Node node(*_xmlFactory);
if (name.size()) node.name(name); if (name.size()) node.name(name);
std::auto_ptr<xml::Node> tpl(node[0].clone()); std::unique_ptr<xml::Node> tpl(node[0].clone());
node.clear(); node.clear();
for (typename CONTAINER_TYPE::const_iterator it = this->begin(); for (typename CONTAINER_TYPE::const_iterator it = this->begin();
it!=this->end(); ++it) { it!=this->end(); ++it) {
typename CONTAINER_TYPE::value_type tmp; typename CONTAINER_TYPE::value_type tmp;
tmp = *it; tmp = *it;
std::auto_ptr<xml::Node> item(tpl->clone()); std::unique_ptr<xml::Node> item(tpl->clone());
Serialize::toNode(&tmp, *item); Serialize::toNode(&tmp, *item);
node<<*item; node<<*item;
} }
@ -1433,7 +1433,7 @@ namespace xml {
checkInit(); checkInit();
xml::Factory factory(_xmlFactory); xml::Factory factory(_xmlFactory);
if (name.size()) factory->name(name); if (name.size()) factory->name(name);
std::auto_ptr<xml::Node> node(factory.read(is)); std::unique_ptr<xml::Node> node(factory.read(is));
CONTAINER_TYPE::clear(); CONTAINER_TYPE::clear();
for (xml::Node::size_type i(0); i<node->children(); ++i) { for (xml::Node::size_type i(0); i<node->children(); ++i) {
typename CONTAINER_TYPE::key_type key; typename CONTAINER_TYPE::key_type key;
@ -1450,8 +1450,8 @@ namespace xml {
checkInit(); checkInit();
xml::Node node(*_xmlFactory); xml::Node node(*_xmlFactory);
if (name.size()) node.name(name); if (name.size()) node.name(name);
std::auto_ptr<xml::Node> tpl1(node[0].clone()); std::unique_ptr<xml::Node> tpl1(node[0].clone());
std::auto_ptr<xml::Node> tpl2(node[1].clone()); std::unique_ptr<xml::Node> tpl2(node[1].clone());
node.clear(); // "node" is now invalid node.clear(); // "node" is now invalid
for (typename AssociativeMap::const_iterator it = this->begin(); for (typename AssociativeMap::const_iterator it = this->begin();
it!=this->end(); ++it) { it!=this->end(); ++it) {
@ -1459,9 +1459,9 @@ namespace xml {
typename CONTAINER_TYPE::mapped_type data; typename CONTAINER_TYPE::mapped_type data;
key = it->first; key = it->first;
data = it->second; data = it->second;
std::auto_ptr<xml::Node> item1(tpl1->clone()); std::unique_ptr<xml::Node> item1(tpl1->clone());
Serialize::toNode(&key, *item1); Serialize::toNode(&key, *item1);
std::auto_ptr<xml::Node> item2(tpl2->clone()); std::unique_ptr<xml::Node> item2(tpl2->clone());
Serialize::toNode(&data, *item2); Serialize::toNode(&data, *item2);
node<<*item1<<*item2; node<<*item1<<*item2;
} }

@ -272,8 +272,8 @@ namespace xml {
The user of this library doesn't have to and is not able to care The user of this library doesn't have to and is not able to care
about the parenting. */ about the parenting. */
std::auto_ptr<Node> Node::clone() const throw() { std::unique_ptr<Node> Node::clone() const throw() {
std::auto_ptr<Node> res(new Node(*this)); std::unique_ptr<Node> res(new Node(*this));
res->_parent = 0; res->_parent = 0;
return res; return res;
} }
@ -600,8 +600,8 @@ namespace xml {
return 0; return 0;
} }
//! Clone a node, but assign a new parent. //! Clone a node, but assign a new parent.
std::auto_ptr<Node> Node::clone(Node* p) const throw() { std::unique_ptr<Node> Node::clone(Node* p) const throw() {
std::auto_ptr<Node> c(clone()); std::unique_ptr<Node> c(clone());
c->_parent = p; c->_parent = p;
return c; return c;
} }
@ -620,8 +620,8 @@ namespace xml {
Node::size_type min, Node::size_type max) throw(): Node::size_type min, Node::size_type max) throw():
Node(name, min, max), _text(text) { Node(name, min, max), _text(text) {
} }
std::auto_ptr<Node> String::clone() const throw() { std::unique_ptr<Node> String::clone() const throw() {
return std::auto_ptr<Node>(new String(*this)); return std::unique_ptr<Node>(new String(*this));
} }
std::string String::text() const throw() { std::string String::text() const throw() {
return _text; return _text;
@ -740,8 +740,8 @@ namespace xml {
size_type min, size_type max) throw(): size_type min, size_type max) throw():
String(name, mrw::string(i), min, max) { String(name, mrw::string(i), min, max) {
} }
std::auto_ptr<Node> UnsignedInteger::clone() const throw() { std::unique_ptr<Node> UnsignedInteger::clone() const throw() {
return std::auto_ptr<Node>(new UnsignedInteger(*this)); return std::unique_ptr<Node>(new UnsignedInteger(*this));
} }
//! An xml::UnsignedInteger must only contain an number. //! An xml::UnsignedInteger must only contain an number.
/*! En exception is thrown, if the contents does not match a number. */ /*! En exception is thrown, if the contents does not match a number. */
@ -876,7 +876,7 @@ namespace xml {
return os; return os;
} }
//! Restore a xml::Node tree from a stream, according to the given schema. //! Restore a xml::Node tree from a stream, according to the given schema.
std::auto_ptr<Node> Factory::read(std::istream& is) std::unique_ptr<Node> Factory::read(std::istream& is)
throw(wrong_end_tag, wrong_start_tag, tag_expected, type_mismatch, throw(wrong_end_tag, wrong_start_tag, tag_expected, type_mismatch,
second_slash_in_tag, second_slash_in_tag,
character_after_slash, missing_end_tag, attribute_value_not_quoted, character_after_slash, missing_end_tag, attribute_value_not_quoted,
@ -888,7 +888,7 @@ namespace xml {
try { try {
_line=1; _line=1;
_open=0; _open=0;
std::auto_ptr<Node> node(read(is, _template)); std::unique_ptr<Node> node(read(is, _template));
if (node->children()==0) if (node->children()==0)
throw tag_expected(_template[0], throw tag_expected(_template[0],
"nothing found, possibly empty stream"); "nothing found, possibly empty stream");
@ -919,14 +919,14 @@ namespace xml {
last = c; last = c;
return c==' '||c=='\t'||c=='\n'||c=='\r'; return c==' '||c=='\t'||c=='\n'||c=='\r';
} }
std::auto_ptr<Node> Factory::read(std::istream& is, const Node& node) std::unique_ptr<Node> Factory::read(std::istream& is, const Node& node)
throw(wrong_end_tag, wrong_start_tag, tag_expected, type_mismatch, throw(wrong_end_tag, wrong_start_tag, tag_expected, type_mismatch,
second_slash_in_tag, second_slash_in_tag,
character_after_slash, missing_end_tag, attribute_value_not_quoted, character_after_slash, missing_end_tag, attribute_value_not_quoted,
access_error, duplicate_attribute, attributes_in_end_tag, access_error, duplicate_attribute, attributes_in_end_tag,
illegal_attribute, mandatory_attribute_missing, illegal_attribute, mandatory_attribute_missing,
wrong_node_number) { wrong_node_number) {
std::auto_ptr<Node> result(node.clone()); std::unique_ptr<Node> result(node.clone());
result->clear(); result->clear();
while (true) { while (true) {
Tag res(tag(is, node)); Tag res(tag(is, node));
@ -939,9 +939,9 @@ namespace xml {
if (res.name!=node.name()) throw wrong_end_tag(node, is, res); if (res.name!=node.name()) throw wrong_end_tag(node, is, res);
} return result; } return result;
case EMPTY: { case EMPTY: {
std::auto_ptr<Node> current(node[res.name].clone()); std::unique_ptr<Node> current(node[res.name].clone());
current->clear()<<res.attributes; current->clear()<<res.attributes;
*result<<*checkChildren(node[res.name], current, is); *result<<*checkChildren(node[res.name], std::move(current), is);
} break; } break;
case START: { case START: {
++_open; ++_open;
@ -952,8 +952,8 @@ namespace xml {
} }
} }
} }
std::auto_ptr<Node> Factory::checkChildren(const xml::Node& tpl, std::unique_ptr<Node> Factory::checkChildren(const xml::Node& tpl,
std::auto_ptr<Node> node, std::unique_ptr<Node> node,
std::istream& is) const std::istream& is) const
throw(wrong_node_number) { throw(wrong_node_number) {
for (Node::size_type i(0); i<tpl.children(); ++i) for (Node::size_type i(0); i<tpl.children(); ++i)
@ -1166,7 +1166,7 @@ namespace xml {
checkInit(); checkInit();
xml::Factory factory(_xmlFactory); xml::Factory factory(_xmlFactory);
if (name.size()) factory->name(name); if (name.size()) factory->name(name);
std::auto_ptr<xml::Node> node(factory.read(is)); std::unique_ptr<xml::Node> node(factory.read(is));
for (std::map<std::string, Any>::const_iterator for (std::map<std::string, Any>::const_iterator
it(_xmlNames.begin()); it(_xmlNames.begin());
it!=_xmlNames.end(); ++it) it!=_xmlNames.end(); ++it)

@ -22,7 +22,7 @@ class NodeTest: public CppUnit::TestFixture {
} }
void clone() { void clone() {
xml::Node t("test"); xml::Node t("test");
std::auto_ptr<xml::Node> p(t.clone()); std::unique_ptr<xml::Node> p(t.clone());
CPPUNIT_ASSERT_EQUAL(std::string("test"), p->name()); CPPUNIT_ASSERT_EQUAL(std::string("test"), p->name());
} }
void shift() { void shift() {
@ -100,7 +100,7 @@ class StringTest: public CppUnit::TestFixture {
} }
void clone() { void clone() {
xml::String t("test"); xml::String t("test");
std::auto_ptr<xml::Node> p(t.clone()); std::unique_ptr<xml::Node> p(t.clone());
CPPUNIT_ASSERT_EQUAL(std::string("test"), p->name()); CPPUNIT_ASSERT_EQUAL(std::string("test"), p->name());
} }
void shift() { void shift() {
@ -181,7 +181,7 @@ class ComplexTest: public CppUnit::TestFixture {
<<"<child a=\"b\"/>" <<"<child a=\"b\"/>"
<<"< otherchild >< / otherchild >< otherchild / >"<<std::endl <<"< otherchild >< / otherchild >< otherchild / >"<<std::endl
<<"</base>"; <<"</base>";
std::auto_ptr<xml::Node> node(factory.read(file1)); // should work std::unique_ptr<xml::Node> node(factory.read(file1)); // should work
CPPUNIT_ASSERT_EQUAL((size_t)2, node->list("child").size()); CPPUNIT_ASSERT_EQUAL((size_t)2, node->list("child").size());
CPPUNIT_ASSERT_EQUAL((size_t)3, (*node)[0].list("childofchild").size()); CPPUNIT_ASSERT_EQUAL((size_t)3, (*node)[0].list("childofchild").size());
CPPUNIT_ASSERT_EQUAL((size_t)4, (*node)[0].list("number").size()); CPPUNIT_ASSERT_EQUAL((size_t)4, (*node)[0].list("number").size());
@ -354,7 +354,7 @@ class FunTest: public CppUnit::TestFixture {
<<xml::String("h1").attr("class", xml::optional) <<xml::String("h1").attr("class", xml::optional)
<<xml::String("h2") <<xml::String("h2")
<<xml::String("p"))); <<xml::String("p")));
std::auto_ptr<xml::Node> read(factory.read(ss)); // read back the example std::unique_ptr<xml::Node> read(factory.read(ss)); // read back the example
std::stringstream ss2; std::stringstream ss2;
read->out(ss2); read->out(ss2);
CPPUNIT_ASSERT_EQUAL(contents, ss2.str()); CPPUNIT_ASSERT_EQUAL(contents, ss2.str());

Loading…
Cancel
Save