1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
/*
* Copyright (C) 2004, 2008 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <wtf/Forward.h>
#include <wtf/HexNumber.h>
#include <wtf/Markable.h>
#include <wtf/OptionSet.h>
#include <wtf/Ref.h>
#include <wtf/RefPtr.h>
#include <wtf/WeakPtr.h>
#include <wtf/text/StringBuilder.h>
namespace WTF {
class TextStream {
WTF_MAKE_FAST_ALLOCATED;
public:
struct FormatNumberRespectingIntegers {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
FormatNumberRespectingIntegers(double number)
: value(number) { }
double value;
};
enum class Formatting : uint8_t {
SVGStyleRect = 1 << 0, // "at (0,0) size 10x10"
NumberRespectingIntegers = 1 << 1,
LayoutUnitsAsIntegers = 1 << 2,
};
enum class LineMode { SingleLine, MultipleLine };
TextStream(LineMode lineMode = LineMode::MultipleLine, OptionSet<Formatting> formattingFlags = { }, unsigned containerSizeLimit = 0)
: m_formattingFlags(formattingFlags)
, m_multiLineMode(lineMode == LineMode::MultipleLine)
, m_containerSizeLimit(containerSizeLimit)
{
}
bool isEmpty() const { return m_text.isEmpty(); }
WTF_EXPORT_PRIVATE TextStream& operator<<(bool);
WTF_EXPORT_PRIVATE TextStream& operator<<(char);
WTF_EXPORT_PRIVATE TextStream& operator<<(int);
WTF_EXPORT_PRIVATE TextStream& operator<<(unsigned);
WTF_EXPORT_PRIVATE TextStream& operator<<(long);
WTF_EXPORT_PRIVATE TextStream& operator<<(unsigned long);
WTF_EXPORT_PRIVATE TextStream& operator<<(long long);
WTF_EXPORT_PRIVATE TextStream& operator<<(unsigned long long);
WTF_EXPORT_PRIVATE TextStream& operator<<(float);
WTF_EXPORT_PRIVATE TextStream& operator<<(double);
WTF_EXPORT_PRIVATE TextStream& operator<<(const char*);
WTF_EXPORT_PRIVATE TextStream& operator<<(const void*);
WTF_EXPORT_PRIVATE TextStream& operator<<(const AtomString&);
WTF_EXPORT_PRIVATE TextStream& operator<<(const CString&);
WTF_EXPORT_PRIVATE TextStream& operator<<(const String&);
WTF_EXPORT_PRIVATE TextStream& operator<<(ASCIILiteral);
WTF_EXPORT_PRIVATE TextStream& operator<<(StringView);
WTF_EXPORT_PRIVATE TextStream& operator<<(const HexNumberBuffer&);
WTF_EXPORT_PRIVATE TextStream& operator<<(const FormattedCSSNumber&);
// Deprecated. Use the NumberRespectingIntegers FormattingFlag instead.
WTF_EXPORT_PRIVATE TextStream& operator<<(const FormatNumberRespectingIntegers&);
#if PLATFORM(COCOA)
WTF_EXPORT_PRIVATE TextStream& operator<<(id);
#endif
OptionSet<Formatting> formattingFlags() const { return m_formattingFlags; }
void setFormattingFlags(OptionSet<Formatting> flags) { m_formattingFlags = flags; }
bool hasFormattingFlag(Formatting flag) const { return m_formattingFlags.contains(flag); }
template<typename T>
void dumpProperty(const String& name, const T& value)
{
TextStream& ts = *this;
ts.startGroup();
ts << name << " " << value;
ts.endGroup();
}
template<typename T>
void dumpProperty(const char* name, const T& value)
{
TextStream& ts = *this;
ts.startGroup();
ts << name << " " << value;
ts.endGroup();
}
template<typename T>
void dumpProperty(ASCIILiteral name, const T& value)
{
TextStream& ts = *this;
ts.startGroup();
ts << name << " "_s << value;
ts.endGroup();
}
WTF_EXPORT_PRIVATE String release();
WTF_EXPORT_PRIVATE void startGroup();
WTF_EXPORT_PRIVATE void endGroup();
WTF_EXPORT_PRIVATE void nextLine(); // Output newline and indent.
int indent() const { return m_indent; }
void setIndent(int indent) { m_indent = indent; }
void increaseIndent(int amount = 1) { m_indent += amount; }
void decreaseIndent(int amount = 1) { m_indent -= amount; ASSERT(m_indent >= 0); }
WTF_EXPORT_PRIVATE void writeIndent();
unsigned containerSizeLimit() const { return m_containerSizeLimit; }
// Stream manipulators.
TextStream& operator<<(TextStream& (*func)(TextStream&))
{
return (*func)(*this);
}
struct Repeat {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
Repeat(unsigned inWidth, char inCharacter)
: width(inWidth), character(inCharacter)
{ }
unsigned width { 0 };
char character { ' ' };
};
TextStream& operator<<(const Repeat& repeated)
{
for (unsigned i = 0; i < repeated.width; ++i)
m_text.append(repeated.character);
return *this;
}
class IndentScope {
public:
IndentScope(TextStream& ts, int amount = 1)
: m_stream(ts)
, m_amount(amount)
{
m_stream.increaseIndent(m_amount);
}
~IndentScope()
{
m_stream.decreaseIndent(m_amount);
}
private:
TextStream& m_stream;
int m_amount;
};
class GroupScope {
public:
GroupScope(TextStream& ts)
: m_stream(ts)
{
m_stream.startGroup();
}
~GroupScope()
{
m_stream.endGroup();
}
private:
TextStream& m_stream;
};
private:
StringBuilder m_text;
int m_indent { 0 };
OptionSet<Formatting> m_formattingFlags;
bool m_multiLineMode { true };
unsigned m_containerSizeLimit { 0 };
};
inline TextStream& indent(TextStream& ts)
{
ts.writeIndent();
return ts;
}
template<typename T>
struct ValueOrNull {
explicit ValueOrNull(T* inValue)
: value(inValue)
{ }
T* value;
};
template<typename T>
TextStream& operator<<(TextStream& ts, ValueOrNull<T> item)
{
if (item.value)
ts << *item.value;
else
ts << "null";
return ts;
}
template<typename Item>
TextStream& operator<<(TextStream& ts, const std::optional<Item>& item)
{
if (item)
return ts << item.value();
return ts << "nullopt";
}
template<typename T, typename Traits>
TextStream& operator<<(TextStream& ts, const Markable<T, Traits>& item)
{
if (item)
return ts << item.value();
return ts << "unset";
}
template<typename SizedContainer>
TextStream& streamSizedContainer(TextStream& ts, const SizedContainer& sizedContainer)
{
ts << "[";
unsigned count = 0;
for (const auto& value : sizedContainer) {
if (count)
ts << ", ";
ts << value;
if (++count == ts.containerSizeLimit())
break;
}
if (count != sizedContainer.size())
ts << ", ...";
return ts << "]";
}
template<typename ItemType, size_t inlineCapacity>
TextStream& operator<<(TextStream& ts, const Vector<ItemType, inlineCapacity>& vector)
{
return streamSizedContainer(ts, vector);
}
template<typename ItemType>
TextStream& operator<<(TextStream& ts, const FixedVector<ItemType>& vector)
{
return streamSizedContainer(ts, vector);
}
template<typename ValueArg, typename HashArg, typename TraitsArg, typename TableTraitsArg, ShouldValidateKey shouldValidateKey>
TextStream& operator<<(TextStream& ts, const HashSet<ValueArg, HashArg, TraitsArg, TableTraitsArg, shouldValidateKey>& set)
{
return streamSizedContainer(ts, set);
}
template<typename T, size_t size>
TextStream& operator<<(TextStream& ts, const std::array<T, size>& array)
{
return streamSizedContainer(ts, array);
}
template<typename T, typename Counter>
TextStream& operator<<(TextStream& ts, const WeakPtr<T, Counter>& item)
{
if (item)
return ts << *item;
return ts << "null";
}
template<typename T>
TextStream& operator<<(TextStream& ts, const RefPtr<T>& item)
{
if (item)
return ts << *item;
return ts << "null";
}
template<typename T>
TextStream& operator<<(TextStream& ts, const Ref<T>& item)
{
return ts << item.get();
}
template<typename T>
TextStream& operator<<(TextStream& ts, const CheckedPtr<T>& item)
{
if (item)
return ts << *item;
return ts << "null";
}
template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
TextStream& operator<<(TextStream& ts, const UncheckedKeyHashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>& map)
{
ts << "{";
unsigned count = 0;
for (const auto& keyValuePair : map) {
if (count)
ts << ", ";
ts << keyValuePair.key << ": " << keyValuePair.value;
if (++count == ts.containerSizeLimit())
break;
}
if (count != map.size())
ts << ", ...";
return ts << "}";
}
template<typename Option>
TextStream& operator<<(TextStream& ts, const OptionSet<Option>& options)
{
ts << "[";
bool needComma = false;
for (auto option : options) {
if (needComma)
ts << ", ";
needComma = true;
ts << option;
}
return ts << "]";
}
template<typename T, typename U>
TextStream& operator<<(TextStream& ts, const std::pair<T, U>& pair)
{
return ts << "[" << pair.first << ", " << pair.second << "]";
}
template<typename, typename = void, typename = void, typename = void, typename = void, size_t = 0>
struct supports_text_stream_insertion : std::false_type { };
template<typename T>
struct supports_text_stream_insertion<T, std::void_t<decltype(std::declval<TextStream&>() << std::declval<T>())>> : std::true_type { };
template<typename ItemType, size_t inlineCapacity>
struct supports_text_stream_insertion<Vector<ItemType, inlineCapacity>> : supports_text_stream_insertion<ItemType> { };
template<typename ValueArg, typename HashArg, typename TraitsArg>
struct supports_text_stream_insertion<HashSet<ValueArg, HashArg, TraitsArg>> : supports_text_stream_insertion<ValueArg> { };
template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
struct supports_text_stream_insertion<UncheckedKeyHashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>> : std::conjunction<supports_text_stream_insertion<KeyArg>, supports_text_stream_insertion<MappedArg>> { };
template<typename T, typename Traits>
struct supports_text_stream_insertion<Markable<T, Traits>> : supports_text_stream_insertion<T> { };
template<typename T>
struct supports_text_stream_insertion<OptionSet<T>> : supports_text_stream_insertion<T> { };
template<typename T>
struct supports_text_stream_insertion<std::optional<T>> : supports_text_stream_insertion<T> { };
template<typename T, typename Counter>
struct supports_text_stream_insertion<WeakPtr<T, Counter>> : supports_text_stream_insertion<T> { };
template<typename T>
struct supports_text_stream_insertion<RefPtr<T>> : supports_text_stream_insertion<T> { };
template<typename T>
struct supports_text_stream_insertion<Ref<T>> : supports_text_stream_insertion<T> { };
template<typename T, size_t size>
struct supports_text_stream_insertion<std::array<T, size>> : supports_text_stream_insertion<T> { };
template<typename T, typename U>
struct supports_text_stream_insertion<std::pair<T, U>> : std::conjunction<supports_text_stream_insertion<T>, supports_text_stream_insertion<U>> { };
template<typename T>
struct ValueOrEllipsis {
explicit ValueOrEllipsis(const T& value)
: value(value)
{ }
const T& value;
};
template<typename T>
TextStream& operator<<(TextStream& ts, ValueOrEllipsis<T> item)
{
if constexpr (supports_text_stream_insertion<T>::value)
ts << item.value;
else
ts << "...";
return ts;
}
// Deprecated. Use TextStream::writeIndent() instead.
WTF_EXPORT_PRIVATE void writeIndent(TextStream&, int indent);
} // namespace WTF
using WTF::TextStream;
using WTF::ValueOrEllipsis;
using WTF::ValueOrNull;
using WTF::indent;
|