Skip to content

Commit

Permalink
HTML rendering stage 1
Browse files Browse the repository at this point in the history
  • Loading branch information
RanolP committed Mar 17, 2020
1 parent 2b8e682 commit 0df41d6
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"iostream": "cpp"
}
}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,8 @@ Read like *spike*.
- spaic

The core runtime

## Build Requirements

- emsdk
[*install guide](https://emscripten.org/docs/getting_started/downloads.html)
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ foreach( source ${SOURCE_LIST} )
PRIVATE
Spaic
SpaicCSS
SpaicDOM
)
endforeach( source ${SOURCE_LIST} )
21 changes: 18 additions & 3 deletions examples/Counter.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <cstdlib>
#include <iostream>
#include <string>

#include <spaic/Prelude.hpp>
#include <spaic-css/Prelude.hpp>
#include <spaic-dom/Render.hpp>

namespace props::counter
{
Expand Down Expand Up @@ -48,10 +50,23 @@ auto counter = create_component(props::counter::all, state::counter::all, update
int main()
{
using namespace props::counter;
auto wtf_is_this_style = css(
width = 10.0_px);

auto rendered = counter(is_dark_theme = true)("Hello, world!", 1, 2.0);
auto node = counter(is_dark_theme = true)("Hello, world!", 1, 2.0);
std::string test = "12345678";
std::vector<VNode> test2;
test2.push_back("1");
test2.push_back(2);
test2.push_back(3.0);

std::cout << spaic::dom::render(node) << std::endl;
std::cout << spaic::dom::render(nullptr) << std::endl;
std::cout << spaic::dom::render(NULL) << std::endl;
std::cout << spaic::dom::render(123456) << std::endl;
std::cout << spaic::dom::render(123.456) << std::endl;
std::cout << spaic::dom::render(true) << std::endl;
std::cout << spaic::dom::render(false) << std::endl;
std::cout << spaic::dom::render("WAAAY") << std::endl;
std::cout << spaic::dom::render(test2) << std::endl;

return EXIT_SUCCESS;
}
9 changes: 9 additions & 0 deletions include/spaic-dom/Render.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <string>
#include <spaic/VNode.hpp>

namespace spaic::dom
{
std::string render(spaic::vnode::VNode node);
}
26 changes: 21 additions & 5 deletions include/spaic/Component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,41 @@
#include <spaic/Property.hpp>
#include <spaic/State.hpp>

namespace spaic::vnode
{
class VNode;
}
namespace spaic::comp
{
using NativeNodeName = std::optional<const char *>;

class ComponentNode
class ComponentParent
{
public:
const NativeNodeName native_node_name;
const std::vector<spaic::vnode::VNode> children;

ComponentParent(NativeNodeName native_node_name, std::vector<spaic::vnode::VNode> children);
};
class ComponentBody
class ComponentSingle
{
public:
const NativeNodeName native_node_name;

ComponentSingle(NativeNodeName native_node_name);

template <typename... T>
ComponentNode operator()(T... children) noexcept;
ComponentParent operator()(T... children) noexcept;
};
template <typename Props>
class Component
{
public:
Component();
const NativeNodeName native_node_name;

Component(NativeNodeName native_node_name);
template <typename... T>
ComponentBody operator()(T... args) noexcept;
ComponentSingle operator()(T... args) noexcept;
};
} // namespace spaic::comp

Expand Down
17 changes: 7 additions & 10 deletions include/spaic/VNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace spaic::vnode
{
// TODO: Array of VNode should be VNode.
using VNodeBase = std::variant<
spaic::comp::ComponentBody,
spaic::comp::ComponentNode,
spaic::comp::ComponentSingle,
spaic::comp::ComponentParent,
std::string,
const char *,
std::nullptr_t,
bool,
short,
Expand All @@ -34,16 +34,13 @@ using VNodeBase = std::variant<

class VNode final
{
private:
// I prefer `_value`
// wtf is that noexcept(noexcept())
std::variant<VNodeBase, std::vector<VNode>> _value;

public:
const std::variant<VNodeBase, std::vector<VNode>> value;

template <typename T>
requires std::is_convertible_v<std::remove_reference_t<T>, VNodeBase> ||
std::is_convertible_v<std::remove_reference_t<T>, std::vector<VNode>>
VNode(T &&v) noexcept(noexcept(std::is_constructible_v<decltype(_value), decltype(std::forward<T>(v))>))
: _value(std::forward<T>(v)) {}
VNode(T &&v) noexcept(noexcept(std::is_constructible_v<decltype(value), decltype(std::forward<T>(v))>))
: value(std::forward<T>(v)) {}
};
} // namespace spaic::vnode
12 changes: 6 additions & 6 deletions include/spaic/detail/Component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
namespace spaic::comp
{
template <typename... T>
ComponentNode ComponentBody::operator()(T... children) noexcept
ComponentParent ComponentSingle::operator()(T... children) noexcept
{
// TODO: ComponentBody::operator()(children)
return ComponentNode();
// TODO: ComponentSingle::operator()(children)
return ComponentParent(this->native_node_name, {children...});
}

template <typename Props>
Component<Props>::Component()
Component<Props>::Component(NativeNodeName native_node_name) : native_node_name(native_node_name)
{
}
template <typename Props>
template <typename... T>
ComponentBody Component<Props>::operator()(T... args) noexcept
ComponentSingle Component<Props>::operator()(T... args) noexcept
{
// TODO: Component::operator()(args)
return ComponentBody();
return ComponentSingle(this->native_node_name);
}
} // namespace spaic::comp
2 changes: 1 addition & 1 deletion include/spaic/detail/ComponentPart.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ template <typename Props, typename StateSet>
Component<Props> create_component(Props props, StateSet state, Update update, Render render)
{
// TODO: create_component(props, state, update, render)
return Component<Props>();
return Component<Props>(std::nullopt);
}
} // namespace spaic::comp
90 changes: 90 additions & 0 deletions src/spaic-dom/Render.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <string>
#include <spaic-dom/Render.hpp>
#include <spaic/VNode.hpp>
#include <type_traits>
#include <variant>
#include <sstream>
#include <vector>

namespace spaic::dom
{

template <class... Ts>
struct overloaded : Ts...
{
using Ts::operator()...;
};
template <class... Ts>
overloaded(Ts...)->overloaded<Ts...>;

std::string render_base(spaic::vnode::VNodeBase node)
{
std::ostringstream stream;
std::visit(
overloaded{
[&](std::nullptr_t &arg) {
// do nothing, nullptr means empty string.
},
[&](std::string &arg) { stream << arg; },
[&](const char *&arg) { stream << arg; },
[&](spaic::comp::ComponentParent &arg) {
if (arg.native_node_name)
{
stream << "<" << *arg.native_node_name << ">\n";
}
for (const auto &child : arg.children)
{
stream << render(child) << "\n";
}
if (arg.native_node_name)
{
stream << "</" << *arg.native_node_name << ">\n";
}
},
[&](spaic::comp::ComponentSingle &arg) { stream << typeid(std::decay_t<decltype(arg)>).name() << "(ComponentSingle)"; },
[&](auto arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, bool>)
{
stream << std::boolalpha << arg << std::noboolalpha;
}
else if constexpr (std::is_fundamental_v<T>)
{
stream << arg;
}
else
{
stream << "비야네 당신은 틀렸어!" << typeid(T).name();
}
},
},
node);
return stream.str();
}
std::string render(spaic::vnode::VNode node)
{
std::ostringstream stream;
std::visit(
[&](auto &&arg) {
// wtf is that fucking bitches
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::vector<spaic::vnode::VNode>>)
{
for (const auto &element : arg)
{
stream << render(element);
}
}
else if constexpr (std::is_same_v<T, spaic::vnode::VNodeBase>)
{
stream << render_base(arg);
}
else
{
stream << "wtf is that you should report this: " << typeid(T).name();
}
},
node.value);
return stream.str();
}
} // namespace spaic::dom
Empty file removed src/spaic-dom/Test.cpp
Empty file.
9 changes: 9 additions & 0 deletions src/spaic/Component.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <spaic/Component.hpp>
#include <spaic/VNode.hpp>

namespace spaic::comp
{
ComponentSingle::ComponentSingle(NativeNodeName native_node_name) : native_node_name(native_node_name) {}

ComponentParent::ComponentParent(NativeNodeName native_node_name, std::vector<spaic::vnode::VNode> children) : native_node_name(native_node_name), children(children) {}
} // namespace spaic::comp

0 comments on commit 0df41d6

Please sign in to comment.