Skip to content

Instantly share code, notes, and snippets.

@jaytaph
Created March 19, 2015 20:27
Show Gist options
  • Save jaytaph/e06cf228fe1c88bede43 to your computer and use it in GitHub Desktop.
Save jaytaph/e06cf228fe1c88bede43 to your computer and use it in GitHub Desktop.
#include "saffire.h"
#include <stdio.h>
Saffire::Object greet_method(Saffire::Arguments &args) {
Saffire::String s;
args.parse("s", &s);
return Saffire::String( std::string("hello ") + s.value() );
}
void goodbye_ctor(Saffire::Arguments &args) {
printf("Hello from the goodbye ctor()!\n");
}
/**
*
*/
Saffire::Module init(void) {
// Define the actual module
Saffire::Module module("hello_mod", "a simple module to demonstrate how easy it should be to connect custom code to saffire");
// Define two objects and attach to our module
Saffire::Object hello_object;
Saffire::Object goodbye_object;
module.addObject(hello_object, "hello");
module.addObject(goodbye_object, "goodbye");
/********************************************************
* Hello class defines
********************************************************/
// Set the parent class
// hello_object.setParent("base");
// Set an interface
// hello_object.addInterface("iterator");
// hello_object.addInterface("subscription");
// Set constructor and destructor
// hello_object.setCtor(ctor_func);
// hello_object.setDtor(dtor_func);
// Add methods
hello_object.addMethod("greet", greet_method);
// Add properties
hello_object.addProperty("question", Saffire::String("The answer to life, the universe and everything"));
hello_object.addProperty("answer", Saffire::Numerical(42));
// Add constants
hello_object.addConstant("pi", Saffire::Numerical(314));
/********************************************************
* Goodbye class defines
********************************************************/
// Set the parent class
goodbye_object.setParent(hello_object);
// Set constructor and destructor
goodbye_object.setCtor(goodbye_ctor);
return module;
}
//SAFFIRE_MODULE(hello, init)
saffire_module_entry module_entry = { init };
#ifndef SAFFIRE_H
#define SAFFIRE_H
#include <string>
#ifdef __cplusplus
extern "C" {
#endif
namespace Saffire {
class Arguments {
public:
void parse(const char *format, ...);
};
class Interface;
class Object {
public:
void addMethod(const char *name, Object(*func)(Arguments &));
void addProperty(const char *name, Object obj);
void addConstant(const char *name, Object obj);
void setParent(Object parent);
void setParent(const char *parent);
void addInterface(Interface *interface);
void addInterface(char *interface);
void setCtor(void(*func)(Arguments &));
void setDtor(void(*func)(void));
};
class Interface : public Object {
};
class Module {
protected:
char *name;
char *desc;
public:
Module(const char *name, const char *desc);
void addObject(Object object, const char *name);
};
class String: public Object {
protected:
std::string strval;
public:
String();
String(std::string val);
String(const char *val);
char *value();
};
class Numerical: public Object {
public:
Numerical(long num);
long value();
};
} // Namespace Saffire
typedef struct _saffire_module_entry {
Saffire::Module(*initfunc)(void);
} saffire_module_entry;
#ifdef __cplusplus
}
#endif
#endif /* SAFFIRE_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment