/usr/include/mlpack/core/util/prefixedoutstream_impl.hpp is in libmlpack-dev 1.0.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /**
* @file prefixedoutstream.hpp
* @author Ryan Curtin
* @author Matthew Amidon
*
* Implementation of templated PrefixedOutStream member functions.
*
* This file is part of MLPACK 1.0.10.
*
* MLPACK is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* MLPACK is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details (LICENSE.txt).
*
* You should have received a copy of the GNU General Public License along with
* MLPACK. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __MLPACK_CORE_UTIL_PREFIXEDOUTSTREAM_IMPL_HPP
#define __MLPACK_CORE_UTIL_PREFIXEDOUTSTREAM_IMPL_HPP
// Just in case it hasn't been included.
#include "prefixedoutstream.hpp"
#include <iostream>
namespace mlpack {
namespace util {
template<typename T>
PrefixedOutStream& PrefixedOutStream::operator<<(const T& s)
{
CallBaseLogic<T>(s);
return *this;
}
//! This handles forwarding all primitive types transparently
template<typename T>
void PrefixedOutStream::CallBaseLogic(const T& s,
typename boost::disable_if<
boost::is_class<T>
>::type*)
{
BaseLogic<T>(s);
}
// Forward all objects that do not implement a ToString() method transparently
template<typename T>
void PrefixedOutStream::CallBaseLogic(const T& s,
typename boost::enable_if<
boost::is_class<T>
>::type*,
typename boost::disable_if<
HasToString<T, std::string(T::*)() const>
>::type*)
{
BaseLogic<T>(s);
}
// Call ToString() on all objects that implement ToString() before forwarding
template<typename T>
void PrefixedOutStream::CallBaseLogic(const T& s,
typename boost::enable_if<
boost::is_class<T>
>::type*,
typename boost::enable_if<
HasToString<T, std::string(T::*)() const>
>::type*)
{
std::string result = s.ToString();
BaseLogic<std::string>(result);
}
template<typename T>
void PrefixedOutStream::BaseLogic(const T& val)
{
// We will use this to track whether or not we need to terminate at the end of
// this call (only for streams which terminate after a newline).
bool newlined = false;
std::string line;
// If we need to, output the prefix.
PrefixIfNeeded();
std::ostringstream convert;
convert << val;
if(convert.fail())
{
PrefixIfNeeded();
if (!ignoreInput)
{
destination << "Failed lexical_cast<std::string>(T) for output; output"
" not shown." << std::endl;
newlined = true;
}
}
else
{
line = convert.str();
// If the length of the casted thing was 0, it may have been a stream
// manipulator, so send it directly to the stream and don't ask questions.
if (line.length() == 0)
{
// The prefix cannot be necessary at this point.
if (!ignoreInput) // Only if the user wants it.
destination << val;
return;
}
// Now, we need to check for newlines in this line. If we find one, output
// up until the newline, then output the newline and the prefix and continue
// looking.
size_t nl;
size_t pos = 0;
while ((nl = line.find('\n', pos)) != std::string::npos)
{
PrefixIfNeeded();
// Only output if the user wants it.
if (!ignoreInput)
{
destination << line.substr(pos, nl - pos);
destination << std::endl;
newlined = true;
}
carriageReturned = true; // Regardless of whether or not we display it.
pos = nl + 1;
}
if (pos != line.length()) // We need to display the rest.
{
PrefixIfNeeded();
if (!ignoreInput)
destination << line.substr(pos);
}
}
// If we displayed a newline and we need to terminate afterwards, do that.
if (fatal && newlined)
exit(1);
}
// This is an inline function (that is why it is here and not in .cc).
void PrefixedOutStream::PrefixIfNeeded()
{
// If we need to, output a prefix.
if (carriageReturned)
{
if (!ignoreInput) // But only if we are allowed to.
destination << prefix;
carriageReturned = false; // Denote that the prefix has been displayed.
}
}
}; // namespace util
}; // namespace mlpack
#endif // MLPACK_CLI_PREFIXEDOUTSTREAM_IMPL_H
|