WIP
This commit is contained in:
parent
995af6d2b6
commit
ec0c0b64e6
@ -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)
|
|
||||||
|
11
src/server/internal_interface.h
Normal file
11
src/server/internal_interface.h
Normal 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);
|
||||||
|
};
|
||||||
|
|
@ -64,61 +64,62 @@ 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 << "Success!" << std::endl;
|
|
||||||
|
|
||||||
void *new_module = library_load(out_path.c_str());
|
|
||||||
if(new_module == NULL){
|
|
||||||
std::cout<<"Failed to load compiled library: "<<dlerror()<<std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
RCCPP_GetInterface GetInterface = (RCCPP_GetInterface)library_get_address(new_module, "rccpp_GetInterface");
|
|
||||||
if(GetInterface == nullptr) {
|
|
||||||
std::cout << "GetInterface is missing from the library" << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
RCCPP_Interface *interface = GetInterface();
|
|
||||||
assert(interface && "Interface is null");
|
|
||||||
RCCPP_Constructor fun_constructor = interface->constructor;
|
|
||||||
RCCPP_Destructor fun_destructor = interface->destructor;
|
|
||||||
RCCPP_PlacementNew fun_placementnew = interface->placementnew;
|
|
||||||
|
|
||||||
if(!(fun_constructor && fun_constructor && fun_placementnew)) {
|
|
||||||
printf("Something failed with the function pointers in the module\n");
|
|
||||||
printf(" constructor: %p (%s)\n", fun_constructor, (fun_constructor != nullptr ? "ok" : "fail"));
|
|
||||||
printf(" destructor: %p (%s)\n", fun_destructor, (fun_destructor != nullptr ? "ok" : "fail"));
|
|
||||||
printf(" placement new: %p (%s)\n", fun_placementnew, (fun_placementnew != nullptr ? "ok" : "fail"));
|
|
||||||
fflush(stdout);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string classname = interface->classname;
|
|
||||||
|
|
||||||
auto it = component_info_.find(classname);
|
|
||||||
if(it != component_info_.end()) {
|
|
||||||
RCCPP_Info &funcs = it->second;
|
|
||||||
funcs.constructor = fun_constructor;
|
|
||||||
funcs.destructor = fun_destructor;
|
|
||||||
funcs.placement_new = fun_placementnew;
|
|
||||||
if(funcs.module_prev) library_unload(funcs.module_prev);
|
|
||||||
funcs.module_prev = funcs.module;
|
|
||||||
funcs.module = new_module;
|
|
||||||
} else {
|
|
||||||
RCCPP_Info funcs;
|
|
||||||
funcs.constructor = fun_constructor;
|
|
||||||
funcs.destructor = fun_destructor;
|
|
||||||
funcs.placement_new = fun_placementnew;
|
|
||||||
funcs.module_prev = nullptr;
|
|
||||||
funcs.module = new_module;
|
|
||||||
funcs.size = interface->original_size;
|
|
||||||
component_info_.emplace(classname, std::move(funcs));
|
|
||||||
}
|
|
||||||
|
|
||||||
changed_classes_.push_back(classname);
|
|
||||||
} else {
|
|
||||||
std::cout << "Failed!" << std::endl;
|
std::cout << "Failed!" << std::endl;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
std::cout << "Success!" << std::endl;
|
||||||
|
|
||||||
|
void *new_module = library_load(out_path.c_str());
|
||||||
|
if(new_module == NULL){
|
||||||
|
std::cout<<"Failed to load compiled library: "<<dlerror()<<std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
RCCPP_GetInterface GetInterface = (RCCPP_GetInterface)library_get_address(new_module, "rccpp_GetInterface");
|
||||||
|
if(GetInterface == nullptr) {
|
||||||
|
std::cout << "GetInterface is missing from the library" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
RCCPP_Interface *interface = GetInterface();
|
||||||
|
assert(interface && "Interface is null");
|
||||||
|
RCCPP_Constructor fun_constructor = interface->constructor;
|
||||||
|
RCCPP_Destructor fun_destructor = interface->destructor;
|
||||||
|
RCCPP_PlacementNew fun_placementnew = interface->placementnew;
|
||||||
|
|
||||||
|
if(!(fun_constructor && fun_constructor && fun_placementnew)) {
|
||||||
|
printf("Something failed with the function pointers in the module\n");
|
||||||
|
printf(" constructor: %p (%s)\n", fun_constructor, (fun_constructor != nullptr ? "ok" : "fail"));
|
||||||
|
printf(" destructor: %p (%s)\n", fun_destructor, (fun_destructor != nullptr ? "ok" : "fail"));
|
||||||
|
printf(" placement new: %p (%s)\n", fun_placementnew, (fun_placementnew != nullptr ? "ok" : "fail"));
|
||||||
|
fflush(stdout);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string classname = interface->classname;
|
||||||
|
|
||||||
|
auto it = component_info_.find(classname);
|
||||||
|
if(it != component_info_.end()) {
|
||||||
|
RCCPP_Info &funcs = it->second;
|
||||||
|
funcs.constructor = fun_constructor;
|
||||||
|
funcs.destructor = fun_destructor;
|
||||||
|
funcs.placement_new = fun_placementnew;
|
||||||
|
if(funcs.module_prev) library_unload(funcs.module_prev);
|
||||||
|
funcs.module_prev = funcs.module;
|
||||||
|
funcs.module = new_module;
|
||||||
|
} else {
|
||||||
|
RCCPP_Info funcs;
|
||||||
|
funcs.constructor = fun_constructor;
|
||||||
|
funcs.destructor = fun_destructor;
|
||||||
|
funcs.placement_new = fun_placementnew;
|
||||||
|
funcs.module_prev = nullptr;
|
||||||
|
funcs.module = new_module;
|
||||||
|
funcs.size = interface->original_size;
|
||||||
|
component_info_.emplace(classname, std::move(funcs));
|
||||||
|
}
|
||||||
|
|
||||||
|
changed_classes_.push_back(classname);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *RCCPP_Compiler::construct(const char *name) {
|
void *RCCPP_Compiler::construct(const char *name) {
|
||||||
|
@ -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)
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user