Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers alignment.h Source File

alignment.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) 2014 jwellbelove
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 #ifndef __ETL_ALIGNEMENT__
00032 #define __ETL_ALIGNEMENT__
00033 
00034 #include <stdint.h>
00035 
00036 #include "platform.h "
00037 #include "type_traits.h "
00038 #include "static_assert.h"
00039 
00040 ///\defgroup alignment alignment
00041 /// Creates a variable of the specified type at the specified alignment.
00042 /// \ingroup utilities
00043 
00044 namespace etl
00045 {
00046   namespace __private_alignment__
00047   {
00048     //***************************************************************************
00049     // Matcher.
00050     //***************************************************************************
00051     template <const bool IS_MATCH, const size_t ALIGNMENT, typename T1 = void, typename T2 = void, typename T3 = void, typename T4 = void, typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void>
00052     class type_with_alignment_matcher;
00053 
00054     // Matching alignment.
00055     template <const size_t ALIGNMENT, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
00056     class type_with_alignment_matcher <true, ALIGNMENT, T1, T2, T3, T4, T5, T6, T7, T8>
00057     {
00058     public:
00059 
00060       typedef T1 type;
00061     };
00062 
00063     // Non-matching alignment.
00064     template <const size_t ALIGNMENT, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
00065     class type_with_alignment_matcher <false, ALIGNMENT, T1, T2, T3, T4, T5, T6, T7, T8>
00066     {
00067     public:
00068 
00069       typedef typename type_with_alignment_matcher<ALIGNMENT <= etl::alignment_of<T2>::value, ALIGNMENT, T2, T3, T4, T5, T6, T7, T8, void>::type type;
00070     };
00071 
00072     // Non-matching alignment, none left.
00073     template <const size_t ALIGNMENT>
00074     class type_with_alignment_matcher <false, ALIGNMENT, void, void, void, void, void, void, void, void>
00075     {
00076     public:
00077 
00078       typedef char type;
00079     };
00080 
00081     //***************************************************************************
00082     // Helper.
00083     //***************************************************************************
00084     template <const size_t ALIGNMENT, typename T1, typename T2 = void, typename T3 = void, typename T4 = void, typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void>
00085     class type_with_alignment_helper
00086     {
00087     public:
00088 
00089       typedef typename type_with_alignment_matcher<ALIGNMENT <= etl::alignment_of<T1>::value, ALIGNMENT, T1, T2, T3, T4, T5, T6, T7, T8>::type type;
00090     };
00091   }
00092 
00093   //***************************************************************************
00094   /// Gets a type that has the same as the specified alignment.
00095   ///\ingroup alignment
00096   //***************************************************************************
00097   template <const size_t ALIGNMENT>
00098   class type_with_alignment
00099   {
00100   public:
00101 
00102     typedef typename __private_alignment__::type_with_alignment_helper<ALIGNMENT, int_least8_t, int_least16_t, int32_t, int64_t, float, double, void*>::type type;
00103   };
00104 
00105   //***************************************************************************
00106   /// Aligned storage
00107   /// LENGTH should be determined in terms of sizeof()
00108   ///\ingroup alignment
00109   //***************************************************************************
00110   template <const size_t LENGTH, const size_t ALIGNMENT>
00111   struct aligned_storage
00112   {
00113     struct type
00114     {
00115       /// Convert to T reference.
00116       template <typename T>
00117       operator T& ()
00118       {
00119         STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
00120         return reinterpret_cast<T&>(*data);
00121       }
00122 
00123       /// Convert to const T reference.
00124       template <typename T>
00125       operator const T& () const
00126       {
00127         STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
00128         return reinterpret_cast<const T&>(*data);
00129       }
00130 
00131       /// Convert to T pointer.
00132       template <typename T>
00133       operator T* ()
00134       {
00135         STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
00136         return reinterpret_cast<T*>(data);
00137       }
00138 
00139       /// Convert to const T pointer.
00140       template <typename T>
00141       operator const T* () const
00142       {
00143         STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
00144         return reinterpret_cast<const T*>(data);
00145       }
00146 
00147       /// Get address as T reference.
00148       template <typename T>
00149       T& get_reference()
00150       {
00151         STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
00152         return reinterpret_cast<T&>(*data);
00153       }
00154 
00155       /// Get address as const T reference.
00156       template <typename T>
00157       const T& get_reference() const
00158       {
00159         STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
00160         return reinterpret_cast<const T&>(*data);
00161       }
00162 
00163       /// Get address as T pointer.
00164       template <typename T>
00165       T* get_address()
00166       {
00167         STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
00168         return reinterpret_cast<T*>(data);
00169       }
00170 
00171       /// Get address as const T pointer.
00172       template <typename T>
00173       const T* get_address() const
00174       {
00175         STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
00176         return reinterpret_cast<const T*>(data);
00177       }
00178 
00179       union
00180       {
00181         char data[LENGTH];
00182         typename etl::type_with_alignment<ALIGNMENT>::type __etl_alignment_type__; // A POD type that has the same alignment as ALIGNMENT.
00183       };
00184     };
00185   };
00186 
00187   //***************************************************************************
00188   /// Aligned storage as
00189   ///\ingroup alignment
00190   //***************************************************************************
00191   template <const size_t LENGTH, typename T>
00192   struct aligned_storage_as : public etl::aligned_storage<LENGTH, etl::alignment_of<T>::value>
00193   {
00194   };
00195 }
00196 
00197 #endif
00198