OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SchemePrimitive.h
Go to the documentation of this file.
1 /*
2  * SchemePrimitive.h
3  *
4  * Allow C++ code to be invoked from scheme --
5  * by creating a new scheme primitive function.
6  *
7  * Copyright (C) 2009,2015 Linas Vepstas
8  */
9 
10 #ifdef HAVE_GUILE
11 
12 #ifndef _OPENCOG_SCHEME_PRIMITIVE_H
13 #define _OPENCOG_SCHEME_PRIMITIVE_H
14 
15 #include <string>
16 
20 #include <libguile.h>
22 
23 namespace opencog {
29 {
30  friend class SchemeEval;
31  friend class SchemeSmob;
32  private:
33  static bool is_inited;
34  static void init(void);
35  static void init_in_module(void*);
36 
37  static void * c_wrap_register(void *);
38  void really_do_register(const char*, const char*, int);
39 
40  const char *tmp_module;
41  const char *tmp_name;
42  int tmp_nargs;
43 
44  static SCM do_call(SCM, SCM);
45  static PrimitiveEnviron *verify_pe(SCM, const char *);
46 
47  protected:
48  void do_register(const char*, const char*, int);
49  virtual SCM invoke (SCM) = 0;
50  virtual const char *get_module(void) = 0;
51  virtual const char *get_name(void) = 0;
52  virtual size_t get_size(void) = 0;
53  virtual ~PrimitiveEnviron();
54 };
55 
68 //
69 template<class T>
71 {
72  private:
73  union
74  {
75  // signature naming convention:
76  // b == bool
77  // d == double
78  // h == Handle
79  // i == int
80  // q == HandleSeq
81  // k == HandleSeqSeq
82  // s == string
83  // p == TruthValuePtr
84  // t == Type
85  // v == void
86  // Extend the above, if required.
87 
88  // Below is the list of currently supported signatures.
89  // Extend as needed.
90  bool (T::*b_hi)(Handle, int);
91  bool (T::*b_hh)(Handle, Handle);
92  double (T::*d_hht)(Handle, Handle, Type);
93  double (T::*d_hhtb)(Handle, Handle, Type, bool);
94  Handle (T::*h_h)(Handle);
95  Handle (T::*h_hi)(Handle, int);
96  Handle (T::*h_hh)(Handle, Handle);
97  Handle (T::*h_hs)(Handle, const std::string&);
98  Handle (T::*h_htq)(Handle, Type, const HandleSeq&);
99  Handle (T::*h_sq)(const std::string&, const HandleSeq&);
100  Handle (T::*h_sqq)(const std::string&,
101  const HandleSeq&, const HandleSeq&);
102  HandleSeq (T::*q_h)(Handle);
103  HandleSeq (T::*q_hti)(Handle, Type, int);
104  HandleSeq (T::*q_htib)(Handle, Type, int, bool);
105  HandleSeqSeq (T::*k_h)(Handle);
106  HandleSeqSeq (T::*k_hi)(Handle, int);
107  const std::string& (T::*s_s)(const std::string&);
108  const std::string& (T::*s_ss)(const std::string&,
109  const std::string&);
110  const std::string& (T::*s_sss)(const std::string&,
111  const std::string&,
112  const std::string&);
113  const std::string& (T::*s_v)(void);
114  TruthValuePtr (T::*p_h)(Handle);
115  void (T::*v_h)(Handle);
116  void (T::*v_s)(const std::string&);
117  void (T::*v_ss)(const std::string&,
118  const std::string&);
119  void (T::*v_sss)(const std::string&,
120  const std::string&,
121  const std::string&);
122  void (T::*v_t)(Type);
123  void (T::*v_ti)(Type, int);
124  void (T::*v_tidi)(Type, int, double, int);
125  void (T::*v_v)(void);
126  } method;
127  T* that;
128  const char *scheme_module;
129  const char *scheme_name;
130  enum
131  {
132  B_HI, // return boolean, take handle and int
133  B_HH, // return boolean, take handle and handle
134  D_HHT, // return double, take handle, handle, and type
135  D_HHTB,// return double, take handle, handle, and type
136  H_H, // return handle, take handle
137  H_HI, // return handle, take handle and int
138  H_HH, // return handle, take handle and handle
139  H_HS, // return handle, take handle and string
140  H_HTQ, // return handle, take handle, type, and HandleSeq
141  H_SQ, // return handle, take string and HandleSeq
142  H_SQQ, // return handle, take string, HandleSeq and HandleSeq
143  Q_H, // return HandleSeq, take handle
144  Q_HTI, // return HandleSeq, take handle, type, and int
145  Q_HTIB,// return HandleSeq, take handle, type, and bool
146  K_H, // return HandleSeqSeq, take Handle
147  K_HI, // return HandleSeqSeq, take Handle, int
148  S_S, // return string, take string
149  S_SS, // return string, take two strings
150  S_SSS, // return string, take three strings
151  S_V, // return string, take void
152  P_H, // return truth value, take Handle
153  V_H, // return void, take Handle
154  V_S, // return void, take string
155  V_SS, // return void, take two strings
156  V_SSS, // return void, take three strings
157  V_T, // return void, take Type
158  V_TI, // return void, take Type and int
159  V_TIDI,// return void, take Type, int, double, and int
160  V_V // return void, take void
161  } signature;
162 
163  virtual SCM invoke (SCM args)
164  {
165  SCM rc = SCM_EOL;
166  switch (signature)
167  {
168  case B_HI:
169  {
170  Handle h(SchemeSmob::verify_handle(scm_car(args), scheme_name));
171  int i = SchemeSmob::verify_int(scm_cadr(args), scheme_name, 2);
172  bool b = (that->*method.b_hi)(h, i);
173  if (b) { rc = SCM_BOOL_T; } else { rc = SCM_BOOL_F; }
174  break;
175  }
176  case B_HH:
177  {
178  Handle h1(SchemeSmob::verify_handle(scm_car(args), scheme_name, 1));
179  Handle h2(SchemeSmob::verify_handle(scm_cadr(args), scheme_name, 2));
180  bool b = (that->*method.b_hh)(h1, h2);
181 
182  if (b)
183  rc = SCM_BOOL_T;
184  else
185  rc = SCM_BOOL_F;
186 
187  break;
188  }
189  case D_HHT:
190  {
191  Handle h1(SchemeSmob::verify_handle(scm_car(args), scheme_name, 1));
192  Handle h2(SchemeSmob::verify_handle(scm_cadr(args), scheme_name, 2));
193  Type t = SchemeSmob::verify_atom_type(scm_caddr(args), scheme_name, 3);
194 
195  double d = (that->*method.d_hht)(h1,h2,t);
196  rc = scm_from_double(d);
197  break;
198  }
199  case D_HHTB:
200  {
201  Handle h1(SchemeSmob::verify_handle(scm_car(args), scheme_name, 1));
202  Handle h2(SchemeSmob::verify_handle(scm_cadr(args), scheme_name, 2));
203  Type t = SchemeSmob::verify_atom_type(scm_caddr(args), scheme_name, 3);
204  bool b = scm_to_bool(scm_cadddr(args));
205 
206  double d = (that->*method.d_hhtb)(h1,h2,t,b);
207  rc = scm_from_double(d);
208  break;
209  }
210  case H_H:
211  {
212  Handle h(SchemeSmob::verify_handle(scm_car(args), scheme_name));
213  Handle rh((that->*method.h_h)(h));
214  rc = SchemeSmob::handle_to_scm(rh);
215  break;
216  }
217  case H_HI:
218  {
219  Handle h(SchemeSmob::verify_handle(scm_car(args), scheme_name));
220  int i = SchemeSmob::verify_int(scm_cadr(args), scheme_name, 2);
221  Handle rh((that->*method.h_hi)(h,i));
222  rc = SchemeSmob::handle_to_scm(rh);
223  break;
224  }
225  case H_HH:
226  {
227  Handle h1(SchemeSmob::verify_handle(scm_car(args),
228  scheme_name));
229  Handle h2(SchemeSmob::verify_handle(scm_cadr(args),
230  scheme_name, 2));
231  Handle rh((that->*method.h_hh)(h1, h2));
232  rc = SchemeSmob::handle_to_scm(rh);
233  break;
234  }
235  case H_HS:
236  {
237  Handle h(SchemeSmob::verify_handle(scm_car(args),
238  scheme_name));
239  std::string s(SchemeSmob::verify_string(scm_cadr(args),
240  scheme_name, 2));
241  Handle rh((that->*method.h_hs)(h,s));
242  rc = SchemeSmob::handle_to_scm(rh);
243  break;
244  }
245  case H_HTQ:
246  {
247  Handle h(SchemeSmob::verify_handle(scm_car(args), scheme_name, 1));
248 
249  Type t = SchemeSmob::verify_atom_type(scm_cadr(args), scheme_name, 2);
250 
251  SCM list = scm_caddr(args);
253 
254  Handle rh((that->*method.h_htq)(h, t, seq));
255  rc = SchemeSmob::handle_to_scm(rh);
256  break;
257  }
258  case H_SQ:
259  {
260  // First argument is a string
261  std::string str = SchemeSmob::verify_string(scm_car(args), scheme_name, 1);
262 
263  // Second arg is a list of Handles
264  SCM list = scm_cadr(args);
266 
267  Handle rh((that->*method.h_sq)(str, seq));
268  rc = SchemeSmob::handle_to_scm(rh);
269  break;
270  }
271  case H_SQQ:
272  {
273  // First argument is a string
274  std::string str = SchemeSmob::verify_string(scm_car(args), scheme_name, 1);
275 
276  // Second arg is a list of Handles
277  SCM list1 = scm_cadr(args);
279 
280  // Third argument is a possibly empty list of Handles
281  SCM list2 = scm_caddr(args);
283 
284  Handle rh((that->*method.h_sqq)(str, seq1, seq2));
285  rc = SchemeSmob::handle_to_scm(rh);
286  break;
287  }
288  case Q_H:
289  {
290  // the only argument is a handle
291  Handle h(SchemeSmob::verify_handle(scm_car(args), scheme_name));
292  HandleSeq rHS((that->*method.q_h)(h));
293 
294  rc = SCM_EOL;
295 
296  // Reverse iteration to preserve order when doing cons
297  for (HandleSeq::reverse_iterator rit = rHS.rbegin(); rit != rHS.rend(); ++rit)
298  rc = scm_cons(SchemeSmob::handle_to_scm(*rit), rc);
299 
300  break;
301  }
302  case Q_HTI:
303  {
304  // First arg is a handle
305  Handle h(SchemeSmob::verify_handle(scm_car(args), scheme_name, 1));
306 
307  // Second arg is a type
308  Type t = SchemeSmob::verify_atom_type(scm_cadr(args), scheme_name, 2);
309 
310  // Third arg is an int
311  int i = SchemeSmob::verify_int(scm_caddr(args), scheme_name, 3);
312 
313  HandleSeq rHS = (that->*method.q_hti)(h,t,i);
314  HandleSeq::iterator it = rHS.begin();
315  if (it != rHS.end())
316  rc = scm_list_1(SchemeSmob::handle_to_scm(*it));
317  ++it;
318  for ( ; it != rHS.end(); ++it)
319  {
320  rc = scm_cons(SchemeSmob::handle_to_scm(*it), rc);
321  }
322  break;
323  }
324  case Q_HTIB:
325  {
326  // First arg is a handle
327  Handle h(SchemeSmob::verify_handle(scm_car(args), scheme_name, 1));
328 
329  // Second arg is a type
330  Type t = SchemeSmob::verify_atom_type(scm_cadr(args), scheme_name, 2);
331 
332  // Third arg is an int
333  int i = SchemeSmob::verify_int(scm_caddr(args), scheme_name, 3);
334 
335  //Fourth arg is a bool
336  bool b = scm_to_bool(scm_cadddr(args));
337 
338  HandleSeq rHS = (that->*method.q_htib)(h,t,i,b);
339  HandleSeq::iterator it = rHS.begin();
340  if (it != rHS.end())
341  rc = scm_list_1(SchemeSmob::handle_to_scm(*it));
342  ++it;
343  for ( ; it != rHS.end(); ++it)
344  {
345  rc = scm_cons(SchemeSmob::handle_to_scm(*it), rc);
346  }
347  break;
348  }
349  case K_H:
350  {
351  // the only argument is a handle
352  Handle h(SchemeSmob::verify_handle(scm_car(args), scheme_name));
353  HandleSeqSeq rHSS = (that->*method.k_h)(h);
354 
355  rc = SCM_EOL;
356 
357  // reverse iteration to preserve order when doing cons
358  for (HandleSeqSeq::reverse_iterator rit = rHSS.rbegin(); rit != rHSS.rend(); ++rit)
359  {
360  HandleSeq rHS = *rit;
361  SCM rcTemp = SCM_EOL;
362 
363  for (HandleSeq::reverse_iterator rrit = rHS.rbegin(); rrit != rHS.rend(); ++rrit)
364  rcTemp = scm_cons(SchemeSmob::handle_to_scm(*rrit), rcTemp);
365 
366  rc = scm_cons(rcTemp, rc);
367  }
368 
369  break;
370  }
371  case K_HI:
372  {
373  // First arg is a handle
374  Handle h(SchemeSmob::verify_handle(scm_car(args), scheme_name, 1));
375 
376  // Second arg is an int
377  int i = SchemeSmob::verify_int(scm_cadr(args), scheme_name, 2);
378 
379  HandleSeqSeq rHSS = (that->*method.k_hi)(h, i);
380 
381  rc = SCM_EOL;
382 
383  // reverse iteration to preserve order when doing cons
384  for (HandleSeqSeq::reverse_iterator rit = rHSS.rbegin(); rit != rHSS.rend(); ++rit)
385  {
386  HandleSeq rHS = *rit;
387  SCM rcTemp = SCM_EOL;
388 
389  for (HandleSeq::reverse_iterator rrit = rHS.rbegin(); rrit != rHS.rend(); ++rrit)
390  rcTemp = scm_cons(SchemeSmob::handle_to_scm(*rrit), rcTemp);
391 
392  rc = scm_cons(rcTemp, rc);
393  }
394 
395  break;
396  }
397  case S_S:
398  {
399  // First argument is a string
400  std::string str(SchemeSmob::verify_string(scm_car(args), scheme_name, 1));
401 
402  const std::string &rs = (that->*method.s_s)(str);
403  rc = scm_from_utf8_string(rs.c_str());
404  break;
405  }
406  case S_SS:
407  {
408  // All args are strings
409  std::string str1(SchemeSmob::verify_string(scm_car(args), scheme_name, 1));
410  std::string str2(SchemeSmob::verify_string(scm_cadr(args), scheme_name, 2));
411 
412  const std::string &rs = (that->*method.s_ss)(str1, str2);
413  rc = scm_from_utf8_string(rs.c_str());
414  break;
415  }
416  case S_SSS:
417  {
418  // All args are strings
419  std::string str1(SchemeSmob::verify_string(scm_car(args), scheme_name, 1));
420  std::string str2(SchemeSmob::verify_string(scm_cadr(args), scheme_name, 2));
421  std::string str3(SchemeSmob::verify_string(scm_caddr(args), scheme_name, 3));
422 
423  const std::string &rs = (that->*method.s_sss)(str1, str2, str3);
424  rc = scm_from_utf8_string(rs.c_str());
425  break;
426  }
427  case S_V:
428  {
429  const std::string &rs = (that->*method.s_v)();
430  rc = scm_from_utf8_string(rs.c_str());
431  break;
432  }
433  case P_H:
434  {
435  Handle h = SchemeSmob::verify_handle(scm_car(args), scheme_name);
436  TruthValuePtr tv((that->*method.p_h)(h));
437  rc = SchemeSmob::tv_to_scm(tv);
438  break;
439  }
440  case V_H:
441  {
442  Handle h(SchemeSmob::verify_handle(scm_car(args), scheme_name));
443  (that->*method.v_h)(h);
444  break;
445  }
446  case V_S:
447  {
448  // First argument is a string
449  std::string str(SchemeSmob::verify_string(scm_car(args), scheme_name, 1));
450 
451  (that->*method.v_s)(str);
452  break;
453  }
454  case V_SS:
455  {
456  // All args are strings
457  std::string str1(SchemeSmob::verify_string(scm_car(args), scheme_name, 1));
458  std::string str2(SchemeSmob::verify_string(scm_cadr(args), scheme_name, 2));
459 
460  (that->*method.v_ss)(str1, str2);
461  break;
462  }
463  case V_SSS:
464  {
465  // All args are strings
466  std::string str1(SchemeSmob::verify_string(scm_car(args), scheme_name, 1));
467  std::string str2(SchemeSmob::verify_string(scm_cadr(args), scheme_name, 2));
468  std::string str3(SchemeSmob::verify_string(scm_caddr(args), scheme_name, 3));
469 
470  (that->*method.v_sss)(str1, str2, str3);
471  break;
472  }
473  case V_T:
474  {
475  Type t = SchemeSmob::verify_atom_type(scm_car(args), scheme_name, 1);
476  (that->*method.v_t)(t);
477  break;
478  }
479  case V_TI:
480  {
481  SCM input = scm_car(args);
482  //Assuming that the type is input as a string or symbol, eg
483  //(f 'SimilarityLink) or (f "SimilarityLink")
484  if (scm_is_true(scm_symbol_p(input)))
485  input = scm_symbol_to_string(input);
486 
487  Type t = NOTYPE;
488  if (scm_is_integer(input))
489  t = scm_to_ushort(input);
490  else
491  {
492  const char *lstr = scm_i_string_chars(input);
493  t = classserver().getType(lstr);
494  }
495 
496  int i = scm_to_int(scm_cadr(args));
497 
498  (that->*method.v_ti)(t, i);
499  break;
500  }
501  case V_TIDI:
502  {
503  SCM input = scm_car(args);
504  //Assuming that the type is input as a string or symbol, eg
505  //(f 'SimilarityLink) or (f "SimilarityLink")
506  if (scm_is_true(scm_symbol_p(input)))
507  input = scm_symbol_to_string(input);
508 
509  Type t = NOTYPE;
510  if (scm_is_integer(input))
511  t = scm_to_ushort(input);
512  else
513  {
514  const char *lstr = scm_i_string_chars(input);
515  t = classserver().getType(lstr);
516  }
517 
518  int i = scm_to_int(scm_cadr(args));
519  double d = scm_to_double(scm_caddr(args));
520  int i2 = scm_to_int(scm_cadddr(args));
521 
522  (that->*method.v_tidi)(t, i, d, i2);
523  break;
524  }
525  case V_V:
526  {
527  (that->*method.v_v)();
528  break;
529  }
530  default:
531  throw RuntimeException(TRACE_INFO,
532  "Unsupported signature: %d\n", signature);
533  }
534  return rc;
535  }
536  protected:
537  virtual const char *get_name(void) { return scheme_name; }
538  virtual const char *get_module(void) { return scheme_module; }
539  virtual size_t get_size(void) { return sizeof (*this); }
540  public:
541 
542 #define DECLARE_CONSTR_0(SIG, LSIG, RET_TYPE) \
543  SchemePrimitive(const char* module, const char* name, \
544  RET_TYPE (T::*cb)(void), T *data) \
545  { \
546  that = data; \
547  method.LSIG = cb; \
548  scheme_module = module; \
549  scheme_name = name; \
550  signature = SIG; \
551  do_register(module, name, 0); /* cb has 0 args */ \
552  }
553 
554 #define DECLARE_CONSTR_1(SIG, LSIG, RET_TYPE, ARG_TYPE) \
555  SchemePrimitive(const char* module, const char* name, \
556  RET_TYPE (T::*cb)(ARG_TYPE), T *data) \
557  { \
558  that = data; \
559  method.LSIG = cb; \
560  scheme_module = module; \
561  scheme_name = name; \
562  signature = SIG; \
563  do_register(module, name, 1); /* cb has 1 arg */ \
564  }
565 
566 #define DECLARE_CONSTR_2(SIG, LSIG, RET_TYPE, ARG1_TYPE, ARG2_TYPE) \
567  SchemePrimitive(const char* module, const char* name, \
568  RET_TYPE (T::*cb)(ARG1_TYPE, ARG2_TYPE), T *data) \
569  { \
570  that = data; \
571  method.LSIG = cb; \
572  scheme_module = module; \
573  scheme_name = name; \
574  signature = SIG; \
575  do_register(module, name, 2); /* cb has 2 args */ \
576  }
577 
578 #define DECLARE_CONSTR_3(SIG, LSIG, RET_TYPE, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE) \
579  SchemePrimitive(const char* module, const char* name, \
580  RET_TYPE (T::*cb)(ARG1_TYPE, ARG2_TYPE, ARG3_TYPE), T *data) \
581  { \
582  that = data; \
583  method.LSIG = cb; \
584  scheme_module = module; \
585  scheme_name = name; \
586  signature = SIG; \
587  do_register(module, name, 3); /* cb has 3 args */ \
588  }
589 #define DECLARE_CONSTR_4(SIG, LSIG, RET_TYPE, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE) \
590  SchemePrimitive(const char* module, const char* name, \
591  RET_TYPE (T::*cb)(ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE), T *data) \
592  { \
593  that = data; \
594  method.LSIG = cb; \
595  scheme_module = module; \
596  scheme_name = name; \
597  signature = SIG; \
598  do_register(module, name, 4); /* cb has 4 args */ \
599  }
600 
601  // Declare and define the constructors for this class. They all have
602  // the same basic form, except for the types.
603  DECLARE_CONSTR_2(B_HI, b_hi, bool, Handle, int)
604  DECLARE_CONSTR_2(B_HH, b_hh, bool, Handle, Handle)
605  DECLARE_CONSTR_3(D_HHT, d_hht, double, Handle, Handle, Type)
606  DECLARE_CONSTR_4(D_HHTB, d_hhtb, double, Handle, Handle, Type, bool)
607  DECLARE_CONSTR_1(H_H, h_h, Handle, Handle)
608  DECLARE_CONSTR_2(H_HI, h_hi, Handle, Handle, int)
609  DECLARE_CONSTR_2(H_HH, h_hh, Handle, Handle, Handle)
610  DECLARE_CONSTR_2(H_HS, h_hs, Handle, Handle, const std::string&)
611  DECLARE_CONSTR_3(H_HTQ, h_htq, Handle, Handle, Type, const HandleSeq&)
612  DECLARE_CONSTR_2(H_SQ, h_sq, Handle, const std::string&, const HandleSeq&)
613  DECLARE_CONSTR_3(H_SQQ, h_sqq, Handle, const std::string&, const HandleSeq&, const HandleSeq&)
614  DECLARE_CONSTR_1(Q_H, q_h, HandleSeq, Handle)
615  DECLARE_CONSTR_3(Q_HTI, q_hti, HandleSeq, Handle, Type, int)
616  DECLARE_CONSTR_4(Q_HTIB, q_htib, HandleSeq, Handle, Type, int, bool)
618  DECLARE_CONSTR_2(K_HI, k_hi, HandleSeqSeq, Handle, int)
619  DECLARE_CONSTR_1(S_S, s_s, const std::string&, const std::string&)
620  DECLARE_CONSTR_2(S_SS, s_ss, const std::string&, const std::string&,
621  const std::string&)
622  DECLARE_CONSTR_3(S_SSS,s_sss,const std::string&, const std::string&,
623  const std::string&, const std::string&)
624  DECLARE_CONSTR_0(S_V, s_v, const std::string&)
626  DECLARE_CONSTR_1(V_H, v_h, void, Handle)
627  DECLARE_CONSTR_1(V_S, v_s, void, const std::string&)
628  DECLARE_CONSTR_2(V_SS, v_ss, void, const std::string&,
629  const std::string&)
630  DECLARE_CONSTR_3(V_SSS,v_sss,void, const std::string&,
631  const std::string&, const std::string&)
632  DECLARE_CONSTR_1(V_T, v_t, void, Type)
633  DECLARE_CONSTR_2(V_TI, v_ti, void, Type, int)
634  DECLARE_CONSTR_4(V_TIDI, v_tidi, void, Type, int, double, int)
635 
636  DECLARE_CONSTR_0(V_V, v_v, void);
637 };
638 
639 #define DECLARE_DECLARE_1(RET,ARG) \
640 template<class T> \
641 inline void define_scheme_primitive(const char *name, RET (T::*cb)(ARG), T *data, const char* module = "extension") \
642 { \
643  /* Note: this is freed automatically by scheme garbage collection */ \
644  /* when it is no longer needed. */ \
645  new SchemePrimitive<T>(module, name, cb, data); \
646 }
647 
648 #define DECLARE_DECLARE_2(RET,ARG1,ARG2) \
649 template<class T> \
650 inline void define_scheme_primitive(const char *name, RET (T::*cb)(ARG1,ARG2), T *data, const char* module = "extension") \
651 { \
652  /* Note: this is freed automatically by scheme garbage collection */ \
653  /* when it is no longer needed. */ \
654  new SchemePrimitive<T>(module, name, cb, data); \
655 }
656 
657 #define DECLARE_DECLARE_3(RET,ARG1,ARG2,ARG3) \
658 template<class T> \
659 inline void define_scheme_primitive(const char *name, RET (T::*cb)(ARG1,ARG2,ARG3), T *data, const char* module = "extension") \
660 { \
661  /* Note: this is freed automatically by scheme garbage collection */ \
662  /* when it is no longer needed. */ \
663  new SchemePrimitive<T>(module, name, cb, data); \
664 }
665 #define DECLARE_DECLARE_4(RET,ARG1,ARG2,ARG3,ARG4) \
666 template<class T> \
667 inline void define_scheme_primitive(const char *name, RET (T::*cb)(ARG1,ARG2,ARG3,ARG4), T *data, const char* module = "extension") \
668 { \
669  /* Note: this is freed automatically by scheme garbage collection */ \
670  /* when it is no longer needed. */ \
671  new SchemePrimitive<T>(module, name, cb, data); \
672 }
673 
674 DECLARE_DECLARE_1(Handle, Handle)
675 DECLARE_DECLARE_1(HandleSeq, Handle)
676 DECLARE_DECLARE_1(HandleSeqSeq, Handle)
677 DECLARE_DECLARE_1(const std::string&, const std::string&)
678 DECLARE_DECLARE_1(const std::string&, void)
679 DECLARE_DECLARE_1(TruthValuePtr, Handle)
680 DECLARE_DECLARE_1(void, Handle)
681 DECLARE_DECLARE_1(void, const std::string&)
682 DECLARE_DECLARE_1(void, Type)
683 DECLARE_DECLARE_1(void, void)
684 DECLARE_DECLARE_2(bool, Handle, int)
685 DECLARE_DECLARE_2(bool, Handle, Handle)
686 DECLARE_DECLARE_2(Handle, Handle, int)
687 DECLARE_DECLARE_2(Handle, Handle, Handle)
688 DECLARE_DECLARE_2(Handle, Handle, const std::string&)
689 DECLARE_DECLARE_2(Handle, const std::string&, const HandleSeq&)
690 DECLARE_DECLARE_2(HandleSeqSeq, Handle, int)
691 DECLARE_DECLARE_2(const std::string&, const std::string&, const std::string&)
692 DECLARE_DECLARE_2(void, const std::string&, const std::string&)
693 DECLARE_DECLARE_2(void, Type, int)
694 DECLARE_DECLARE_3(double, Handle, Handle, Type)
695 DECLARE_DECLARE_3(Handle, Handle, Type, const HandleSeq&)
696 DECLARE_DECLARE_3(Handle, const std::string&, const HandleSeq&, const HandleSeq&)
697 DECLARE_DECLARE_3(HandleSeq, Handle, Type, int)
698 DECLARE_DECLARE_3(const std::string&, const std::string&,
699  const std::string&, const std::string&)
700 DECLARE_DECLARE_3(void, const std::string&,
701  const std::string&, const std::string&)
702 DECLARE_DECLARE_4(double, Handle, Handle, Type, bool)
703 DECLARE_DECLARE_4(void, Type, int, double, int)
704 DECLARE_DECLARE_4(HandleSeq, Handle, Type, int, bool)
705 
707 }
708 
709 #endif // _OPENCOG_SCHEME_PRIMITIVE_H
710 
711 #endif // HAVE_GUILE
712 
HandleSeq(T::* q_h)(Handle)
void(T::* v_sss)(const std::string &, const std::string &, const std::string &)
bool(T::* b_hi)(Handle, int)
void(T::* v_ss)(const std::string &, const std::string &)
HandleSeqSeq(T::* k_hi)(Handle, int)
Handle(T::* h_hh)(Handle, Handle)
virtual SCM invoke(SCM)=0
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
double(T::* d_hhtb)(Handle, Handle, Type, bool)
static SCM do_call(SCM, SCM)
Handle(T::* h_hi)(Handle, int)
const std::string const std::string const std::string const std::string const std::string const std::string const std::string & DECLARE_CONSTR_0(V_V, v_v, void)
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
virtual const char * get_name(void)
const std::string const std::string const std::string const std::string void
Handle(T::* h_htq)(Handle, Type, const HandleSeq &)
Handle(T::* h_h)(Handle)
#define DECLARE_CONSTR_4(SIG, LSIG, RET_TYPE, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE)
const std::string &(T::* s_sss)(const std::string &, const std::string &, const std::string &)
virtual const char * get_module(void)
std::vector< HandleSeq > HandleSeqSeq
a list of lists of handles
Definition: Handle.h:248
static int verify_int(SCM, const char *, int pos=1, const char *msg="expecting integer")
Handle(T::* h_hs)(Handle, const std::string &)
static SCM tv_to_scm(TruthValuePtr)
DECLARE_DECLARE_3(const std::string &, const std::string &, const std::string &, const std::string &) DECLARE_DECLARE_3(void
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
static void * c_wrap_register(void *)
Handle(T::* h_sq)(const std::string &, const HandleSeq &)
double(T::* d_hht)(Handle, Handle, Type)
enum opencog::SchemePrimitive::@1 signature
const std::string &(T::* s_v)(void)
#define DECLARE_DECLARE_1(RET, ARG)
void do_register(const char *, const char *, int)
static Type verify_atom_type(SCM, const char *, int pos=1)
tuple args
Definition: benchmark.py:79
const std::string &(T::* s_ss)(const std::string &, const std::string &)
void really_do_register(const char *, const char *, int)
#define DECLARE_DECLARE_4(RET, ARG1, ARG2, ARG3, ARG4)
#define DECLARE_CONSTR_1(SIG, LSIG, RET_TYPE, ARG_TYPE)
static std::vector< Handle > verify_handle_list(SCM, const char *, int pos=1)
virtual const char * get_name(void)=0
#define DECLARE_DECLARE_2(RET, ARG1, ARG2)
const std::string &(T::* s_s)(const std::string &)
static PrimitiveEnviron * verify_pe(SCM, const char *)
union opencog::SchemePrimitive::@0 method
HandleSeq(T::* q_htib)(Handle, Type, int, bool)
void(T::* v_s)(const std::string &)
virtual const char * get_module(void)=0
bool(T::* b_hh)(Handle, Handle)
void(T::* v_tidi)(Type, int, double, int)
Type getType(const std::string &typeName)
Definition: ClassServer.cc:138
static std::string verify_string(SCM, const char *, int pos=1, const char *msg="expecting string")
static void init_in_module(void *)
virtual size_t get_size(void)=0
DECLARE_CONSTR_2(S_SS, s_ss, const std::string &, const std::string &, const std::string &) DECLARE_CONSTR_3(S_SSS
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
static Handle verify_handle(SCM, const char *, int pos=1)
static SCM handle_to_scm(Handle)
virtual SCM invoke(SCM args)
HandleSeqSeq(T::* k_h)(Handle)
HandleSeq(T::* q_hti)(Handle, Type, int)
#define DECLARE_CONSTR_3(SIG, LSIG, RET_TYPE, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE)
void(T::* v_ti)(Type, int)
virtual size_t get_size(void)
Handle(T::* h_sqq)(const std::string &, const HandleSeq &, const HandleSeq &)
void(T::* v_h)(Handle)
TruthValuePtr(T::* p_h)(Handle)