Home | History | Annotate | Download | only in Support
      1 //===- HandleToArea.h -----------------------------------------------------===//
      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 #ifndef MCLD_FILE_HANDLE_TO_MEMORY_AREA_H
     10 #define MCLD_FILE_HANDLE_TO_MEMORY_AREA_H
     11 #ifdef ENABLE_UNITTEST
     12 #include <gtest.h>
     13 #endif
     14 #include <mcld/ADT/Uncopyable.h>
     15 #include <mcld/ADT/TypeTraits.h>
     16 #include <mcld/ADT/StringHash.h>
     17 #include <mcld/Support/Path.h>
     18 #include <mcld/Support/FileHandle.h>
     19 #include <vector>
     20 
     21 namespace mcld {
     22 
     23 class MemoryArea;
     24 
     25 /** \class HandleToArea
     26  *
     27  *  Special double-key associative container. Keys are Path and file handler,
     28  *  associative value is MemoryArea.
     29  *
     30  *  For high performance, HandleToArea is not designed to contain unique
     31  *  <key, value> pair. The key and value may be duplicated.
     32  *
     33  *  Like FileHandle, HandleToArea should neither throw exception nor call
     34  *  expressive diagnostic.
     35  */
     36 class HandleToArea : private Uncopyable
     37 {
     38 private:
     39   struct Bucket {
     40     unsigned int hash_value;
     41     FileHandle* handle;
     42     MemoryArea* area;
     43   };
     44 
     45   // the best data structure is a binary search tree.
     46   // However, by the shrinking time-to-market constraint, I used
     47   // vector and sequential search here.
     48   typedef std::vector<Bucket> HandleToAreaMap;
     49 
     50   typedef hash::StringHash<hash::BKDR> HashFunction;
     51 
     52 public:
     53   typedef HandleToAreaMap::iterator iterator;
     54   typedef HandleToAreaMap::const_iterator const_iterator;
     55 
     56 public:
     57   struct Result {
     58   public:
     59     Result(FileHandle* pHandle, MemoryArea* pArea)
     60       : handle(pHandle), area(pArea) { }
     61 
     62   public:
     63     FileHandle* handle;
     64     MemoryArea* area;
     65   };
     66 
     67   struct ConstResult {
     68   public:
     69     ConstResult(const FileHandle* pHandle, const MemoryArea* pArea)
     70       : handle(pHandle), area(pArea) { }
     71 
     72   public:
     73     const FileHandle* handle;
     74     const MemoryArea* area;
     75   };
     76 
     77 public:
     78   bool push_back(FileHandle* pHandle, MemoryArea* pArea);
     79 
     80   bool erase(MemoryArea* pArea);
     81 
     82   bool erase(const sys::fs::Path& pPath);
     83 
     84   Result findFirst(const sys::fs::Path& pPath);
     85 
     86   ConstResult findFirst(const sys::fs::Path& pPath) const;
     87 
     88   iterator begin()
     89   { return m_AreaMap.begin(); }
     90 
     91   iterator end()
     92   { return m_AreaMap.end(); }
     93 
     94   const_iterator begin() const
     95   { return m_AreaMap.begin(); }
     96 
     97   const_iterator end() const
     98   { return m_AreaMap.end(); }
     99 
    100   // -----  capacity  ----- //
    101   bool empty() const
    102   { return m_AreaMap.empty(); }
    103 
    104   size_t size() const
    105   { return m_AreaMap.size(); }
    106 
    107   HandleToArea() : m_AreaMap() { }
    108 
    109   ~HandleToArea() { }
    110 
    111 private:
    112   HandleToAreaMap m_AreaMap;
    113 };
    114 
    115 } // namespace of mcld
    116 
    117 #endif
    118 
    119