Okay, I like cout and I like printf but I would rather use C++ versions of functions for my OS. I have jimmy rigged a cout function using custom typedefs.
I have right now 3 overloaded versions of the operator<< ostream function.
cout << "TEST"; // prints TEST to the screen.
cout << (int)0x20; // prints 32 to the screen using an i to a function.
cout << "0x" << (hex)0x20; // prints 0x20 using the same i to a function modified for base 16.
What I want to know is if there is a better way to incorporate the printf % options in a cout style statement?
Thanks for any suggestions
printf functionality from cout?
printf functionality from cout?
Getting back in the game.
Re: printf functionality from cout?
I've opted to have both in my kernel. Not just for the sake of convenience, but also because of the issue of locking. If several threads are using cout, you might get a mixup of the output. A solution would be to require that every use of cout uses endl to release the lock, but it's easy to forget that (and will result in blocking a thread if you do!). So I decided to do it a little differently from the standard C++ way:
- There is a single 'console' object, which handles raw output.
- You can do Kout::printf, which locks the console, prints a string, then releases it.
- You can also acquire a 'console handle', which I usually call kout, which is a local object that allows you to do C++-style formatting. But the object gets destructed at the end of scope, so the lock is never held indefinitely.
An issue with this approach I haven't fixed yet is that a function that holds the lock with a console handle can't call functions which also require a lock. If you do, the second function will block indefinitely because its caller already holds the lock. I realise I might be making this way too complicated, but it's the best solution I've been able to think of.
- There is a single 'console' object, which handles raw output.
- You can do Kout::printf, which locks the console, prints a string, then releases it.
- You can also acquire a 'console handle', which I usually call kout, which is a local object that allows you to do C++-style formatting. But the object gets destructed at the end of scope, so the lock is never held indefinitely.
An issue with this approach I haven't fixed yet is that a function that holds the lock with a console handle can't call functions which also require a lock. If you do, the second function will block indefinitely because its caller already holds the lock. I realise I might be making this way too complicated, but it's the best solution I've been able to think of.
Re: printf functionality from cout?
I assume you mean "formatting options". I use an enum type that is one of the types for the << overloading:LordMage wrote:What I want to know is if there is a better way to incorporate the printf % options in a cout style statement?
Code: Select all
enum CtrlChar_t { newline, hex };
...
CVideo& operator<<(CtrlChar_t CtrlChar);
Code: Select all
CVideo& CVideo::operator<<(CtrlChar_t CtrlChar)
{
switch (CtrlChar)
{
case newline:
NewLine();
break;
case hex:
OutputHex = true;
break;
}
return *this;
}
Code: Select all
*pVideo << " addr: " << hex << pSection->sh_addr << newline;
JAL
Re: printf functionality from cout?
thank you,
That is precisly the type of thing I wanted, that allows me to specify what I want without having to define the variable to be specifically that type.
That is precisly the type of thing I wanted, that allows me to specify what I want without having to define the variable to be specifically that type.
Getting back in the game.