[Gambas-user] Dynamically loading components

Bruce Bruen bbruen at ...2308...
Wed May 11 01:07:33 CEST 2011


On 11/05/11 08:13, Sebi Kul wrote:
> Hi!
>
> I am using Gambas 3 to develop a modular application. I would like to
> know if it's possible to load components dynamically (If the component
> file exist, load it, if not, skip the execution code where the
> component's functions are called)
>
> What I have so far is this:
>
> ==========
> Try Component.Load("daily-school-online.gambas")
>
> If Component.IsLoaded("daily-school-online.gambas") Then
>      OnlineModuleLoaded = True
> Endif
> =========
>
> But for this to work, I have to add the component as a library on the
> projects setting dialog, meaning that if the file does not exist, the
> program wont start. I also have the problem that if I don't add the
> component as a library, the project wont compile, as the parser will
> find functions that don't exist on this project, but are declared on the
> component.
>
> Does anybody have any ideas on how to achieve this?
>    
>
>
> ------------------------------------------------------------------------------
> Achieve unprecedented app performance and reliability
> What every C/C++ and Fortran developer should know.
> Learn how Intel has extended the reach of its next-generation tools
> to help boost performance applications - inlcuding clusters.
> http://p.sf.net/sfu/intel-dev2devmay
>
>
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>    
I don't know if this will help, but I have had a situation that is a 
little bit like what I think you are after and solved it this way.

Create a base component containing classes from which your optional 
component classes will inherit and in each of these, provide "stub" 
functions that will be overridden by the optional class functions.  You 
can also use the _unknown special method in some instances to provide a 
general handler for "bad" calls.

In other words, say daily-school-online has a class "school" with a 
method "connect", then create a component "daily-school-base" with a 
"school-base" class and have school inherit school-base.  school-base 
should expose a method "connect" that may just raise an error "not 
online" or whatever.

In essence, I suppose, this is a way of specifying template classes for 
your real classes that will provide fallback implementations of 
functions that will not be available if the real component is not loaded.

Note that, in my case I also have to declare the object variables as the 
base class and then instatiate them as the real class.  For example:
     PRIVATE o$localschool as school-base  'as a global
and in the optional code section
     o$localschool=NEW school()

But you may have to instantiate the object using a class name variable 
in your case (i.e. object.new("school")  ) to get the compiler to ignore 
the missing classes.

hth,
bruce



More information about the User mailing list