Line data Source code
1 : //
2 : // Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
3 : //
4 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 : //
7 : // Official repository: https://github.com/cppalliance/capy
8 : //
9 :
10 : #ifndef BOOST_CAPY_EMBED_HPP
11 : #define BOOST_CAPY_EMBED_HPP
12 :
13 : #include <boost/capy/detail/config.hpp>
14 : #include <boost/core/detail/string_view.hpp>
15 :
16 : #if __cpp_lib_string_view >= 201606L
17 : # include <string_view>
18 : #endif
19 :
20 : namespace boost {
21 : namespace capy {
22 :
23 : /** Embed a string literal as a string_view
24 :
25 : The embed class template is used to embed a string literal
26 : in source code as a string_view. The first character of
27 : the string literal will be removed, typically this is a
28 : newline, allowing the string to be formatted nicely in code.
29 :
30 : @par Example
31 : @code
32 : embed text(R"(
33 : Hello "world"
34 : This has quotes and )
35 : )");
36 : core::string_view sv = text.get();
37 : @endcode
38 : The resulting string_view `sv` will contain:
39 : ```
40 : Hello "world"
41 : This has quotes and )
42 : ```
43 : */
44 : struct embed
45 : {
46 : /** Constructor
47 : The string literal `s` should be a raw string literal.
48 : The first character (typically a newline) will be
49 : removed from the resulting string_view.
50 : @param s The string literal
51 : */
52 : template<std::size_t N>
53 : constexpr
54 : embed(
55 : const char (&s)[N]) noexcept
56 : : s_(s + 1, N - 2)
57 : {
58 : }
59 :
60 : /** Conversion to string_view
61 : */
62 1 : operator core::string_view() const noexcept
63 : {
64 1 : return s_;
65 : }
66 :
67 : #if __cpp_lib_string_view >= 201606L
68 : /** Conversion to std::string_view
69 : */
70 : operator std::string_view() const noexcept
71 : {
72 : return std::string_view(s_.data(), s_.size());
73 : }
74 : #endif
75 :
76 : /** Return the string_view
77 : */
78 : core::string_view
79 1 : get() const noexcept
80 : {
81 1 : return s_;
82 : }
83 :
84 : /** Dereference operator
85 : */
86 : core::string_view
87 1 : operator*() const noexcept
88 : {
89 1 : return s_;
90 : }
91 :
92 : /** Member access operator
93 : */
94 : core::string_view const*
95 1 : operator->() const noexcept
96 : {
97 1 : return &s_;
98 : }
99 :
100 : private:
101 : core::string_view s_;
102 : };
103 :
104 : } // capy
105 : } // boost
106 :
107 : #endif
|