Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers packet.h Source File

packet.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 https://www.etlcpp.com
00009 
00010 Copyright(c) 2017 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_PACKET__
00032 #define __ETL_PACKET__
00033 
00034 #include "platform.h "
00035 #include "static_assert.h"
00036 #include "alignment.h "
00037 
00038 #undef ETL_FILE
00039 #define ETL_FILE "38"
00040 
00041 //*****************************************************************************
00042 ///\defgroup packet packet
00043 /// A class that can contain one a several related types.
00044 ///\ingroup containers
00045 //*****************************************************************************
00046 
00047 namespace etl
00048 {
00049   //***************************************************************************
00050   /// A template class that can store any types derived from TBase that conform
00051   /// to the size and alignment requirements.
00052   ///\ingroup packet
00053   //***************************************************************************
00054   template <typename TBase, size_t SIZE, size_t ALIGNMENT>            
00055   class packet
00056   {
00057   public:
00058 
00059     //***************************************************************************
00060     /// Constructor that static asserts any types that do not conform to the max size and alignment.
00061     //***************************************************************************
00062     template <typename T>
00063     explicit packet(const T& value)
00064     {
00065       STATIC_ASSERT((etl::is_base_of<TBase, T>::value), "Unsupported type");
00066       STATIC_ASSERT(sizeof(T) <= SIZE, "Unsupported size");
00067       STATIC_ASSERT(etl::alignment_of<T>::value <= ALIGNMENT, "Unsupported alignment");
00068 
00069       ::new (static_cast<T*>(data)) T(value);
00070     }
00071 
00072     //***************************************************************************
00073     /// Destructor
00074     //***************************************************************************
00075     ~packet()
00076     {
00077       static_cast<TBase*>(data)->~TBase();
00078     }
00079 
00080     //***************************************************************************
00081     /// Assignment operator for type.
00082     ///\param value The value to assign.
00083     //***************************************************************************
00084     template <typename T>
00085     packet& operator =(const T& value)
00086     {
00087       STATIC_ASSERT((etl::is_base_of<TBase, T>::value), "Unsupported type");
00088       STATIC_ASSERT(sizeof(T) <= SIZE, "Unsupported size");
00089       STATIC_ASSERT(etl::alignment_of<T>::value <= ALIGNMENT, "Unsupported alignment");
00090 
00091       static_cast<TBase*>(data)->~TBase();
00092       ::new (static_cast<T*>(data)) T(value);
00093 
00094       return *this;
00095     }
00096 
00097     //***************************************************************************
00098     /// Get access to the contained object.
00099     //***************************************************************************
00100     TBase& get()
00101     {
00102       return *static_cast<TBase*>(data);
00103     }
00104 
00105     //***************************************************************************
00106     /// Get access to the contained object.
00107     //***************************************************************************
00108     const TBase& get() const
00109     {
00110       return *static_cast<const TBase*>(data);
00111     }
00112 
00113   private:
00114     
00115     packet(const packet& other);
00116     packet& operator =(const packet& other);
00117 
00118     //***************************************************************************
00119     /// The internal storage.
00120     /// Aligned on a suitable boundary, which should be good for all types.
00121     //***************************************************************************
00122     typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
00123   };
00124 }
00125 
00126 #undef ETL_FILE
00127 
00128 #endif
00129