aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/include/CStringView.h
blob: af8d683e769dcc5aed1e5b6135713ff0e9812e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once
#include <string>

namespace vcpkg
{
    template<class CharType>
    struct BasicCStringView
    {
        constexpr BasicCStringView() : cstr(nullptr) {}
        constexpr BasicCStringView(const CharType* cstr) : cstr(cstr) {}
        constexpr BasicCStringView(const BasicCStringView&) = default;
        BasicCStringView(const std::basic_string<CharType>& str) : cstr(str.c_str()) {}

        constexpr operator const CharType*() const { return cstr; }
        constexpr const CharType* c_str() const { return cstr; }

    private:
        const CharType* cstr;
    };

    using CStringView = BasicCStringView<char>;
    using CWStringView = BasicCStringView<wchar_t>;

    inline const char* to_printf_arg(const CStringView string_view) { return string_view.c_str(); }

    inline const wchar_t* to_wprintf_arg(const CWStringView string_view) { return string_view.c_str(); }

    static_assert(sizeof(CStringView) == sizeof(void*), "CStringView must be a simple wrapper around char*");
    static_assert(sizeof(CWStringView) == sizeof(void*), "CWStringView must be a simple wrapper around wchar_t*");
}