Page 2 of 2

Re:Using C++ for os development

Posted: Wed Nov 03, 2004 1:10 am
by Solar
aladdin wrote: I think that coding a kernel with C++ is not a very good idea since the ame of using such languages is reusable modules, but kernels doesn't need such features
Erm... no, and no.

The aim of C++ is to provide a language allowing to do more things. Namespaces and function overloading alone made it worthwhile for me.

I implemented a KVideo class instantiated as a global kout object, and did kout << "Hello " << 42 << hex << 127 << endl; and got the same output as from kprintf("Hello %d%x\n", 42, 127); - only without having to do string parsing and variable argument list parsing. Just as an example.

And what do you mean with "reusable modules"? Any decent language attempts to minimize interdependencies. C does it with modules, C++ does it with classes. I don't want to start an argument on which one is better, or that you should use C++, but saying that "kernels should be in C because they don't need modularity" is a bit short-sighted.

Re:Using C++ for os development

Posted: Wed Nov 03, 2004 8:48 am
by oswizard
Disclaimer: This is simply my opinion.

I couldn't imagine coding a kernel, or anything, for that matter, in plain C. Function overloading, for one thing, as Solar said, is great, and the C practice of declaring all variables at the start of the function drives me crazy. Not to mention the /*---*/ comments for everything...

On the more serious stuff, I have written an object manager where all the objects derive from an Object class, all devices derive from a Device class which derives from the Object class, all filesystems ..., all files ..., you get the picture. I am using virtual functions all the time.

Oh, and call me a traitor, but I am using Microsoft's compiler to build my OS completely within Visual Studio. A quick search of the MSDN documentation brings up a discussion of how global constructors and destructors are handled, and I am not using any exceptions, RTTI, or other standard c++ features. I tried writing my OS using GCC, but, being a newbie to its interface, its myriad command line options and horrid (IMHO) inline asm syntax, I was discouraged. Oh - and my boot loader can read a PE kernel - I compile it as a DLL.

Anyways, just my opinion on why C++ is such a great thing.

Mike

Re:Using C++ for os development

Posted: Wed Nov 03, 2004 10:33 am
by Colonel Kernel
Mike wrote: ...the C practice of declaring all variables at the start of the function drives me crazy. Not to mention the /*---*/ comments for everything...
Neither of these issues apply to C99. I happily declare variables at the point of use and use // comments. "const" doesn't seem to be "const" the way I expect it though -- declaring a static array sized with a "const" won't compile in C99, although it does in C++. :(
I am using virtual functions all the time.
I've come across the need for polymorphism here and there, but I choose to implement it without vtables. From a design perspective, I'm doing everything in a purely interface-based manner -- no inheritance whatsoever. Here's what it looks like in C (note that this is off the top of my head and probably won't compile):

Code: Select all

// IFoo.h

typedef void (* IFoo_helloFunc)( void*, const char* );

typedef struct
{
    IFoo_helloFunc hello;
} IFoo_itable;

typedef struct
{
    IFoo_itable* iptr;
    void* obj;
} IFoo;

...

// MyFoo.h
typedef struct MyFooStruct MyFoo;

void MyFoo_hello( MyFoo* this, const char* msg );

IFoo createFoo( void ); // Factory function.

// MyFoo.c

static IFoo_itable s_MyFoo_itable;

struct MyFooStruct
{
    ... // Actual fields of MyFoo.
};


void MyFoo_hello( MyFoo* this, const char* msg )
{
    ...
}

...

// Probably done in some kind of "init" function... Moral
// equivalent of a static constructor in C#...
s_MyFoo_itable.hello = MyFoo_hello;

...

IFoo createFoo( void )
{
    MyFoo* myFoo = ... // Get a MyFoo from somewhere...
    IFoo foo;
    foo.iptr = &s_MyFoo_itable;
    foo.obj = myFoo;
    return foo;
}

// SomewhereElse.c

// Notice the actual type of foo is unknown, and the behaviour
// is determined by the interface dispatch table (itable).
IFoo foo = createFoo();
foo.iptr->hello( foo.obj, "world!" );
It's quite verbose, but it gets the job done. It has the advantages that:
  • the fields of "classes" such as MyFoo are better encapsulated (due to the use of opaque pointers), and
  • If you have a "class" that implements multiple interfaces, the size of each object does not grow with additional vptrs, because there are no vptrs.
The obvious disadvantage (besides the syntax) is that "interface references" are twice the size of regular pointers.

It's actually not possible to do this directly in C++, although there was a compiler out there called HeronFront that could do this kind of thing automatically (http://www.heron-language.com/).

Re:Using C++ for os development

Posted: Thu Nov 04, 2004 11:40 am
by Christopher Diggins
Greetings,

I wrote the HeronFront translator, it is no longer supported but is still available at http://www.cdiggins.com . It is now being replaced by an interfaces library written using macros in C++ which does exactly what you want. An early version of the library is available at http://www.heron-language.com/heronfront.html . This library should be in excellent shape with full documentation in two to four weeks. The interfaces library is intended to be submitted eventually to Boost.

Christopher Diggins
http://www.heron-language.com