pdf_pages_cache.h

Go to the documentation of this file.
00001 /*
00002  * File Name: pdf_pages_cache.h
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-pdf.
00007  *
00008  * uds-plugin-pdf is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-pdf is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #ifndef PDF_PAGES_CACHE_H_
00028 #define PDF_PAGES_CACHE_H_
00029 
00030 #include "log.h"
00031 #include "mutex.h"
00032 
00033 #include "pdf_define.h"
00034 #include "pdf_page.h"
00035 
00036 namespace pdf
00037 {
00038 
00039 /// PagesCache caches the used PDFPage instances
00040 class PDFPage;
00041 class PagesCache
00042 {
00043 public:
00044     PagesCache(void);
00045     ~PagesCache(void);
00046 
00047     /// Reset the size of pages cache
00048     bool reset(const unsigned int size);
00049 
00050     /// Get the size of pages cache
00051     unsigned int size();
00052 
00053     /// Clear the pages cache
00054     void clear();
00055 
00056     /// Add a new page
00057     void add_page(PagePtr p);
00058 
00059     /// Remove the old pages to make sure the memory is enough
00060     /// NOTE: length might be less than 0
00061     bool make_enough_memory(const int page_num, const int length);
00062 
00063     /// Clear the cached bitmaps but locked page
00064     void clear_cached_bitmaps();
00065 
00066     /// Get a page
00067     PagePtr get_page(const size_t idx);
00068 
00069     /// Increase total length by adding the page length
00070     void update_mem_usage(const int length);
00071 
00072     /// Get the mutex, for externally locking the cache
00073     Mutex & get_mutex() { return cache_mutex; }
00074 
00075 private:
00076     // remove a page: return true when a page is sucessfully deleted;
00077     // otherwise there is only one page in cache and it is locked.
00078     bool remove_page(const int page_num = -1);
00079 
00080 private:
00081     typedef std::tr1::unordered_map<size_t, PagePtr> Pages;
00082     typedef Pages::iterator PagesIter;
00083 
00084 private:
00085     // the size limit
00086     unsigned int size_limit;
00087 
00088     // the memory cost of current cached pages
00089     int total_length;
00090 
00091     // the pages list
00092     Pages pages;
00093 
00094     // the mutext of the cached pages
00095     Mutex cache_mutex;
00096 };
00097 
00098 };//namespace pdf
00099 
00100 #endif //PDF_PAGES_CACHE_H_
00101 
Generated by  doxygen 1.6.2-20100208