There we are, my first entry about something really related to programming.
I found out, working on various testing project that the main problem was interaction between elements in the vessel when we are programming for Orbiter. In this entry, I will explain my idea to correct that problem and ease the development of vessels.
For example, let's base ourselves with a car. If I would like to program a car, I would probably consider it has a big class with basic I/O functions (accelerate, brake, change oil, etc.). This class, in order to work, would need smaller classes like the transmission, the wheels, the motor, etc. And these classes would need to interact each other. The motor would rotate the transmission, and transmission would rotate the wheels, etc.
One solution would be to let the car handle all the interactions. This is fairly simple to implement in real code. However, it creates a lot of mutator functions (also known as setter). In our example, the car would need to call an accessor function of the motor to know what is the current force created by the motor. Then, the car would set this force to the transmission using a setter and etc.
Therefore, I found a way to bypass this limitation. However, there was a cost for this. I sacrificed the encapsulation process just a bit. In fact, every components of the vessel class will be publicly available. This does not mean that the smaller classes do not use encapsulation. They actually use it. However, they have direct access to the other classes. Here is a schematic of the process:
As you can see, the vessel class has 3 smaller classes. These classes have a pointer to the vessel class, who has the pointers to every sub-classes. Therefore, every classes know each other.
Now, the sub-classes can use the pointer to the vessel (m_pVessel in our example) and and interact with the other classes. For example, SmallerClass1 could do m_pVessel->m_smallerClass2->Run();. It calls a function inside SmallerClass2 (m_smallerClass2 is a SmallerClass2 defined in Vessel).
There you are, you can now have sub-classes that know about the other sub-classes and interact with them.
Happy programming!
I found out, working on various testing project that the main problem was interaction between elements in the vessel when we are programming for Orbiter. In this entry, I will explain my idea to correct that problem and ease the development of vessels.
Making the objects know each other
The first thing I had to correct was that each component of the vessel needs to know about the other objects and interact with them. However, why do I have this problem? Simply because I see the vessel as a big class with smaller classes that interact with each other.
For example, let's base ourselves with a car. If I would like to program a car, I would probably consider it has a big class with basic I/O functions (accelerate, brake, change oil, etc.). This class, in order to work, would need smaller classes like the transmission, the wheels, the motor, etc. And these classes would need to interact each other. The motor would rotate the transmission, and transmission would rotate the wheels, etc.
One solution would be to let the car handle all the interactions. This is fairly simple to implement in real code. However, it creates a lot of mutator functions (also known as setter). In our example, the car would need to call an accessor function of the motor to know what is the current force created by the motor. Then, the car would set this force to the transmission using a setter and etc.
Therefore, I found a way to bypass this limitation. However, there was a cost for this. I sacrificed the encapsulation process just a bit. In fact, every components of the vessel class will be publicly available. This does not mean that the smaller classes do not use encapsulation. They actually use it. However, they have direct access to the other classes. Here is a schematic of the process:
As you can see, the vessel class has 3 smaller classes. These classes have a pointer to the vessel class, who has the pointers to every sub-classes. Therefore, every classes know each other.
How to implement it in C++
That was one of my problem. I had the idea, but I could not do it until I found this way. Let's look at each file.
- The header of our vessel class (Vessel.h) will include every header of its sub-classes. This is logic, because it needs to know those classes before defining them.
- The implementation of our vessel class (Vessel.cpp) will simply include its header. This is all standard for now.
- The header of our sub-classes (SmallerClass.h) will define our vessel by itself, without specifing its component. In other words, it will say to the smaller class that Vessel class does exist. What it looks like, we don't know yet. In its definition, it will be able to define a Vessel pointer in its private members. In actual code, it will look like this:
Code:
class Vessel; // Vessel class does exist
class SmallerClass
{
public:
SmallerClass(Vessel* vs); // we need a Vessel pointer at construction
private:
Vessel* m_pVessel;
};
- The implementation of our sub-class (SmallerClass.cpp) will include its header and Vessel.h.
Now, the sub-classes can use the pointer to the vessel (m_pVessel in our example) and and interact with the other classes. For example, SmallerClass1 could do m_pVessel->m_smallerClass2->Run();. It calls a function inside SmallerClass2 (m_smallerClass2 is a SmallerClass2 defined in Vessel).
There you are, you can now have sub-classes that know about the other sub-classes and interact with them.
Happy programming!
