Source: fun/LoadableClass.h


Annotated List
Files
Globals
Hierarchy
Index
//  LoadableClass - nearly useless base class for use with ClassLoader
//  Copyright (C) 2000 rusty, rusty@sgi.com
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#ifndef _FUN_LOADABLECLASS_H
#define _FUN_LOADABLECLASS_H

#include 
#include 

namespace fun {

        /**
         *  This is an extreme bummer.  ClassLoader::instantiate() wasn't working when
         *  INSTANTIATOR(FooClass) was returning a void * instead of a pointer to a
         *  polymorphic object.  (Nuts!)  So unfortunately this stuff must intrude
         *  even more into your code.  Any class loaded & instantiated by ClassLoader
         *  must inherit from LoadableClass.
         */
        class LoadableClass
        {
	public:
                /**
                 *  A no-op default constructor.
                 */
                LoadableClass() {};
                /**
                 *  This does nothing, except to insure that your LoadableClass is
                 *  polymorphic.  I'm really depressed about this.
                 */
                virtual ~LoadableClass() {};
        };

        /**
         *  If you want a class to be loadable by ClassLoader, put this macro
         *  in the class' .c++ file.  Also, your class needs to have a constructor
         *  which takes no arguments, as that's what's going to be called.  (And...
         *  sigh... your class must inherit from LoadableClass.)
         *
         *  All "INSTANTIATOR(Foo)" does is create the following code:
         *    extern "C" { LoadableClass * newFoo() { return new Foo(); } }
         *  So if you want to do that yourself, or something more sophisticated,
         *  knock yourself out.
         */
#define INSTANTIATOR(clsnm) extern "C" { LoadableClass * new ## clsnm() { return new clsnm; } }

        /**
         *  If you want a class to be loadable by ClassLoader, and you multiply
         *  inherit from more than one ClassLoader subclass (that is, the compiler
         *  gives you "'LoadableClass' is an ambiguous base class..." errors), put
         *  this macro (instead of INSTANTIATOR) in the class' .c++ file.  Also, your
         *  class needs to have a constructor which takes no arguments, as that's
         *  what's going to be called.  (And... sigh... your class must inherit from
         *  LoadableClass.)
         *
         *  All "INSTANTIATOR_UNAMBIGUOUS(Foo, SuperFoo)" does is create the following code:
         *    extern "C" { LoadableClass * newFoo() { return (SuperFoo *)(new Foo()); } }
         *  So if you want to do that yourself, or something more sophisticated,
         *  knock yourself out.
         */
#define INSTANTIATOR_UNAMBIGUOUS(clsnm, supercls) extern "C" { LoadableClass * new ## clsnm() { return (supercls *)(new clsnm); } }

        /**
         *  If you want a class defined in a namespace to be loadable by ClassLoader,
         *  put this macro (instead of INSTANTIATOR) in the class' .c++ file.  Also,
         *  your class needs to have a constructor which takes no arguments, as that's
         *  what's going to be called.  (And... sigh... your class must inherit from
         *  LoadableClass.)
         *
         *  All "INSTANTIATOR(NameSpace, Foo)" does is create the following code:
         *    extern "C" { LoadableClass * newNameSpace_Foo() { return new NameSpace::Foo(); } }
         *  So if you want to do that yourself, or something more sophisticated,
         *  knock yourself out.
         */
#define NAMESPACE_INSTANTIATOR(ns, clsnm) extern "C" { LoadableClass * new ## ns ## _ ## clsnm() { return new ns::clsnm; } }

        /**
         *  If you want a class defined in a namespace to be loadable by ClassLoader,
         *  and you multiply inherit from more than one ClassLoader subclass (that is,
         *  the compiler gives you "'LoadableClass' is an ambiguous base class..."
         *  errors), put this macro (instead of NAMESPACE_INSTANTIATOR) in the class'
         *  .c++ file.  Also, your class needs to have a constructor which takes no
         *  arguments, as that's what's going to be called.  (And... sigh... your
         *  class must inherit from LoadableClass.)
         *
         *  All "NAMESPACE_INSTANTIATOR(NameSpace, Foo, SuperFoo)" does is create the following code:
         *    extern "C" { LoadableClass * newNameSpace_Foo() { return (SuperFoo *)(new NameSpace::Foo); } }
         *  So if you want to do that yourself, or something more sophisticated,
         *  knock yourself out.
         */
#define NAMESPACE_INSTANTIATOR_UNAMBIGUOUS(ns, clsnm, supercls) extern "C" { LoadableClass * new ## ns ## _ ## clsnm() { return (supercls *)(new ns::clsnm); } }

};  //  namespace fun

#endif  //  _FUN_LOADABLECLASS_H

Generated by: stephan on cheyenne on Mon Aug 11 14:06:52 2003, using kdoc 2.0a54.