output.h
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 //
00015 // Output extension hooks for the Format library.
00016 // `internal::InvokeFlush` calls the appropriate flush function for the
00017 // specified output argument.
00018 // `BufferRawSink` is a simple output sink for a char buffer. Used by SnprintF.
00019 // `FILERawSink` is a std::FILE* based sink. Used by PrintF and FprintF.
00020 
00021 #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_
00022 #define ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_
00023 
00024 #include <cstdio>
00025 #include <ostream>
00026 #include <string>
00027 
00028 #include "absl/base/port.h"
00029 #include "absl/strings/string_view.h"
00030 
00031 class Cord;
00032 
00033 namespace absl {
00034 namespace str_format_internal {
00035 
00036 // RawSink implementation that writes into a char* buffer.
00037 // It will not overflow the buffer, but will keep the total count of chars
00038 // that would have been written.
00039 class BufferRawSink {
00040  public:
00041   BufferRawSink(char* buffer, size_t size) : buffer_(buffer), size_(size) {}
00042 
00043   size_t total_written() const { return total_written_; }
00044   void Write(string_view v);
00045 
00046  private:
00047   char* buffer_;
00048   size_t size_;
00049   size_t total_written_ = 0;
00050 };
00051 
00052 // RawSink implementation that writes into a FILE*.
00053 // It keeps track of the total number of bytes written and any error encountered
00054 // during the writes.
00055 class FILERawSink {
00056  public:
00057   explicit FILERawSink(std::FILE* output) : output_(output) {}
00058 
00059   void Write(string_view v);
00060 
00061   size_t count() const { return count_; }
00062   int error() const { return error_; }
00063 
00064  private:
00065   std::FILE* output_;
00066   int error_ = 0;
00067   size_t count_ = 0;
00068 };
00069 
00070 // Provide RawSink integration with common types from the STL.
00071 inline void AbslFormatFlush(std::string* out, string_view s) {
00072   out->append(s.data(), s.size());
00073 }
00074 inline void AbslFormatFlush(std::ostream* out, string_view s) {
00075   out->write(s.data(), s.size());
00076 }
00077 
00078 template <class AbslCord, typename = typename std::enable_if<
00079                               std::is_same<AbslCord, ::Cord>::value>::type>
00080 inline void AbslFormatFlush(AbslCord* out, string_view s) {
00081   out->Append(s);
00082 }
00083 
00084 inline void AbslFormatFlush(FILERawSink* sink, string_view v) {
00085   sink->Write(v);
00086 }
00087 
00088 inline void AbslFormatFlush(BufferRawSink* sink, string_view v) {
00089   sink->Write(v);
00090 }
00091 
00092 template <typename T>
00093 auto InvokeFlush(T* out, string_view s)
00094     -> decltype(str_format_internal::AbslFormatFlush(out, s)) {
00095   str_format_internal::AbslFormatFlush(out, s);
00096 }
00097 
00098 }  // namespace str_format_internal
00099 }  // namespace absl
00100 
00101 #endif  // ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15