1 | /* A very simple string class. |
2 | Copyright (C) 2015-2025 Free Software Foundation, Inc. |
3 | |
4 | This program is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU General Public License as published by the |
6 | Free Software Foundation; either version 3, or (at your option) any |
7 | later version. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License |
15 | along with this program; see the file COPYING3. If not see |
16 | <http://www.gnu.org/licenses/>. |
17 | |
18 | In other words, you are welcome to use, share and improve this program. |
19 | You are forbidden to forbid anyone else to use, share and improve |
20 | what you give them. Help stamp out software-hoarding! */ |
21 | |
22 | #ifndef LIBCPP_LABEL_TEXT_H |
23 | #define LIBCPP_LABEL_TEXT_H |
24 | |
25 | /* A struct for the result of range_label::get_text: a NUL-terminated buffer |
26 | of localized text, and a flag to determine if the caller should "free" the |
27 | buffer. */ |
28 | |
29 | class label_text |
30 | { |
31 | public: |
32 | label_text () |
33 | : m_buffer (NULL), m_owned (false) |
34 | {} |
35 | |
36 | ~label_text () |
37 | { |
38 | if (m_owned) |
39 | free (ptr: m_buffer); |
40 | } |
41 | |
42 | /* Move ctor. */ |
43 | label_text (label_text &&other) |
44 | : m_buffer (other.m_buffer), m_owned (other.m_owned) |
45 | { |
46 | other.release (); |
47 | } |
48 | |
49 | /* Move assignment. */ |
50 | label_text & operator= (label_text &&other) |
51 | { |
52 | if (m_owned) |
53 | free (ptr: m_buffer); |
54 | m_buffer = other.m_buffer; |
55 | m_owned = other.m_owned; |
56 | other.release (); |
57 | return *this; |
58 | } |
59 | |
60 | /* Delete the copy ctor and copy-assignment operator. */ |
61 | label_text (const label_text &) = delete; |
62 | label_text & operator= (const label_text &) = delete; |
63 | |
64 | /* Create a label_text instance that borrows BUFFER from a |
65 | longer-lived owner. */ |
66 | static label_text borrow (const char *buffer) |
67 | { |
68 | return label_text (const_cast <char *> (buffer), false); |
69 | } |
70 | |
71 | /* Create a label_text instance that takes ownership of BUFFER. */ |
72 | static label_text take (char *buffer) |
73 | { |
74 | return label_text (buffer, true); |
75 | } |
76 | |
77 | void release () |
78 | { |
79 | m_buffer = NULL; |
80 | m_owned = false; |
81 | } |
82 | |
83 | const char *get () const |
84 | { |
85 | return m_buffer; |
86 | } |
87 | |
88 | bool is_owner () const |
89 | { |
90 | return m_owned; |
91 | } |
92 | |
93 | private: |
94 | char *m_buffer; |
95 | bool m_owned; |
96 | |
97 | label_text (char *buffer, bool owned) |
98 | : m_buffer (buffer), m_owned (owned) |
99 | {} |
100 | }; |
101 | |
102 | #endif /* !LIBCPP_LABEL_TEXT_H */ |
103 | |