#if defined WIN32
#pragma warning( disable : 4786)
#endif

#include "Prototype_repository_impl.h"

#include "Log.h"

using namespace std;

namespace MM
{

namespace 
{
template <typename T>
void destroy_prototype ();
}

Prototype_repository_impl::
~Prototype_repository_impl()
{
    /*for( map <Text, Any>::iterator it = map_.begin();
         it!=map_.end();  ++it )
    {
        bool success = it->second.destroy();
        CHECK ("Deleted", success);
    }//*/

    /*while (! stack_.empty())
    {
        delete stack_.top();
        stack_.pop();
    }//*/

    while (! stack_.empty())
    {
		Text name = stack_.top().type().name();
        bool del = stack_.top().destroy();
		
		{
			if (del)
				log() << "destroy " << name << "\n";
			else
				log() << "remove  " << name << "\n";
		}//*/

        stack_.pop();
    }
}

void Prototype_repository_impl::
adopt (Any const& prototype, type_info const & type)
{
    //REQUIRE (Text("No '") + type.name() + "' key.",
    //    map_.find (type.name()) == map_.end());
    //log() << "adopt   " << type.name() << "\n";

    map_[ type.name() ] = prototype;
    stack_.push (prototype);
}

//void * Prototype_repository_impl::
Any & Prototype_repository_impl::
find (type_info const & type)
{
    REQUIRE (Text("Prototype of \"") + type.name() + "\" exist",
    	map_.find (type.name()) != map_.end());

    return map_.find (type.name())->second;
}

Any & Prototype_repository_impl::
find  (Text const& type)
{
    REQUIRE (Text("Prototype of \"") + type + "\" exist",
        map_.find (type) != map_.end());

    return map_.find (type)->second;
}

void Prototype_repository_impl::
adopt (Text const& type, Any  const& prototype)
{
    map_[type] = prototype;
    stack_.push (prototype);
}

}//MM
