<Joy>
One of the attractive features of the BeOS for me is that development of applications for it is meant to be done using C++. I also think highly of the messaging architecture used in the BeOS, and when the BArchivable mechanism was added to the Be Application Programming Interface (API), I was very much intrigued -- I thought I could see ways to apply the mechanism to implement some ideas I have been developing in a desultory way for the past couple of years.Thus it was a bitter pill to swallow when I discovered that the BArchivable mechanism simply didn't work when the objects I wanted to archive were instances of classes defined by templates, or, rather, what didn't work was re-instantiating them from their archive BMessage. The reason, I discovered, was that instantiate_object( ) doesn't take the naming conventions used for member functions in template classes into account when it's searching for the appropriate instantiate( ) function to call.
</Joy><Search>
In general, it would be very difficult to reimplement instantiate_object( ) to insure that it could handle this task, but my particular interest is in template classes used only with a parameter that's a built-in data type. That is, for a template liketemplate< class T > class Number : public BArchivable { . . . };
its agreed-upon use would be restricted to the built-in data types in C++, so I could haveNumber< int > a; Number< float > x;
but neverclass Spiffy; Number< Spiffy > some_spiffy_number;
Working with tools supplied for the PowerPC platform, and given this restriction to use only built-in data types as the parameters for the templates I wanted to derive from BArchivable, I next examined whether I could get any farther by calling find_instantiation_func( ) directly.It also fails to find the appropriate instantiate( ) function, but by running pefdump on the shared library containing the implementations for my template classes, I was able to see what were the actual names of the instantiate( ) functions produced by the compiler. Then I changed the archive( ) functions in the templates so that they added their actual class names to the archive BMessage, and used this actual name in my call to find_instantiation_func( ), and, lo and behold, success!
</Search><Joy>
But only on PowerPC!!
<abrupt /Joy>
On the x86 platform, under BeOS Release 3.x, my approach was doomed, because the Metrowerks compiler then being used truncated the names of functions it passed on to the linker to 30 characters in length (or maybe it was the linker that did the erroneous truncation; I don't recall). So that even if I passed find_instantiation_func( ) the actual name of the class whose instantiate( ) I wanted it to find, that name was no longer available in the binary file!
<Resume Search>
With Release 4 of the BeOS came the change to using the gnupro development tools, and after a while I returned to considering how to have my templates be BArchivable. I found that although the truncation problem no longer was a factor, find_instantiation_func( ) was unable to find the instantiate( ) function I wanted it to.<What about add-ons?>
Meanwhile, I had poked around with add-ons some, and been pleased to learn that applications, shared libraries, and add-ons are very similar to each other on both the PowerPC and x86 platforms; if I couldn't get find_instantiation_func( ) to find my functions in my shared library, what would prevent me from loading it as an add-on and searching for the function myself? The answer turned out to be: nothing, and my really_find_instantiation_func( ) works on both platforms with Release 4.x.
</What about add-ons?></Resumed Search>
<Joy>
Here is how I make this work:
<Caveat>
What I've got here is not supported by Be, Inc., and depends on information I'm sure they reserve the right to change with any release of the BeOS. Consider carefully whether you want to make use of my approach in any of your development.
</Caveat>Here's an example for both platforms of the use of the BArchivable mechanism with a C++ template class parameterized by built-in types only: BArchivable_templates.zip. It contains BeIDE projects for BArchivable_lib.so, the implementation of the BArchivable template classes, BArchivable_symbols, a program which loads BArchivable_lib.so as an add-on and prints all of the symbols in it to stdout (a tool I used to develop really_find_instantiation_func( )), and BArchivable, a program which instantiates a Derived< int32 > object on the stack, archives it, then reinstantiates it from the archive as another object on the heap.
Copyright © 1999, 2000 by John R. Ashmun ( Be Developer ID #2795).
All rights reserved.
5 Dec 1999
Updated 4 Apr 2000