RAUL 0.7.0

AtomLiblo.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
00003  *
00004  * Raul is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  *
00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  *
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00016  */
00017 
00018 #ifndef RAUL_ATOM_LIBLO_HPP
00019 #define RAUL_ATOM_LIBLO_HPP
00020 
00021 #include <iostream>
00022 #include <lo/lo.h>
00023 #include "raul/log.hpp"
00024 #include "raul/Atom.hpp"
00025 
00026 namespace Raul {
00027 
00032 namespace AtomLiblo {
00033 
00035 inline void
00036 lo_message_add_atom(lo_message m, const Atom& atom)
00037 {
00038     switch (atom.type()) {
00039     case Atom::INT:
00040         lo_message_add_int32(m, atom.get_int32());
00041         break;
00042     case Atom::FLOAT:
00043         lo_message_add_float(m, atom.get_float());
00044         break;
00045     case Atom::STRING:
00046         lo_message_add_string(m, atom.get_string());
00047         break;
00048     case Atom::URI:
00049         lo_message_add_symbol(m, atom.get_uri());
00050         break;
00051     case Atom::BOOL:
00052         if (atom.get_bool())
00053             lo_message_add_true(m);
00054         else
00055             lo_message_add_false(m);
00056         break;
00057     case Atom::BLOB:
00058         if (atom.data_size() > 0)
00059             lo_message_add_blob(m, lo_blob_new(atom.data_size(), atom.get_blob()));
00060         else
00061             lo_message_add_nil(m);
00062         break;
00063     case Atom::NIL:
00064     default:
00065         lo_message_add_nil(m);
00066         break;
00067     }
00068 }
00069 
00070 
00072 inline Atom
00073 lo_arg_to_atom(char type, lo_arg* arg)
00074 {
00075     switch (type) {
00076     case 'i':
00077         return Atom(arg->i);
00078     case 'f':
00079         return Atom(arg->f);
00080     case 's':
00081         return Atom(&arg->s);
00082     case 'S':
00083         return Atom(Atom::URI, &arg->S);
00084     case 'T':
00085         return Atom((bool)true);
00086     case 'F':
00087         return Atom((bool)false);
00088     default:
00089         warn << "Unable to convert OSC type '"
00090             << type << "' to Atom" << std::endl;
00091         return Atom();
00092     }
00093 }
00094 
00095 
00096 } // namespace AtomLiblo
00097 } // namespace Raul
00098 
00099 #endif // RAUL_ATOM_LIBLO_HPP