diff options
Diffstat (limited to 'raul')
-rw-r--r-- | raul/Atom.hpp | 54 | ||||
-rw-r--r-- | raul/AtomLiblo.hpp | 14 | ||||
-rw-r--r-- | raul/AtomRDF.hpp | 18 | ||||
-rw-r--r-- | raul/Configuration.hpp | 19 |
4 files changed, 62 insertions, 43 deletions
diff --git a/raul/Atom.hpp b/raul/Atom.hpp index 4c1ab2e..b464a87 100644 --- a/raul/Atom.hpp +++ b/raul/Atom.hpp @@ -31,6 +31,7 @@ namespace Raul { class URI; +class Forge; /** A piece of data with some type. * @@ -42,6 +43,8 @@ class URI; */ class Atom { public: + Atom() : _type(NIL), _blob_val(0) {} + enum Type { NIL, INT, @@ -53,25 +56,6 @@ public: DICT }; - Atom() : _type(NIL), _blob_val(0) {} - Atom(int32_t val) : _type(INT), _int_val(val) {} - Atom(float val) : _type(FLOAT), _float_val(val) {} - Atom(bool val) : _type(BOOL), _bool_val(val) {} - Atom(const char* val) : _type(STRING), _string_val(g_strdup(val)) {} - - Atom(const std::string& val) : _type(STRING), _string_val(g_strdup(val.c_str())) {} - - /** URI constructor (@a t must be URI) */ - Atom(Type t, const std::string& val) : _type(t), _string_val(g_intern_string(val.c_str())) { - assert(t == URI); - } - - Atom(const char* type_uri, size_t size, void* val) - : _type(BLOB), _blob_val(new BlobValue(type_uri, size, val)) {} - - typedef std::map<Raul::Atom, Raul::Atom> DictValue; - Atom(const DictValue& dict) : _type(DICT), _dict_val(new DictValue(dict)) {} - ~Atom() { dealloc(); } Atom(const Atom& copy) @@ -173,9 +157,29 @@ public: inline const char* get_blob_type() const { assert(_type == BLOB); return _blob_val->type(); } inline const void* get_blob() const { assert(_type == BLOB); return _blob_val->data(); } + typedef std::map<Raul::Atom, Raul::Atom> DictValue; + inline const DictValue& get_dict() const { assert(_type == DICT); return *_dict_val; } private: + friend class Forge; + Atom(int32_t val) : _type(INT), _int_val(val) {} + Atom(float val) : _type(FLOAT), _float_val(val) {} + Atom(bool val) : _type(BOOL), _bool_val(val) {} + Atom(const char* val) : _type(STRING), _string_val(g_strdup(val)) {} + + Atom(const std::string& val) : _type(STRING), _string_val(g_strdup(val.c_str())) {} + + /** URI constructor (@a t must be URI) */ + Atom(Type t, const std::string& val) : _type(t), _string_val(g_intern_string(val.c_str())) { + assert(t == URI); + } + + Atom(const char* type_uri, size_t size, void* val) + : _type(BLOB), _blob_val(new BlobValue(type_uri, size, val)) {} + + Atom(const DictValue& dict) : _type(DICT), _dict_val(new DictValue(dict)) {} + Type _type; friend class Raul::URI; @@ -237,6 +241,18 @@ private: }; }; +class Forge { +public: + Atom make() { return Atom(); } + Atom make(int32_t v) { return Atom(v); } + Atom make(float v) { return Atom(v); } + Atom make(bool v) { return Atom(v); } + Atom make(const char* v) { return Atom(v); } + Atom alloc(const std::string& v) { return Atom(v); } + Atom alloc(const Atom::DictValue& v) { return Atom(v); } + Atom alloc(Atom::Type t, const std::string& v) { return Atom(t, v); } +}; + } // namespace Raul static inline std::ostream& operator<<(std::ostream& os, const Raul::Atom& atom) diff --git a/raul/AtomLiblo.hpp b/raul/AtomLiblo.hpp index 5e2442e..f0e07cf 100644 --- a/raul/AtomLiblo.hpp +++ b/raul/AtomLiblo.hpp @@ -71,21 +71,21 @@ lo_message_add_atom(lo_message m, const Atom& atom) /** Convert a liblo argument to a Raul::Atom */ inline Atom -lo_arg_to_atom(char type, lo_arg* arg) +lo_arg_to_atom(Raul::Forge& forge, char type, lo_arg* arg) { switch (type) { case 'i': - return Atom(arg->i); + return forge.make(arg->i); case 'f': - return Atom(arg->f); + return forge.make(arg->f); case 's': - return Atom(&arg->s); + return forge.make(&arg->s); case 'S': - return Atom(Atom::URI, &arg->S); + return forge.make(Atom::URI, &arg->S); case 'T': - return Atom((bool)true); + return forge.make((bool)true); case 'F': - return Atom((bool)false); + return forge.make((bool)false); default: warn << "Unable to convert OSC type '" << type << "' to Atom" << std::endl; diff --git a/raul/AtomRDF.hpp b/raul/AtomRDF.hpp index 71b9c79..00a9332 100644 --- a/raul/AtomRDF.hpp +++ b/raul/AtomRDF.hpp @@ -40,28 +40,28 @@ namespace AtomRDF { /** Convert a Sord::Node to a Raul::Atom */ inline Atom -node_to_atom(Sord::Model& model, const Sord::Node& node) +node_to_atom(Forge& forge, Sord::Model& model, const Sord::Node& node) { if (node.is_bool()) { - return Atom(bool(node.to_bool())); + return forge.make(bool(node.to_bool())); } else if (node.is_uri()) { - return Atom(Atom::URI, node.to_c_string()); + return forge.alloc(Atom::URI, node.to_c_string()); } else if (node.is_float()) { - return Atom(node.to_float()); + return forge.make(node.to_float()); } else if (node.is_int()) { - return Atom(node.to_int()); + return forge.make(node.to_int()); } else if (node.is_blank()) { Atom::DictValue dict; Sord::Node nil; for (Sord::Iter i = model.find(node, nil, nil); !i.end(); ++i) { Sord::Node predicate = i.get_predicate(); Sord::Node object = i.get_object(); - dict.insert(std::make_pair(node_to_atom(model, predicate), - node_to_atom(model, object))); + dict.insert(std::make_pair(node_to_atom(forge, model, predicate), + node_to_atom(forge, model, object))); } - return Atom(dict); + return forge.alloc(dict); } else { - return Atom(node.to_c_string()); + return forge.alloc(node.to_c_string()); } } diff --git a/raul/Configuration.hpp b/raul/Configuration.hpp index 655696a..9efca42 100644 --- a/raul/Configuration.hpp +++ b/raul/Configuration.hpp @@ -34,18 +34,20 @@ namespace Raul { */ class Configuration { public: - Configuration(const std::string& shortdesc, const std::string& desc) - : _shortdesc(shortdesc) + Configuration(Forge* forge, + const std::string& shortdesc, + const std::string& desc) + : _forge(forge) + , _shortdesc(shortdesc) , _desc(desc) , _max_name_length(0) {} - Configuration& add( - const std::string& name, - char letter, - const std::string& desc, - const Atom::Type type, - const Atom& value); + Configuration& add(const std::string& name, + char letter, + const std::string& desc, + const Atom::Type type, + const Atom& value); void print_usage(const std::string& program, std::ostream& os); @@ -92,6 +94,7 @@ private: int set_value_from_string(Configuration::Option& option, const std::string& value) throw (Configuration::CommandLineError); + Forge* _forge; const std::string _shortdesc; const std::string _desc; Options _options; |