Skip to content

Commit

Permalink
Structure the concept of APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
RanolP committed Feb 23, 2020
1 parent b6f0446 commit 8961949
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 11 deletions.
20 changes: 16 additions & 4 deletions examples/Counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
namespace props::counter
{
auto is_dark_theme = fallback<bool>(false);
}

auto all = is_dark_theme;
} // namespace props::counter
namespace state::counter
{
State<int> count;
}

auto all = count;
} // namespace state::counter
namespace msg
{
Message<int> increment;
Expand All @@ -22,19 +26,27 @@ ShouldRender update()
using namespace msg;
if (increment)
{
count += increment.get<0>();
*count += increment.get<0>();
return true;
}
if (decrement)
{
count -= decrement.get<0>();
*count -= decrement.get<0>();
}
return true;
}

VNode render()
{
return 0;
}

auto counter = create_component(props::counter::all, state::counter::all, update, render);

int main()
{
using namespace props::counter;
counter(is_dark_theme = true)("Hello, world!");
// new Counter(is_dark_theme = true);
// Counter{is_dark_theme = true};
// Counter(is_dark_theme = true);
Expand Down
25 changes: 19 additions & 6 deletions include/spaic/Component.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
#pragma once

#include <functional>
#include <variant>
#include <spaic/VNode.hpp>
#include <spaic/Property.hpp>
#include <spaic/State.hpp>

namespace spaic::comp
{
using ShouldRender = bool;
template <typename State, typename Msg>
using Update = std::function(Msg)->ShouldRender;
using Update = std::function<ShouldRender()>;
using Render = std::function<spaic::vnode::VNode()>;

template <typename Props, typename State>
class ComponentBody;
template <typename Props>
class Component
{
public:
template <typename... T>
ComponentBody operator()(T... args) noexcept;
};
template <typename Props, typename State>
Component<Props, State> create_component(Props props, State state);
} // namespace spaic
class ComponentBody
{
public:
spaic::vnode::VNode operator()(spaic::vnode::VNode children...) noexcept;
};
template <typename Props, typename StateSet>
Component<Props> create_component(Props props, StateSet state, Update update, Render render);
} // namespace spaic::comp
4 changes: 3 additions & 1 deletion include/spaic/Message.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <tuple>

namespace spaic::msg
{
template <typename... T>
Expand All @@ -11,7 +13,7 @@ class Message
operator bool();

template <size_t I>
std::tuple_element<I, T...> get();
typename std::tuple_element<I, std::tuple<T...>>::type get();
};

} // namespace spaic::msg
1 change: 1 addition & 0 deletions include/spaic/Prelude.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ using namespace spaic::props;
using namespace spaic::msg;
using namespace spaic::state;
using namespace spaic::comp;
using namespace spaic::vnode;
3 changes: 3 additions & 0 deletions include/spaic/State.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ class State
{
public:
State() {}

// todo:
T &operator*();
};
} // namespace spaic::state
30 changes: 30 additions & 0 deletions include/spaic/VNode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <variant>
#include <string>
#include <vector>

namespace spaic::vnode
{
using VNode = std::variant<
std::string,
std::nullptr_t,
bool,
short,
unsigned short,
int,
unsigned int,
long,
unsigned long,
long long,
unsigned long long,
char,
signed char,
unsigned char,
wchar_t,
char16_t,
char32_t,
float,
double,
long double>;
} // namespace spaic::vnode

0 comments on commit 8961949

Please sign in to comment.