Create factory allocator

This commit is contained in:
James Whiteman 2022-01-10 18:56:46 -08:00
parent af0d9b895c
commit cea3710c76
8 changed files with 53 additions and 18 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
link/ link/
.vscode/

5
command/linux/archive.sh Executable file
View File

@ -0,0 +1,5 @@
F_ROOT=$(readlink -f "$0")
D_ROOT=$(dirname "${F_ROOT}")/../../.
F_ARCHIVE=/tmp/cpp_launchpad.tar
cd ${D_ROOT}
git archive --format=tar -o ${F_ARCHIVE} HEAD && echo "Saved archive to ${F_ARCHIVE}"

View File

@ -1,10 +1,19 @@
#include "controller/root.h" #include "act/root.h"
#include "service/window.h" #include "draw/window.h"
#include <stdlib.h>
root_controller_t::configuration_t::configuration_t(std::vector<root_controller_t::mode_t> modes_p) {
modes_m = modes_p;
}
root_controller_t::root_controller_t(root_controller_t::configuration_t configuration_p) { root_controller_t::root_controller_t(root_controller_t::configuration_t configuration_p) {
configuration_m = configuration_p; configuration_m = configuration_p;
} }
root_controller_t root_controller_factory_t::create(root_controller_t::configuration_t configuration_p) { root_controller_t* root_controller_factory_t::create(root_controller_t::configuration_t configuration_p) {
return new root_controller_t(configuration_p); void* ptr = malloc(sizeof(root_controller_t));
return static_cast<root_controller_t*>(ptr);
}
factory_t::status_t root_controller_factory_t::dispose(root_controller_t* root_controller_p) {
free(root_controller_p);
return factory_t::status_t::ok;
} }
controller_t::status_t root_controller_t::on_event(controller_t::event_t event_p) { controller_t::status_t root_controller_t::on_event(controller_t::event_t event_p) {
if ( if (

View File

@ -1,22 +1,30 @@
#ifndef ACT_ROOT #ifndef ACT_ROOT
#define ACT_ROOT #define ACT_ROOT
#include "controller/controller.h" #include "act/controller.h"
#include "allocate/factory.h" #include "allocate/factory.h"
#include <vector>
class root_controller_t : public controller_t { class root_controller_t : public controller_t {
public: public:
enum mode_t {
cli,
app,
game
};
class configuration_t { class configuration_t {
private: private:
mode_t mode_m; std::vector<mode_t> modes_m;
public: public:
configuration_t(mode_t mode_p); configuration_t(std::vector<mode_t> modes_p);
}; };
controller_t::status_t on_event(event_t event_p) override; controller_t::status_t on_event(event_t event_p) override;
private: private:
configuration_t configuration_m;
root_controller_t(configuration_t configuration_p); root_controller_t(configuration_t configuration_p);
friend class root_controller_factory_t; friend class root_controller_factory_t;
}; };
class root_controller_factory_t : public factory_t<root_controller_t::configuration_t, root_controller_t> { class root_controller_factory_t : public factory_t<root_controller_t::configuration_t, root_controller_t> {
public: public:
root_controller_t create(root_controller_t::configuration_t configuration_p) override; root_controller_t* create(root_controller_t::configuration_t* configuration_p) override;
factory_t::status_t dispose(root_controller_t* root_controller_p) override;
}; };
#endif #endif

View File

@ -1,6 +1,13 @@
#ifndef ALLOCATE_FACTORY #ifndef ALLOCATE_FACTORY
#define ALLOCATE_FACTORY #define ALLOCATE_FACTORY
template <class I, class O> class factory_t { template <class I, class O> class factory_t {
virtual O* create(I*) = 0; public:
enum status_t {
ok,
error
};
private:
virtual O* create(I* i) = 0;
virtual status_t dispose(O* o) = 0;
}; };
#endif #endif

View File

@ -1,8 +1,13 @@
#include "controller/controller.h" #include "act/controller.h"
#include "controller/root.h" #include "act/root.h"
#include <vector>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
controller_t* controller = new root_controller_t(); root_controller_factory_t factory_l;
controller_t::status_t status = controller->on_event(controller_t::event_t::start); std::vector<root_controller_t::mode_t> modes_l;
delete controller; modes_l.push_back(root_controller_t::mode_t::app);
return status; root_controller_t::configuration_t configuration_l(modes_l);
root_controller_t* controller_l = factory_l.create(&configuration_l);
controller_t::status_t status_l = controller_l->on_event(controller_t::event_t::start);
factory_l.dispose(controller_l);
return status_l;
} }

View File

@ -1,3 +1,3 @@
main.cpp main.cpp
controller/root.cpp act/root.cpp
platform/linux/service/window.cpp platform/linux/draw/window.cpp

View File

@ -1,4 +1,4 @@
#include "service/window.h" #include "draw/window.h"
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>