Page 2 of 2

Re:Could drivers be objects??

Posted: Thu Oct 17, 2002 12:44 pm
by dronkit
this is just an example of a class using a driver interface. It is also an example of how to "get the code into memory" in a safe manner.

This is just an example, of course it is incomplete, you must code whatever you need in your video driver ;)

Re:Could drivers be objects??

Posted: Thu Oct 17, 2002 1:57 pm
by soilwork
so it is C++ not java


to me that looks like a linked list that is not a list but only the first

From what I read there its a class that creates one instance of itself implicitly...

but the thing is: How am I going to be able to access the instance from the gui obj if its within the class??

I mean how can I pass the obj to the GUI as an attribute??

Re:Could drivers be objects??

Posted: Thu Oct 17, 2002 2:49 pm
by dronkit
it IS java ;)

And it's definite not a linked list ;)

It's been a while I'm out of the C++ field so i'll try to explain what pype.clicker suggested in his code and later i could give you some
tested usable code ;)

Code: Select all

class VideoDriver implements Driver {
    static private VideoDriver driver=null;
    static public VideoDriver getDriver() {
         if (driver==null) driver=new VideoDriver();
         return driver;
    }
    private VideoDriver() {
         // initialization & stuff
    }
}
VideoDriver() is the constructor. See it is declared as private. That's because of the existence of getDriver() and that you don't want to handle several VideoDrivers.

A static variable in java means that no matter how many objects you have
instantiated for this class (no matter if 0 or 1 or 'n', that means this applies to abstract classes too) they will only reference to one and only one memory position.

So the variable "driver" here is private AND static. That means it
will not be accesible from outside the code of this class or its descendants, none of them can override it and they will ALWAYS
reference this variable when "getDriver()" is called.

So you can't instantiate this class directly, but you have the static method getDriver(). A static method is like a static variable, you can always use it wether you have an object or not.

So it is an interesting example, indeed. From your kernel, you declare a variable of type "VideoDriver". To initialize the object, you don't call the constructor but the method "getDriver()". This method will return a valid reference to a valid VideoDriver object and no matter how many you want to initialize, only the first one matters.

In the second example, pype.clicker used exceptions (avalable in c++ too) to inform the caller that the driver is already in use.

Note that VideoDriver is a class that can't be instantiated but it still can give you a valid instance ;)

There's the object you passed to the gui.

Re:Could drivers be objects??

Posted: Thu Oct 17, 2002 3:06 pm
by dronkit
Also forgot to say that the first line declares a class VideoDriver which "implements" an interface called "Driver".

This interface can be an abstract class in C++ and defines very basic things for a generic driver.

Re:Could drivers be objects??

Posted: Thu Oct 17, 2002 3:08 pm
by soilwork
THat is COOL.. Java is as cool as C++ :)

ok so that is the way Id go about creating the thing in Java...


I have a question:
can I do the getdriver() thing in C++??

and if I use the constructor in C++ does the object load in a diff place in the program??? I mean it loads as opposed of just getting the address of the original not instance class...

now just using a constructor in C++ means doing it statically, right?? That means every time I make an instance it copys itself in memory again???

and dynamically would be what??
dynamically in C++ is using the new and delete operators and accessing the data and procedures with -> instead of . (dot)


thanks for the info.. if u can explain a bit about the C++ works please.. dynamic vs static... and how they load in memory...

Re:Could drivers be objects??

Posted: Thu Oct 17, 2002 3:13 pm
by soilwork
so in C++ Id first create an abstract Driver class and then inherit Video Driver from it??? to do about the same thing with that java code??

thanks


PS. What java compillers are out there?? Could I write an OS in Java?? One of my main targets for my OS is portability.. so thats one of the reasons I want to use only objects/classes.. Java from what I heard is a modified C++ that has a diff memory alocation and.. for objects that makes stuff platform independent and more portable...

either way, C++ is my fave so far.. Javas interesting too.. Pascal is OK and Visual Basic SUX(The RAD is good though.. just like BCBuilder)

Re:Could drivers be objects??

Posted: Thu Oct 17, 2002 3:27 pm
by dronkit
There's the JSDK from Sun (Java Standard Development Kit). it works fine. There's kaffe from ibm (correct me if i'm wrong), then you have the blackdown port...oh well, there are plenty.

http://java.sun.com
http://www.kaffe.org
http://www.blackdown.org/

Well, everything has it pro's and con's.

Pascal:
* Don't you even think about it ! ;)

C:
* Excellent choice for everything 'codeable' ;)
* can be hard to master.
* quite portable. i'll say pretty damn portable. but it always depend on what your intentions are.

C++:
* can be *very* hard to master and make bug-proofable code.
* pretty portable too.
* slower than c.
* requires extra job done like garbage collection and a correct environment to run.
* when the code is good... it IS good. ;) but you can get shot on the foot too ;)

Java:
* it can be SLOW. *bad*
* Portable! you can run your os on a smart card or even a pen ;) *good*
* quite easy to program *good*
* security comes along with the compiler, so code will tend to contain less bugs *good*
* strictness and loss of freedom came with security. You loose the freedom C/C++ give you *bad*

So it's up to you. There are a couple of Java OS'es around there like http://www.jos.org and jos.sourceforge.net. There are some in C++ too, i have no links of these. sorry.

regards

Re:Could drivers be objects??

Posted: Thu Oct 17, 2002 4:54 pm
by soilwork
Ok, thanks. I think Ill stick to C++ due to my experience and the fact that its faster and the fact that I already think C++ and would help in making bug free code.

Now, how would I put that java code into C++??

thanks

now basic C is easy to use as a basic procedural language... same logic with assembler like MASM... etc.

So how do I do that in C++?

Re:Could drivers be objects??

Posted: Thu Oct 17, 2002 4:57 pm
by soilwork
I figgured out that static vs dynamic in C++ is only linktime vs runtime... in terms of making readonly objects etc... putting objs into ROM


now which one whould I use for my os?? really I dont get what the advantages and disadvantages are.. I use static cuz thats what Im used to, but y should I use either one???

thanks

Re:Could drivers be objects??

Posted: Thu Oct 17, 2002 5:18 pm
by dronkit
I'm sorry, i think i missed one of your posts with a lot of questions ;)

i'll answer them:
I have a question:
can I do the getdriver() thing in C++??
No, C++ will not let you declare the constructor as private.

The thing here is philosophy. Java has one, and it differs from C++. That code is 100% tipical java. So you can't translate it literally to C++ (because if you're going to use C++ you may like to actually take advantage from it)
and if I use the constructor in C++ does the object load in a diff place in the program??? I mean it loads as opposed of just getting the address of the original not instance class...
You can merely instantiate it. Your variable will be just a regular offset within your data section.
now just using a constructor in C++ means doing it statically, right?? That means every time I make an instance it copys itself in memory again???
Yes, you get separated offsets in memory of every object you instantiate. This is the core of OOP programming.
and dynamically would be what??
dynamically in C++ is using the new and delete operators and accessing the data and procedures with -> instead of . (dot)
It's up to you. If you have the memory manager up & running (like you should be if you're gonna mess with objects ;) ) there's no problem in using one thing or the other.
thanks for the info.. if u can explain a bit about the C++ works please.. dynamic vs static... and how they load in memory...
static data is "physically" stored in the file, at least its space. dinamic data is when you "new" or "malloc()" memory space.
I figgured out that static vs dynamic in C++ is only linktime vs runtime... in terms of making readonly objects etc... putting objs into ROM
now which one whould I use for my os?? really I dont get what the advantages and disadvantages are.. I use static cuz thats what Im used to, but y should I use either one???
thanks
It's up to you. speed vs size.
Ok, thanks. I think Ill stick to C++ due to my experience and the fact that its faster and the fact that I already think C++ and would help in making bug free code.
good. watch your foot. ;)
Now, how would I put that java code into C++??
thanks
now basic C is easy to use as a basic procedural language... same logic with assembler like MASM... etc.
So how do I do that in C++?
well, the static keyword when used in variables inside classes has the same meaning in c++ and java, so that's the same thing.

the only problem has to do with the constructor, you can't translate that code literally.

It requires a little thought i think, maybe some c++ guru is hearing us and eager to enlighten us ;)

Re:Could drivers be objects??

Posted: Fri Oct 18, 2002 5:29 am
by Pype.Clicker
i personally use C for the implementation (because it's close to the hardware, well-known calling conventions, etc. which makes it easy to interface with low-level stuffs), but as i use object-oriented design, JAva is the best meta-language to talk about how things will look like and express what i've got in my mind ...

i would *not* recommend java for OS programming because Java assumes a lot of things from the JVM (automatic memory management, safe multithreading, etc.) that you will HAVE to implement in an OS ...

Re:Could drivers be objects??

Posted: Fri Oct 18, 2002 7:03 am
by dronkit
well put, certainly.