Home | History | Annotate | Download | only in MC
      1 //===- AttributeFactory.cpp -----------------------------------------------===//
      2 //
      3 //                     The MCLinker Project
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 #include "mcld/MC/MCLDAttribute.h"
     10 #include "mcld/MC/AttributeFactory.h"
     11 
     12 using namespace mcld;
     13 
     14 //==========================
     15 // AttributeFactory
     16 AttributeFactory::AttributeFactory()
     17   : m_AttrSet() {
     18   m_AttrSet.push_back(new mcld::Attribute());
     19   m_pLast = new AttributeProxy(*this, *m_AttrSet.front());
     20 }
     21 
     22 AttributeFactory::AttributeFactory(size_t pNum)
     23   : m_AttrSet() {
     24   m_AttrSet.reserve(pNum);
     25   m_AttrSet.push_back(new mcld::Attribute());
     26   m_pLast = new AttributeProxy(*this, *m_AttrSet.front());
     27 }
     28 
     29 AttributeFactory::~AttributeFactory()
     30 {
     31   iterator cur = m_AttrSet.begin();
     32   iterator aEnd = m_AttrSet.end();
     33   while(cur != aEnd) {
     34     delete (*cur);
     35     ++cur;
     36   }
     37   m_AttrSet.clear();
     38   delete m_pLast;
     39 }
     40 
     41 void AttributeFactory::reserve(size_t pNum)
     42 {
     43   m_AttrSet.reserve(pNum);
     44 }
     45 
     46 Attribute &AttributeFactory::predefined()
     47 {
     48   return *m_AttrSet.front();
     49 }
     50 
     51 const Attribute &AttributeFactory::predefined() const
     52 {
     53   return *m_AttrSet.front();
     54 }
     55 
     56 AttributeProxy* AttributeFactory::produce()
     57 {
     58   m_pLast->change(m_AttrSet.front());
     59   return m_pLast->clone();
     60 }
     61 
     62 AttributeProxy& AttributeFactory::last()
     63 {
     64   return *m_pLast;
     65 }
     66 
     67 const AttributeProxy& AttributeFactory::last() const
     68 {
     69   return *m_pLast;
     70 }
     71 
     72 Attribute* AttributeFactory::exists(const Attribute& pAttr) const
     73 {
     74   const_iterator cur = m_AttrSet.begin();
     75   const_iterator aEnd = m_AttrSet.end();
     76   while(cur != aEnd) {
     77     if (*(*cur) == pAttr) {
     78       m_pLast->change(*cur);
     79       return *cur;
     80     }
     81     ++cur;
     82   }
     83   return 0;
     84 }
     85 
     86 void AttributeFactory::record(mcld::Attribute &pAttr)
     87 {
     88   m_AttrSet.push_back(&pAttr);
     89   m_pLast->change(m_AttrSet.back());
     90 }
     91 
     92