IOutputStream.hh 5.72 KB
Newer Older
1 2 3 4 5 6 7
// (C) Copyright 2016 by Autodesk, Inc.

#ifndef BASE_IOUTPUTSTREAM_HH_INCLUDE
#define BASE_IOUTPUTSTREAM_HH_INCLUDE

#include <string>
#include <vector>
8
#if (_MSC_VER >= 1900 || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__))
9
#include <array>
10
#endif
11

Max Lyon's avatar
Max Lyon committed
12 13
#include <Base/Config/BaseDefines.hh>

14 15 16 17 18 19 20 21
namespace boost {
namespace filesystem {
class path;
}
}

namespace Base {

22 23
typedef unsigned int uint;

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
struct Command
{
  enum Type
  {
    END     = 0x01,
    END_LF  = 0x03,
    END_ERR = 0x07
  };

  Type cmd;

  Command(Type _cmd) : cmd(_cmd) {}
};

// use Base::ENDL to stream endline to IOutputStream (same as std::endl)
const Command ENDL = Command::END_LF;
const Command LF = Command::END_LF;

42
class BASEDLLEXPORT IOutputStream
43 44 45
{
public:
  virtual ~IOutputStream() {}
46 47 48 49 50 51
  virtual IOutputStream& print(const char) = 0;
  virtual IOutputStream& print(const int) = 0;
  virtual IOutputStream& print(const size_t) = 0;
  virtual IOutputStream& print(const double) = 0;
  virtual IOutputStream& print(const char* const) = 0;
  virtual IOutputStream& print(const Command&) = 0;
52 53
};

Max Lyon's avatar
Max Lyon committed
54
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const int _i);
55

Max Lyon's avatar
Max Lyon committed
56
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const double _d);
57

Max Lyon's avatar
Max Lyon committed
58
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const char* const _s);
59

Max Lyon's avatar
Max Lyon committed
60
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const char _c);
61

Max Lyon's avatar
Max Lyon committed
62
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const size_t _i);
63

Max Lyon's avatar
Max Lyon committed
64
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const unsigned int _i);
65

Max Lyon's avatar
Max Lyon committed
66
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const float _f);
67

Max Lyon's avatar
Max Lyon committed
68
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const std::string& _s);
69

Max Lyon's avatar
Max Lyon committed
70
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const Command& _co);
71

Max Lyon's avatar
Max Lyon committed
72
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const Command::Type _cmd_type);
73

Max Lyon's avatar
Max Lyon committed
74
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds,
75 76 77 78 79 80 81
  const boost::filesystem::path& _path);

// IStream operator for std::vector<>
template< typename ElementT>
IOutputStream& operator<<(IOutputStream& _ds, const std::vector<ElementT>& _vec)
{
  _ds << "[ ";
82 83 84
  for (typename std::vector<ElementT>::const_iterator el_it = _vec.begin(); el_it != _vec.end(); ++el_it)
  {
    const ElementT el = *el_it;
85
    _ds << el << " ";
86
  }
87 88 89 90
  _ds << "]";
  return _ds;
}

91
#if (_MSC_VER >= 1900 || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__))
92 93 94 95 96 97 98 99 100 101 102
// IStream operator for std::array<>
template< typename ElementT, size_t _el_nmbr>
IOutputStream& operator<<(IOutputStream& _ds, 
  const std::array<ElementT, _el_nmbr>& _vec)
{
  _ds << "[ ";
  for (const auto el : _vec)
    _ds << el << " ";
  _ds << "]";
  return _ds;
}
103
#endif
104 105 106 107 108 109

// IStream operator for fixed size arrays
template <typename ElementT, size_t _el_nmbr>
IOutputStream& operator<<(IOutputStream& _ds, const ElementT(&_arr)[_el_nmbr])
{
  _ds << "[ ";
110 111
  for (size_t i = 0; i < _el_nmbr; ++i)
    _ds << _arr[i] << " ";
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  _ds << "]";
  return _ds;
}

// IStream std::pair<>
template <typename T0, typename T1>
IOutputStream& operator<<(IOutputStream& _ds, const std::pair<T0, T1>& _pair)
{
  _ds << "(" << _pair.first << ", " << _pair.second << ")";
  return _ds;
}

template <class StreamT> 
class OutputStreamAdaptT : public IOutputStream
{
public:
#if 0
  // This can pass any variable number of arguments, not available in VC2012
  template <typename... ArgsT> OutputStreamAdaptT(const ArgsT&... _args)
    : strm_(_args...) {}

#else
  OutputStreamAdaptT() {}

136 137 138
//  //TODO: get rid of move constructor, cannot submit C++11 code in Base for now
//  OutputStreamAdaptT(OutputStreamAdaptT&& _oth) : strm_(std::move(_oth.strm_))
//  {}
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

  template <typename ArgT> 
  OutputStreamAdaptT(const ArgT& _arg) : strm_(_arg) {}
#endif

  virtual IOutputStream& print(const char _c) { strm_ << _c; return *this; }
  virtual IOutputStream& print(const int _i) { strm_ << _i; return *this;  }
  virtual IOutputStream& print(const size_t _i) { strm_ << _i; return *this;  }
  virtual IOutputStream& print(const double _d) { strm_ << _d; return *this;  }
  virtual IOutputStream& print(const char* const _str) 
  { 
    strm_ << _str; 
    return *this; 
  }

  virtual IOutputStream& print(const Command& _cmd)
  {
    if ((_cmd.cmd & Command::END_LF) == (int)Command::END_LF)
      strm_ << '\n'; // end line on both END_LF and END_ERR
    return *this;
  }

  StreamT& stream() { return strm_; }
  const StreamT& stream() const { return strm_; }

protected:
  StreamT strm_;
private:
  OutputStreamAdaptT(const OutputStreamAdaptT& _oth);
  OutputStreamAdaptT& operator=(const OutputStreamAdaptT&);
};

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
/*! 
Old Style secure print function, should be used directly only in exceptional
cases. Note: Defined in OStringStream.cc.
*/
extern int print(char* _bffr, const size_t _bffr_size, const char* _frmt, ...);

//! Format a variable for streaming
template <typename T, const size_t _bffr_size = 128>
struct FormatT
{
public:
  typedef FormatT<T, _bffr_size> Self;

  FormatT(const char* _frmt, const T& _vrbl)
  {
    print(bffr_, _bffr_size, _frmt, _vrbl);
  }

  friend IOutputStream& operator<<(IOutputStream& _os, const Self& _frmt)
  {
    return _os << _frmt.bffr_;
  }

private:
  char bffr_[_bffr_size];
};

//! Convenient access to format a variable for streaming
template <typename T>
inline FormatT<T> format(const char* _frmt, const T& _vrbl) 
{ 
  return FormatT<T>(_frmt, _vrbl); 
}

//! Format a 64bit size_t variable for streaming in hex (e.g. for hash)
inline FormatT<size_t> format_hex(const size_t _vrbl) 
{ 
  return FormatT<size_t>("%I64x", _vrbl); 
}

//! Format a 32bit uint variable for streaming in hex (e.g. for hash)
inline FormatT<uint> format_hex(const uint _vrbl) 
{ 
  return FormatT<uint>("%x", _vrbl); 
}

217 218
}//namespace Base
#endif//BASE_IOUTPUTSTREAM_HH_INCLUDE