nsPrintfCString.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsPrintfCString_h___
  6. #define nsPrintfCString_h___
  7. #include "nsString.h"
  8. /**
  9. * nsPrintfCString lets you create a nsCString using a printf-style format
  10. * string. For example:
  11. *
  12. * NS_WARNING(nsPrintfCString("Unexpected value: %f", 13.917).get());
  13. *
  14. * nsPrintfCString has a small built-in auto-buffer. For larger strings, it
  15. * will allocate on the heap.
  16. *
  17. * See also nsCString::AppendPrintf().
  18. */
  19. class nsPrintfCString : public nsFixedCString
  20. {
  21. typedef nsCString string_type;
  22. public:
  23. explicit nsPrintfCString(const char_type* aFormat, ...)
  24. : nsFixedCString(mLocalBuffer, kLocalBufferSize, 0)
  25. {
  26. va_list ap;
  27. va_start(ap, aFormat);
  28. AppendPrintf(aFormat, ap);
  29. va_end(ap);
  30. }
  31. private:
  32. static const uint32_t kLocalBufferSize = 16;
  33. char_type mLocalBuffer[kLocalBufferSize];
  34. };
  35. #endif // !defined(nsPrintfCString_h___)