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