WIP: Module loading starts to take shape

This commit is contained in:
Perttu Ahola 2014-09-16 23:21:41 +03:00
parent 389872bafe
commit 6414b38518
6 changed files with 36 additions and 17 deletions

View File

@ -8,6 +8,7 @@ namespace interface
struct Module
{
virtual ~Module(){};
virtual void start() = 0;
virtual int test_add(int a, int b) = 0;
};
}

View File

@ -1,4 +1,5 @@
#include "rccpp.h"
#include "interface/server.h"
#include <c55_filesys.h>
@ -23,7 +24,7 @@
namespace rccpp {
typedef void *(*RCCPP_Constructor)();
typedef void *(*RCCPP_Constructor)(interface::Server *server);
struct RCCPP_Info {
void *module;
@ -34,10 +35,10 @@ struct CCompiler: public Compiler
{
CCompiler();
void build(const std::string &module_name,
bool build(const std::string &module_name,
const std::string &in_path, const std::string &out_path);
void *construct(const char *name);
void* construct(const char *name, interface::Server *server);
private:
std::unordered_map<std::string, RCCPP_Info> component_info_;
@ -90,7 +91,7 @@ bool CCompiler::compile(const std::string &in_path, const std::string &out_path)
return exit_status == 0;
}
void CCompiler::build(const std::string &module_name,
bool CCompiler::build(const std::string &module_name,
const std::string &in_path, const std::string &out_path)
{
std::cout << "Building " << module_name << ": "
@ -101,23 +102,22 @@ void CCompiler::build(const std::string &module_name,
if(!compile(in_path, out_path)) {
std::cout << "Failed!" << std::endl;
return;
return false;
}
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;
return false;
}
RCCPP_Constructor constructor = (RCCPP_Constructor)library_get_address(new_module, "createModule");
if(constructor == nullptr) {
std::cout << "createModule() is missing from the library" << std::endl;
return;
return false;
}
//std::string classname = interface->classname;
std::string classname = module_name;
auto it = component_info_.find(classname);
@ -133,9 +133,10 @@ void CCompiler::build(const std::string &module_name,
}
changed_classes_.push_back(classname);
return true;
}
void *CCompiler::construct(const char *name) {
void *CCompiler::construct(const char *name, interface::Server *server) {
auto component_info_it = component_info_.find(name);
if(component_info_it == component_info_.end()) {
assert(nullptr && "Failed to get class info");
@ -145,7 +146,7 @@ void *CCompiler::construct(const char *name) {
RCCPP_Info &info = component_info_it->second;
RCCPP_Constructor constructor = info.constructor;
void *result = constructor();
void *result = constructor(server);
auto it = constructed_objects.find(std::string(name));

View File

@ -1,15 +1,19 @@
#pragma once
#include "core/types.h"
namespace interface {
struct Server;
}
namespace rccpp
{
struct Compiler {
virtual ~Compiler(){}
virtual void build(const std::string &module_name,
virtual bool build(const std::string &module_name,
const std::string &in_path, const std::string &out_path) = 0;
virtual void *construct(const char *name) = 0;
virtual void *construct(const char *name, interface::Server *server) = 0;
std::vector<std::string> include_directories;
std::vector<std::string> library_directories;

View File

@ -32,9 +32,10 @@ struct CState: public State, public interface::Server
m_compiler->build(module_name, path+"/server/main.cpp", build_dst);
interface::Module *m = static_cast<interface::Module*>(
m_compiler->construct(module_name.c_str()));
int a = m->test_add(1, 2);
std::cout<<"a = "<<a<<std::endl;
m_compiler->construct(module_name.c_str(), this));
//int a = m->test_add(1, 2);
//std::cout<<"a = "<<a<<std::endl;
m->start();
}
void load_modules(const ss_ &path)

View File

@ -3,7 +3,10 @@
struct Module: public interface::Module
{
Module()
interface::Server *m_server;
Module(interface::Server *server):
m_server(server)
{
}
@ -11,6 +14,11 @@ struct Module: public interface::Module
{
}
void start()
{
m_server->load_module("foo", "bar");
}
int test_add(int a, int b)
{
return a + b;
@ -20,6 +28,6 @@ struct Module: public interface::Module
extern "C" {
EXPORT void* createModule(interface::Server *server)
{
return (void*)(new Module());
return (void*)(new Module(server));
}
}

View File

@ -11,6 +11,10 @@ struct Module: public interface::Module
{
}
void start()
{
}
int test_add(int a, int b)
{
return a + b;