/* 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_CONTEXT
#define OSGCOMPUTE_CONTEXT 1

#include <limits.h>
#include <vector>
#include <set>
#include <osg/Object>
#include <osg/GraphicsContext>
#include <osgCompute/Export>
#include <osgCompute/Resource>

namespace osgCompute
{
    class Context;
    class Resource;

    typedef std::map<unsigned int, osg::ref_ptr<Context> >						ContextMap;
    typedef std::map<unsigned int, osg::ref_ptr<Context> >::iterator			ContextMapItr;
    typedef std::map<unsigned int, osg::ref_ptr<Context> >::const_iterator		ContextMapCnstItr;


    /**
    */
    class LIBRARY_EXPORT Context : public osg::Referenced
    {
    public:
        Context();

        virtual bool init();
        virtual void apply();
        virtual void detach();

        virtual unsigned int getId() const;

        virtual void connectWithGraphicsContext( osg::GraphicsContext& gc );
        virtual osg::GraphicsContext* getGraphicsContext();
        virtual const osg::GraphicsContext* getGraphicsContext() const;
        virtual void removeGraphicsContext();
        virtual bool isConnectedWithGraphicsContext() const;

        virtual void setDevice( int device );
        virtual int getDevice() const;

        virtual bool isResourceRegistered( const Resource& resource ) const;
        virtual void clearResources() const;

        static unsigned int getAppliedContextId();
        static const Context* getAppliedContext();
        static const Context* getContext( unsigned int id );
        static const Context* getContextFromGraphicsContext( unsigned int graphicContextId );

        virtual void clear();
        virtual bool isClear() const;

    protected:
        friend class Resource;
        virtual ~Context();
        void clearLocal();

        void registerResource( const Resource& resource ) const;
        void unregisterResource( const Resource& resource ) const;

        unsigned int                        _id;
        osg::ref_ptr<osg::GraphicsContext>  _gc;
        bool                                _clear;
        int                                 _device;
        mutable ResourcePtrSet				_resources;

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

#endif //OSGCOMPUTE_CONTEXT
