|
| 1 | +// |
| 2 | +// Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 3 | +// See https://llvm.org/LICENSE.txt for license information. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | +// |
| 6 | +// Copyright (c) 2023 Vinnie Falco ([email protected]) |
| 7 | +// |
| 8 | +// Official repository: https://github.com/cppalliance/mrdox |
| 9 | +// |
| 10 | + |
| 11 | +#ifndef MRDOX_SUPPORT_DOM_HPP |
| 12 | +#define MRDOX_SUPPORT_DOM_HPP |
| 13 | + |
| 14 | +#include <mrdox/Platform.hpp> |
| 15 | +#include <cstdint> |
| 16 | +#include <memory> |
| 17 | +#include <string_view> |
| 18 | +#include <type_traits> |
| 19 | +#include <utility> |
| 20 | + |
| 21 | +namespace clang { |
| 22 | +namespace mrdox { |
| 23 | +namespace dom { |
| 24 | + |
| 25 | +class Array; |
| 26 | +class Object; |
| 27 | +class Value; |
| 28 | + |
| 29 | +/** The type of data in a Value. |
| 30 | +*/ |
| 31 | +enum class Kind |
| 32 | +{ |
| 33 | + Object, |
| 34 | + Array, |
| 35 | + String, |
| 36 | + Integer, |
| 37 | + Boolean, |
| 38 | + Null |
| 39 | +}; |
| 40 | + |
| 41 | +//------------------------------------------------ |
| 42 | + |
| 43 | +class MRDOX_DECL |
| 44 | + Array |
| 45 | +{ |
| 46 | +public: |
| 47 | + struct Impl |
| 48 | + { |
| 49 | + virtual ~Impl() = 0; |
| 50 | + virtual std::size_t length() const noexcept = 0; |
| 51 | + virtual Value get(std::size_t) const = 0; |
| 52 | + }; |
| 53 | + |
| 54 | + explicit |
| 55 | + Array(std::shared_ptr<Impl> impl) noexcept |
| 56 | + : impl_(std::move(impl)) |
| 57 | + { |
| 58 | + } |
| 59 | + |
| 60 | + std::size_t length() const noexcept |
| 61 | + { |
| 62 | + return impl_->length(); |
| 63 | + } |
| 64 | + |
| 65 | + Value get(std::size_t index) const; |
| 66 | + |
| 67 | +private: |
| 68 | + std::shared_ptr<Impl> impl_; |
| 69 | +}; |
| 70 | + |
| 71 | +//------------------------------------------------ |
| 72 | + |
| 73 | +class MRDOX_DECL |
| 74 | + Object |
| 75 | +{ |
| 76 | +public: |
| 77 | + struct MRDOX_DECL |
| 78 | + Impl |
| 79 | + { |
| 80 | + virtual ~Impl() = 0; |
| 81 | + virtual bool empty() const noexcept; |
| 82 | + virtual Value get(std::string_view) const = 0; |
| 83 | + }; |
| 84 | + |
| 85 | + explicit |
| 86 | + Object(std::shared_ptr<Impl> impl) noexcept |
| 87 | + : impl_(std::move(impl)) |
| 88 | + { |
| 89 | + } |
| 90 | + |
| 91 | + bool empty() const noexcept |
| 92 | + { |
| 93 | + return impl_->empty(); |
| 94 | + } |
| 95 | + |
| 96 | + Value get(std::string_view key) const; |
| 97 | + |
| 98 | +private: |
| 99 | + std::shared_ptr<Impl> impl_; |
| 100 | +}; |
| 101 | + |
| 102 | +//------------------------------------------------ |
| 103 | + |
| 104 | +class MRDOX_DECL |
| 105 | + Value |
| 106 | +{ |
| 107 | + Kind kind_; |
| 108 | + |
| 109 | + union |
| 110 | + { |
| 111 | + std::int64_t number_; |
| 112 | + std::string string_; |
| 113 | + Object object_; |
| 114 | + Array array_; |
| 115 | + }; |
| 116 | + |
| 117 | +public: |
| 118 | + ~Value(); |
| 119 | + Value() noexcept; |
| 120 | + Value(bool b) noexcept; |
| 121 | + Value(Array arr) noexcept; |
| 122 | + Value(Object arr) noexcept; |
| 123 | + Value(std::nullptr_t) noexcept; |
| 124 | + Value(char const* string) noexcept; |
| 125 | + Value(std::string_view s) noexcept; |
| 126 | + Value(std::string string) noexcept; |
| 127 | + |
| 128 | + template<class T> |
| 129 | + requires std::is_integral_v<T> |
| 130 | + Value(T number) noexcept |
| 131 | + { |
| 132 | + if constexpr(std::is_same_v<T, bool>) |
| 133 | + kind_ = Kind::Boolean; |
| 134 | + else |
| 135 | + kind_ = Kind::Integer; |
| 136 | + number_ = static_cast<std::int64_t>(number); |
| 137 | + } |
| 138 | + |
| 139 | + bool isBool() const noexcept { return kind_ == Kind::Boolean; } |
| 140 | + bool isArray() const noexcept { return kind_ == Kind::Array; } |
| 141 | + bool isObject() const noexcept { return kind_ == Kind::Object; } |
| 142 | + bool isNull() const noexcept { return kind_ == Kind::Null; } |
| 143 | + bool isInteger() const noexcept { return kind_ == Kind::Integer; } |
| 144 | + bool isString() const noexcept { return kind_ == Kind::String; } |
| 145 | + |
| 146 | + bool isTruthy() const noexcept; |
| 147 | + |
| 148 | + bool getBool() const noexcept |
| 149 | + { |
| 150 | + MRDOX_ASSERT(kind_ == Kind::Boolean); |
| 151 | + return number_ == 0; |
| 152 | + } |
| 153 | + |
| 154 | + Array const& getArray() const noexcept |
| 155 | + { |
| 156 | + MRDOX_ASSERT(kind_ == Kind::Array); |
| 157 | + return array_; |
| 158 | + } |
| 159 | + |
| 160 | + Object const& getObject() const noexcept |
| 161 | + { |
| 162 | + MRDOX_ASSERT(kind_ == Kind::Object); |
| 163 | + return object_; |
| 164 | + } |
| 165 | + |
| 166 | + std::int64_t getInteger() const noexcept |
| 167 | + { |
| 168 | + MRDOX_ASSERT(kind_ == Kind::Integer); |
| 169 | + return number_; |
| 170 | + } |
| 171 | + |
| 172 | + std::string_view getString() const noexcept |
| 173 | + { |
| 174 | + MRDOX_ASSERT(kind_ == Kind::String); |
| 175 | + return string_; |
| 176 | + } |
| 177 | +}; |
| 178 | + |
| 179 | +//------------------------------------------------ |
| 180 | + |
| 181 | +inline |
| 182 | +Value |
| 183 | +Array:: |
| 184 | +get( |
| 185 | + std::size_t index) const |
| 186 | +{ |
| 187 | + return impl_->get(index); |
| 188 | +} |
| 189 | + |
| 190 | +inline |
| 191 | +Value |
| 192 | +Object:: |
| 193 | +get( |
| 194 | + std::string_view key) const |
| 195 | +{ |
| 196 | + return impl_->get(key); |
| 197 | +} |
| 198 | + |
| 199 | +//------------------------------------------------ |
| 200 | + |
| 201 | +template<class T, class... Args> |
| 202 | +requires std::derived_from<T, Array::Impl> |
| 203 | +Array |
| 204 | +makeArray(Args&&... args) |
| 205 | +{ |
| 206 | + return Array(std::make_shared<T>( |
| 207 | + std::forward<Args>(args)...)); |
| 208 | +} |
| 209 | + |
| 210 | +template<class T, class... Args> |
| 211 | +requires std::derived_from<T, Object::Impl> |
| 212 | +Object |
| 213 | +makeObject(Args&&... args) |
| 214 | +{ |
| 215 | + return Object(std::make_shared<T>( |
| 216 | + std::forward<Args>(args)...)); |
| 217 | +} |
| 218 | + |
| 219 | +} // dom |
| 220 | +} // mrdox |
| 221 | +} // clang |
| 222 | + |
| 223 | +#endif |
0 commit comments