Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers icache.h Source File

icache.h

Go to the documentation of this file.
00001 ///\file
00002 
00003 /******************************************************************************
00004 The MIT License(MIT)
00005 
00006 Embedded Template Library.
00007 https://github.com/ETLCPP/etl
00008 http://www.etlcpp.com
00009 
00010 Copyright(c) 2017 jwellbelove, rlindeman
00011 
00012 Permission is hereby granted, free of charge, to any person obtaining a copy
00013 of this software and associated documentation files(the "Software"), to deal
00014 in the Software without restriction, including without limitation the rights
00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00016 copies of the Software, and to permit persons to whom the Software is
00017 furnished to do so, subject to the following conditions :
00018 
00019 The above copyright notice and this permission notice shall be included in all
00020 copies or substantial portions of the Software.
00021 
00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00028 SOFTWARE.
00029 ******************************************************************************/
00030 
00031 //*****************************************************************************
00032 #error  This header is in development. Do not use.
00033 //*****************************************************************************
00034 
00035 #ifndef __ETL_ICACHE__
00036 #define __ETL_ICACHE__
00037 
00038 #include "platform.h "
00039 #include "function.h "
00040 #include "nullptr.h "
00041 
00042 #include <utility>
00043 
00044 namespace etl
00045 {
00046   ///**************************************************************************
00047   /// The base class for all caches.
00048   ///**************************************************************************
00049   template <typename TKey, typename TValue>
00050   class icache
00051   {
00052   public:
00053 
00054     ///************************************************************************
00055     /// Constructor.
00056     /// By default, 'write_through' is set to true.
00057     ///************************************************************************
00058     icache()
00059       : write_through(true),
00060         p_read_store(std::nullptr),
00061         p_write_store(std::nullptr)
00062     {
00063     }
00064 
00065     ///************************************************************************
00066     /// Destructor.
00067     /// Flushes the cache if necessary.
00068     ///************************************************************************
00069     virtual ~icache()
00070     {
00071       if (!write_through)
00072       {
00073         flush();
00074       }
00075     }
00076 
00077     ///************************************************************************
00078     /// Sets the function that reads from the store.
00079     ///************************************************************************
00080     void set_read_function(etl::ifunction<key_value_t&> * p_read)
00081     {
00082       p_read_store = p_read;
00083     }
00084 
00085     ///************************************************************************
00086     /// Sets the function that writes to the store.
00087     ///************************************************************************
00088     void set_write_function(etl::ifunction<const key_value_t&> * p_write)
00089     {
00090       p_write_store = p_write;
00091     }
00092 
00093     ///************************************************************************
00094     /// Sets the 'write through'' flag.
00095     ///************************************************************************
00096     void set_write_through(bool write_through_)
00097     {
00098       write_through = write_through_;
00099     }
00100 
00101     virtual const T& read(const TKey& key) const = 0;             ///< Reads from the cache. May read from the store using p_read_store.
00102     virtual void write(const TKey& key, const TValue& value) = 0; ///< Writes to the cache. May write to the store using p_write_store.
00103     virtual void flush() = 0;                                     ///< The overridden function should write all changed values to the store.
00104 
00105   protected:
00106 
00107     typedef std::pair<TKey, TValue> key_value_t;
00108 
00109     bool write_through; ///< If true, the cache should write changed items back to the store immediately. If false then a flush() or destruct will be required.
00110 
00111     etl::ifunction<key_value_t&> *       p_read_store;  ///< A pointer to the function that will read a value from the store into the cache.
00112     etl::ifunction<const key_value_t&> * p_write_store; ///< A pointer to the function that will write a value from the cache into the store.
00113   }
00114 }
00115 
00116 #endif
00117