LCOV - code coverage report
Current view: top level - variable/include/scipp/variable - string.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 8 8 100.0 %
Date: 2024-04-28 01:25:40 Functions: 4 5 80.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: BSD-3-Clause
       2             : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
       3             : /// @file
       4             : /// @author Simon Heybrock
       5             : #pragma once
       6             : 
       7             : #include <map>
       8             : #include <string>
       9             : 
      10             : #include "scipp-variable_export.h"
      11             : #include "scipp/common/index.h"
      12             : #include "scipp/variable/variable.h"
      13             : 
      14             : namespace scipp::variable {
      15             : 
      16          28 : template <class T> std::string format_variable_like(const T &obj) {
      17          56 :   auto s =
      18             :       "(dims=" + to_string(obj.dims()) + ", dtype=" + to_string(obj.dtype());
      19          28 :   if (obj.unit() != units::none)
      20          28 :     s += ", unit=" + to_string(obj.unit());
      21          56 :   return s + ')';
      22          28 : }
      23             : 
      24             : SCIPP_VARIABLE_EXPORT std::ostream &operator<<(std::ostream &os,
      25             :                                                const Variable &variable);
      26             : 
      27             : SCIPP_VARIABLE_EXPORT std::string to_string(const Variable &variable);
      28             : SCIPP_VARIABLE_EXPORT std::string
      29             : to_string(const std::pair<Dim, Variable> &coord);
      30             : SCIPP_VARIABLE_EXPORT std::string
      31             : to_string(const std::pair<std::string, Variable> &attr);
      32             : 
      33             : SCIPP_VARIABLE_EXPORT std::string
      34             : format_variable(const Variable &variable,
      35             :                 const std::optional<Sizes> &datasetSizes = std::nullopt);
      36             : SCIPP_VARIABLE_EXPORT std::string
      37             : format_variable_compact(const Variable &variable);
      38             : 
      39             : /// Abstract base class for formatters for variables with element types not in
      40             : /// scipp-variable module.
      41             : class SCIPP_VARIABLE_EXPORT AbstractFormatter {
      42             : public:
      43          25 :   virtual ~AbstractFormatter() = default;
      44             :   [[nodiscard]] virtual std::string format(const Variable &var) const = 0;
      45             : };
      46             : 
      47             : /// Concrete class for formatting variables with element types not in
      48             : /// scipp-variable.
      49             : template <class T> class Formatter : public AbstractFormatter {
      50             :   [[nodiscard]] std::string format(const Variable &var) const override;
      51             : };
      52             : 
      53             : /// Registry of formatters.
      54             : ///
      55             : /// Modules instantiating variables with custom dtype can call `emplace` to
      56             : /// register a formatter.
      57             : class SCIPP_VARIABLE_EXPORT FormatterRegistry {
      58             : public:
      59           5 :   FormatterRegistry() = default;
      60             :   FormatterRegistry(const FormatterRegistry &) = delete;
      61             :   FormatterRegistry &operator=(const FormatterRegistry &) = delete;
      62             :   void emplace(const DType key, std::unique_ptr<AbstractFormatter> formatter);
      63             :   [[nodiscard]] bool contains(const DType key) const noexcept;
      64             :   [[nodiscard]] std::string format(const Variable &var) const;
      65             : 
      66             : private:
      67             :   std::map<DType, std::unique_ptr<AbstractFormatter>> m_formatters;
      68             : };
      69             : 
      70             : /// Return the global FormatterRegistry instrance
      71             : SCIPP_VARIABLE_EXPORT FormatterRegistry &formatterRegistry();
      72             : 
      73             : } // namespace scipp::variable

Generated by: LCOV version 1.14