Line data Source code
1 : /* A very simple string class.
2 : Copyright (C) 2015-2026 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 1254173 : label_text ()
33 938414 : : m_buffer (NULL), m_owned (false)
34 879720 : {}
35 :
36 76657388 : ~label_text ()
37 : {
38 76622626 : if (m_owned)
39 359133 : free (m_buffer);
40 802 : }
41 :
42 : /* Move ctor. */
43 55273805 : label_text (label_text &&other)
44 55273933 : : m_buffer (other.m_buffer), m_owned (other.m_owned)
45 : {
46 55273933 : other.release ();
47 : }
48 :
49 : /* Move assignment. */
50 4938676 : label_text & operator= (label_text &&other)
51 : {
52 4938676 : if (m_owned)
53 10 : free (m_buffer);
54 4938676 : m_buffer = other.m_buffer;
55 4938676 : m_owned = other.m_owned;
56 4938676 : other.release ();
57 4938676 : 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 75298078 : static label_text borrow (const char *buffer)
67 : {
68 75298078 : return label_text (const_cast <char *> (buffer), false);
69 : }
70 :
71 : /* Create a label_text instance that takes ownership of BUFFER. */
72 383238 : static label_text take (char *buffer)
73 : {
74 383238 : return label_text (buffer, true);
75 : }
76 :
77 60221411 : void release ()
78 : {
79 60221411 : m_buffer = NULL;
80 55282735 : m_owned = false;
81 : }
82 :
83 168810272 : const char *get () const
84 : {
85 168794271 : return m_buffer;
86 : }
87 :
88 8802 : bool is_owner () const
89 : {
90 8802 : return m_owned;
91 : }
92 :
93 : private:
94 : char *m_buffer;
95 : bool m_owned;
96 :
97 75681316 : label_text (char *buffer, bool owned)
98 75329997 : : m_buffer (buffer), m_owned (owned)
99 : {}
100 : };
101 :
102 : #endif /* !LIBCPP_LABEL_TEXT_H */
|