libfoedus-core
FOEDUS Core Library
foedus::assorted::Hex Struct Reference

Convenient way of writing hex integers to stream. More...

Detailed Description

Convenient way of writing hex integers to stream.

Use it as follows.

std::cout << Hex(1234) << ...
// same output as:
// std::cout << "0x" << std::hex << std::uppercase << 1234 << std::nouppercase << std::dec << ...

Definition at line 124 of file assorted_func.hpp.

#include <assorted_func.hpp>

Public Member Functions

template<typename T >
 Hex (T val, int fix_digits=-1)
 

Public Attributes

uint64_t val_
 
int fix_digits_
 

Friends

std::ostream & operator<< (std::ostream &o, const Hex &v)
 

Constructor & Destructor Documentation

template<typename T >
foedus::assorted::Hex::Hex ( val,
int  fix_digits = -1 
)
inline

Definition at line 126 of file assorted_func.hpp.

126 : val_(static_cast<uint64_t>(val)), fix_digits_(fix_digits) {}

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  o,
const Hex v 
)
friend

Definition at line 92 of file assorted_func.cpp.

92  {
93  // Duh, even if I recover the flags, not good to contaminate the given ostream object.
94  // std::ios::fmtflags old_flags = o.flags();
95  // o << "0x";
96  // if (v.fix_digits_ >= 0) {
97  // o.width(v.fix_digits_);
98  // o.fill('0');
99  // }
100  // o << std::hex << std::uppercase << v.val_;
101  // o.flags(old_flags);
102 
103  // Let's do it ourselves
104  char buffer[17];
105  buffer[16] = 0;
106  for (uint16_t i = 0; i < 16U; ++i) {
107  buffer[i] = kUpperHexChars[(v.val_ >> ((15 - i) * 4)) & 0xFU];
108  }
109  uint16_t start_pos;
110  for (start_pos = 0; start_pos < 15U; ++start_pos) {
111  if (buffer[start_pos] != '0') {
112  break;
113  }
114  if (v.fix_digits_ >= 0 && start_pos > 16 - v.fix_digits_) {
115  break;
116  }
117  }
118 
119  o << "0x" << (buffer + start_pos);
120  return o;
121 }
const char * kUpperHexChars

Member Data Documentation

int foedus::assorted::Hex::fix_digits_

Definition at line 129 of file assorted_func.hpp.

Referenced by foedus::assorted::operator<<().

uint64_t foedus::assorted::Hex::val_

Definition at line 128 of file assorted_func.hpp.

Referenced by foedus::assorted::operator<<().


The documentation for this struct was generated from the following file: