BEAST/BSE - Better Audio System and Sound Engine  0.8.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
bsecxxarg.hh
Go to the documentation of this file.
00001  // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
00002 #ifndef __BSE_CXX_ARG_H__
00003 #define __BSE_CXX_ARG_H__
00004 
00005 #include <bse/bsecxxvalue.hh>
00006 
00007 /* Closure Argument implementation. For a given type, these templates
00008  * provide a class Arg with get() and set() functions on class Value,
00009  * and a token() function which returns a single character string to
00010  * identify the argument type.
00011  */
00012 
00013 namespace Bse {
00014 
00015 /* default Arg type, this either supports a CxxBase* pointer or errors out */
00016 template<typename T>
00017 struct Arg {
00018   T            get   (const Value *v) { return (T) v->get_base(); }
00019   void         set   (Value *v, T  t) { v->set_base (t); }
00020   const String token ()               { void (*f) (T) = 0; return tokenize (f); }
00021 private:
00022   template<typename U> static const String
00023   ptokenize (CxxBase*)
00024   {     // CxxBase* is a supported pointer type
00025     return "X";
00026   }
00027   template<typename U> static const String
00028   ptokenize (void const *)
00029   {     // other pointer types are not supported
00030     return "?";
00031     static_assert (0 == sizeof (U*), "unsupported pointer type");
00032   }
00033   template<typename U> const String
00034   tokenize (void (*) (U*))
00035   {     // relay to pointer type tokenizer
00036     U *p = 0;
00037     return ptokenize<U> (p);
00038   }
00039   template<typename U> const String
00040   tokenize (void (*) (U))
00041   {     // non-pointer type, not supported
00042     return "?";
00043     static_assert (sizeof (U) != sizeof (U), "unsupported non-pointer type");
00044   }
00045 };
00046 const String tokenize_gtype (GType t);
00047 
00048 
00049 /* specialize Arg template for standard primitive types */
00050 #define BSE__SPECIALIZE(TYPE, vtype, tok, GCast, SCast) \
00051 template<> struct Arg<TYPE> {                           \
00052   TYPE         get   (const Value *v)                   \
00053   { return GCast (v->get_##vtype ()); }                 \
00054   void         set   (Value *v, TYPE t)                 \
00055   { v->set_##vtype (SCast (t)); }                       \
00056   const String token ()                                 \
00057   { return tok; }                                       \
00058 private:                                                \
00059   template<typename T> static inline T no_cast (T t)    \
00060   { return t; }                                         \
00061 }
00062 BSE__SPECIALIZE(bool,                 bool,   "b", no_cast, no_cast);
00063 // BSE__SPECIALIZE(char,              int,    "i", no_cast, no_cast);
00064 // BSE__SPECIALIZE(signed char,       int,    "i", no_cast, no_cast);
00065 // BSE__SPECIALIZE(unsigned char,     int,    "i", no_cast, no_cast);
00066 // BSE__SPECIALIZE(signed short,      int,    "i", no_cast, no_cast);
00067 // BSE__SPECIALIZE(unsigned short,    int,    "i", no_cast, no_cast);
00068 BSE__SPECIALIZE(signed int,           int,    "i", no_cast, no_cast);
00069 BSE__SPECIALIZE(uint,                 int,    "i", no_cast, no_cast);
00070 BSE__SPECIALIZE(signed long,          int,    "i", no_cast, no_cast);
00071 BSE__SPECIALIZE(unsigned long,        int,    "i", no_cast, no_cast);
00072 BSE__SPECIALIZE(signed long long,     num,    "n", no_cast, no_cast);
00073 BSE__SPECIALIZE(unsigned long long,   num,    "n", no_cast, no_cast);
00074 BSE__SPECIALIZE(float,                real,   "r", no_cast, no_cast);
00075 BSE__SPECIALIZE(double,               real,   "r", no_cast, no_cast);
00076 BSE__SPECIALIZE(gpointer,             pointer,"*", no_cast, no_cast);
00077 BSE__SPECIALIZE(GParamSpec*,          pspec,  "P", no_cast, no_cast);
00078 //BSE__SPECIALIZE(unsigned char*,       string, "s", no_cast, no_cast);
00079 //BSE__SPECIALIZE(signed char*,         string, "s", no_cast, no_cast);
00080 BSE__SPECIALIZE(String,               string, "s", no_cast, no_cast);
00081 //BSE__SPECIALIZE(const unsigned char*, string, "s", no_cast, no_cast);
00082 //BSE__SPECIALIZE(const signed char*,   string, "s", no_cast, no_cast);
00083 //BSE__SPECIALIZE(const char*,          string, "s", no_cast, no_cast);
00084 BSE__SPECIALIZE(const String,         string, "s", no_cast, no_cast);
00085 BSE__SPECIALIZE(char*,                string, "s", const_cast<char*>, no_cast);
00086 BSE__SPECIALIZE(GObject*,             object, "O", no_cast, no_cast);
00087 BSE__SPECIALIZE(BseObject*,           object, "O", reinterpret_cast<BseObject*>, reinterpret_cast<GObject*>);
00088 BSE__SPECIALIZE(BseItem*,             object, "O", reinterpret_cast<BseItem*>, reinterpret_cast<GObject*>);
00089 BSE__SPECIALIZE(BseSource*,           object, "O", reinterpret_cast<BseSource*>, reinterpret_cast<GObject*>);
00090 #undef BSE__SPECIALIZE
00091 
00092 } // Bse
00093 
00094 
00095 #endif /* __BSE_CXX_ARG_H__ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines