Virtual C is the name of a custom C++-like syntax that we developed to support some Object Oriented Programming features in VUEngine.
It consist of a set of scripts that act together as a transpiler that converts code written in Virtual C to standard C.
Features
Virtual C supports the following usual OOP features:
And adds a few features of its own:
Limitations
Virtual C doesn’t support all of the common OOP concepts or common features of full blown OOP languages. Among those unsupported are:
- Multiple inheritance
- Public or private attributes
- Protected methods
- Stack allocated class instances
- Method/operator overloading
In Virtual C, all attributes are protected; while all methods are either public or private.
Another limitation is that all class instances have to be dynamically allocated since ClassName
declares always a pointer and there is no mechanism to automatically call a constructor at the declaration of instances, therefore, there can no be instances allocated in the stack. But since Virtual C only targets -for now- the Virtual Boy, and its CPU doesn’t have data cache, the performance impact is not as big as on architectures that have it.
Virtual C adds a couple features of its own, thanks to the fact that it is a custom implementation on top of C.
Class modifiers
The class modifiers are:
abstract
singleton
dynamic_singleton
static
extension
mutation
Abstract classes
The abstract
class modifier prevents the creation of class instances through the new
keyword, without the requirement of there being at least one pure virtual method.
Classes that have pure virtual methods (by qualifying their declaration with = 0;
) are implicitly abstract.
Singleton classes
The language support the declaration of classes that can only have a single instance and adds automatically the necessary code to prevent more than one instantiation.
The singleton classes’ instances are allocated in global memory.
Since singletons are globally accesible, they put at risk the stability of the program by being modifiable from anywhere. So, it is important to have some mechanism to help preventing as much as possible such modifications.
The transpiler adds the secure
keyword to decorate non static methods of singleton classes that should not be called but by specific classes. Then, to restrict from were it is legal to call such method, an array of classes must be defined globally in non volatitle memory.
There are some limitations due to the fact that globality cannot really be so easily defeated. And this contraption doesn’t pretend to achieve that. Instead, it is a tool to help preventing potentially dangerous code paths when developing programs using Virtual C.
Extensions classes
Classes can be extended through the use of the extension
keyword. They provide a mechanism to achieve something like runtime method re-implementation on specific virtual methods. Extension classes don’t have to, and cannot, provide a constructor and destructor. Entensions classes are meant to provide a mechanism to change at runtime the implementation of a virtual method affecting all the instances of the original class immediately.
Mutation classes
Virtual C implements polymorphism by adding a virtual table pointer to each object, which means that it can be manipulated in real time. Mutation classes permit to override or to extend a class’ functionality by allowing an object’s virtual table pointer to change its target during runtime, making the instance subject to different implementations of virtual methods or capable of reacting to new methods provided by the mutation class.
These classes have the following constraints:
- They have to inherit from a non abstract class
- They have to be data-invariant with respect to the base class (ie: they cannot add attributes of their own)
- They cannot provide a constructor or...