degate  0.1.2
StoragePolicies.h
Go to the documentation of this file.
00001 /* -*-c++-*-
00002 
00003  This file is part of the IC reverse engineering tool degate.
00004 
00005  Copyright 2008, 2009, 2010 by Martin Schobert
00006 
00007  Degate is free software: you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation, either version 3 of the License, or
00010  any later version.
00011 
00012  Degate is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with degate. If not, see <http://www.gnu.org/licenses/>.
00019 
00020 */
00021 
00022 #ifndef __STORAGEPOLICIES_H__
00023 #define __STORAGEPOLICIES_H__
00024 
00025 #include "globals.h"
00026 #include "MemoryMap.h"
00027 #include "Configuration.h"
00028 #include "FileSystem.h"
00029 #include "Image.h"
00030 
00031 namespace degate {
00032 
00033 
00034   /* -------------------------------------------------------------------------- *
00035    * storage policies
00036    * -------------------------------------------------------------------------- */
00037 
00038 
00039   /**
00040    * Base class for the storage policy of an image.
00041    * This is basically the same as StoragePolicy_GenericBase, but
00042    * adds some methods depending on the PixelPolicy specialization.
00043    */
00044   template<class PixelPolicy>
00045   class StoragePolicy_Base  {
00046 
00047   public:
00048     typedef typename PixelPolicy::pixel_type pixel_type;
00049     typedef PixelPolicy pixel_policy;
00050 
00051     StoragePolicy_Base() {}
00052 
00053     virtual ~StoragePolicy_Base() {}
00054 
00055     /**
00056      * Get pixel for "native" pixel types.
00057      * This method is abstract. If you derive from this class, you should
00058      * implement it for a concrete StoragePolicy.
00059      */
00060     virtual pixel_type get_pixel(unsigned int x, unsigned int y) const = 0;
00061 
00062     /**
00063      * Set "native" pixel.
00064      * This method is abstract. If you derive from this class, you should
00065      * implement it for a concrete StoragePolicy.
00066      */
00067     virtual void set_pixel(unsigned int x, unsigned int y, pixel_type new_val) = 0;
00068 
00069   };
00070 
00071 
00072 
00073 
00074   /**
00075    * Storage policy for image objects that resists in memory.
00076    */
00077   template<class PixelPolicy>
00078   class StoragePolicy_Memory : public StoragePolicy_Base<PixelPolicy> {
00079 
00080   protected:
00081 
00082     static const MAP_STORAGE_TYPE storage_type = MAP_STORAGE_TYPE_MEM;
00083     MemoryMap<typename PixelPolicy::pixel_type> memory_map;
00084 
00085   public:
00086 
00087     StoragePolicy_Memory(unsigned int _width, unsigned int _height) :
00088       memory_map(_width, _height) {
00089     }
00090 
00091     virtual ~StoragePolicy_Memory() {}
00092 
00093     inline typename PixelPolicy::pixel_type get_pixel(unsigned int x, unsigned int y) const {
00094       return memory_map.get(x, y);
00095     }
00096 
00097     inline void set_pixel(unsigned int x, unsigned int y,
00098                    typename PixelPolicy::pixel_type new_val) {
00099       memory_map.set(x, y, new_val);
00100     }
00101 
00102   };
00103 
00104 
00105   /**
00106    * Storage policy for image objects that are stored in a file.
00107    */
00108 
00109   template<class PixelPolicy>
00110   class StoragePolicy_File : public StoragePolicy_Base<PixelPolicy> {
00111 
00112   protected:
00113 
00114     MemoryMap<typename PixelPolicy::pixel_type> memory_map;
00115 
00116   public:
00117 
00118     StoragePolicy_File(unsigned int _width,
00119                        unsigned int _height,
00120                        std::string const& filename,
00121                        bool persistent = false) :
00122       memory_map(_width, _height,
00123                  persistent == false ? MAP_STORAGE_TYPE_TEMP_FILE : MAP_STORAGE_TYPE_PERSISTENT_FILE,
00124                  filename) {
00125     }
00126 
00127     virtual ~StoragePolicy_File() {}
00128 
00129     inline typename PixelPolicy::pixel_type get_pixel(unsigned int x,
00130                                                        unsigned int y) const {
00131       return memory_map.get(x, y);
00132     }
00133 
00134     inline void set_pixel(unsigned int x, unsigned int y,
00135                           typename PixelPolicy::pixel_type new_val) {
00136       memory_map.set(x, y, new_val);
00137     }
00138 
00139   };
00140 
00141 
00142   /**
00143    * Storage policy for image objects that are stored in a temporary file.
00144    */
00145   template<class PixelPolicy>
00146   class StoragePolicy_TempFile : public StoragePolicy_File<PixelPolicy> {
00147 
00148   public:
00149     StoragePolicy_TempFile(unsigned int _width,
00150                            unsigned int _height) :
00151       StoragePolicy_File<PixelPolicy>(_width, _height,
00152                                       generate_temp_file_pattern(get_temp_directory()),
00153                                       false) {}
00154 
00155     virtual ~StoragePolicy_TempFile() {}
00156   };
00157 
00158   /**
00159    * Storage policy for image objects that are stored in a persistent file.
00160    */
00161   template<class PixelPolicy>
00162   class StoragePolicy_PersistentFile : public StoragePolicy_File<PixelPolicy> {
00163 
00164   public:
00165 
00166     StoragePolicy_PersistentFile(unsigned int _width,
00167                                  unsigned int _height,
00168                                  std::string const& filename) :
00169       StoragePolicy_File<PixelPolicy>(_width, _height, filename, true) {}
00170 
00171     virtual ~StoragePolicy_PersistentFile() {}
00172   };
00173 
00174 
00175 }
00176 
00177 #endif