BEAST/BSE - Better Audio System and Sound Engine  0.8.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
bsecxxutils.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_UTILS_H__
00003 #define __BSE_CXX_UTILS_H__
00004 #include <bse/bseutils.hh>
00005 #include <sfi/sficxx.hh>
00006 #include <vector>
00007 #include <algorithm>
00008 namespace Bse {
00009 
00011 namespace Procedure {
00012 typedef SfiBool     Bool;
00013 typedef SfiInt      Int;
00014 typedef SfiNum      Num;
00015 typedef SfiTime     Time;
00016 typedef SfiNote     Note;
00017 typedef SfiReal     Real;
00018 typedef SfiChoice   Choice;
00019 typedef std::string String;  /* not using SfiString resp. gchar* here */
00020 typedef SfiBBlock   BBlock;
00021 typedef SfiFBlock   FBlock;
00022 typedef SfiSeq      Seq;
00023 typedef SfiRec      Rec;
00024 typedef SfiProxy    Proxy;
00025 };
00026 
00027 /* --- type alias frequently used standard lib things --- */
00028 typedef std::string String;
00029 
00030 
00031 /* --- generally useful templates --- */
00032 template<class Data> static void
00033 delete_this (Data *d)
00034 {
00035   delete d;
00036 }
00037 /* check derivation of Derived from Base */
00038 template<class Derived, class Base>     // ex: EnforceDerivedFrom<Child, Base> assertion;
00039 struct EnforceDerivedFrom {
00040   EnforceDerivedFrom (Derived *derived = 0,
00041                       Base    *base = 0)
00042   {
00043     base = derived;
00044   }
00045 };
00046 /* check derivation of Derived* from Base* */
00047 template<class Derived, class Base>     // ex: EnforceDerivedFrom<Child*, Base*> assertion;
00048 struct EnforceDerivedFrom<Derived*, Base*> {
00049   EnforceDerivedFrom (Derived *derived = 0,
00050                       Base    *base = 0)
00051   {
00052     base = derived;
00053   }
00054 };
00055 /* check derivation through EnforceDerivedFrom<>; */
00056 template<class Derived, class Base> void
00057 assert_derived_from (void)
00058 {
00059   EnforceDerivedFrom<Derived, Base> assertion;
00060 }
00061 
00062 
00063 /* --- exceptions --- */
00064 struct Exception : std::exception {
00065   explicit Exception (const char *_where) : loc (_where) {};
00066   virtual const char* where() { return loc; }
00067 private:
00068   const char *loc;
00069 };
00070 struct InvalidArgument2 : Exception {
00071   const char *item;
00072   InvalidArgument2 (const char *where, const char *item) : Exception (where), item (item) {};
00073   const char* what() const throw() { return g_intern_strconcat ("invalid argument: ", item, NULL); }
00074 };
00075 #define InvalidArgument(WHAT)   InvalidArgument2 (G_STRFUNC, #WHAT)
00076 struct WrongTypeGValue : Exception {
00077   WrongTypeGValue (const char *where) : Exception (where) {};
00078   const char* what() const throw() { return "GValue contains wrong type for this kind of use"; }
00079 };
00080 struct DontReach : Exception {
00081   DontReach (const char *where) : Exception (where) {};
00082   const char* what() const throw() { return "Code section should not be reached"; }
00083 };
00084 struct InvalidConnection : Exception {
00085   InvalidConnection (const char *where) : Exception (where) {};
00086   const char* what() const throw() { return "Function to be connected has invalid signature"; }
00087 };
00088 
00089 /* --- records & sequences --- */
00090 class Record {
00091   Record&          operator= (const Record&);
00092   explicit         Record    (const Record&);
00093 public:
00094   explicit         Record    ();
00095   virtual SfiRec*  to_rec    ();
00096   virtual         ~Record    ();
00097 };
00098 
00099 
00100 /* --- class registration --- */
00101 #define BSE_CXX_TYPE_REGISTER(ObjectType, parent, class_info)          \
00102           BSE_CXX_TYPE_REGISTER_INITIALIZED (ObjectType, parent, class_info, NULL, TypeRegistry::NONE)
00103 #define BSE_CXX_TYPE_REGISTER_ABSTRACT(ObjectType, parent, class_info) \
00104           BSE_CXX_TYPE_REGISTER_INTERN (ObjectType, parent, class_info, NULL, NULL, TypeRegistry::ABSTRACT)
00105 
00106 /* --- class information --- */
00107 struct ClassInfo
00108 {
00109   const char *category;
00110   const char *blurb;
00111   const char *file;
00112   int         line;
00113   ClassInfo (const char *category,
00114              const char *blurb,
00115              const char *file,
00116              int         line)
00117   {
00118     this->category = category;
00119     this->blurb = blurb;
00120     this->file = file;
00121     this->line = line;
00122   }
00123 };
00124 
00125 
00126 /* --- type registration internals --- */
00127 struct CxxBaseClass;
00128 class TypeRegistry
00129 {
00130   GType gtype_id;
00131 public:
00132   enum Flags {
00133     NONE        = 0,
00134     ABSTRACT    = G_TYPE_FLAG_ABSTRACT
00135   };
00136   TypeRegistry (guint             instance_size,
00137                 const gchar      *name,
00138                 const gchar      *parent,
00139                 const ClassInfo  *cinfo,
00140                 GBaseInitFunc     binit,
00141                 void            (*class_init) (CxxBaseClass*),
00142                 GInstanceInitFunc iinit,
00143                 Flags             flags);
00144   const GType
00145   get_type () const
00146   {
00147     return gtype_id;
00148   }
00149   static void
00150   init_types ();
00151   struct TypeEntry;
00152 };
00153 
00154 template<class C> const GType
00155 bse_type_id_wrapper (const char *type_name)
00156 {
00157   static GType type = 0;
00158   if (!type)
00159     {
00160       type = g_type_from_name (type_name);
00161       g_assert (type);
00162     }
00163   return type;
00164 }
00165 
00166 #define BSE_CXX_TYPE_GET_REGISTERED(NameSpace, ObjectType) \
00167   (::Bse::bse_type_id_wrapper<ObjectType> (#NameSpace #ObjectType))
00168 #define BSE_CXX_TYPE_REGISTER_INITIALIZED(ObjectType, parent, cinfo, binit, flags) \
00169   BSE_CXX_TYPE_REGISTER_INTERN (ObjectType, parent, cinfo, binit,                  \
00170                                 ::Bse::cxx_instance_init_trampoline<ObjectType>, flags)
00171 #define BSE_CXX_TYPE_REGISTER_INTERN(ObjectType, parent, cinfo, binit, iinit, flags) \
00172   static Bse::TypeRegistry                                                      \
00173     ObjectType ## _type_keeper (sizeof (ObjectType), "Bse" #ObjectType, parent, \
00174                                 cinfo, binit,                                   \
00175                                 ::Bse::cxx_class_init_trampoline<ObjectType>,   \
00176                                 iinit, flags);
00177 #define BSE_CXX_UTILS_ALIGNMENT         (2 * sizeof (gsize))
00178 #define BSE_CXX_UTILS_ALIGN(offset)     ((offset + BSE_CXX_UTILS_ALIGNMENT - 1) & -BSE_CXX_UTILS_ALIGNMENT)
00179 #define BSE_CXX_SIZEOF(Class)           BSE_CXX_UTILS_ALIGN (sizeof (Class))
00180 #define BSE_CXX_COMMON_CLASS_SIZE       sizeof (CxxBaseClass)
00181 
00182 } // Bse
00183 
00184 #endif /* __BSE_CXX_UTILS_H__ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines