Path: blob/main_old/src/compiler/translator/InfoSink.h
1693 views
//1// Copyright 2002 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56#ifndef COMPILER_TRANSLATOR_INFOSINK_H_7#define COMPILER_TRANSLATOR_INFOSINK_H_89#include <math.h>10#include <stdlib.h>11#include "GLSLANG/ShaderLang.h"12#include "compiler/translator/Common.h"13#include "compiler/translator/Severity.h"1415namespace sh16{1718class ImmutableString;19class TType;2021// Returns the fractional part of the given floating-point number.22inline float fractionalPart(float f)23{24float intPart = 0.0f;25return modff(f, &intPart);26}2728class ImmutableString;2930//31// Encapsulate info logs for all objects that have them.32//33// The methods are a general set of tools for getting a variety of34// messages and types inserted into the log.35//36class TInfoSinkBase37{38public:39TInfoSinkBase() {}4041template <typename T>42TInfoSinkBase &operator<<(const T &t)43{44TPersistStringStream stream = sh::InitializeStream<TPersistStringStream>();45stream << t;46sink.append(stream.str());47return *this;48}49// Override << operator for specific types. It is faster to append strings50// and characters directly to the sink.51TInfoSinkBase &operator<<(char c)52{53sink.append(1, c);54return *this;55}56TInfoSinkBase &operator<<(const char *str)57{58sink.append(str);59return *this;60}61TInfoSinkBase &operator<<(const TPersistString &str)62{63sink.append(str);64return *this;65}66TInfoSinkBase &operator<<(const TString &str)67{68sink.append(str.c_str());69return *this;70}71TInfoSinkBase &operator<<(const ImmutableString &str);7273TInfoSinkBase &operator<<(const TType &type);7475// Make sure floats are written with correct precision.76TInfoSinkBase &operator<<(float f)77{78// Make sure that at least one decimal point is written. If a number79// does not have a fractional part, the default precision format does80// not write the decimal portion which gets interpreted as integer by81// the compiler.82TPersistStringStream stream = sh::InitializeStream<TPersistStringStream>();83if (fractionalPart(f) == 0.0f)84{85stream.precision(1);86stream << std::showpoint << std::fixed << f;87}88else89{90stream.unsetf(std::ios::fixed);91stream.unsetf(std::ios::scientific);92stream.precision(8);93stream << f;94}95sink.append(stream.str());96return *this;97}98// Write boolean values as their names instead of integral value.99TInfoSinkBase &operator<<(bool b)100{101const char *str = b ? "true" : "false";102sink.append(str);103return *this;104}105106void erase()107{108sink.clear();109binarySink.clear();110}111int size() { return static_cast<int>(isBinary() ? binarySink.size() : sink.size()); }112113const TPersistString &str() const114{115ASSERT(!isBinary());116return sink;117}118const char *c_str() const119{120ASSERT(!isBinary());121return sink.c_str();122}123124void prefix(Severity severity);125void location(int file, int line);126127bool isBinary() const { return !binarySink.empty(); }128void setBinary(BinaryBlob &&binary) { binarySink = std::move(binary); }129const BinaryBlob &getBinary() const130{131ASSERT(isBinary());132return binarySink;133}134135private:136// The data in the info sink is either in human readable form (|sink|) or binary (|binarySink|).137TPersistString sink;138BinaryBlob binarySink;139};140141class TInfoSink142{143public:144TInfoSinkBase info;145TInfoSinkBase debug;146TInfoSinkBase obj;147};148149} // namespace sh150151#endif // COMPILER_TRANSLATOR_INFOSINK_H_152153154