From cd96a31b3ea11edf26512c33ca599258601c31a0 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 10 Aug 2024 10:50:47 -0400 Subject: move library to a folder --- attr/dimensions.hpp | 14 ------------ attr/ondraw.hpp | 21 ----------------- attr/position.hpp | 14 ------------ attr/string.hpp | 24 -------------------- find.hpp | 57 ----------------------------------------------- main.cpp | 9 +++++--- mbuoy/attr/dimensions.hpp | 14 ++++++++++++ mbuoy/attr/ondraw.hpp | 21 +++++++++++++++++ mbuoy/attr/position.hpp | 14 ++++++++++++ mbuoy/attr/string.hpp | 24 ++++++++++++++++++++ mbuoy/find.hpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++ mbuoy/port.hpp | 18 +++++++++++++++ mbuoy/ui/button.hpp | 37 ++++++++++++++++++++++++++++++ mbuoy/ui/label.hpp | 32 ++++++++++++++++++++++++++ mbuoy/view.hpp | 29 ++++++++++++++++++++++++ port.hpp | 18 --------------- ui/button.hpp | 37 ------------------------------ ui/label.hpp | 32 -------------------------- view.hpp | 29 ------------------------ 19 files changed, 252 insertions(+), 249 deletions(-) delete mode 100644 attr/dimensions.hpp delete mode 100644 attr/ondraw.hpp delete mode 100644 attr/position.hpp delete mode 100644 attr/string.hpp delete mode 100644 find.hpp create mode 100644 mbuoy/attr/dimensions.hpp create mode 100644 mbuoy/attr/ondraw.hpp create mode 100644 mbuoy/attr/position.hpp create mode 100644 mbuoy/attr/string.hpp create mode 100644 mbuoy/find.hpp create mode 100644 mbuoy/port.hpp create mode 100644 mbuoy/ui/button.hpp create mode 100644 mbuoy/ui/label.hpp create mode 100644 mbuoy/view.hpp delete mode 100644 port.hpp delete mode 100644 ui/button.hpp delete mode 100644 ui/label.hpp delete mode 100644 view.hpp diff --git a/attr/dimensions.hpp b/attr/dimensions.hpp deleted file mode 100644 index 9c1fda9..0000000 --- a/attr/dimensions.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef MBUOY_ATTR_DIMENSIONS_HPP -#define MBUOY_ATTR_DIMENSIONS_HPP - -namespace mbuoy { - -struct dimensions -{ - int width, height; -}; - -} // namespace mbuoy - -#endif // MBUOY_ATTR_DIMENSIONS_HPP - diff --git a/attr/ondraw.hpp b/attr/ondraw.hpp deleted file mode 100644 index d0932ef..0000000 --- a/attr/ondraw.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef MBUOY_ATTR_ONDRAW_HPP -#define MBUOY_ATTR_ONDRAW_HPP - -#include "position.hpp" -#include "dimensions.hpp" - -namespace mbuoy { - -struct ondraw -{ - void (*func)(const position&, const dimensions&); - - void operator()(const position& pos, const dimensions& dim) const noexcept { - func(pos, dim); - } -}; - -} // namespace mbuoy - -#endif // MBUOY_ATTR_ONDRAW_HPP - diff --git a/attr/position.hpp b/attr/position.hpp deleted file mode 100644 index dfb9cc7..0000000 --- a/attr/position.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef MBUOY_ATTR_POSITION_HPP -#define MBUOY_ATTR_POSITION_HPP - -namespace mbuoy { - -struct position -{ - int x, y; -}; - -} // namespace mbuoy - -#endif // MBUOY_ATTR_POSITION_HPP - diff --git a/attr/string.hpp b/attr/string.hpp deleted file mode 100644 index d7e1fcb..0000000 --- a/attr/string.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef MBUOY_ATTR_STRING_HPP -#define MBUOY_ATTR_STRING_HPP - -namespace mbuoy { - -template -struct string -{ - consteval string(const char (&s_)[N]) { - for (int i = 0; i < N; ++i) - s[i] = s_[i]; - } - - consteval operator const char *() const { - return s; - } - - char s[N]; -}; - -} // namespace mbuoy - -#endif // MBUOY_ATTR_STRING_HPP - diff --git a/find.hpp b/find.hpp deleted file mode 100644 index edba06d..0000000 --- a/find.hpp +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef MBUOY_FIND_HPP -#define MBUOY_FIND_HPP - -#include -#include - -namespace mbuoy { - -template -concept can_static_cast = requires(U u) { - static_cast(u); -}; - -template -consteval std::optional find(T val) -{ - return val; -} - -template - requires (!std::is_same_v && can_static_cast) -consteval std::optional find(U val) -{ - return val; -} - -template - requires (!std::is_same_v && !can_static_cast) -consteval std::optional find(U val) -{ - return {}; -} - -template -consteval std::optional find(T val, auto... vals) -{ - return val; -} - -template - requires (!std::is_same_v && can_static_cast) -consteval std::optional find(U val, auto... vals) -{ - return val; -} - -template - requires (!std::is_same_v && !can_static_cast) -consteval auto find(U val, auto... vals) -{ - return find(vals...); -} - -} // namespace mbuoy - -#endif // MBUOY_FIND_HPP - diff --git a/main.cpp b/main.cpp index 590f1ed..25c6512 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,8 @@ -#include "view.hpp" -#include "ui/button.hpp" -#include "ui/label.hpp" +#include "mbuoy/view.hpp" +#include "mbuoy/ui/button.hpp" +#include "mbuoy/ui/label.hpp" + +#include void drawButton(const mbuoy::position& pos, const mbuoy::dimensions& dim) { @@ -21,6 +23,7 @@ int main() mbuoy::dimensions(40, 40), mbuoy::ondraw(drawButton)>{} >; + test.render(); } diff --git a/mbuoy/attr/dimensions.hpp b/mbuoy/attr/dimensions.hpp new file mode 100644 index 0000000..9c1fda9 --- /dev/null +++ b/mbuoy/attr/dimensions.hpp @@ -0,0 +1,14 @@ +#ifndef MBUOY_ATTR_DIMENSIONS_HPP +#define MBUOY_ATTR_DIMENSIONS_HPP + +namespace mbuoy { + +struct dimensions +{ + int width, height; +}; + +} // namespace mbuoy + +#endif // MBUOY_ATTR_DIMENSIONS_HPP + diff --git a/mbuoy/attr/ondraw.hpp b/mbuoy/attr/ondraw.hpp new file mode 100644 index 0000000..d0932ef --- /dev/null +++ b/mbuoy/attr/ondraw.hpp @@ -0,0 +1,21 @@ +#ifndef MBUOY_ATTR_ONDRAW_HPP +#define MBUOY_ATTR_ONDRAW_HPP + +#include "position.hpp" +#include "dimensions.hpp" + +namespace mbuoy { + +struct ondraw +{ + void (*func)(const position&, const dimensions&); + + void operator()(const position& pos, const dimensions& dim) const noexcept { + func(pos, dim); + } +}; + +} // namespace mbuoy + +#endif // MBUOY_ATTR_ONDRAW_HPP + diff --git a/mbuoy/attr/position.hpp b/mbuoy/attr/position.hpp new file mode 100644 index 0000000..dfb9cc7 --- /dev/null +++ b/mbuoy/attr/position.hpp @@ -0,0 +1,14 @@ +#ifndef MBUOY_ATTR_POSITION_HPP +#define MBUOY_ATTR_POSITION_HPP + +namespace mbuoy { + +struct position +{ + int x, y; +}; + +} // namespace mbuoy + +#endif // MBUOY_ATTR_POSITION_HPP + diff --git a/mbuoy/attr/string.hpp b/mbuoy/attr/string.hpp new file mode 100644 index 0000000..d7e1fcb --- /dev/null +++ b/mbuoy/attr/string.hpp @@ -0,0 +1,24 @@ +#ifndef MBUOY_ATTR_STRING_HPP +#define MBUOY_ATTR_STRING_HPP + +namespace mbuoy { + +template +struct string +{ + consteval string(const char (&s_)[N]) { + for (int i = 0; i < N; ++i) + s[i] = s_[i]; + } + + consteval operator const char *() const { + return s; + } + + char s[N]; +}; + +} // namespace mbuoy + +#endif // MBUOY_ATTR_STRING_HPP + diff --git a/mbuoy/find.hpp b/mbuoy/find.hpp new file mode 100644 index 0000000..edba06d --- /dev/null +++ b/mbuoy/find.hpp @@ -0,0 +1,57 @@ +#ifndef MBUOY_FIND_HPP +#define MBUOY_FIND_HPP + +#include +#include + +namespace mbuoy { + +template +concept can_static_cast = requires(U u) { + static_cast(u); +}; + +template +consteval std::optional find(T val) +{ + return val; +} + +template + requires (!std::is_same_v && can_static_cast) +consteval std::optional find(U val) +{ + return val; +} + +template + requires (!std::is_same_v && !can_static_cast) +consteval std::optional find(U val) +{ + return {}; +} + +template +consteval std::optional find(T val, auto... vals) +{ + return val; +} + +template + requires (!std::is_same_v && can_static_cast) +consteval std::optional find(U val, auto... vals) +{ + return val; +} + +template + requires (!std::is_same_v && !can_static_cast) +consteval auto find(U val, auto... vals) +{ + return find(vals...); +} + +} // namespace mbuoy + +#endif // MBUOY_FIND_HPP + diff --git a/mbuoy/port.hpp b/mbuoy/port.hpp new file mode 100644 index 0000000..366f498 --- /dev/null +++ b/mbuoy/port.hpp @@ -0,0 +1,18 @@ +#ifndef MBUOY_PORT_HPP +#define MBUOY_PORT_HPP + +#include + +namespace mbuoy { +namespace port { + +inline void puts(int x, int y, const char *s) +{ + std::printf("[%d, %d]: %s\n", x, y, s); +} + +} // namespace port +} // namespace mbuoy + +#endif // MBUOY_PORT_HPP + diff --git a/mbuoy/ui/button.hpp b/mbuoy/ui/button.hpp new file mode 100644 index 0000000..c6e5b66 --- /dev/null +++ b/mbuoy/ui/button.hpp @@ -0,0 +1,37 @@ +#ifndef MBUOY_UI_BUTTON_HPP +#define MBUOY_UI_BUTTON_HPP + +#include "../attr/dimensions.hpp" +#include "../attr/ondraw.hpp" +#include "../attr/position.hpp" +#include "../attr/string.hpp" +#include "../find.hpp" +#include "../port.hpp" + +namespace mbuoy { + +template +struct button +{ + static constexpr auto point = find(Attr...); + static constexpr auto dims = find(Attr...); + static constexpr auto text = find(Attr...); + static constexpr auto draw = find(Attr...); + + static consteval void init(unsigned char *(&ptr)) { + *ptr++ = 0; // pressed? + } + + static void render() { + (*draw)(*point, *dims); + } + + static consteval int size() { + return 1; + } +}; + +} // namespace mbuoy + +#endif // MBUOY_UI_BUTTON_HPP + diff --git a/mbuoy/ui/label.hpp b/mbuoy/ui/label.hpp new file mode 100644 index 0000000..c6cc581 --- /dev/null +++ b/mbuoy/ui/label.hpp @@ -0,0 +1,32 @@ +#ifndef MBUOY_UI_LABEL_HPP +#define MBUOY_UI_LABEL_HPP + +#include "../attr/position.hpp" +#include "../attr/string.hpp" +#include "../find.hpp" +#include "../port.hpp" + +namespace mbuoy { + +template +struct label +{ + static constexpr auto point = find(Attr...); + static constexpr auto text = find(Attr...); + + static consteval void init(unsigned char *(&ptr)) { + } + + static void render() { + port::puts(point->x, point->y, *text); + } + + static consteval int size() { + return 0; + } +}; + +} // namespace mbuoy + +#endif // MBUOY_UI_LABEL_HPP + diff --git a/mbuoy/view.hpp b/mbuoy/view.hpp new file mode 100644 index 0000000..29915fd --- /dev/null +++ b/mbuoy/view.hpp @@ -0,0 +1,29 @@ +#ifndef MBUOY_VIEW_HPP +#define MBUOY_VIEW_HPP + +namespace mbuoy { + +template +class view_t +{ +public: + consteval view_t() { + auto ptr = data; + (Objs.init(ptr), ...); + } + + void render() { + (Objs.render(), ...); + } + +private: + unsigned char data[(0 + ... + Objs.size())] = {}; +}; + +template +constinit auto view = view_t(); + +} // namespace mbuoy + +#endif // MBUOY_VIEW_HPP + diff --git a/port.hpp b/port.hpp deleted file mode 100644 index 366f498..0000000 --- a/port.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef MBUOY_PORT_HPP -#define MBUOY_PORT_HPP - -#include - -namespace mbuoy { -namespace port { - -inline void puts(int x, int y, const char *s) -{ - std::printf("[%d, %d]: %s\n", x, y, s); -} - -} // namespace port -} // namespace mbuoy - -#endif // MBUOY_PORT_HPP - diff --git a/ui/button.hpp b/ui/button.hpp deleted file mode 100644 index 4a0a9f5..0000000 --- a/ui/button.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef MBUOY_UI_BUTTON_HPP -#define MBUOY_UI_BUTTON_HPP - -#include "attr/dimensions.hpp" -#include "attr/ondraw.hpp" -#include "attr/position.hpp" -#include "attr/string.hpp" -#include "find.hpp" -#include "port.hpp" - -namespace mbuoy { - -template -struct button -{ - static constexpr auto point = find(Attr...); - static constexpr auto dims = find(Attr...); - static constexpr auto text = find(Attr...); - static constexpr auto draw = find(Attr...); - - static consteval void init(unsigned char *(&ptr)) { - *ptr++ = 0; // pressed? - } - - static void render() { - (*draw)(*point, *dims); - } - - static consteval int size() { - return 1; - } -}; - -} // namespace mbuoy - -#endif // MBUOY_UI_BUTTON_HPP - diff --git a/ui/label.hpp b/ui/label.hpp deleted file mode 100644 index 9524007..0000000 --- a/ui/label.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef MBUOY_UI_LABEL_HPP -#define MBUOY_UI_LABEL_HPP - -#include "attr/position.hpp" -#include "attr/string.hpp" -#include "find.hpp" -#include "port.hpp" - -namespace mbuoy { - -template -struct label -{ - static constexpr auto point = find(Attr...); - static constexpr auto text = find(Attr...); - - static consteval void init(unsigned char *(&ptr)) { - } - - static void render() { - port::puts(point->x, point->y, *text); - } - - static consteval int size() { - return 0; - } -}; - -} // namespace mbuoy - -#endif // MBUOY_UI_LABEL_HPP - diff --git a/view.hpp b/view.hpp deleted file mode 100644 index 29915fd..0000000 --- a/view.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef MBUOY_VIEW_HPP -#define MBUOY_VIEW_HPP - -namespace mbuoy { - -template -class view_t -{ -public: - consteval view_t() { - auto ptr = data; - (Objs.init(ptr), ...); - } - - void render() { - (Objs.render(), ...); - } - -private: - unsigned char data[(0 + ... + Objs.size())] = {}; -}; - -template -constinit auto view = view_t(); - -} // namespace mbuoy - -#endif // MBUOY_VIEW_HPP - -- cgit v1.2.3