This commit is contained in:
Perttu Ahola 2014-09-16 21:28:54 +03:00
parent 995af6d2b6
commit ec0c0b64e6
6 changed files with 89 additions and 76 deletions

View File

@ -1,14 +1,2 @@
#pragma once #pragma once
#include "server/rccpp.h"
class Module : public RuntimeClass<Module> {
CLASS_INTERNALS(Module)
public:
Module();
~Module();
RUNTIME_VIRTUAL int test_add(int a);
int m_sum;
};
RUNTIME_EXPORT_CLASS(Module)

View File

@ -0,0 +1,11 @@
#pragma once
#include "server/rccpp.h"
class Module : public RuntimeClass<Module> {
CLASS_INTERNALS(Module)
public:
Module();
~Module();
RUNTIME_VIRTUAL int test_add(int a, int b);
};

View File

@ -64,12 +64,16 @@ void RCCPP_Compiler::build(const std::string &in_path, const std::string &out_pa
std::string out_dir = c55fs::stripFilename(out_path); std::string out_dir = c55fs::stripFilename(out_path);
c55fs::CreateAllDirs(out_dir); c55fs::CreateAllDirs(out_dir);
if(compile(in_path, out_path)) { if(!compile(in_path, out_path)) {
std::cout << "Failed!" << std::endl;
return;
}
std::cout << "Success!" << std::endl; std::cout << "Success!" << std::endl;
void *new_module = library_load(out_path.c_str()); void *new_module = library_load(out_path.c_str());
if(new_module == NULL){ if(new_module == NULL){
std::cout<<"Failed to load compiled library: "<<dlerror()<<std::endl; std::cout<<"Failed to load compiled library: "<<dlerror()<<std::endl;
return;
} }
RCCPP_GetInterface GetInterface = (RCCPP_GetInterface)library_get_address(new_module, "rccpp_GetInterface"); RCCPP_GetInterface GetInterface = (RCCPP_GetInterface)library_get_address(new_module, "rccpp_GetInterface");
@ -116,9 +120,6 @@ void RCCPP_Compiler::build(const std::string &in_path, const std::string &out_pa
} }
changed_classes_.push_back(classname); changed_classes_.push_back(classname);
} else {
std::cout << "Failed!" << std::endl;
}
} }
void *RCCPP_Compiler::construct(const char *name) { void *RCCPP_Compiler::construct(const char *name) {

View File

@ -3,7 +3,7 @@
// This is what you get when the RCCPP is not enabled // This is what you get when the RCCPP is not enabled
#if !defined(RCCPP_ENABLED) && !defined(RCCPP) #if !defined(RCCPP_ENABLED) && !defined(RCCPP)
#define RUNTIME_EXPORT_CLASS(C) #define RUNTIME_EXPORT_MODULE(C)
#define RUNTIME_VIRTUAL #define RUNTIME_VIRTUAL
#endif #endif
@ -33,7 +33,7 @@ struct RCCPP_Interface {
#if defined(RCCPP) && !defined(RCCPP_ENABLED) #if defined(RCCPP) && !defined(RCCPP_ENABLED)
#include <new> #include <new>
#define EXPORT __attribute__ ((visibility ("default"))) #define EXPORT __attribute__ ((visibility ("default")))
#define RUNTIME_EXPORT_CLASS(C) \ #define RUNTIME_EXPORT_MODULE(C) \
extern "C" { \ extern "C" { \
static void *rccpp_constructor() { return new C(); } \ static void *rccpp_constructor() { return new C(); } \
static void rccpp_destructor(void *c) { delete reinterpret_cast<C*>(c); } \ static void rccpp_destructor(void *c) { delete reinterpret_cast<C*>(c); } \
@ -59,7 +59,7 @@ extern "C" { \
#define RCCPP_MOVE "rccpp_move" #define RCCPP_MOVE "rccpp_move"
#define RCCPP_CLASSNAME "rccpp_classname" #define RCCPP_CLASSNAME "rccpp_classname"
#define RUNTIME_EXPORT_CLASS(C) #define RUNTIME_EXPORT_MODULE(C)
#define RUNTIME_VIRTUAL virtual #define RUNTIME_VIRTUAL virtual
#include <vector> #include <vector>
@ -76,17 +76,15 @@ struct RCCPP_Info {
RCCPP_Destructor destructor; RCCPP_Destructor destructor;
RCCPP_PlacementNew placement_new; RCCPP_PlacementNew placement_new;
size_t size; size_t size;
std::string classname;
}; };
class RuntimeClassBase;
class RCCPP_Compiler { class RCCPP_Compiler {
public: public:
RCCPP_Compiler(); RCCPP_Compiler();
void build(const std::string &in_path, const std::string &out_path); void build(const std::string &in_path, const std::string &out_path);
template<typename T> T *construct() { return (T*)construct(T::NAME); }
void *construct(const char *name); void *construct(const char *name);
#if defined(RCCPP_USE_SINGLETON_NEWING) #if defined(RCCPP_USE_SINGLETON_NEWING)

View File

@ -1,6 +1,8 @@
#include "state.h" #include "state.h"
#include "rccpp.h" #include "rccpp.h"
#include "config.h" #include "config.h"
#include "internal_interface.h"
#include <iostream>
extern server::Config g_server_config; extern server::Config g_server_config;
@ -22,6 +24,10 @@ struct CState: public State
{ {
ss_ build_dst = g_server_config.rccpp_build_path + "/foo.o"; ss_ build_dst = g_server_config.rccpp_build_path + "/foo.o";
m_compiler.build(path+"/server/main.cpp", build_dst); m_compiler.build(path+"/server/main.cpp", build_dst);
Module *m = static_cast<Module*>(m_compiler.construct("Test1Module"));
int a = m->test_add(1, 2);
std::cout<<"a = "<<a<<std::endl;
} }
}; };

View File

@ -1,15 +1,24 @@
#include <core.h> #include "server/rccpp.h"
Module::Module() class Test1Module : public RuntimeClass<Test1Module> {
CLASS_INTERNALS(Test1Module)
public:
Test1Module();
~Test1Module();
RUNTIME_VIRTUAL int test_add(int a, int b);
};
RUNTIME_EXPORT_MODULE(Test1Module)
Test1Module::Test1Module()
{ {
} }
Module::~Module() Test1Module::~Test1Module()
{ {
} }
int Module::test_add(int a) int Test1Module::test_add(int a, int b)
{ {
return a; return a + b;
} }