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.ch</url>\
</address>");
std::auto_ptr<xml::Node> author(addrTpl.read(ss));
std::unique_ptr<xml::Node> author(addrTpl.read(ss));
// write to stdout
std::cout<<"Successfully read:"<<std::endl
<<"------------------------------"<<std::endl

@ -22,7 +22,7 @@ int main(int, char**) {
" <element>Hello</element>\n"
" </child>\n"
"</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: "
<<(*file)[xml::name::child][xml::name::element].text()
<<std::endl;

@ -306,7 +306,7 @@ class MethodTrace {
" <element>Hello</element>\n"
" </child>\n"
"</base>");
std::auto_ptr<xml::Node> file(test.read(ss));
std::unique_ptr<xml::Node> file(test.read(ss));
std::cout<<"The element is: "
<<(*file)["child"]["element"]
<<std::endl;
@ -432,7 +432,7 @@ class MethodTrace {
.attr("id", xml::mandatory)))));
[...]
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,
// but we must check optional elements before access, otherwise
// we get an exception.
@ -784,7 +784,7 @@ namespace xml {
Node(const Node& o) throw();
virtual ~Node() 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
throw();
virtual std::string text() const throw();
@ -836,7 +836,7 @@ namespace xml {
Attributes _attributes;
private:
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
Contents _contents;
std::string _name;
@ -854,7 +854,7 @@ namespace xml {
String(std::string name, const std::string& text,
Node::size_type min=0, Node::size_type max=0) 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 String& text(const std::string& txt) throw(tag_expected,
type_mismatch);
@ -885,7 +885,7 @@ namespace xml {
public:
UnsignedInteger(std::string name, unsigned long i=0,
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& text(const std::string& txt)
throw(tag_expected, type_mismatch);
@ -954,7 +954,7 @@ namespace xml {
throw(factory_not_valid);
static std::ostream& print(std::ostream& os, const Node& node,
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,
second_slash_in_tag, character_after_slash,
missing_end_tag, attribute_value_not_quoted, access_error,
@ -972,7 +972,7 @@ namespace xml {
Node& operator*() throw(factory_not_valid);
Node*const operator->() throw(factory_not_valid);
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,
second_slash_in_tag, character_after_slash,
missing_end_tag,
@ -980,8 +980,8 @@ namespace xml {
attributes_in_end_tag,
illegal_attribute, mandatory_attribute_missing,
wrong_node_number);
std::auto_ptr<Node> checkChildren(const xml::Node& tpl,
std::auto_ptr<Node> node,
std::unique_ptr<Node> checkChildren(const xml::Node& tpl,
std::unique_ptr<Node> node,
std::istream& is) const
throw(wrong_node_number);
Tag tag(std::istream& is, const Node& position)
@ -1302,7 +1302,7 @@ namespace xml {
checkInit();
xml::Factory factory(_xmlFactory);
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();
for (xml::Node::size_type i(0); i<node->children(); ++i) {
typename CONTAINER_TYPE::value_type tmp;
@ -1317,13 +1317,13 @@ namespace xml {
checkInit();
xml::Node node(*_xmlFactory);
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();
for (typename Container::const_iterator it = this->begin();
it!=this->end(); ++it) {
typename CONTAINER_TYPE::value_type tmp;
tmp = *it;
std::auto_ptr<xml::Node> item(tpl->clone());
std::unique_ptr<xml::Node> item(tpl->clone());
Serialize::toNode(&tmp, *item);
node<<*item;
}
@ -1366,7 +1366,7 @@ namespace xml {
checkInit();
xml::Factory factory(_xmlFactory);
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();
for (xml::Node::size_type i(0); i<node->children(); ++i) {
typename CONTAINER_TYPE::value_type tmp;
@ -1381,13 +1381,13 @@ namespace xml {
checkInit();
xml::Node node(*_xmlFactory);
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();
for (typename CONTAINER_TYPE::const_iterator it = this->begin();
it!=this->end(); ++it) {
typename CONTAINER_TYPE::value_type tmp;
tmp = *it;
std::auto_ptr<xml::Node> item(tpl->clone());
std::unique_ptr<xml::Node> item(tpl->clone());
Serialize::toNode(&tmp, *item);
node<<*item;
}
@ -1433,7 +1433,7 @@ namespace xml {
checkInit();
xml::Factory factory(_xmlFactory);
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();
for (xml::Node::size_type i(0); i<node->children(); ++i) {
typename CONTAINER_TYPE::key_type key;
@ -1450,8 +1450,8 @@ namespace xml {
checkInit();
xml::Node node(*_xmlFactory);
if (name.size()) node.name(name);
std::auto_ptr<xml::Node> tpl1(node[0].clone());
std::auto_ptr<xml::Node> tpl2(node[1].clone());
std::unique_ptr<xml::Node> tpl1(node[0].clone());
std::unique_ptr<xml::Node> tpl2(node[1].clone());
node.clear(); // "node" is now invalid
for (typename AssociativeMap::const_iterator it = this->begin();
it!=this->end(); ++it) {
@ -1459,9 +1459,9 @@ namespace xml {
typename CONTAINER_TYPE::mapped_type data;
key = it->first;
data = it->second;
std::auto_ptr<xml::Node> item1(tpl1->clone());
std::unique_ptr<xml::Node> item1(tpl1->clone());
Serialize::toNode(&key, *item1);
std::auto_ptr<xml::Node> item2(tpl2->clone());
std::unique_ptr<xml::Node> item2(tpl2->clone());
Serialize::toNode(&data, *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
about the parenting. */
std::auto_ptr<Node> Node::clone() const throw() {
std::auto_ptr<Node> res(new Node(*this));
std::unique_ptr<Node> Node::clone() const throw() {
std::unique_ptr<Node> res(new Node(*this));
res->_parent = 0;
return res;
}
@ -600,8 +600,8 @@ namespace xml {
return 0;
}
//! Clone a node, but assign a new parent.
std::auto_ptr<Node> Node::clone(Node* p) const throw() {
std::auto_ptr<Node> c(clone());
std::unique_ptr<Node> Node::clone(Node* p) const throw() {
std::unique_ptr<Node> c(clone());
c->_parent = p;
return c;
}
@ -620,8 +620,8 @@ namespace xml {
Node::size_type min, Node::size_type max) throw():
Node(name, min, max), _text(text) {
}
std::auto_ptr<Node> String::clone() const throw() {
return std::auto_ptr<Node>(new String(*this));
std::unique_ptr<Node> String::clone() const throw() {
return std::unique_ptr<Node>(new String(*this));
}
std::string String::text() const throw() {
return _text;
@ -740,8 +740,8 @@ namespace xml {
size_type min, size_type max) throw():
String(name, mrw::string(i), min, max) {
}
std::auto_ptr<Node> UnsignedInteger::clone() const throw() {
return std::auto_ptr<Node>(new UnsignedInteger(*this));
std::unique_ptr<Node> UnsignedInteger::clone() const throw() {
return std::unique_ptr<Node>(new UnsignedInteger(*this));
}
//! An xml::UnsignedInteger must only contain an number.
/*! En exception is thrown, if the contents does not match a number. */
@ -876,7 +876,7 @@ namespace xml {
return os;
}
//! 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,
second_slash_in_tag,
character_after_slash, missing_end_tag, attribute_value_not_quoted,
@ -888,7 +888,7 @@ namespace xml {
try {
_line=1;
_open=0;
std::auto_ptr<Node> node(read(is, _template));
std::unique_ptr<Node> node(read(is, _template));
if (node->children()==0)
throw tag_expected(_template[0],
"nothing found, possibly empty stream");
@ -919,14 +919,14 @@ namespace xml {
last = c;
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,
second_slash_in_tag,
character_after_slash, missing_end_tag, attribute_value_not_quoted,
access_error, duplicate_attribute, attributes_in_end_tag,
illegal_attribute, mandatory_attribute_missing,
wrong_node_number) {
std::auto_ptr<Node> result(node.clone());
std::unique_ptr<Node> result(node.clone());
result->clear();
while (true) {
Tag res(tag(is, node));
@ -939,9 +939,9 @@ namespace xml {
if (res.name!=node.name()) throw wrong_end_tag(node, is, res);
} return result;
case EMPTY: {
std::auto_ptr<Node> current(node[res.name].clone());
std::unique_ptr<Node> current(node[res.name].clone());
current->clear()<<res.attributes;
*result<<*checkChildren(node[res.name], current, is);
*result<<*checkChildren(node[res.name], std::move(current), is);
} break;
case START: {
++_open;
@ -952,8 +952,8 @@ namespace xml {
}
}
}
std::auto_ptr<Node> Factory::checkChildren(const xml::Node& tpl,
std::auto_ptr<Node> node,
std::unique_ptr<Node> Factory::checkChildren(const xml::Node& tpl,
std::unique_ptr<Node> node,
std::istream& is) const
throw(wrong_node_number) {
for (Node::size_type i(0); i<tpl.children(); ++i)
@ -1166,7 +1166,7 @@ namespace xml {
checkInit();
xml::Factory factory(_xmlFactory);
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
it(_xmlNames.begin());
it!=_xmlNames.end(); ++it)

@ -22,7 +22,7 @@ class NodeTest: public CppUnit::TestFixture {
}
void clone() {
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());
}
void shift() {
@ -100,7 +100,7 @@ class StringTest: public CppUnit::TestFixture {
}
void clone() {
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());
}
void shift() {
@ -181,7 +181,7 @@ class ComplexTest: public CppUnit::TestFixture {
<<"<child a=\"b\"/>"
<<"< otherchild >< / otherchild >< otherchild / >"<<std::endl
<<"</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)3, (*node)[0].list("childofchild").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("h2")
<<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;
read->out(ss2);
CPPUNIT_ASSERT_EQUAL(contents, ss2.str());

Loading…
Cancel
Save