473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

std::ostream data-modifying manipulator?

I tried to create my own manipulator that would both set the width of the
subsequent output field, and cast an unsigned char to unsigned in. I came
up with the following rather ugly hack. Notice that I am instantiating UI
before any call to out.operator<<(). Is there a better way to accomplish this?

What I'd like is something I could simply place in the output call between '<<'
operators without having to instantiate it first. The reason this is different
from a typical manipulator is because it is operating on the data as well as the
stream.

typedef unsigned char byte;

struct UI {
unsigned _w;
std::ostream* _out;
UI(unsigned w_=2)
:_w(w_)
{}
UI& setOut(std::ostream& out_){
_out = &out_;
return *this;
}
std::ostream& operator<<(byte b) { return *_out<<std::setw(_w)<<unsigned(b); }
};

UI& operator<<(std::ostream& out, UI& u) { return u.setOut(out); }

std::ostream& describe(std::ostream& out, const Identification_IF& subject ){
std::vector<byte> data(subject.getData());
std::ios::fmtflags savedFormat(out.flags());
UI ui;
return out
<<std::hex<<std::setfill('0')
<<"unsigned char Elf32_Ehdr::e_ident[EI_NIDENT]; EI_NIDENT = " << EI_NIDENT << "\n"
<<" [ 0] = "<<ui<<data[ 0]<<" EI_MAG0 : Always ELFMAG0 = '0x7f'\n"
<<" [ 1] = "<<ui<<data[ 1]<<" EI_MAG1 : Always ELFMAG1 = 'E' = "<<ui<<'E'<<"\n"
<<" [ 2] = "<<ui<<data[ 2]<<" EI_MAG2 : Always ELFMAG1 = 'L' = "<<ui<<'L'<<"\n"
<<" [ 3] = "<<ui<<data[ 3]<<" EI_MAG3 : Always ELFMAG1 = 'F' = "<<ui<<'F'<<"\n"
<<" [ 4] = "<<ui<<data[ 4]<<" EI_CLASS : ELFCLASSNONE = 0 | ELFCLASS32 = 1 | ELFCLASS64 = 2\n"
<<" [ 5] = "<<ui<<data[ 5]<<" EI_DATA : ELFDATANONE = 0 | ELFDATA2LSB = 1 | ELFDATA2MSB = 2\n"
<<" [ 6] = "<<ui<<data[ 6]<<" EI_VERSION : ELF version of the file\n"
<<" [ 7] = "<<ui<<data[ 7]<<" EI_PAD : end of ELF Identification data\n"
<<" [ 8] = "<<ui<<data[ 8]<<" Unused\n"
<<" [ 9] = "<<ui<<data[ 9]<<" Unused\n"
<<" [10] = "<<ui<<data[10]<<" Unused\n"
<<" [11] = "<<ui<<data[11]<<" Unused\n"
<<" [12] = "<<ui<<data[12]<<" Unused\n"
<<" [13] = "<<ui<<data[13]<<" Unused\n"
<<" [14] = "<<ui<<data[14]<<" Unused\n"
<<" [15] = "<<ui<<data[15]<<" Unused\n";
}
--

Aug 5 '05 #1
0 1490

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
10
by: Johannes Barop | last post by:
Hi, I want to format the output of a 'std::ostream', but i dont know how to do it . Example: int main() { my_out << "Hi.\nI'am a" << " Computer."; my_out << "Nice.";
1
by: Johannes Barop | last post by:
Hello, i try to implement a streambuffer. I overwrote streambuf::overflow() and streambuf::xsputn(). Both are protected and virtual (http://www.cplusplus.com/ref/iostream/streambuf/). But...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
3
by: Matt | last post by:
Does the standard define default values for the format state of an ostream? I would like to be able to put a stream in a standard format state without thinking about how some other routine may...
6
by: Philip Potter | last post by:
Hello there, I'm reading about the std::for_each() function in TC++PL, 3rd Ed. It seems like a good idea, but in practice I can never see a way to bend it to my wishes without writing huge...
6
by: syang8 | last post by:
Any one can specify the problem of the following code? The compiling error is on the friend function. If the base class is not inherited from ostream, or I just remove the friend function from the...
1
by: AJG | last post by:
Hi there. I am using a library called SOCI that has a method to set a stream which it uses to log SQL queries. The signature is as follows: void setLogStream(std::ostream *s); This works great...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.