Objects Unique IDs
Well this week was not really fruitful, it was more into doing research and understanding what should be done next.So before talking about what happened let me start first by briefing you of what has been accomplished so far.
- Codemirror integration with Witty
- Saving and Loading the user code file
- Checking the user code for syntax errors
- updating the user with those errors and warning.
You can refer to my previous blogs entries for more information about these topics. 🙂 .
The new challenge for this week was to come up with a way to :
- know when the user define or declare objects of certain classes.
- know when the user delete those objects
These information is really important for the communication between the codemirror and the other schematic editor used in this project.The first thing came to my mind was to check the user code and extract the the objects names and use them as IDs using the regular expressions but it’s not that simple as it sounds, because the way the user do coding is really unpredictable and moreover there are several ways to declare objects for example :
Alphabet G ; // the most common way- declaring object G of class Alphabet.
new Alphabet ; // The object declared doesn’t have name
Alphabet *p=new Aphabet  ; // declaring 26 objects share the same name
So apparently, using the objects name was not a reliable idea.Well after that I was advised by Dr. Shawn to try to use the object file information to come up with something that can be used as an ID for the user object. It was really my first time to go this deep, I never bothered myself before to go and look for what the output of compiler looks like or how the final executable file is produced. But this maybe because usually when I do programming I use IDE which takes care of all these steps [ compiling-linking and executing].
Next I’ll briefly talk about the object file .. you can skip the next paragraph if you already know what it is.
In short, the object file is a file produced by the compiler, assembler or any other language translator. and it’s used as an input for the linker( which combines the object files and libraries to produce one executable ) . The object file usually contains information like Symbols names, compiled code, imports(which symbols the compiled code references) and exports (which symbols the object file makes available to OTHER object files) .
When you use certain function like cos() (function that computes the cosine) from the cmath library, the actual code of this function doesn’t reside in you .cpp file it’s inside the cmath library so during the compilation phase the compiler knows that this function is defined somewhere. The linker then must be invoked after the compilation for linking and producing one executable that contains all the memory addresses references for symbol it contains (functions, global variables..etc). The linker could also report errors or notify the compiler if the function doesn’t exist.
Okay now let’s go back to the main issue, after doing some exploration I came to these finding:
The object file usually contains information about the functions and global variables specially (which makes sense because this what linker needs).
This means if the user declare the objects of those classes as local variables they will not be existed (as far as I know) in the object file.
So the solution of this was to define constructor for every class, so during the declaration the constructor will be called. This actually worked and now there was some information regarding those classes. But a problem occurred, regardless how many objects of class the user have declared the output in the object file using “nm command” contained one constructor call for that class. Hmmmmm… later I explored the elf file (Executable, linkable format) but nothing useful was there too.
I thought of using the objects addresses as IDs as well but this didn’t work. As the address of the object changes with different program execution therefore, I won’t be able to keep tracking them. Moreover, the user can also change the order of the declaration or insert the declaration of the object inside a function which will change its address too . So I really didn’t come up with a way to solve this matter, but what I have decided for now is to follow the suggestion which was previously made by Dr.shawn which was asking the user to provide different IDs for different objects of class he/she declares , meaning that he will pass the ID as an argument of the class template.The class name and ID could be then obtained from the object file. This may affect the user experience as he/she should keep tracking of what ID he/she has declared before to avoid repetitions, but I think a good notifying technique will make it – a little bit- more friendly 🙂