/**
 * @file
 * $Id: framework.h 2492 2005-09-28 20:28:01Z plasmahh $
 * $Revision: 2492 $
 * $Author: plasmahh $
 * $Date: 2005-09-28 22:28:01 +0200 (Mi, 28 Sep 2005) $
 *
 * This file is part of The iWear Framework.
 * In particular this file is part of the Framework Core Library
 *
 * The iWear Framework 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 as in version 2 of the License.

 * 
 * The iWear Framework 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
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __IWEAR_AUTOLOADER_H
#define __IWEAR_AUTOLOADER_H
/**
 * @file
 * This is the autoloader header. it contains several classes, that will be
 * used to have a singleton-like interface to automatically load and initialize
 * needed components for the iwear framework components.
 * The header is sensitive to the components already included, so if you want
 * to use some classes, make sure they are included <b>before</b> this header
 */
namespace iwear {

#define MEM01 // Reserved for configuration
#define MEM02
#define MEM03
#define MEM04
#define MEM05
#define MEM06
#define MEM07
#define MEM08
#define MEM09
#define MEM10
#define MEM11

#define ARG01 // Reserved for configuration
#define ARG02
#define ARG03
#define ARG04
#define ARG05
#define ARG06
#define ARG07
#define ARG08
#define ARG09
#define ARG10
#define ARG11

#define ACT01 // Reserved for configuration
#define ACT02
#define ACT03
#define ACT04
#define ACT05
#define ACT06
#define ACT07
#define ACT08
#define ACT09
#define ACT10
#define ACT11

// We can later add more here if we need
#define MEM_INITIALIZER_LIST MEM01 MEM02 MEM03 MEM04 MEM05 MEM06 MEM07 MEM08 MEM09 MEM10 MEM11
#define CTOR_ARGUMENT_LIST ARG01 ARG02 ARG03 ARG04 ARG05 ARG06 ARG07 ARG08 ARG09 ARG10 ARG11
#define DTOR_ACTION_LIST ACT11 ACT10 ACT09 ACT08 ACT07 ACT06 ACT05 ACT04 ACT03 ACT02 ACT01 

/**
 * Everything that can be gathered from the autoloaders needs a config,
 * therefore we have a config here.
 */
struct empty_autoloader_base { };

class autoloader : public empty_autoloader_base 
{
#if defined(__IWEAR_CONFIGURATION_H)

#undef MEM01
#define MEM01 configuration(0),
    
#undef ACT01
#define ACT01 delete configuration;
protected:
    Configuration* configuration;

public:
    Configuration* get_configuration( void )
    {
	if(!configuration)
	{
	    configuration = new Configuration(bp,gp);
	}
	return configuration;
    }
#else
#error Need to include at least some basic management headers
#endif // defined(__IWEAR_CONFIGURATION_H)

#if defined(__IWEAR_BASEMODULEMANAGER_H)
#undef MEM02
#define MEM02 basemodulemanager(0),
#undef ACT02
#define ACT02 delete basemodulemanager;
protected:
    BaseModuleManager* basemodulemanager;

public:
    BaseModuleManager* get_basemodulemanager( void )
    {
	if( ! basemodulemanager )
	{
	    basemodulemanager = new BaseModuleManager(*get_configuration());
	}
	return basemodulemanager;
    }
#endif // __IWEAR_BASEMODULEMANAGER_H

#if defined(__IWEAR_SERVICEMANAGER_H)
#undef MEM03
#define MEM03 servicemanager(0),
#undef ACT03
#define ACT03 delete servicemanager;
protected:
    ServiceManager* servicemanager;

public:
    ServiceManager* get_servicemanager( void )
    {
	if( ! servicemanager )
	{
	    servicemanager = new ServiceManager( *get_configuration());
	}
	return servicemanager;
    }
#endif // __IWEAR_SERVICEMANAGER_H


#if defined(__IWEAR_EVENTDISPATCHER_H)
#undef MEM04
#define MEM04 eventdispatcher(0),
#undef ACT04
#define ACT04 delete eventdispatcher;
protected:
    EventDispatcher* eventdispatcher;

public:
    EventDispatcher* get_eventdispatcher( void )
    {
	if( ! eventdispatcher) 
	{
	    eventdispatcher = new EventDispatcher( get_servicemanager() );
	    eventdispatcher->Init();
	    eventdispatcher->Start();
	}
	return eventdispatcher;
    }
#endif // __IWEAR_EVENTDISPATCHER_H

#if defined(__IWREMOTE_ROOTARTANO_H)
#undef MEM05
#define MEM05 rootartano(0),
#undef ACT05
#define ACT05 delete rootartano;
protected:
    iwear::net::RootArtano* rootartano;
public:
    iwear::net::RootArtano* get_rootartano( void )
    {
	if( ! rootartano) 
	{
	    rootartano = new iwear::net::RootArtano( get_servicemanager(),get_eventdispatcher() );
	    rootartano->Init();
	}
	return rootartano;
    }
#endif // __IWREMOTE_ROOTARTANO_H

#if defined(__IWSENS_SENSORMANAGER_H)
#undef MEM06
#define MEM06 sensormanager(0),
#undef ACT06
#define ACT06 delete sensormanager;
protected:
    iwear::sensor::SensorManager* sensormanager;
public:
    iwear::sensor::SensorManager* get_sensormanager( void )
    {
	if( ! sensormanager )
	{
	    sensormanager = new iwear::sensor::SensorManager( get_basemodulemanager(), *(get_configuration()));
	}
	return sensormanager;
    }

#endif // __IWSENS_SENSORMANAGER_H

#if defined(__IWLOCATOR_LOCATIONMANAGER_H)
#undef MEM07
#define MEM07 locationmanager(0),
#undef ACT07
#define ACT07 delete locationmanager;
protected:
    iwear::sensor::location::LocationManager* locationmanager;
public:
    iwear::sensor::location::LocationManager* get_locationmanager( db::IWDB* db, const std::string& table )
    {
	if( ! locationmanager )
	{
	    locationmanager = new iwear::sensor::location::LocationManager(get_sensormanager(), db, table, *(get_configuration()));
	}
	return locationmanager;
    }
#endif // __IWLOCATOR_LOCATIONMANAGER_H

#if defined(__IWSENS_SENSORHANDLER_H)
#undef MEM08
#define MEM08 sensorhandler(0),
#undef ACT08
#define ACT08 delete sensorhandler;
protected:
    iwear::sensor::SensorHandler* sensorhandler;
public:
    iwear::sensor::SensorHandler* get_sensorhandler( void )
    {
	if( ! sensorhandler )
	{
	    sensorhandler = new iwear::sensor::SensorHandler( get_sensormanager(), *(get_configuration()));
	}
	return sensorhandler;
    }

#endif // __IWSENS_SENSORHANDLER_H



protected:
    std::string bp;
    std::string gp;

public:
    // Only declare that *after* all other macros
    autoloader( const std::string& b, const std::string& g
	    CTOR_ARGUMENT_LIST ) : 
	MEM_INITIALIZER_LIST
	bp(b),gp(g) 
    { 
    }

    ~autoloader() 
    { 
	DTOR_ACTION_LIST
    }
};

/// Legacy support typedef
typedef autoloader AutoLoader;
/*
    InputManager* inputmanager;

    SensorManager* sensormanager;

    SensorHandler* sensorhandler;

    power::PowerManager* powermanager;

    OutputManager* outputmanager;

    SensorSurveillance* sensorsurveillance;

    InputManager* get_inputmanager( void );

    ServiceManager* get_servicemanager( void );

    SensorManager* get_sensormanager( void );

    SensorHandler* get_sensorhandler( void );

    power::PowerManager* get_powermanager( void );

    OutputManager* get_outputmanager( void );

    SensorSurveillance* get_sensorsurveillance( void );
*/
} // namespace iwear

#endif // __IWEAR_AUTOLOADER_H
