Skip to content

File stringView.h

File List > jac > machine > stringView.h

Go to the documentation of this file

#pragma once

#include <quickjs.h>
#include <string>
#include <string_view>

#include "context.h"


namespace jac {


class StringView : public std::basic_string_view<char> {
    using basic_string_view = std::basic_string_view<char>;

    ContextRef _ctx = nullptr;
public:
    StringView(StringView &&other) : basic_string_view(other), _ctx(other._ctx) {
        other._ctx = nullptr;
    }

    StringView &operator=(StringView &&other) {
        if (_ctx) {
            JS_FreeCString(_ctx, data());
        }
        basic_string_view::operator=(other);
        _ctx = other._ctx;
        other._ctx = nullptr;
        return *this;
    }

    StringView(const basic_string_view &other) = delete;
    StringView(const StringView &other) = delete;
    StringView &operator=(const StringView &other) = delete;

    const char* c_str() const {
        return data();
    }

    StringView() = default;

    StringView(ContextRef ctx, const char* str) : basic_string_view(str), _ctx(ctx) {}

    ~StringView() {
        if (_ctx) {
            JS_FreeCString(_ctx, data());
        }
    }

    operator std::string() const {
        return std::string(data(), size());
    }
};


}  // namespace jac