pugixml/contrib/pugilua/pugilua_lib.cpp

100 lines
2.1 KiB
C++
Raw Normal View History

2012-10-02 00:42:05 +04:00
#include "pugilua_lib.h"
#include <pugixml.hpp>
#include <luabridge.h>
#include <RefCountedPtr.h>
#include <string>
namespace pugi {
namespace lua {
class lxml_parse_result {
public:
lxml_parse_result(pugi::xml_parse_result& r):res(r) { }
lxml_parse_result() { }
2012-10-02 08:49:35 +04:00
2012-10-02 00:42:05 +04:00
public:
std::string description() const {
return res.description();
}
2012-10-02 08:49:35 +04:00
bool valid() const {
return (bool)res;
}
2012-10-02 00:42:05 +04:00
private:
pugi::xml_parse_result res;
};
class lxml_node {
public:
lxml_node(pugi::xml_node& n):node(n){}
lxml_node() { }
2012-10-02 08:49:35 +04:00
public:
bool valid() const {
return (bool)node;
}
RefCountedPtr<lxml_node> child(char const* name) {
return RefCountedPtr<lxml_node>(new lxml_node(node.child(name)));
}
std::string name() const {
return node.name();
}
private:
pugi::xml_node node;
};
2012-10-02 00:42:05 +04:00
class lxml_document {
public:
RefCountedPtr<lxml_parse_result> load_file(char const* path) {
return RefCountedPtr<lxml_parse_result>(new lxml_parse_result(doc.load_file(path)));
}
// redundant, but defined due to composition up to now
RefCountedPtr<lxml_node> child(char const* name) {
return RefCountedPtr<lxml_node>(new lxml_node(doc.child(name)));
}
2012-10-02 08:49:35 +04:00
bool valid() const {
return (bool)doc;
}
2012-10-02 00:42:05 +04:00
private:
pugi::xml_document doc;
};
}
}
void register_pugilua (lua_State* L) {
using namespace pugi::lua;
luabridge::getGlobalNamespace(L)
.beginNamespace("pugi")
2012-10-02 08:49:35 +04:00
.beginClass<lxml_parse_result>("xml_parse_result")
.addConstructor<void (*)()>()
.addProperty("description",&lxml_parse_result::description)
.addProperty("valid",&lxml_parse_result::valid)
.endClass()
.beginClass<lxml_node>("xml_node")
.addConstructor<void (*)()>()
.addProperty("valid",&lxml_node::valid)
.addProperty("name",&lxml_node::name)
.addFunction("child",&lxml_node::child)
.endClass()
.beginClass<lxml_document>("xml_document")
.addConstructor<void (*)()>()
.addProperty("valid",&lxml_document::valid)
.addFunction("load_file",&lxml_document::load_file)
.addFunction("child",&lxml_document::child)
.endClass()
2012-10-02 00:42:05 +04:00
.endNamespace()
;
}