/* Metallurgy - Higher Level Interface to Chordata
 * Copyright (C) 2001-2002  Jay Freeman (saurik)
*/

/*
 *        Redistribution and use in source and binary
 * forms, with or without modification, are permitted
 * provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the
 *    above copyright notice, this list of conditions
 *    and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions
 *    and the following disclaimer in the documentation
 *    and/or other materials provided with the
 *    distribution.
 * 3. The name of the author may not be used to endorse
 *    or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef METALLURGY_MODULE_H
#define METALLURGY_MODULE_H

#if _MSC_VER > 1000
#pragma once

// C4231: nonstandard extension used : 'extern' before template explicit instantiation
// C4251: '...' : class '...' needs to have dll-interface to be used by clients of class '...'
// C4660: template-class specialization '...' is already instantiated
#pragma warning ( disable : 4231 4251 4660 )

#endif // _MSC_VER > 1000

#include "menes-com/diapexis.h"
#include "menes-com/Handle.h"

#include "metallurgy/metallurgy.h"
#include "metallurgy/ModuleToken.h"
#include "metallurgy/TokenList.h"

#include "chordata/Tokens.h"
#include "chordata/IMetaDataImport.h"

#include "menes/win32/mmap.hpp"
#include "filet/PE.h"

#include <map>

namespace Metallurgy {

	class Assembly;
	class TypeDef;
	class TypeRef;

	class METALLURGY_API Module : public ModuleToken {
	  protected:
		Diapexis::Handle<Chordata::IMetaDataImport> import;

		std::wstring path;
		std::wstring name;
		GUID vid;

        menes::win32::mmapbuf mmap;

        Filet::Headers::DOS *dos;
        Filet::Headers::NT32 *nt;

        /*METALLURGY_EXT template class METALLURGY_API std::_Tree<Chordata::Token, std::pair<Chordata::Token const, class Token *>, struct std::map<Chordata::Token, class Token *>::_Kfn, std::less<Chordata::Token>, std::allocator<class Token *> >;
        METALLURGY_EXT template class METALLURGY_API std::map<Chordata::Token, class Token *>;
        typedef std::pair<Chordata::Token, class Token *> TokenPair;
        typedef std::map<Chordata::Token, class Token *> TokenMap;*/

        typedef std::map<Chordata::Token, Token *> TokenMap;
		mutable TokenMap tokens;

		mutable TokenList *typedefs;

	  public:
		Module(const std::wstring &path, const Assembly *assembly = NULL);
		virtual ~Module();

		Chordata::IMetaDataImport *operator ->() const;

		Chordata::IMetaDataImport *getImport() const;
		std::string getVersion() const;

		void *FindRVA(uint32_t rva) const;
		ModuleToken *ResolveToken(Chordata::Token token) const;

		// XXX: Wouldn't it be nice if this worked?!?
		template <typename type>
		type *Resolve(Chordata::Token token) const {
			return dynamic_cast<type *>(ResolveToken(token));
		}

		std::wstring getPath() const;
		std::wstring getName() const;
		GUID getVID() const;

		std::wstring getUserString(Chordata::Tokens::mdString token) const;

		TypeDef *FindTypeDef(const std::wstring &name) const;
		TypeRef *FindTypeRef(const std::wstring &name) const;

		Token *XmlKeyLookup(const std::wstring &key) const;

		TokenList getFields() const;
		TokenList getMethods() const;
		TokenList getModuleRefs() const;
		TokenList getTypeDefs() const;
		TokenList getTypeRefs() const;
	};

}

#endif//METALLURGY_MODULE_H