| // Copyright 2025 Google Inc. All Rights Reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include "output_stream.h" |
| |
| #include "util.h" |
| |
| OutputStream& OutputStream::Format(const char* fmt, ...) { |
| va_list args; |
| va_start(args, fmt); |
| FormatArgs(fmt, args); |
| va_end(args); |
| return *this; |
| } |
| |
| OutputStream& OutputStream::FormatArgs(const char* fmt, va_list args) { |
| std::string str = StringFormat(fmt, args); |
| Write(str.c_str(), str.size()); |
| return *this; |
| } |
| |
| OutputStream& OutputStream::operator<<(bool value) { |
| if (value) |
| Write("true", 4u); |
| else |
| Write("false", 5u); |
| return *this; |
| } |
| |
| OutputStream& OutputStream::operator<<(char value) { |
| Write(&value, 1u); |
| return *this; |
| } |
| |
| OutputStream& OutputStream::operator<<(int value) { |
| char buffer[16]; |
| int len = snprintf(buffer, sizeof(buffer), "%d", value); |
| if (len > 0) |
| Write(buffer, static_cast<size_t>(len)); |
| return *this; |
| } |
| |
| OutputStream& OutputStream::operator<<(long value) { |
| char buffer[32]; |
| int len = snprintf(buffer, sizeof(buffer), "%ld", value); |
| if (len > 0) |
| Write(buffer, static_cast<size_t>(len)); |
| return *this; |
| } |
| |
| OutputStream& OutputStream::operator<<(long long value) { |
| char buffer[32]; |
| int len = snprintf(buffer, sizeof(buffer), "%lld", value); |
| if (len > 0) |
| Write(buffer, static_cast<size_t>(len)); |
| return *this; |
| } |
| |
| OutputStream& OutputStream::operator<<(unsigned value) { |
| char buffer[16]; |
| int len = snprintf(buffer, sizeof(buffer), "%u", value); |
| if (len > 0) |
| Write(buffer, static_cast<size_t>(len)); |
| return *this; |
| } |
| |
| OutputStream& OutputStream::operator<<(unsigned long value) { |
| char buffer[32]; |
| int len = snprintf(buffer, sizeof(buffer), "%lu", value); |
| if (len > 0) |
| Write(buffer, static_cast<size_t>(len)); |
| return *this; |
| } |
| |
| OutputStream& OutputStream::operator<<(unsigned long long value) { |
| char buffer[32]; |
| int len = snprintf(buffer, sizeof(buffer), "%llu", value); |
| if (len > 0) |
| Write(buffer, static_cast<size_t>(len)); |
| return *this; |
| } |
| |
| OutputStream& OutputStream::operator<<(double value) { |
| char buffer[32]; |
| int len = snprintf(buffer, sizeof(buffer), "%g", value); |
| if (len > 0) |
| Write(buffer, static_cast<size_t>(len)); |
| return *this; |
| } |
| |
| OutputStream& OutputStream::operator<<(const void* value) { |
| char buffer[32]; |
| int len = snprintf(buffer, sizeof(buffer), "%p", value); |
| if (len > 0) |
| Write(buffer, static_cast<size_t>(len)); |
| return *this; |
| } |