C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
compiler.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "../../abi/json.hpp"
26#include "../../abi/vector.hpp"
27
28#include <concepts>
29#include <cstddef>
30#include <cstdint>
31#include <memory>
32#include <string_view>
33#include <type_traits>
34#include <utility>
35
36namespace essence::globalization::abstract {
40 class compiler {
41 public:
42 template <typename T>
43 requires(!std::same_as<std::decay_t<T>, compiler>)
44 explicit compiler(T&& value) : wrapper_{std::make_shared<wrapper<T>>(std::forward<T>(value))} {}
45
50 [[nodiscard]] std::uint32_t version() const {
51 return wrapper_->version();
52 }
53
59 void to_file(const abi::json& json, std::string_view path) const {
60 wrapper_->to_file(json, path);
61 }
62
68 [[nodiscard]] abi::vector<std::byte> to_bytes(const abi::json& json) const {
69 return wrapper_->to_bytes(json);
70 }
71
77 [[nodiscard]] abi::string to_base64(const abi::json& json) const {
78 return wrapper_->to_base64(json);
79 }
80
81 private:
82 struct base {
83 virtual ~base() = default;
84 virtual std::uint32_t version() = 0;
85 virtual void to_file(const abi::json& json, std::string_view path) = 0;
86 virtual abi::vector<std::byte> to_bytes(const abi::json& json) = 0;
87 virtual abi::string to_base64(const abi::json& json) = 0;
88 };
89
90 template <typename T>
91 class wrapper final : public base {
92 public:
93 template <std::convertible_to<T> U>
94 explicit wrapper(U&& value) : value_{std::forward<U>(value)} {}
95
96 std::uint32_t version() override {
97 return value_.version();
98 }
99
100 void to_file(const abi::json& json, std::string_view path) override {
101 value_.to_file(json, path);
102 }
103
104 abi::vector<std::byte> to_bytes(const abi::json& json) override {
105 return value_.to_bytes(json);
106 }
107
108 abi::string to_base64(const abi::json& json) override {
109 return value_.to_base64(json);
110 }
111
112 private:
113 T value_;
114 };
115
116 std::shared_ptr<base> wrapper_;
117 };
118} // namespace essence::globalization::abstract
namespace for Niels Lohmann
Definition json.hpp:19415
A compiler to translate globalized texts into particular binary sequences.
Definition compiler.hpp:40
std::uint32_t version() const
Gets the version of the compiler.
Definition compiler.hpp:50
abi::string to_base64(const abi::json &json) const
Compiles a JSON value into a base64 string.
Definition compiler.hpp:77
abi::vector< std::byte > to_bytes(const abi::json &json) const
Compiles a JSON value into a byte array.
Definition compiler.hpp:68
void to_file(const abi::json &json, std::string_view path) const
Compiles a JSON value into a language file.
Definition compiler.hpp:59