Branch data Line data Source code
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 : 1207271 : label_text ()
33 : 895105 : : m_buffer (NULL), m_owned (false)
34 : 836278 : {}
35 : :
36 : 82722930 : ~label_text ()
37 : : {
38 : 82689666 : if (m_owned)
39 : 352207 : free (m_buffer);
40 : 218 : }
41 : :
42 : : /* Move ctor. */
43 : 59722433 : label_text (label_text &&other)
44 : 59722433 : : m_buffer (other.m_buffer), m_owned (other.m_owned)
45 : : {
46 : 59722433 : other.release ();
47 : : }
48 : :
49 : : /* Move assignment. */
50 : 5127115 : label_text & operator= (label_text &&other)
51 : : {
52 : 5127115 : if (m_owned)
53 : 10 : free (m_buffer);
54 : 5127115 : m_buffer = other.m_buffer;
55 : 5127115 : m_owned = other.m_owned;
56 : 5127115 : other.release ();
57 : 5127115 : 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 : 81415061 : static label_text borrow (const char *buffer)
67 : : {
68 : 81415061 : return label_text (const_cast <char *> (buffer), false);
69 : : }
70 : :
71 : : /* Create a label_text instance that takes ownership of BUFFER. */
72 : 375819 : static label_text take (char *buffer)
73 : : {
74 : 375819 : return label_text (buffer, true);
75 : : }
76 : :
77 : 64858467 : void release ()
78 : : {
79 : 64858467 : m_buffer = NULL;
80 : 59731352 : m_owned = false;
81 : : }
82 : :
83 : 184587689 : const char *get () const
84 : : {
85 : 184571341 : return m_buffer;
86 : : }
87 : :
88 : 8919 : bool is_owner () const
89 : : {
90 : 8919 : return m_owned;
91 : : }
92 : :
93 : : private:
94 : : char *m_buffer;
95 : : bool m_owned;
96 : :
97 : 81790880 : label_text (char *buffer, bool owned)
98 : 81785936 : : m_buffer (buffer), m_owned (owned)
99 : : {}
100 : : };
101 : :
102 : : #endif /* !LIBCPP_LABEL_TEXT_H */
|