diff options
Diffstat (limited to 'raul/Atom.hpp')
-rw-r--r-- | raul/Atom.hpp | 86 |
1 files changed, 82 insertions, 4 deletions
diff --git a/raul/Atom.hpp b/raul/Atom.hpp index 443c0ae..c030bc5 100644 --- a/raul/Atom.hpp +++ b/raul/Atom.hpp @@ -22,6 +22,16 @@ #include <cassert> #include <cstring> #include <string> +#include <sstream> +#include <iostream> + +#include CONFIG_H_PATH +#ifdef HAVE_REDLANDMM +#include <redlandmm/Node.hpp> +#include <redlandmm/World.hpp> +#endif + +#define CUC(x) ((const unsigned char*)(x)) namespace Raul { @@ -33,8 +43,6 @@ namespace Raul { class Atom { public: - //TODO: Add a bool type here that serialises nicely to Turtle "true" and "false" - enum Type { NIL, INT, @@ -50,10 +58,32 @@ public: Atom(bool val) : _type(BOOL), _bool_val(val) {} Atom(const char* val) : _type(STRING), _string_val(strdup(val)) {} Atom(const std::string& val) : _type(STRING), _string_val(strdup(val.c_str())) {} - + Atom(void* val) : _type(BLOB), _blob_size(sizeof(val)), _blob_val(malloc(_blob_size)) { memcpy(_blob_val, val, sizeof(_blob_size)); } +#ifdef HAVE_REDLANDMM + Atom(const Redland::Node& node) + { + if (node.type() == Redland::Node::RESOURCE) { + _type = STRING; + _string_val = strdup(node.to_string().c_str()); + } else if (node.is_float()) { + _type = FLOAT; + _float_val = node.to_float(); + } else if (node.is_int()) { + _type = INT; + _int_val = node.to_int(); + } else if (node.is_bool()) { + _type = BOOL; + _bool_val = node.to_bool(); + } else { + _type = STRING; + _string_val = strdup(node.to_string().c_str()); + } + } +#endif + ~Atom() { if (_type == STRING) @@ -116,12 +146,60 @@ public: inline int32_t get_int32() const { assert(_type == INT); return _int_val; } inline float get_float() const { assert(_type == FLOAT); return _float_val; } - inline bool get_bool() const { assert(_type == BOOL); return _bool_val; } + inline bool get_bool() const { assert(_type == BOOL); return _bool_val; } inline const char* get_string() const { assert(_type == STRING); return _string_val; } inline const void* get_blob() const { assert(_type == BLOB); return _blob_val; } inline operator bool() const { return (_type != NIL); } + +#ifdef HAVE_REDLANDMM + Redland::Node to_rdf_node(Redland::World& world) const + { + std::ostringstream os; + std::string str; + librdf_uri* type = NULL; + librdf_node* node = NULL; + + switch (_type) { + case Atom::INT: + os << get_int32(); + str = os.str(); + // xsd:integer -> pretty integer literals in Turtle + type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#integer")); + break; + case Atom::FLOAT: + os.precision(20); + os << get_float(); + str = os.str(); + // xsd:decimal -> pretty decimal (float) literals in Turtle + type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#decimal")); + break; + case Atom::BOOL: + // xsd:boolean -> pretty boolean literals in Turtle + if (get_bool()) + str = "true"; + else + str = "false"; + type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#boolean")); + break; + case Atom::STRING: + str = get_string(); + break; + case Atom::BLOB: + case Atom::NIL: + default: + std::cerr << "WARNING: Unserializable Atom!" << std::endl; + } + + if (str != "") + node = librdf_new_node_from_typed_literal(world.world(), CUC(str.c_str()), NULL, type); + + return Redland::Node(world, node); + } +#endif + + private: Type _type; |