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 : 1214741 : label_text ()
33 : 902074 : : m_buffer (NULL), m_owned (false)
34 : 843009 : {}
35 : :
36 : 74441712 : ~label_text ()
37 : : {
38 : 74408426 : if (m_owned)
39 : 355251 : free (m_buffer);
40 : 223 : }
41 : :
42 : : /* Move ctor. */
43 : 53917900 : label_text (label_text &&other)
44 : 53917900 : : m_buffer (other.m_buffer), m_owned (other.m_owned)
45 : : {
46 : 53917900 : other.release ();
47 : : }
48 : :
49 : : /* Move assignment. */
50 : 4634387 : label_text & operator= (label_text &&other)
51 : : {
52 : 4634387 : if (m_owned)
53 : 10 : free (m_buffer);
54 : 4634387 : m_buffer = other.m_buffer;
55 : 4634387 : m_owned = other.m_owned;
56 : 4634387 : other.release ();
57 : 4634387 : 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 : 73123851 : static label_text borrow (const char *buffer)
67 : : {
68 : 73123851 : return label_text (const_cast <char *> (buffer), false);
69 : : }
70 : :
71 : : /* Create a label_text instance that takes ownership of BUFFER. */
72 : 378874 : static label_text take (char *buffer)
73 : : {
74 : 378874 : return label_text (buffer, true);
75 : : }
76 : :
77 : 58561207 : void release ()
78 : : {
79 : 58561207 : m_buffer = NULL;
80 : 53926820 : m_owned = false;
81 : : }
82 : :
83 : 162792514 : const char *get () const
84 : : {
85 : 162776044 : return m_buffer;
86 : : }
87 : :
88 : 8920 : bool is_owner () const
89 : : {
90 : 8920 : return m_owned;
91 : : }
92 : :
93 : : private:
94 : : char *m_buffer;
95 : : bool m_owned;
96 : :
97 : 73502725 : label_text (char *buffer, bool owned)
98 : 73497781 : : m_buffer (buffer), m_owned (owned)
99 : : {}
100 : : };
101 : :
102 : : #endif /* !LIBCPP_LABEL_TEXT_H */
|