TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/host/diskCache/DiskCacheTranslator.hpp
Go to the documentation of this file.
00001 #ifndef _TUTTLEOFX_HOST_DISKCACHETRANSLATOR_HPP_
00002 #define _TUTTLEOFX_HOST_DISKCACHETRANSLATOR_HPP_
00003 
00004 #include <boost/filesystem/path.hpp>
00005 
00006 #include <cstddef>
00007 #include <limits>
00008 
00009 namespace tuttle {
00010 namespace host {
00011 
00012 /**
00013  * @brief An helper to cache any kind of files on your HDD.
00014  * It allows to:
00015  *  - translate a key into a disk path
00016  *  - check if the files exists
00017  *  - create the sub-directories when needed
00018  * 
00019  * Paths used here are only paths inside the cache.
00020  */
00021 class DiskCacheTranslator
00022 {
00023 public:
00024     typedef std::size_t KeyType;
00025 
00026 private:
00027     static const std::size_t s_digits = std::numeric_limits<KeyType>::digits;
00028     static const std::size_t s_nbCharSplit = 8;
00029 
00030 public:
00031     /**
00032      * @brief Set the base directory for all cached files.
00033      */
00034     void setRootDir( const boost::filesystem::path& rootDir ) { _rootDir = rootDir; }
00035     
00036     /**
00037      * @brief Convert a @p key into a filepath.
00038      */
00039     boost::filesystem::path keyToAbsolutePath( const KeyType key ) const;
00040     
00041     /**
00042      * @brief Convert a @p key into a filepath.
00043      */
00044     boost::filesystem::path keyToRelativePath( const KeyType key ) const;
00045     
00046     /**
00047      * @brief Convert an absolute keypath into a relative keypath.
00048      */
00049     boost::filesystem::path absolutePathToRelativePath( const boost::filesystem::path& absoluteKeypath ) const;
00050     /**
00051      * @brief Convert a relative keypath into an absolute keypath.
00052      */
00053     boost::filesystem::path relativePathToAbsolutePath( const boost::filesystem::path& relativeKeypath ) const
00054     {
00055         return _rootDir / relativeKeypath;
00056     }
00057 
00058     /**
00059      * @brief Convert a @p filepath relative to basedir into a key.
00060      */
00061     KeyType relativePathToKey( const boost::filesystem::path& filepath ) const;
00062     
00063     /**
00064      * @brief Convert an absolute @p filepath into a key.
00065      */
00066     KeyType absolutePathToKey( const boost::filesystem::path& filepath ) const;
00067     
00068     
00069     /**
00070      * @brief Check if the @p key exists.
00071      */
00072     bool contains( const KeyType key ) const;
00073     
00074     /**
00075      * @brief Check if the @p key exists.
00076      */
00077     bool contains( const KeyType key, std::time_t& lastWriteTime ) const;
00078     
00079     /**
00080      * @brief Check if the @p keypath exists.
00081      * @param[in] keypath path inside the cache (absolute or relative path)
00082      */
00083     bool contains( const boost::filesystem::path& keypath ) const;
00084     
00085     /**
00086      * @brief Check if the @p keypath exists (absolute or relative path).
00087      * @param[in] keypath path inside the cache (absolute or relative path)
00088      * @param[out] lastWriteTime last write time of the cached file
00089      */
00090     bool contains( const boost::filesystem::path& keypath, std::time_t& lastWriteTime ) const;
00091     
00092     /**
00093      * @brief Retrieve an existing key.
00094      * Throw an error if the key doesn't exist.
00095      */
00096     boost::filesystem::path get( const KeyType key ) const;
00097     
00098     /**
00099      * @brief Retrieve a absolute filepath from a @p key and create needed directories.
00100      * Throw an error if you can't create directories or if basedir is not setted.
00101      */
00102     boost::filesystem::path create( const KeyType key );
00103     
00104 private:
00105     boost::filesystem::path _rootDir;
00106 };
00107 
00108 }
00109 }
00110 
00111 #endif
00112