Skip to content

Commit 9dc1009

Browse files
committed
move Rcpp-specific changes to exterior header file
1 parent 3af6789 commit 9dc1009

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1+
namespace Rcpp {
2+
void stop(const std::string& message);
3+
}
4+
#define TINYFORMAT_ERROR(REASON) ::Rcpp::stop(REASON)
5+
6+
#if __cplusplus >= 201103L
7+
#define TINYFORMAT_USE_VARIADIC_TEMPLATES
8+
#else
9+
// Don't use C++11 features (support older compilers)
10+
#define TINYFORMAT_NO_VARIADIC_TEMPLATES
11+
#endif
12+
113
#include "tinyformat/tinyformat.h"

inst/include/Rcpp/utils/tinyformat/tinyformat.h

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// #nocov start
12
// tinyformat.h
23
// Copyright (C) 2011, Chris Foster [chris42f (at) gmail (d0t) com]
34
//
@@ -123,30 +124,23 @@ namespace tinyformat {}
123124
namespace tfm = tinyformat;
124125

125126
// Error handling; calls assert() by default.
126-
namespace Rcpp {
127-
void stop(const std::string& message);
128-
}
129-
#define TINYFORMAT_ERROR(REASON) ::Rcpp::stop(REASON)
127+
// #define TINYFORMAT_ERROR(reasonString) your_error_handler(reasonString)
130128

131129
// Define for C++11 variadic templates which make the code shorter & more
132130
// general. If you don't define this, C++11 support is autodetected below.
133-
#if __cplusplus >= 201103L
134-
#define TINYFORMAT_USE_VARIADIC_TEMPLATES
135-
#else
136-
// Don't use C++11 features (support older compilers)
137-
#define TINYFORMAT_NO_VARIADIC_TEMPLATES
138-
#endif
131+
// #define TINYFORMAT_USE_VARIADIC_TEMPLATES
132+
139133

140134
//------------------------------------------------------------------------------
141135
// Implementation details.
142136
#include <algorithm>
143-
// #include <cassert>
137+
#include <cassert>
144138
#include <iostream>
145139
#include <sstream>
146140

147-
// #ifndef TINYFORMAT_ERROR
148-
// # define TINYFORMAT_ERROR(reason) assert(0 && reason)
149-
// #endif
141+
#ifndef TINYFORMAT_ERROR
142+
# define TINYFORMAT_ERROR(reason) assert(0 && reason)
143+
#endif
150144

151145
#if !defined(TINYFORMAT_USE_VARIADIC_TEMPLATES) && !defined(TINYFORMAT_NO_VARIADIC_TEMPLATES)
152146
# ifdef __GXX_EXPERIMENTAL_CXX0X__
@@ -217,15 +211,15 @@ template<int n> struct is_wchar<wchar_t[n]> {};
217211
template<typename T, typename fmtT, bool convertible = is_convertible<T, fmtT>::value>
218212
struct formatValueAsType
219213
{
220-
static void invoke(std::ostream& /*out*/, const T& /*value*/) { /*assert(0);*/ }
214+
static void invoke(std::ostream& /*out*/, const T& /*value*/) { assert(0); }
221215
};
222216
// Specialized version for types that can actually be converted to fmtT, as
223217
// indicated by the "convertible" template parameter.
224218
template<typename T, typename fmtT>
225219
struct formatValueAsType<T,fmtT,true>
226220
{
227-
static void invoke(std::ostream& out, const T& value) // #nocov
228-
{ out << static_cast<fmtT>(value); } // #nocov
221+
static void invoke(std::ostream& out, const T& value)
222+
{ out << static_cast<fmtT>(value); }
229223
};
230224

231225
#ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
@@ -254,29 +248,29 @@ struct formatZeroIntegerWorkaround<T,true>
254248
template<typename T, bool convertible = is_convertible<T,int>::value>
255249
struct convertToInt
256250
{
257-
static int invoke(const T& /*value*/) // #nocov start
251+
static int invoke(const T& /*value*/)
258252
{
259253
TINYFORMAT_ERROR("tinyformat: Cannot convert from argument type to "
260254
"integer for use as variable width or precision");
261255
return 0;
262-
} // #nocov end
256+
}
263257
};
264258
// Specialization for convertToInt when conversion is possible
265259
template<typename T>
266260
struct convertToInt<T,true>
267261
{
268-
static int invoke(const T& value) { return static_cast<int>(value); } // #nocov
262+
static int invoke(const T& value) { return static_cast<int>(value); }
269263
};
270264

271265
// Format at most ntrunc characters to the given stream.
272266
template<typename T>
273-
inline void formatTruncated(std::ostream& out, const T& value, int ntrunc) // #nocov start
267+
inline void formatTruncated(std::ostream& out, const T& value, int ntrunc)
274268
{
275269
std::ostringstream tmp;
276270
tmp << value;
277271
std::string result = tmp.str();
278272
out.write(result.c_str(), (std::min)(ntrunc, static_cast<int>(result.size())));
279-
}
273+
}
280274
#define TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(type) \
281275
inline void formatTruncated(std::ostream& out, type* value, int ntrunc) \
282276
{ \
@@ -287,11 +281,11 @@ inline void formatTruncated(std::ostream& out, type* value, int ntrunc) \
287281
}
288282
// Overload for const char* and char*. Could overload for signed & unsigned
289283
// char too, but these are technically unneeded for printf compatibility.
290-
TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(const char)
284+
TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(const char)
291285
TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(char)
292286
#undef TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR
293287

294-
} // namespace detail // #nocov end
288+
} // namespace detail
295289

296290

297291
//------------------------------------------------------------------------------
@@ -327,7 +321,7 @@ inline void formatValue(std::ostream& out, const char* /*fmtBegin*/,
327321
// could otherwise lead to a crash when printing a dangling (const char*).
328322
const bool canConvertToChar = detail::is_convertible<T,char>::value;
329323
const bool canConvertToVoidPtr = detail::is_convertible<T, const void*>::value;
330-
if(canConvertToChar && *(fmtEnd-1) == 'c') // #nocov start
324+
if(canConvertToChar && *(fmtEnd-1) == 'c')
331325
detail::formatValueAsType<T, char>::invoke(out, value);
332326
else if(canConvertToVoidPtr && *(fmtEnd-1) == 'p')
333327
detail::formatValueAsType<T, const void*>::invoke(out, value);
@@ -339,7 +333,7 @@ inline void formatValue(std::ostream& out, const char* /*fmtBegin*/,
339333
// Take care not to overread C strings in truncating conversions like
340334
// "%.4s" where at most 4 characters may be read.
341335
detail::formatTruncated(out, value, ntrunc);
342-
} // #nocov end
336+
}
343337
else
344338
out << value;
345339
}
@@ -509,10 +503,10 @@ class FormatArg
509503
m_formatImpl(out, fmtBegin, fmtEnd, ntrunc, m_value);
510504
}
511505

512-
int toInt() const // #nocov start
506+
int toInt() const
513507
{
514508
return m_toIntImpl(m_value);
515-
} // #nocov end
509+
}
516510

517511
private:
518512
template<typename T>
@@ -523,10 +517,10 @@ class FormatArg
523517
}
524518

525519
template<typename T>
526-
TINYFORMAT_HIDDEN static int toIntImpl(const void* value) // #nocov start
520+
TINYFORMAT_HIDDEN static int toIntImpl(const void* value)
527521
{
528522
return convertToInt<T>::invoke(*static_cast<const T*>(value));
529-
} // #nocov end
523+
}
530524

531525
const void* m_value;
532526
void (*m_formatImpl)(std::ostream& out, const char* fmtBegin,
@@ -537,13 +531,13 @@ class FormatArg
537531

538532
// Parse and return an integer from the string c, as atoi()
539533
// On return, c is set to one past the end of the integer.
540-
inline int parseIntAndAdvance(const char*& c) // #nocov start
534+
inline int parseIntAndAdvance(const char*& c)
541535
{
542536
int i = 0;
543537
for(;*c >= '0' && *c <= '9'; ++c)
544538
i = 10*i + (*c - '0');
545539
return i;
546-
} // #nocov end
540+
}
547541

548542
// Print literal part of format string and return next format spec
549543
// position.
@@ -566,8 +560,8 @@ inline const char* printFormatStringLiteral(std::ostream& out, const char* fmt)
566560
if(*(c+1) != '%')
567561
return c;
568562
// for "%%", tack trailing % onto next literal section.
569-
fmt = ++c; // #nocov
570-
break; // #nocov
563+
fmt = ++c;
564+
break;
571565
default:
572566
break;
573567
}
@@ -592,7 +586,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& spacePadPositi
592586
{
593587
if(*fmtStart != '%')
594588
{
595-
TINYFORMAT_ERROR("tinyformat: Not enough conversion specifiers in format string"); // #nocov
589+
TINYFORMAT_ERROR("tinyformat: Not enough conversion specifiers in format string");
596590
return fmtStart;
597591
}
598592
// Reset stream state to defaults.
@@ -608,7 +602,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& spacePadPositi
608602
int widthExtra = 0;
609603
const char* c = fmtStart + 1;
610604
// 1) Parse flags
611-
for(;; ++c) // #nocov start
605+
for(;; ++c)
612606
{
613607
switch(*c)
614608
{
@@ -767,7 +761,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& spacePadPositi
767761
out.setf(std::ios::internal, std::ios::adjustfield);
768762
out.fill('0');
769763
}
770-
return c+1; // #nocov end
764+
return c+1;
771765
}
772766

773767

@@ -793,7 +787,7 @@ inline void formatImpl(std::ostream& out, const char* fmt,
793787
if (argIndex >= numFormatters)
794788
{
795789
// Check args remain after reading any variable width/precision
796-
TINYFORMAT_ERROR("tinyformat: Not enough format arguments"); // #nocov
790+
TINYFORMAT_ERROR("tinyformat: Not enough format arguments");
797791
return;
798792
}
799793
const FormatArg& arg = formatters[argIndex];
@@ -806,22 +800,22 @@ inline void formatImpl(std::ostream& out, const char* fmt,
806800
// between stream formatting and the printf() behaviour. Simulate
807801
// it crudely by formatting into a temporary string stream and
808802
// munging the resulting string.
809-
std::ostringstream tmpStream; // #nocov start
803+
std::ostringstream tmpStream;
810804
tmpStream.copyfmt(out);
811805
tmpStream.setf(std::ios::showpos);
812806
arg.format(tmpStream, fmt, fmtEnd, ntrunc);
813807
std::string result = tmpStream.str(); // allocates... yuck.
814808
for(size_t i = 0, iend = result.size(); i < iend; ++i)
815809
if(result[i] == '+') result[i] = ' ';
816-
out << result; // #nocov end
810+
out << result;
817811
}
818812
fmt = fmtEnd;
819813
}
820814

821815
// Print remaining part of format string.
822816
fmt = printFormatStringLiteral(out, fmt);
823817
if(*fmt != '\0')
824-
TINYFORMAT_ERROR("tinyformat: Too many conversion specifiers in format string"); // #nocov
818+
TINYFORMAT_ERROR("tinyformat: Too many conversion specifiers in format string");
825819

826820
// Restore stream state
827821
out.width(origWidth);
@@ -877,7 +871,7 @@ class FormatListN : public FormatList
877871
template<TINYFORMAT_ARGTYPES(n)> \
878872
FormatListN(TINYFORMAT_VARARGS(n)) \
879873
: FormatList(&m_formatterStore[0], n) \
880-
{/*assert(n == N);*/init(0, TINYFORMAT_PASSARGS(n)); } \
874+
{ assert(n == N); init(0, TINYFORMAT_PASSARGS(n)); } \
881875
\
882876
template<TINYFORMAT_ARGTYPES(n)> \
883877
void init(int i, TINYFORMAT_VARARGS(n)) \
@@ -1044,3 +1038,4 @@ TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMAT_FUNCS)
10441038
} // namespace tinyformat
10451039

10461040
#endif // TINYFORMAT_H_INCLUDED
1041+
// #nocov end

0 commit comments

Comments
 (0)