blob: 4d5127849050367035b3357daf842486169c6272 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 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
38#ifndef CERES_INTERNAL_STRINGPRINTF_H_
39#define CERES_INTERNAL_STRINGPRINTF_H_
40
41#include <cstdarg>
42#include <string>
43
44#include "ceres/internal/port.h"
45
46namespace ceres {
47namespace internal {
48
49#if (defined(__GNUC__) || defined(__clang__))
50// Tell the compiler to do printf format string checking if the compiler
51// supports it; see the 'format' attribute in
52// <http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html>.
53//
54// N.B.: As the GCC manual states, "[s]ince non-static C++ methods
55// have an implicit 'this' argument, the arguments of such methods
56// should be counted from two, not one."
57#define CERES_PRINTF_ATTRIBUTE(string_index, first_to_check) \
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080058 __attribute__((__format__(__printf__, string_index, first_to_check)))
Austin Schuh70cc9552019-01-21 19:46:48 -080059#define CERES_SCANF_ATTRIBUTE(string_index, first_to_check) \
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080060 __attribute__((__format__(__scanf__, string_index, first_to_check)))
Austin Schuh70cc9552019-01-21 19:46:48 -080061#else
62#define CERES_PRINTF_ATTRIBUTE(string_index, first_to_check)
63#endif
64
65// Return a C++ string.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080066CERES_EXPORT_INTERNAL extern std::string StringPrintf(const char* format, ...)
Austin Schuh70cc9552019-01-21 19:46:48 -080067 // Tell the compiler to do printf format string checking.
68 CERES_PRINTF_ATTRIBUTE(1, 2);
69
70// Store result into a supplied string and return it.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080071CERES_EXPORT_INTERNAL extern const std::string& SStringPrintf(
72 std::string* dst, const char* format, ...)
Austin Schuh70cc9552019-01-21 19:46:48 -080073 // Tell the compiler to do printf format string checking.
74 CERES_PRINTF_ATTRIBUTE(2, 3);
75
76// Append result to a supplied string.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080077CERES_EXPORT_INTERNAL extern void StringAppendF(std::string* dst,
78 const char* format,
79 ...)
Austin Schuh70cc9552019-01-21 19:46:48 -080080 // Tell the compiler to do printf format string checking.
81 CERES_PRINTF_ATTRIBUTE(2, 3);
82
83// Lower-level routine that takes a va_list and appends to a specified string.
84// All other routines are just convenience wrappers around it.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080085CERES_EXPORT_INTERNAL extern void StringAppendV(std::string* dst,
86 const char* format,
87 va_list ap);
Austin Schuh70cc9552019-01-21 19:46:48 -080088
89#undef CERES_PRINTF_ATTRIBUTE
90
91} // namespace internal
92} // namespace ceres
93
94#endif // CERES_INTERNAL_STRINGPRINTF_H_