stringprintf.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: Sanjay Ghemawat
  30. //
  31. // Printf variants that place their output in a C++ string.
  32. //
  33. // Usage:
  34. // string result = StringPrintf("%d %s\n", 10, "hello");
  35. // SStringPrintf(&result, "%d %s\n", 10, "hello");
  36. // StringAppendF(&result, "%d %s\n", 20, "there");
  37. #ifndef CERES_INTERNAL_STRINGPRINTF_H_
  38. #define CERES_INTERNAL_STRINGPRINTF_H_
  39. #include <cstdarg>
  40. #include <string>
  41. #include "ceres/internal/disable_warnings.h"
  42. #include "ceres/internal/export.h"
  43. namespace ceres::internal {
  44. #if (defined(__GNUC__) || defined(__clang__))
  45. // Tell the compiler to do printf format string checking if the compiler
  46. // supports it; see the 'format' attribute in
  47. // <http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html>.
  48. //
  49. // N.B.: As the GCC manual states, "[s]ince non-static C++ methods
  50. // have an implicit 'this' argument, the arguments of such methods
  51. // should be counted from two, not one."
  52. #define CERES_PRINTF_ATTRIBUTE(string_index, first_to_check) \
  53. __attribute__((__format__(__printf__, string_index, first_to_check)))
  54. #define CERES_SCANF_ATTRIBUTE(string_index, first_to_check) \
  55. __attribute__((__format__(__scanf__, string_index, first_to_check)))
  56. #else
  57. #define CERES_PRINTF_ATTRIBUTE(string_index, first_to_check)
  58. #endif
  59. // Return a C++ string.
  60. CERES_NO_EXPORT extern std::string StringPrintf(const char* format, ...)
  61. // Tell the compiler to do printf format string checking.
  62. CERES_PRINTF_ATTRIBUTE(1, 2);
  63. // Store result into a supplied string and return it.
  64. CERES_NO_EXPORT extern const std::string& SStringPrintf(std::string* dst,
  65. const char* format,
  66. ...)
  67. // Tell the compiler to do printf format string checking.
  68. CERES_PRINTF_ATTRIBUTE(2, 3);
  69. // Append result to a supplied string.
  70. CERES_NO_EXPORT extern void StringAppendF(std::string* dst,
  71. const char* format,
  72. ...)
  73. // Tell the compiler to do printf format string checking.
  74. CERES_PRINTF_ATTRIBUTE(2, 3);
  75. // Lower-level routine that takes a va_list and appends to a specified string.
  76. // All other routines are just convenience wrappers around it.
  77. CERES_NO_EXPORT extern void StringAppendV(std::string* dst,
  78. const char* format,
  79. va_list ap);
  80. #undef CERES_PRINTF_ATTRIBUTE
  81. } // namespace ceres::internal
  82. #include "ceres/internal/reenable_warnings.h"
  83. #endif // CERES_INTERNAL_STRINGPRINTF_H_