Beast - Music Synthesizer and Composer  0.11.1+10.g2da35
bsecxxutils.hh
Go to the documentation of this file.
1  // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
2 #ifndef __BSE_CXX_UTILS_H__
3 #define __BSE_CXX_UTILS_H__
4 #include <bse/bseutils.hh>
5 #include <sfi/sficxx.hh>
6 #include <vector>
7 #include <algorithm>
8 namespace Bse {
9 
11 namespace Procedure {
12 typedef SfiBool Bool;
13 typedef SfiInt Int;
14 typedef SfiNum Num;
15 typedef SfiTime Time;
16 typedef SfiNote Note;
17 typedef SfiReal Real;
18 typedef SfiChoice Choice;
19 typedef std::string String;
20 typedef SfiBBlock BBlock;
21 typedef SfiFBlock FBlock;
22 typedef SfiSeq Seq;
23 typedef SfiRec Rec;
24 typedef SfiProxy Proxy;
25 };
26 
27 /* --- type alias frequently used standard lib things --- */
28 typedef std::string String;
29 
30 
31 /* --- generally useful templates --- */
32 template<class Data> static void
33 delete_this (Data *d)
34 {
35  delete d;
36 }
37 /* check derivation of Derived from Base */
38 template<class Derived, class Base> // ex: EnforceDerivedFrom<Child, Base> assertion;
40  EnforceDerivedFrom (Derived *derived = 0,
41  Base *base = 0)
42  {
43  base = derived;
44  }
45 };
46 /* check derivation of Derived* from Base* */
47 template<class Derived, class Base> // ex: EnforceDerivedFrom<Child*, Base*> assertion;
48 struct EnforceDerivedFrom<Derived*, Base*> {
49  EnforceDerivedFrom (Derived *derived = 0,
50  Base *base = 0)
51  {
52  base = derived;
53  }
54 };
55 /* check derivation through EnforceDerivedFrom<>; */
56 template<class Derived, class Base> void
57 assert_derived_from (void)
58 {
60 }
61 
62 
63 /* --- exceptions --- */
65  explicit Exception (const char *_where) : loc (_where) {};
66  virtual const char* where() { return loc; }
67 private:
68  const char *loc;
69 };
71  const char *item;
72  InvalidArgument2 (const char *where, const char *item) : Exception (where), item (item) {};
73  const char* what() const noexcept { return g_intern_strconcat ("invalid argument: ", item, NULL); }
74 };
75 #define InvalidArgument(WHAT) InvalidArgument2 (__func__, #WHAT)
77  WrongTypeGValue (const char *where) : Exception (where) {};
78  const char* what() const noexcept { return "GValue contains wrong type for this kind of use"; }
79 };
80 struct DontReach : Exception {
81  DontReach (const char *where) : Exception (where) {};
82  const char* what() const noexcept { return "Code section should not be reached"; }
83 };
85  InvalidConnection (const char *where) : Exception (where) {};
86  const char* what() const noexcept { return "Function to be connected has invalid signature"; }
87 };
88 
89 /* --- records & sequences --- */
90 class Record {
91  Record& operator= (const Record&);
92  explicit Record (const Record&);
93 public:
94  explicit Record ();
95  virtual SfiRec* to_rec ();
96  virtual ~Record ();
97 };
98 
99 
100 /* --- class registration --- */
101 #define BSE_CXX_TYPE_REGISTER(ObjectType, parent, class_info) \
102  BSE_CXX_TYPE_REGISTER_INITIALIZED (ObjectType, parent, class_info, NULL, TypeRegistry::NONE)
103 #define BSE_CXX_TYPE_REGISTER_ABSTRACT(ObjectType, parent, class_info) \
104  BSE_CXX_TYPE_REGISTER_INTERN (ObjectType, parent, class_info, NULL, NULL, TypeRegistry::ABSTRACT)
105 
106 /* --- class information --- */
107 struct ClassInfo
108 {
109  const char *category;
110  const char *blurb;
111  const char *file;
112  int line;
113  ClassInfo (const char *category,
114  const char *blurb,
115  const char *file,
116  int line)
117  {
118  this->category = category;
119  this->blurb = blurb;
120  this->file = file;
121  this->line = line;
122  }
123 };
124 
125 
126 /* --- type registration internals --- */
127 struct CxxBaseClass;
129 {
130  GType gtype_id;
131 public:
132  enum Flags {
133  NONE = 0,
134  ABSTRACT = G_TYPE_FLAG_ABSTRACT
135  };
136  TypeRegistry (guint instance_size,
137  const gchar *name,
138  const gchar *parent,
139  const ClassInfo *cinfo,
140  GBaseInitFunc binit,
141  void (*class_init) (CxxBaseClass*),
142  GInstanceInitFunc iinit,
143  Flags flags);
144  const GType
145  get_type () const
146  {
147  return gtype_id;
148  }
149  static void
150  init_types ();
151  struct TypeEntry;
152 };
153 
154 template<class C> const GType
155 bse_type_id_wrapper (const char *type_name)
156 {
157  static GType type = 0;
158  if (!type)
159  {
160  type = g_type_from_name (type_name);
161  RAPICORN_ASSERT (type);
162  }
163  return type;
164 }
165 
166 #define BSE_CXX_TYPE_GET_REGISTERED(NameSpace, ObjectType) \
167  (::Bse::bse_type_id_wrapper<ObjectType> (#NameSpace #ObjectType))
168 #define BSE_CXX_TYPE_REGISTER_INITIALIZED(ObjectType, parent, cinfo, binit, flags) \
169  BSE_CXX_TYPE_REGISTER_INTERN (ObjectType, parent, cinfo, binit, \
170  ::Bse::cxx_instance_init_trampoline<ObjectType>, flags)
171 #define BSE_CXX_TYPE_REGISTER_INTERN(ObjectType, parent, cinfo, binit, iinit, flags) \
172  static Bse::TypeRegistry \
173  ObjectType ## _type_keeper (sizeof (ObjectType), "Bse" #ObjectType, parent, \
174  cinfo, binit, \
175  ::Bse::cxx_class_init_trampoline<ObjectType>, \
176  iinit, flags);
177 #define BSE_CXX_UTILS_ALIGNMENT (2 * sizeof (gsize))
178 #define BSE_CXX_UTILS_ALIGN(offset) ((offset + BSE_CXX_UTILS_ALIGNMENT - 1) & -BSE_CXX_UTILS_ALIGNMENT)
179 #define BSE_CXX_SIZEOF(Class) BSE_CXX_UTILS_ALIGN (sizeof (Class))
180 #define BSE_CXX_COMMON_CLASS_SIZE sizeof (CxxBaseClass)
181 
182 } // Bse
183 
184 #endif /* __BSE_CXX_UTILS_H__ */
Definition: bsecxxutils.hh:70
The Bse namespace contains all functions of the synthesis engine.
Definition: bstbseutils.cc:91
Definition: bsecxxutils.hh:128
Definition: bsecxxutils.hh:90
STL class.
Definition: bsecxxutils.hh:80
STL class.
Definition: bsecxxutils.hh:84
Definition: bsecxxutils.hh:76
Definition: bsecxxutils.hh:107
#define RAPICORN_ASSERT(cond)
Definition: bsecxxbase.hh:14