OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PrimitiveExample.cc
Go to the documentation of this file.
1 /*
2  * PrimitiveExample.cc
3  *
4  * Example code showing how declare a C++ method so that it can
5  * be called from scheme.
6  *
7  * Copyright (C) 2009 Linas Vepstas
8  */
9 
10 
14 
15 using namespace opencog;
16 
17 // Some example class
19 {
20  private:
22  int _id; // some value in the instance
23  public:
24 
25  MyTestClass(AtomSpace* as, int id) : _as(as), _id(id) {}
26 
27  // An example method -- accepts a handle, and wraps it
28  // with a ListLink.
30  {
31  Handle hlist;
32  Type t = h->getType();
33  if (classserver().isA(t, NODE))
34  {
35  NodePtr n = NodeCast(h);
36  std::string name = n->getName();
37  printf("Info: my_func instance %d received the node: %s\n",
38  _id, name.c_str());
39  hlist = _as->add_link(LIST_LINK, h);
40  }
41  else
42  {
43  printf("Warning: my_func instance %d called with invalid handle\n", _id);
44  }
45  return hlist;
46  }
47 
49  {
50  throw (RuntimeException(TRACE_INFO, "I threw an exception %d", _id));
51  return Handle::UNDEFINED;
52  }
53 };
54 
55 int main ()
56 {
57  // Need to access the atomspace to get it to initialize itself.
58  AtomSpace* as = new AtomSpace();
59 
60  // Do this early, so that guile is initialized.
61  SchemeEval* eval = new SchemeEval(as);
62 
63  printf("\nInfo: Start creating a scheme call into C++\n");
64 
65  // Create the example class, and define a scheme function,
66  // named "bingo", that will call one of its methods
67  MyTestClass *mtc = new MyTestClass(as, 42);
68  define_scheme_primitive("bingo", &MyTestClass::my_func, mtc);
69 
70  // Now, call bingo, with a reasonable argument. Since
71  // MyTestClass::my_func is expecting a handle, we better pass
72  // bingo a handle.
73  eval->eval("(define nnn (cog-new-node 'ConceptNode \"Hello World!\"))");
74  std::string rslt = eval->eval("(bingo nnn)");
75  if (eval->eval_error())
76  {
77  printf("Error: failed evaluation\n");
78  }
79 
80  // Print the result of calling MyTestClass::my_func
81  printf("Info: Result of scheme evaluation is %s", rslt.c_str());
82 
83  // Now try throwing an exception.
84  define_scheme_primitive("whoops", &MyTestClass::my_other_func, mtc);
85 
86  rslt = eval->eval("(whoops nnn)");
87  if (!eval->eval_error())
88  {
89  printf("XXX ERROR XXX: an error should have been thrown, but wasn't!\n");
90  }
91 
92  // Print the result of calling MyTestClass::my_func
93  printf("Info: Intentional throw gave the following output:\n%s", rslt.c_str());
94 
95  delete eval;
96  printf("\nInfo: We are done, bye!\n");
97  return 0;
98 }
Type getType() const
Definition: Atom.h:197
std::string eval(const std::string &expr)
Definition: SchemeEval.h:155
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
static NodePtr NodeCast(const Handle &h)
Definition: Node.h:113
virtual bool eval_error(void)
Definition: GenericEval.h:75
static const Handle UNDEFINED
Definition: Handle.h:77
int main()
Handle my_other_func(Handle h)
MyTestClass(AtomSpace *as, int id)
std::shared_ptr< Node > NodePtr
Definition: Node.h:112
AtomSpace * _as
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
Handle my_func(Handle h)