From e3ac7ebf66f6d10d686c1c572ec81d625f0598e6 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Sun, 17 Aug 2003 05:31:01 +0000 Subject: [PATCH] First (really quick&dirty) dynamic version. It compiles a bife library (as a shared object) with core bife classes: HIT, GHIT, CHIT, Widget, String, Container and Fallback. It compiles a Translate plug-in (as a shared object) with Translate class. If compiles a parser_test, linked to libbife that loads translate as a plug-in dynamically. A lot of cleanning have to be done... --- Makefile | 49 ++++++++++++++++++++++++++++++++++++++------ parser.cpp | 20 +++++++++++++----- parser.h | 7 +++++++ parser_test.cpp | 3 +++ translate_loader.cpp | 20 ++++++++++++++++++ 5 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 translate_loader.cpp diff --git a/Makefile b/Makefile index 09e9010..3809eb7 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,55 @@ -CPPFLAGS=-g3 -Wall -I/usr/include/libxml++-1.0 -I/usr/include/libxml2 -DDEBUG +DEBUG=-g3 -DDEBUG -Wall +#CPPFLAGS=-g3 -Wall -I/usr/include/libxml++-1.0 -I/usr/include/libxml2 -DDEBUG #CPPFLAGS=-O3 -Wall -I/usr/include/libxml++-1.0 -I/usr/include/libxml2 -LDFLAGS=-lxml++-0.1 #-lxml +TAGETS=hit.o ghit.o chit.o widget.o container.o fallback.o string.o -TAGETS=hit.o ghit.o chit.o widget.o container.o fallback.o string.o translate.o parser.o - -all: main +all: parser_test main: $(TAGETS) -parser_test: $(TAGETS) + +CPPFLAGS=-fPIC $(DEBUG) hit.o: hit.h hit.cpp +ghit.o: hit.o ghit.h ghit.cpp +chit.o: ghit.o chit.h chit.cpp + +widget.o: widget.h widget.cpp +string.o: widget.o string.h string.cpp +container.o: widget.o container.h container.cpp +fallback.o: container.o fallback.h fallback.cpp + +translate.o: translate.h translate.cpp +translate_loader.o: translate_loader.cpp +#.o: .o .h .cpp + +# LIBBIFE +LIBBIFE_TARGETS=hit.o ghit.o chit.o widget.o string.o container.o fallback.o +libbife.so: $(LIBBIFE_TARGETS) + $(CXX) $(DEBUG) -Wl,-soname,libbife.so -shared -o libbife.so $(LIBBIFE_TARGETS) + +# TRANSLATE FALLBACK PLUG-IN +TRANSLATE_TARGETS=translate.o translate_loader.o +translate.so: libbife.so $(TRANSLATE_TARGETS) + $(CXX) $(DEBUG) -L. -lbife -Wl,-soname,translate.so -shared -o translate.so $(TRANSLATE_TARGETS) + + +# Parser example. +PARSER_FLAGS=-I/usr/include/libxml++-1.0 -I/usr/include/libxml2 +parser.o: parser.h parser.cpp + $(CXX) $(DEBUG) $(PARSER_FLAGS) -c parser.cpp +parser_test.o: parser_test.cpp + $(CXX) $(DEBUG) $(PARSER_FLAGS) -c parser_test.cpp + +PARSER_TARGETS=parser.o parser_test.o +parser_test: translate.so $(PARSER_TARGETS) + $(CXX) $(DEBUG) $(PARSER_FLAGS) -L. -lxml++-0.1 -ldl -lbife -o parser_test $(PARSER_TARGETS) + @echo + @echo ---------------------------------------------- + @echo export LD_LIBRARY_PATH=. + @echo to use ./parser_test clean: @rm -f *.o main parser_test diff --git a/parser.cpp b/parser.cpp index 9f4cfee..34cf1bb 100644 --- a/parser.cpp +++ b/parser.cpp @@ -1,11 +1,12 @@ // vim: set expandtab tabstop=4 shiftwidth=4: -// FIXME - ver tema de fallback. -#include "translate.h" - +#include "widget.h" +#include "container.h" +#include "fallback.h" #include "string.h" #include "parser.h" #include +#include using std::stringstream; using namespace bife; @@ -41,7 +42,7 @@ void Parser::on_start_element(const string& name, const AttributeMap& attrs) { } cerr << "]);" << endl; #endif - stack.push(new Translate(name, attrs)); + stack.push(fb_create(name, attrs)); } void Parser::on_end_element(const string& name) { @@ -121,10 +122,19 @@ void Parser::on_validity_warning(const string& warn) { #endif } -Parser::Parser(void): root(NULL) { +Parser::Parser(void): fb_create(NULL), fb_destroy(NULL), root(NULL) { #ifdef DEBUG cerr << "In Parser::Parser();" << endl; #endif + void* fb = dlopen("./translate.so", RTLD_NOW | RTLD_GLOBAL); // TODO - mas rapido: RTLD_LAZY); + if (!fb) { + throw string("No se puede cargar el plug-in: ") + dlerror(); + } + fb_create = (create_t*)dlsym(fb, "create"); + fb_destroy = (destroy_t*)dlsym(fb, "destroy"); + if (!fb_create || !fb_destroy) { + throw string("No se puede cargar el creador del plug-in: ") + dlerror(); + } } //Parser::Parser(const Hash& attrs): attrs(attrs) { diff --git a/parser.h b/parser.h index f792646..3476d87 100644 --- a/parser.h +++ b/parser.h @@ -10,6 +10,10 @@ #include #include +// FIXME - Poner esto en un lugar más bonito. +typedef bife::Widget* create_t(const std::string& name, const bife::Hash&); +typedef void destroy_t(bife::Widget*); + namespace bife { using std::string; @@ -30,6 +34,9 @@ namespace bife { WidgetStack stack; // TODO Fallback. + create_t* fb_create; + destroy_t* fb_destroy; + public: /// Widget attributes. Widget* root; diff --git a/parser_test.cpp b/parser_test.cpp index 705b165..992d6fe 100644 --- a/parser_test.cpp +++ b/parser_test.cpp @@ -27,6 +27,9 @@ int main(int argc, char* argv[]) { } catch (exception e) { cerr << "Error: " << e.what() << endl; return 1; + } catch (string e) { + cerr << "Error: " << e << endl; + return 2; } return 0; } diff --git a/translate_loader.cpp b/translate_loader.cpp new file mode 100644 index 0000000..e122fb7 --- /dev/null +++ b/translate_loader.cpp @@ -0,0 +1,20 @@ +#include "translate.h" +#include "widget.h" +#include "hash.h" + +using bife::Translate; +using bife::Widget; +using bife::Hash; +using std::string; + +// the types of the class factories +typedef bife::Widget* create_t(const string& name, const Hash&); +typedef void destroy_t(Widget*); + +// the class factories +extern "C" Widget* create(const string& name, const Hash& attrs) { + return new Translate(name, attrs); +} +extern "C" void destroy(Widget* w) { + delete w; +} -- 2.43.0