UniqueGCFactory.h revision 37b74a387bb3993387029859c2d9d051c41c724e
15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===- UniqueGCFactory.h --------------------------------------------------===// 25460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// 35460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// The MCLinker Project 45460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// 55460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// This file is distributed under the University of Illinois Open Source 65460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// License. See LICENSE.TXT for details. 75460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// 85460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===// 937b74a387bb3993387029859c2d9d051c41c724eStephen Hines#ifndef MCLD_SUPPORT_UNIQUEGCFACTORY_H_ 1037b74a387bb3993387029859c2d9d051c41c724eStephen Hines#define MCLD_SUPPORT_UNIQUEGCFACTORY_H_ 115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include "mcld/Support/GCFactory.h" 1337b74a387bb3993387029859c2d9d051c41c724eStephen Hines 145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <map> 155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <utility> 165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1737b74a387bb3993387029859c2d9d051c41c724eStephen Hinesnamespace mcld { 185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class UniqueGCFactoryBase 205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * \brief UniqueGCFactories are unique associative factories, meaning that 215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * no two elements have the same key. 225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */ 2337b74a387bb3993387029859c2d9d051c41c724eStephen Hinestemplate <typename KeyType, typename DataType, size_t ChunkSize> 2437b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass UniqueGCFactoryBase 2537b74a387bb3993387029859c2d9d051c41c724eStephen Hines : public GCFactoryBase<LinearAllocator<DataType, ChunkSize> > { 2637b74a387bb3993387029859c2d9d051c41c724eStephen Hines protected: 275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typedef GCFactoryBase<LinearAllocator<DataType, ChunkSize> > Alloc; 285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typedef std::map<KeyType, DataType*> KeyMap; 295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 3037b74a387bb3993387029859c2d9d051c41c724eStephen Hines protected: 315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao UniqueGCFactoryBase() 3237b74a387bb3993387029859c2d9d051c41c724eStephen Hines : GCFactoryBase<LinearAllocator<DataType, ChunkSize> >() {} 335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 3437b74a387bb3993387029859c2d9d051c41c724eStephen Hines explicit UniqueGCFactoryBase(size_t pNum) 3537b74a387bb3993387029859c2d9d051c41c724eStephen Hines : GCFactoryBase<LinearAllocator<DataType, ChunkSize> >(pNum) {} 365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 3737b74a387bb3993387029859c2d9d051c41c724eStephen Hines public: 3837b74a387bb3993387029859c2d9d051c41c724eStephen Hines virtual ~UniqueGCFactoryBase() { f_KeyMap.clear(); } 395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao DataType* find(const KeyType& pKey) { 415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typename KeyMap::iterator dataIter = f_KeyMap.find(pKey); 425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao if (dataIter != f_KeyMap.end()) 435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao return dataIter->second; 445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao return 0; 455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao } 465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao const DataType* find(const KeyType& pKey) const { 485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typename KeyMap::const_iterator dataIter = f_KeyMap.find(pKey); 495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao if (dataIter != f_KeyMap.end()) 505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao return dataIter->second; 515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao return 0; 525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao } 535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao DataType* produce(const KeyType& pKey, bool& pExist) { 555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typename KeyMap::iterator dataIter = f_KeyMap.find(pKey); 565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao if (dataIter != f_KeyMap.end()) { 575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao pExist = true; 585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao return dataIter->second; 595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao } 605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao DataType* data = Alloc::allocate(); 615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao construct(data); 625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao f_KeyMap.insert(std::make_pair(pKey, data)); 635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao pExist = false; 645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao return data; 655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao } 665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao DataType* produce(const KeyType& pKey, const DataType& pValue, bool& pExist) { 685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao typename KeyMap::iterator dataIter = f_KeyMap.find(pKey); 695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao if (dataIter != f_KeyMap.end()) { 705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao pExist = true; 715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao return dataIter->second; 725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao } 735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao DataType* data = Alloc::allocate(); 745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao construct(data, pValue); 755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao f_KeyMap.insert(std::make_pair(pKey, data)); 765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao pExist = false; 775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao return data; 785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao } 795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 8037b74a387bb3993387029859c2d9d051c41c724eStephen Hines protected: 815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao KeyMap f_KeyMap; 825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}; 835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 8437b74a387bb3993387029859c2d9d051c41c724eStephen Hines} // namespace mcld 855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 8637b74a387bb3993387029859c2d9d051c41c724eStephen Hines#endif // MCLD_SUPPORT_UNIQUEGCFACTORY_H_ 87