Program Listing for File xpath.hpp

Return to documentation for file (zeem/xpath.hpp)

// Copyright (c) 2024-2026 Maarten L. Hekkelman
// SPDX-License-Identifier: BSD-2-Clause

#pragma once

#ifndef ZEEM_CXX_MODULE
# include <memory>
# include <string>
# include <string_view>
# include <type_traits>
# include <vector>
#endif

namespace zeem
{

class node;

// --------------------------------------------------------------------

ZEEM_EXPORT class context final
{
  public:
    context();

    context(const context &ctxt) = default;

    context(context &&ctxt) noexcept
    {
        std::swap(m_impl, ctxt.m_impl);
    }

    context &operator=(context ctxt)
    {
        std::swap(m_impl, ctxt.m_impl);
        return *this;
    }

    void set(const std::string &name, std::string value);

    void set(const std::string &name, double value);

    template <typename T>
        requires std::is_same_v<T, std::string> or std::is_same_v<T, double>
    T get(std::string name);

  private:
    friend class xpath;

    std::shared_ptr<struct context_imp> m_impl;
};

// --------------------------------------------------------------------

ZEEM_EXPORT class xpath final
{
  public:
    explicit xpath(std::string_view path);

    xpath(const xpath &rhs) = default;

    xpath(xpath &&rhs) noexcept
    {
        std::swap(m_impl, rhs.m_impl);
    }

    xpath &operator=(xpath xp)
    {
        std::swap(m_impl, xp.m_impl);
        return *this;
    }

    template <typename T>
    [[nodiscard]] std::vector<T *> evaluate(const node &root, const context &ctxt = {}) const;

    bool matches(const node *n, const context &ctxt = {}) const;

  private:
    std::shared_ptr<class expression> m_impl;
};

} // namespace zeem