diff --git a/.gitignore b/.gitignore index 8688e5b..4e9c87a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ build/ -bin/ -samples/HelloWorld/bin -samples/HelloWorld/build \ No newline at end of file +bin/ \ No newline at end of file diff --git a/Crafter.CppDOM-Bindings.cppm b/Crafter.CppDOM-Bindings.cppm new file mode 100644 index 0000000..92d0b00 --- /dev/null +++ b/Crafter.CppDOM-Bindings.cppm @@ -0,0 +1,16 @@ +module; +#include +export module Crafter.CppDOM:Bindings; + + +export namespace Crafter::CppDOM::Bindings { + __attribute__((import_module("env"), import_name("getElementById"))) void* GetElementById(const char* id, std::size_t idLenght); + inline void* GetElementById(const std::string& id) { + return GetElementById(id.c_str(), id.size()); + } + __attribute__((import_module("env"), import_name("setInnerHTML"))) void SetInnerHTML(void* ptr, const char* html, std::size_t htmlLenght); + inline void SetInnerHTML(void* ptr, const std::string& html) { + SetInnerHTML(ptr, html.c_str(), html.size()); + } + __attribute__((import_module("env"), import_name("freeJs"))) void FreeJs(void* ptr); +} diff --git a/Crafter.CppDOM-HtmlElement.cppm b/Crafter.CppDOM-HtmlElement.cppm new file mode 100644 index 0000000..f9c79ae --- /dev/null +++ b/Crafter.CppDOM-HtmlElement.cppm @@ -0,0 +1,26 @@ +module; +#include +export module Crafter.CppDOM:HtmlElement; +import :Bindings; + +namespace Crafter::CppDOM { + export class HtmlElement { + public: + void* const ptr; + inline HtmlElement(const char* id, std::size_t idLenght): ptr(Bindings::GetElementById(id, idLenght)) { + + } + inline HtmlElement(const std::string& id): ptr(Bindings::GetElementById(id)) { + + } + inline void SetInnerHTML(const char* html, std::size_t htmlLenght) { + Bindings::SetInnerHTML(ptr, html, htmlLenght); + } + inline void SetInnerHTML(const std::string& html) { + Bindings::SetInnerHTML(ptr, html); + } + inline ~HtmlElement(){ + Bindings::FreeJs(ptr); + } + }; +} \ No newline at end of file diff --git a/Crafter.CppDOM.cppm b/Crafter.CppDOM.cppm index 566b036..6e8c63b 100644 --- a/Crafter.CppDOM.cppm +++ b/Crafter.CppDOM.cppm @@ -18,12 +18,9 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -module; -#include -#include -#include -#include export module Crafter.CppDOM; +export import :Bindings; +export import :HtmlElement; extern "C" { void __cxa_allocate_exception() { @@ -34,24 +31,3 @@ extern "C" { } } - - - -export namespace Crafter::CppDOM::Bindings { - __attribute__((import_module("env"), import_name("getElementById"))) void* GetElementById(const char* id, std::size_t idLenght); - inline void* GetElementById(const std::string& id){ - return GetElementById(id.c_str(), id.size()); - } - __attribute__((import_module("env"), import_name("setInnerHTML"))) void SetInnerHTML(void* ptr, const char* html, std::size_t htmlLenght); - inline void SetInnerHTML(void* ptr, const std::string& html){ - SetInnerHTML(ptr, html.c_str(), html.size()); - } - __attribute__((import_module("env"), import_name("freeJs"))) void FreeJs(void* ptr); - // __attribute__((import_module("env"), import_name("strokeRect"))) void StrokeRect(std::uint64_t id, std::uint32_t x, std::uint32_t y, std::uint32_t width, std::uint32_t height, const char* color, std::size_t colorLenght); - // __attribute__((import_module("env"), import_name("putImageData"))) void PutImageData(std::uint64_t id, Crafter::Web::Pixel* buffer, std::uint32_t width, std::uint32_t height); - // __attribute__((import_module("env"), import_name("fetch"))) void Fetch(void* obj, const char* url, std::size_t urlCallback, char* buffer, void (*listener)(void* obj, std::uint32_t, char*)); - // __attribute__((import_module("env"), import_name("setTimeout"))) void SetTimeout(void* obj, std::uint32_t time, void (*listener)(void* obj)); - // __attribute__((import_module("env"), import_name("draw"))) void Draw(std::uint64_t id, std::uint32_t x, std::uint32_t y, const char* text, std::uint32_t textLenght); - //__attribute__((import_module("env"), import_name("addEventListener"))) void AddEventListener(std::string id, std::string eventId, void (*listener)(void)); -} - diff --git a/README.md b/README.md index 07f6f69..f550563 100644 --- a/README.md +++ b/README.md @@ -38,13 +38,13 @@ Create a basic project file, that describes your web project. Save and close the file, create a ``main.cpp`` ```cpp import Crafter.CppDOM; +using namespace Crafter::CppDOM; int main(){ - void* body = Crafter::CppDOM::Bindings::GetElementById("body"); - Crafter::CppDOM::Bindings::SetInnerHTML(body, "Hello World!"); - Crafter::CppDOM::Bindings::FreeJs(body); + HtmlElement body("body"); + body.SetInnerHTML("Hello World!"); } ``` Save and close, then run ``crafter-webbuild serve -c debug``. Now you can open the browser at ``http://localhost:8080/`` and ``Hello World!`` will appear in the browser. -This sample can also be viewed in the [Hello World sample](https://github.com/Catcrafts/Crafter.CppDOM/tree/master/samples/HelloWorld) +This sample can also be viewed in the [HelloElement sample](https://github.com/Catcrafts/Crafter.CppDOM/tree/master/samples/HelloElement) diff --git a/project.json b/project.json index 66ef5c2..9de4828 100644 --- a/project.json +++ b/project.json @@ -5,7 +5,7 @@ "name": "base", "standard": "c++26", "source_files": [], - "module_files": ["Crafter.CppDOM"], + "module_files": ["Crafter.CppDOM-HtmlElement", "Crafter.CppDOM", "Crafter.CppDOM-Bindings"], "additional_files": ["Crafter.CppDOM.js"], "build_dir": "./build", "output_dir": "./bin", diff --git a/samples/HelloWorld/main.cpp b/samples/HelloWorld/main.cpp index d7d87b6..1fe57d6 100644 --- a/samples/HelloWorld/main.cpp +++ b/samples/HelloWorld/main.cpp @@ -1,7 +1,8 @@ import Crafter.CppDOM; +using namespace Crafter::CppDOM::Bindings; int main(){ - void* body = Crafter::CppDOM::Bindings::GetElementById("body"); - Crafter::CppDOM::Bindings::SetInnerHTML(body, "Hello World!"); - Crafter::CppDOM::Bindings::FreeJs(body); + void* body = GetElementById("body"); + SetInnerHTML(body, "Hello World!"); + FreeJs(body); } \ No newline at end of file diff --git a/samples/HelloWorld/project.json b/samples/HelloWorld/project.json index fbdde2b..de6c4fb 100644 --- a/samples/HelloWorld/project.json +++ b/samples/HelloWorld/project.json @@ -14,7 +14,7 @@ "type": "executable", "dependencies": [ { - "path":"/home/jorijn/repos/Crafter.CppDOM/project.json", + "path":"../../project.json", "configuration":"debug" } ]