Beast - Music Synthesizer and Composer  0.11.1+10.g2da35
bsecxxbase.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_BASE_H__
3 #define __BSE_CXX_BASE_H__
4 
5 #include <bse/bsesource.hh>
6 #include <bse/bsecxxvalue.hh>
7 #include <bse/bsecxxclosure.hh>
8 
9 namespace Bse {
10 #define BSE_CXX_INSTANCE_OFFSET BSE_CXX_SIZEOF (BseSource)
11 
12 #define BSE_TYPE_CXX_BASE (BSE_CXX_TYPE_GET_REGISTERED (Bse, CxxBase))
13 
14 class CxxBaseClass : public BseSourceClass {
15 public:
16  void add_param (const char *group,
17  guint prop_id,
18  GParamSpec *pspec);
19  void add_param (guint prop_id,
20  GParamSpec *grouped_pspec);
21  void set_accessors (void (*get_property) (GObject*, guint, GValue*, GParamSpec*),
22  void (*set_property) (GObject*, guint, const GValue*, GParamSpec*) = NULL,
23  gboolean (*editable_property) (BseObject*, guint, GParamSpec*) = NULL,
24  void (*get_candidates) (BseItem*, guint, BsePropertyCandidates*, GParamSpec*) = NULL,
25  void (*property_updated) (BseSource*, guint, guint64, double, GParamSpec*) = NULL);
26  guint add_signal (const gchar *signal_name,
27  GSignalFlags flags,
28  guint n_params,
29  ...);
30  void add_ochannel (const char *ident,
31  const char *label,
32  const char *blurb,
33  int assert_id = -1);
34  void add_ichannel (const char *ident,
35  const char *label,
36  const char *blurb,
37  int assert_id = -1);
38  void add_jchannel (const char *ident,
39  const char *label,
40  const char *blurb,
41  int assert_id = -1);
42 };
43 class CxxBase {
44  void* cast_to_gobject ();
45  static CxxBase* cast_from_gobject (void *o);
46 public:
47  static CxxBase* base_from_gobject (GObject *o) { return cast_from_gobject (o); }
48 protected:
49  GObject* gobject () const;
50  BseItem* item ();
51 public:
52  /*Con*/ CxxBase ();
53  CxxBase* ref ();
54  void unref ();
55  void freeze_notify ();
56  void notify (const gchar *property);
57  void thaw_notify ();
58  void set (const gchar *first_property_name,
59  ...) G_GNUC_NULL_TERMINATED;
60  void get (const gchar *first_property_name,
61  ...) G_GNUC_NULL_TERMINATED;
62  void set_property (guint prop_id,
63  const Value &value,
64  GParamSpec *pspec);
65  void get_property (guint prop_id,
66  Value &value,
67  GParamSpec *pspec);
68 #if 0
69  gulong connect (const gchar *signal,
70  GClosure *closure,
71  bool after);
72  gulong connect (const gchar *signal,
73  GClosure *closure) { return connect (signal, closure, false); }
74 #endif
75  gulong connect (const gchar *signal,
76  CxxClosure *closure,
77  bool after);
78  gulong connect (const gchar *signal,
79  CxxClosure *closure) { return connect (signal, closure, false); }
80  const String tokenize_signal (const gchar *signal);
81  GType type ();
82  virtual void compat_setup (guint vmajor,
83  guint vminor,
84  guint vmicro);
85  virtual void restore_finished (guint vmajor,
86  guint vminor,
87  guint vmicro);
88  virtual ~CxxBase ();
89 
90  static void class_init (CxxBaseClass *klass);
91 
92  static inline bool instance_is_a (CxxBase *cbase,
93  GType iface_type)
94  {
95  if (cbase)
96  {
97  GObject *gobject = cbase->gobject();
98  return G_TYPE_CHECK_INSTANCE_TYPE (gobject, iface_type);
99  }
100  else
101  return FALSE;
102  }
103 
104  template<class OType> static inline OType*
105  value_get_gobject (const GValue *v)
106  {
107  gpointer p;
108  if (SFI_VALUE_HOLDS_PROXY (v))
109  p = bse_object_from_id (sfi_value_get_proxy (v));
110  else
111  p = g_value_get_object (v);
112  return (OType*) p;
113  }
114  template<class CxxType> static inline CxxType
115  value_get_object (const GValue *v)
116  {
117  assert_derived_from<CxxType, CxxBase*>();
118  GObject *p = value_get_gobject<GObject> (v);
119  CxxBase *b = CxxBase::base_from_gobject (p);
120  CxxType to = static_cast<CxxType> (b);
121  return to;
122  }
123  static inline void
124  value_set_gobject (GValue *value,
125  gpointer object)
126  {
127  if (SFI_VALUE_HOLDS_PROXY (value))
128  sfi_value_set_proxy (value, BSE_IS_OBJECT (object) ? ((BseObject*) object)->unique_id : 0);
129  else
130  g_value_set_object (value, object);
131  }
132  static inline void
133  value_set_object (GValue *value,
134  const CxxBase *self)
135  {
136  value_set_gobject (value, self->gobject());
137  }
138  template<class Accepted, class Casted>
139  static inline void
140  value_set_casted (GValue *value,
141  const Accepted *obj)
142  {
143  const Casted *self = static_cast<const Casted*> (obj);
144  value_set_object (value, self);
145  }
146 
147  class Pointer {
148  CxxBase *p;
149  public:
150  Pointer (CxxBase *t) { p = t; }
151  /* second part of to-GObject* casts: */
152  operator GObject* () { return (GObject*) p->cast_to_gobject (); }
153  operator BseObject* () { return (BseObject*) p->cast_to_gobject (); }
154  operator BseItem* () { return (BseItem*) p->cast_to_gobject (); }
155  operator BseSource* () { return (BseSource*) p->cast_to_gobject (); }
156  };
157  /* first part of to-GObject* casts: */
158  static inline CxxBase::Pointer cast (CxxBase *c) { return CxxBase::Pointer (c); }
159  /* from-GObject* casts: */
160  static CxxBase* cast (GObject *o) { return cast_from_gobject (o); }
161  static CxxBase* cast (BseSource *o) { return cast_from_gobject (o); }
162  static CxxBase* cast (BseItem *o) { return cast_from_gobject (o); }
163  static CxxBase* cast (BseObject *o) { return cast_from_gobject (o); }
164 };
165 /* first part of to-GObject* casts: */
166 static inline CxxBase::Pointer cast (CxxBase *c) { return CxxBase::Pointer (c); }
167 /* match from-GObject* casts: */
168 template<class T> CxxBase* cast (T *t) { return CxxBase::cast (t); }
169 
170 /* --- trampoline templates --- */
171 template<class ObjectType> static void
172 cxx_class_init_trampoline (CxxBaseClass *klass)
173 {
174  ObjectType::class_init (klass);
175 }
176 
177 template<class ObjectType> static void
178 cxx_instance_init_trampoline (GTypeInstance *instance,
179  gpointer g_class)
180 { /* invoke C++ constructor upon _init of destination type */
181  if (G_TYPE_FROM_INSTANCE (instance) == G_TYPE_FROM_CLASS (g_class))
182  new (BSE_CXX_INSTANCE_OFFSET + (char*) instance) ObjectType ();
183 }
184 
185 template<class ObjectType, typename PropertyID> static void
186 cxx_get_property_trampoline (GObject *o,
187  guint prop_id,
188  GValue *value,
189  GParamSpec *pspec)
190 {
191  CxxBase *cbase = cast (o);
192  Value *v = (Value*) value;
193  ObjectType *instance = static_cast<ObjectType*> (cbase);
194  if (0) // check ObjectType::get_property() member and prototype
195  (void) static_cast<void (ObjectType::*) (PropertyID, Value&, GParamSpec*)> (&ObjectType::get_property);
196  instance->get_property (static_cast<PropertyID> (prop_id), *v, pspec);
197 }
198 
199 template<class ObjectType, typename PropertyID> static void
200 cxx_set_property_trampoline (GObject *o,
201  guint prop_id,
202  const GValue *value,
203  GParamSpec *pspec)
204 {
205  CxxBase *cbase = cast (o);
206  const Value *v = (const Value*) value;
207  ObjectType *instance = static_cast<ObjectType*> (cbase);
208  if (0) // check ObjectType::set_property() member and prototype
209  (void) static_cast<void (ObjectType::*) (PropertyID, const Value&, GParamSpec*)> (&ObjectType::set_property);
210  instance->set_property (static_cast<PropertyID> (prop_id), *v, pspec);
211 }
212 
213 template<class ObjectType, typename PropertyID> static gboolean
214 cxx_editable_property_trampoline (BseObject *o,
215  guint prop_id,
216  GParamSpec *pspec)
217 {
218  CxxBase *cbase = cast (o);
219  ObjectType *instance = static_cast<ObjectType*> (cbase);
220  if (0) // check ObjectType::editable_property() member and prototype
221  (void) static_cast<bool (ObjectType::*) (PropertyID, GParamSpec*)> (&ObjectType::editable_property);
222  return instance->editable_property (static_cast<PropertyID> (prop_id), pspec);
223 }
224 
225 template<class ObjectType, typename PropertyID> static void
226 cxx_get_candidates_trampoline (BseItem *item,
227  guint prop_id,
228  BsePropertyCandidates *pc,
229  GParamSpec *pspec); /* defined in bsecxxplugin.hh */
230 
231 template<class ObjectType, typename PropertyID> static void
232 cxx_property_updated_trampoline (BseSource *source,
233  guint prop_id,
234  guint64 tick_stamp,
235  double prop_value,
236  GParamSpec *pspec)
237 {
238  CxxBase *cbase = cast (source);
239  ObjectType *instance = static_cast<ObjectType*> (cbase);
240  if (0) // check ObjectType::property_updated() member and prototype
241  (void) static_cast<void (ObjectType::*) (PropertyID, guint64, double, GParamSpec*)> (&ObjectType::property_updated);
242  instance->property_updated (static_cast<PropertyID> (prop_id), tick_stamp, prop_value, pspec);
243 }
244 
245 } // Bse
246 
247 
248 #endif /* __BSE_CXX_BASE_H__ */
The Bse namespace contains all functions of the synthesis engine.
Definition: bstbseutils.cc:91
Definition: bsecxxbase.hh:147
Definition: bsecxxvalue.hh:13
STL class.
Definition: bsecxxbase.hh:43
Definition: bseobject.hh:57
signal
Definition: bsesource.hh:82
Definition: bsesource.hh:90
connect
Definition: bsecxxclosure.hh:10
Definition: bseitem.hh:32
Definition: bsecxxbase.hh:14