|
| 1 | +// Copyright 2026 PISA Developers |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <concepts> |
| 16 | +#include <cstdint> |
| 17 | +#include <memory> |
| 18 | +#include <ostream> |
| 19 | +#include <sstream> |
| 20 | + |
| 21 | +namespace pisa { |
| 22 | + |
| 23 | +namespace detail { |
| 24 | + class StatsBuilderInterface { |
| 25 | + public: |
| 26 | + virtual void add(std::string const& key, bool value) = 0; |
| 27 | + virtual void add(std::string const& key, std::int64_t value) = 0; |
| 28 | + virtual void add(std::string const& key, std::uint64_t value) = 0; |
| 29 | + virtual void add(std::string const& key, double value) = 0; |
| 30 | + virtual void add(std::string const& key, const char* value) = 0; |
| 31 | + virtual void add(std::string const& key, std::string value) = 0; |
| 32 | + [[nodiscard]] virtual auto build() const -> std::string = 0; |
| 33 | + }; |
| 34 | +} // namespace detail |
| 35 | + |
| 36 | +template <typename T> |
| 37 | +concept Streamable = requires(std::ostream& os, T value) { |
| 38 | + { os << value } -> std::convertible_to<std::ostream&>; |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * Builds a simple key-value JSON for printing statistics. |
| 43 | + */ |
| 44 | +class StatsBuilder { |
| 45 | + private: |
| 46 | + std::unique_ptr<::pisa::detail::StatsBuilderInterface> m_impl; |
| 47 | + |
| 48 | + explicit StatsBuilder(std::unique_ptr<::pisa::detail::StatsBuilderInterface> impl); |
| 49 | + |
| 50 | + public: |
| 51 | + StatsBuilder(); |
| 52 | + auto add(std::string const& key, bool value) -> StatsBuilder&; |
| 53 | + auto add(std::string const& key, int value) -> StatsBuilder&; |
| 54 | + auto add(std::string const& key, unsigned int value) -> StatsBuilder&; |
| 55 | + auto add(std::string const& key, long value) -> StatsBuilder&; |
| 56 | + auto add(std::string const& key, unsigned long value) -> StatsBuilder&; |
| 57 | + auto add(std::string const& key, double value) -> StatsBuilder&; |
| 58 | + auto add(std::string const& key, const char* value) -> StatsBuilder&; |
| 59 | + auto add(std::string const& key, std::string value) -> StatsBuilder&; |
| 60 | + auto add(std::string const& key, std::string_view value) -> StatsBuilder&; |
| 61 | + |
| 62 | + template <typename T> |
| 63 | + requires Streamable<T> |
| 64 | + auto add(std::string const& key, T const& value) -> StatsBuilder& { |
| 65 | + std::ostringstream out; |
| 66 | + out << value; |
| 67 | + add(key, out.str()); |
| 68 | + return *this; |
| 69 | + } |
| 70 | + |
| 71 | + /** Builds the JSON. */ |
| 72 | + [[nodiscard]] auto build() const -> std::string; |
| 73 | +}; |
| 74 | + |
| 75 | +[[nodiscard]] auto stats_builder() -> StatsBuilder; |
| 76 | + |
| 77 | +} // namespace pisa |
0 commit comments