Skip to content

Instantly share code, notes, and snippets.

@objectx
Last active February 4, 2018 08:20
Show Gist options
  • Save objectx/80b17700b54eb0191a2118e7f26ef8ec to your computer and use it in GitHub Desktop.
Save objectx/80b17700b54eb0191a2118e7f26ef8ec to your computer and use it in GitHub Desktop.
State preserving IO manipulator example
/*
* manips.h:
*
*/
#pragma once
#ifndef manips_h__f1fd7c75_0593_40cb_9c6b_848e2f1cd153
#define manips_h__f1fd7c75_0593_40cb_9c6b_848e2f1cd153 1
#include <iostream>
#include <iomanip>
template <typename T_>
class put_hex_ {
private:
int width_ ;
const T_ & value_ ;
public:
put_hex_ (const T_ &value, int width) : width_ (width), value_ (value) {
/* NO-OP */
}
std::ostream & write (std::ostream &out) const {
std::ios::fmtflags f = out.setf (std::ios::hex, std::ios::basefield) ;
char fill = out.fill ('0') ;
out << std::setw (width_) << value_ ;
out.fill (fill) ;
out.setf (f, std::ios::basefield) ;
return out ;
}
} ;
template <typename T_>
std::ostream & operator << (std::ostream &out, const put_hex_<T_> &arg) {
return arg.write (out) ;
}
template <typename T_>
put_hex_<T_> put_hex (const T_ &value, int width = 0) {
return put_hex_<T_> (value, width) ;
}
#endif /* manips_h__f1fd7c75_0593_40cb_9c6b_848e2f1cd153 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment