OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ExampleSCM.cc
Go to the documentation of this file.
1 /*
2  * ExampleSCM.cc
3  *
4  * Example Guile Scheme bindings for C++ code.
5  * Copyright (c) 2008, 2014, 2015 Linas Vepstas <linas@linas.org>
6  */
7 
8 
9 #include <cstddef>
12 
13 #include "ExampleSCM.h"
14 
15 
16 // ========================================================
17 
18 using namespace opencog;
19 
20 // Below are the functions to be placed into the module.
21 // At this time, the way that the module wrapper is designed,
22 // the wrapper only allows two types of functions to be wrapped:
23 // Those that take a single handle, and return either a handle,
24 // or a truth value. The SchemePrimitive code allows far more
25 // general functions to be wrapped; however, for the simplified
26 // creation of modules, these two types seems to be enough to
27 // handle all current cases.
28 //
29 // You can wrap more general methods; see the PrimitiveExample.cc
30 // for that.
32 {
33  Handle reth = h;
34  printf("Hello I got this atom: %s\n", h->toShortString().c_str());
35  return reth;
36 }
37 
39 {
40  printf("This is the atom: %s\n", h->toString().c_str());
41  return h->getTruthValue();
42 }
43 
44 // ========================================================
45 
46 // XXX HACK ALERT This needs to be static, in order for python to
47 // work correctly. The problem is that python keeps creating and
48 // destroying this class, but it expects things to stick around.
49 // Oh well. I guess that's OK, since the definition is meant to be
50 // for the lifetime of the server, anyway.
51 std::vector<FunctionWrap*> ExampleSCM::_binders;
52 
53 // ModuleWrap takes the string name of the guile module.
55  ModuleWrap("opencog example")
56 {}
57 
60 void ExampleSCM::init(void)
61 {
62  _binders.push_back(new FunctionWrap(ss_print,
63  "hey-print", "example"));
64 
65  _binders.push_back(new FunctionWrap(ss_printmore,
66  "hey-printmore", "example"));
67 }
68 
70 {
71 #if PYTHON_BUG_IS_FIXED
72  for (FunctionWrap* pw : _binders)
73  delete pw;
74 #endif
75 }
76 
77 // Create a single static instance.
79 {
80  static ExampleSCM example;
81  example.module_init();
82 }
static TruthValuePtr ss_printmore(AtomSpace *atomspace, const Handle &h)
Definition: ExampleSCM.cc:38
void opencog_example_init(void)
Definition: ExampleSCM.cc:78
void module_init(void)
Definition: SchemeModule.cc:62
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
virtual std::string toShortString(std::string indent="")=0
static std::vector< FunctionWrap * > _binders
Definition: ExampleSCM.h:19
Wrapper class, to invoke misc extension code from guile.
Definition: SchemeModule.h:20
static Handle ss_print(AtomSpace *atomspace, const Handle &h)
Definition: ExampleSCM.cc:31
virtual std::string toString(std::string indent="")=0
virtual void init(void)
Definition: ExampleSCM.cc:60
TruthValuePtr getTruthValue()
Definition: Atom.cc:104