1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html |
3 | /* |
4 | ****************************************************************************** |
5 | * |
6 | * Copyright (C) 2016, International Business Machines |
7 | * Corporation and others. All Rights Reserved. |
8 | * |
9 | ****************************************************************************** |
10 | * |
11 | * File: cstr.h |
12 | */ |
13 | |
14 | #ifndef CSTR_H |
15 | #define CSTR_H |
16 | |
17 | #include "unicode/unistr.h" |
18 | #include "unicode/uobject.h" |
19 | #include "unicode/utypes.h" |
20 | |
21 | #include "charstr.h" |
22 | |
23 | /** |
24 | * ICU-internal class CStr, a small helper class to facilitate passing UnicodeStrings |
25 | * to functions needing (const char *) strings, such as printf(). |
26 | * |
27 | * It is intended primarily for use in debugging or in tests. Uses platform |
28 | * default code page conversion, which will do the best job possible, |
29 | * but may be lossy, depending on the platform. |
30 | * |
31 | * If no other conversion is available, use invariant conversion and substitute |
32 | * '?' for non-invariant characters. |
33 | * |
34 | * Example Usage: |
35 | * UnicodeString s = whatever; |
36 | * printf("%s", CStr(s)()); |
37 | * |
38 | * The explicit call to the CStr() constructor creates a temporary object. |
39 | * Operator () on the temporary object returns a (const char *) pointer. |
40 | * The lifetime of the (const char *) data is that of the temporary object, |
41 | * which works well when passing it as a parameter to another function, such as printf. |
42 | */ |
43 | |
44 | U_NAMESPACE_BEGIN |
45 | |
46 | class U_COMMON_API CStr : public UMemory { |
47 | public: |
48 | CStr(const UnicodeString &in); |
49 | ~CStr(); |
50 | const char * operator ()() const; |
51 | |
52 | private: |
53 | CharString s; |
54 | CStr(const CStr &other) = delete; // Forbid copying of this class. |
55 | CStr &operator =(const CStr &other) = delete; // Forbid assignment. |
56 | }; |
57 | |
58 | U_NAMESPACE_END |
59 | |
60 | #endif |
61 | |