cstr.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #ifndef CSTR_H
  14. #define CSTR_H
  15. #include "unicode/unistr.h"
  16. #include "unicode/uobject.h"
  17. #include "unicode/utypes.h"
  18. #include "charstr.h"
  19. /**
  20. * ICU-internal class CStr, a small helper class to facilitate passing UnicodeStrings
  21. * to functions needing (const char *) strings, such as printf().
  22. *
  23. * It is intended primarily for use in debugging or in tests. Uses platform
  24. * default code page conversion, which will do the best job possible,
  25. * but may be lossy, depending on the platform.
  26. *
  27. * If no other conversion is available, use invariant conversion and substitue
  28. * '?' for non-invariant characters.
  29. *
  30. * Example Usage:
  31. * UnicodeString s = whatever;
  32. * printf("%s", CStr(s)());
  33. *
  34. * The explicit call to the CStr() constructor creates a temporary object.
  35. * Operator () on the temporary object returns a (const char *) pointer.
  36. * The lifetime of the (const char *) data is that of the temporary object,
  37. * which works well when passing it as a parameter to another function, such as printf.
  38. */
  39. U_NAMESPACE_BEGIN
  40. class U_COMMON_API CStr : public UMemory {
  41. public:
  42. CStr(const UnicodeString &in);
  43. ~CStr();
  44. const char * operator ()() const;
  45. private:
  46. CharString s;
  47. CStr(const CStr &other); // Forbid copying of this class.
  48. CStr &operator =(const CStr &other); // Forbid assignment.
  49. };
  50. U_NAMESPACE_END
  51. #endif