/* osgCompute - Copyright (C) 2008-2009 SVT Group
 *                                                                     
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *                                                                     
 * This library 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 Lesse General Public License for more details.
 *
 * The full license is in LICENSE file included with this distribution.
*/

#ifndef OSGCOMPUTE_MODULE
#define OSGCOMPUTE_MODULE 1

#include <vector>
#include <osg/Object>
#include <osg/NodeVisitor>
#include <osgCompute/Export>
#include <osgCompute/Resource>
#include <osgCompute/Callback>

#define META_Module( libraryname, classname )                           \
    META_Resource( libraryname, classname, this )                       \

    
namespace osgCompute
{
    class Context;
    class Module;

    typedef std::vector<osg::ref_ptr<osgCompute::Module> >                          ModuleList;
    typedef std::vector<osg::ref_ptr<osgCompute::Module> >::iterator                ModuleListItr;
    typedef std::vector<osg::ref_ptr<osgCompute::Module> >::const_iterator          ModuleListCnstItr;

    /**
    */
    class LIBRARY_EXPORT Module : public Resource, public osg::Object
    {
    public:
        Module() : Resource(), osg::Object() { clearLocal(); }

        virtual bool init();
        virtual void clear();
        virtual void launch( const Context& context ) const = 0;

        inline void setLaunchCallback( LaunchCallback* pc );
        inline LaunchCallback* getLaunchCallback();
        inline const LaunchCallback* getLaunchCallback() const;

        virtual void acceptResource( Resource& resource );
        virtual bool usesResource( const std::string& handle ) const;
        virtual void removeResource( const Resource& resource );
        virtual void removeResource( const std::string& handle );
        virtual Resource* getResource( const std::string& handle );
        virtual const Resource* getResource( const std::string& handle ) const;

        inline void enable();
        inline void disable();
        inline bool isEnabled() const;

    protected:
        virtual ~Module() { clearLocal(); }
        void clearLocal();

        virtual bool init( const Context& context ) const;
        virtual void clear( const Context& context ) const;

        osg::ref_ptr<LaunchCallback>   _launchCallback;
        bool                           _enabled;

    private:
        // copy constructor and operator should not be called
        Module( const Module&, const osg::CopyOp& ) {}
        Module& operator=( const Module& copy ) { return (*this); }
    };

    //------------------------------------------------------------------------------
    inline void Module::setLaunchCallback( LaunchCallback* pc ) 
    { 
        _launchCallback = pc; 
    }

    //------------------------------------------------------------------------------
    inline LaunchCallback* Module::getLaunchCallback() 
    { 
        return _launchCallback; 
    }

    //------------------------------------------------------------------------------
    inline const LaunchCallback* Module::getLaunchCallback() const 
    { 
        return _launchCallback; 
    }

    //------------------------------------------------------------------------------
    inline void Module::enable() 
    { 
        _enabled = true; 
    }

    //------------------------------------------------------------------------------
    inline void Module::disable() 
    { 
        _enabled = false; 
    }

    //------------------------------------------------------------------------------
    inline bool Module::isEnabled() const
    {
        return _enabled;
    }
}

#endif //OSGCOMPUTE_MODULE
