/******************************************************************************
The MIT License(MIT)

Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com

Copyright(c) 2020 jwellbelove

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#if 0
#error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
#endif

//***************************************************************************
// THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.
//***************************************************************************

//***************************************************************************
// To generate to header file, run this at the command line.
// Note: You will need Python and COG installed.
//
// python -m cogapp -d -e -omessage_packet.h -DHandlers=<n> message_packet_generator.h
// Where <n> is the number of messages to support.
//
// e.g.
// To generate handlers for up to 16 messages...
// python -m cogapp -d -e -omessage_packet.h -DHandlers=16 message_packet_generator.h
//
// See generate.bat
//***************************************************************************

#ifndef ETL_MESSAGE_PACKET_INCLUDED
#define ETL_MESSAGE_PACKET_INCLUDED

#include "message.h"
#include "error_handler.h"
#include "static_assert.h"
#include "largest.h"
#include "alignment.h"
#include "utility.h"

#include <stdint.h>

#undef ETL_FILE
#define ETL_FILE "55"

namespace etl
{
  //***************************************************************************
  // The definition for all 16 message types.
  //***************************************************************************
  template <typename T1, typename T2 = void, typename T3 = void, typename T4 = void, 
            typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void, 
            typename T9 = void, typename T10 = void, typename T11 = void, typename T12 = void, 
            typename T13 = void, typename T14 = void, typename T15 = void, typename T16 = void>
  class message_packet
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          case T5::ID: static_cast<T5*>(pmsg)->~T5(); break;
          case T6::ID: static_cast<T6*>(pmsg)->~T6(); break;
          case T7::ID: static_cast<T7*>(pmsg)->~T7(); break;
          case T8::ID: static_cast<T8*>(pmsg)->~T8(); break;
          case T9::ID: static_cast<T9*>(pmsg)->~T9(); break;
          case T10::ID: static_cast<T10*>(pmsg)->~T10(); break;
          case T11::ID: static_cast<T11*>(pmsg)->~T11(); break;
          case T12::ID: static_cast<T12*>(pmsg)->~T12(); break;
          case T13::ID: static_cast<T13*>(pmsg)->~T13(); break;
          case T14::ID: static_cast<T14*>(pmsg)->~T14(); break;
          case T15::ID: static_cast<T15*>(pmsg)->~T15(); break;
          case T16::ID: static_cast<T16*>(pmsg)->~T16(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<const T5&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<const T6&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<const T7&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<const T8&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<const T9&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<const T10&>(msg)); break;
        case T11::ID: ::new (p) T11(static_cast<const T11&>(msg)); break;
        case T12::ID: ::new (p) T12(static_cast<const T12&>(msg)); break;
        case T13::ID: ::new (p) T13(static_cast<const T13&>(msg)); break;
        case T14::ID: ::new (p) T14(static_cast<const T14&>(msg)); break;
        case T15::ID: ::new (p) T15(static_cast<const T15&>(msg)); break;
        case T16::ID: ::new (p) T16(static_cast<const T16&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<T5&&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<T6&&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<T7&&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<T8&&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<T9&&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<T10&&>(msg)); break;
        case T11::ID: ::new (p) T11(static_cast<T11&&>(msg)); break;
        case T12::ID: ::new (p) T12(static_cast<T12&&>(msg)); break;
        case T13::ID: ::new (p) T13(static_cast<T13&&>(msg)); break;
        case T14::ID: ::new (p) T14(static_cast<T14&&>(msg)); break;
        case T15::ID: ::new (p) T15(static_cast<T15&&>(msg)); break;
        case T16::ID: ::new (p) T16(static_cast<T16&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 15 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3, typename T4, 
            typename T5, typename T6, typename T7, typename T8, 
            typename T9, typename T10, typename T11, typename T12, 
            typename T13, typename T14, typename T15>
  class message_packet<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          case T5::ID: static_cast<T5*>(pmsg)->~T5(); break;
          case T6::ID: static_cast<T6*>(pmsg)->~T6(); break;
          case T7::ID: static_cast<T7*>(pmsg)->~T7(); break;
          case T8::ID: static_cast<T8*>(pmsg)->~T8(); break;
          case T9::ID: static_cast<T9*>(pmsg)->~T9(); break;
          case T10::ID: static_cast<T10*>(pmsg)->~T10(); break;
          case T11::ID: static_cast<T11*>(pmsg)->~T11(); break;
          case T12::ID: static_cast<T12*>(pmsg)->~T12(); break;
          case T13::ID: static_cast<T13*>(pmsg)->~T13(); break;
          case T14::ID: static_cast<T14*>(pmsg)->~T14(); break;
          case T15::ID: static_cast<T15*>(pmsg)->~T15(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<const T5&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<const T6&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<const T7&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<const T8&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<const T9&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<const T10&>(msg)); break;
        case T11::ID: ::new (p) T11(static_cast<const T11&>(msg)); break;
        case T12::ID: ::new (p) T12(static_cast<const T12&>(msg)); break;
        case T13::ID: ::new (p) T13(static_cast<const T13&>(msg)); break;
        case T14::ID: ::new (p) T14(static_cast<const T14&>(msg)); break;
        case T15::ID: ::new (p) T15(static_cast<const T15&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<T5&&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<T6&&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<T7&&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<T8&&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<T9&&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<T10&&>(msg)); break;
        case T11::ID: ::new (p) T11(static_cast<T11&&>(msg)); break;
        case T12::ID: ::new (p) T12(static_cast<T12&&>(msg)); break;
        case T13::ID: ::new (p) T13(static_cast<T13&&>(msg)); break;
        case T14::ID: ::new (p) T14(static_cast<T14&&>(msg)); break;
        case T15::ID: ::new (p) T15(static_cast<T15&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 14 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3, typename T4, 
            typename T5, typename T6, typename T7, typename T8, 
            typename T9, typename T10, typename T11, typename T12, 
            typename T13, typename T14>
  class message_packet<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          case T5::ID: static_cast<T5*>(pmsg)->~T5(); break;
          case T6::ID: static_cast<T6*>(pmsg)->~T6(); break;
          case T7::ID: static_cast<T7*>(pmsg)->~T7(); break;
          case T8::ID: static_cast<T8*>(pmsg)->~T8(); break;
          case T9::ID: static_cast<T9*>(pmsg)->~T9(); break;
          case T10::ID: static_cast<T10*>(pmsg)->~T10(); break;
          case T11::ID: static_cast<T11*>(pmsg)->~T11(); break;
          case T12::ID: static_cast<T12*>(pmsg)->~T12(); break;
          case T13::ID: static_cast<T13*>(pmsg)->~T13(); break;
          case T14::ID: static_cast<T14*>(pmsg)->~T14(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<const T5&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<const T6&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<const T7&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<const T8&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<const T9&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<const T10&>(msg)); break;
        case T11::ID: ::new (p) T11(static_cast<const T11&>(msg)); break;
        case T12::ID: ::new (p) T12(static_cast<const T12&>(msg)); break;
        case T13::ID: ::new (p) T13(static_cast<const T13&>(msg)); break;
        case T14::ID: ::new (p) T14(static_cast<const T14&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<T5&&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<T6&&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<T7&&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<T8&&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<T9&&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<T10&&>(msg)); break;
        case T11::ID: ::new (p) T11(static_cast<T11&&>(msg)); break;
        case T12::ID: ::new (p) T12(static_cast<T12&&>(msg)); break;
        case T13::ID: ::new (p) T13(static_cast<T13&&>(msg)); break;
        case T14::ID: ::new (p) T14(static_cast<T14&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 13 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3, typename T4, 
            typename T5, typename T6, typename T7, typename T8, 
            typename T9, typename T10, typename T11, typename T12, 
            typename T13>
  class message_packet<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          case T5::ID: static_cast<T5*>(pmsg)->~T5(); break;
          case T6::ID: static_cast<T6*>(pmsg)->~T6(); break;
          case T7::ID: static_cast<T7*>(pmsg)->~T7(); break;
          case T8::ID: static_cast<T8*>(pmsg)->~T8(); break;
          case T9::ID: static_cast<T9*>(pmsg)->~T9(); break;
          case T10::ID: static_cast<T10*>(pmsg)->~T10(); break;
          case T11::ID: static_cast<T11*>(pmsg)->~T11(); break;
          case T12::ID: static_cast<T12*>(pmsg)->~T12(); break;
          case T13::ID: static_cast<T13*>(pmsg)->~T13(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<const T5&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<const T6&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<const T7&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<const T8&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<const T9&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<const T10&>(msg)); break;
        case T11::ID: ::new (p) T11(static_cast<const T11&>(msg)); break;
        case T12::ID: ::new (p) T12(static_cast<const T12&>(msg)); break;
        case T13::ID: ::new (p) T13(static_cast<const T13&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<T5&&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<T6&&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<T7&&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<T8&&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<T9&&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<T10&&>(msg)); break;
        case T11::ID: ::new (p) T11(static_cast<T11&&>(msg)); break;
        case T12::ID: ::new (p) T12(static_cast<T12&&>(msg)); break;
        case T13::ID: ::new (p) T13(static_cast<T13&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 12 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3, typename T4, 
            typename T5, typename T6, typename T7, typename T8, 
            typename T9, typename T10, typename T11, typename T12>
  class message_packet<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, void, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          case T5::ID: static_cast<T5*>(pmsg)->~T5(); break;
          case T6::ID: static_cast<T6*>(pmsg)->~T6(); break;
          case T7::ID: static_cast<T7*>(pmsg)->~T7(); break;
          case T8::ID: static_cast<T8*>(pmsg)->~T8(); break;
          case T9::ID: static_cast<T9*>(pmsg)->~T9(); break;
          case T10::ID: static_cast<T10*>(pmsg)->~T10(); break;
          case T11::ID: static_cast<T11*>(pmsg)->~T11(); break;
          case T12::ID: static_cast<T12*>(pmsg)->~T12(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<const T5&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<const T6&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<const T7&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<const T8&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<const T9&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<const T10&>(msg)); break;
        case T11::ID: ::new (p) T11(static_cast<const T11&>(msg)); break;
        case T12::ID: ::new (p) T12(static_cast<const T12&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<T5&&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<T6&&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<T7&&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<T8&&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<T9&&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<T10&&>(msg)); break;
        case T11::ID: ::new (p) T11(static_cast<T11&&>(msg)); break;
        case T12::ID: ::new (p) T12(static_cast<T12&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 11 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3, typename T4, 
            typename T5, typename T6, typename T7, typename T8, 
            typename T9, typename T10, typename T11>
  class message_packet<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, void, void, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          case T5::ID: static_cast<T5*>(pmsg)->~T5(); break;
          case T6::ID: static_cast<T6*>(pmsg)->~T6(); break;
          case T7::ID: static_cast<T7*>(pmsg)->~T7(); break;
          case T8::ID: static_cast<T8*>(pmsg)->~T8(); break;
          case T9::ID: static_cast<T9*>(pmsg)->~T9(); break;
          case T10::ID: static_cast<T10*>(pmsg)->~T10(); break;
          case T11::ID: static_cast<T11*>(pmsg)->~T11(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<const T5&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<const T6&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<const T7&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<const T8&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<const T9&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<const T10&>(msg)); break;
        case T11::ID: ::new (p) T11(static_cast<const T11&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<T5&&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<T6&&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<T7&&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<T8&&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<T9&&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<T10&&>(msg)); break;
        case T11::ID: ::new (p) T11(static_cast<T11&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 10 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3, typename T4, 
            typename T5, typename T6, typename T7, typename T8, 
            typename T9, typename T10>
  class message_packet<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, void, void, void, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          case T5::ID: static_cast<T5*>(pmsg)->~T5(); break;
          case T6::ID: static_cast<T6*>(pmsg)->~T6(); break;
          case T7::ID: static_cast<T7*>(pmsg)->~T7(); break;
          case T8::ID: static_cast<T8*>(pmsg)->~T8(); break;
          case T9::ID: static_cast<T9*>(pmsg)->~T9(); break;
          case T10::ID: static_cast<T10*>(pmsg)->~T10(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<const T5&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<const T6&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<const T7&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<const T8&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<const T9&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<const T10&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<T5&&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<T6&&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<T7&&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<T8&&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<T9&&>(msg)); break;
        case T10::ID: ::new (p) T10(static_cast<T10&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 9 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3, typename T4, 
            typename T5, typename T6, typename T7, typename T8, 
            typename T9>
  class message_packet<T1, T2, T3, T4, T5, T6, T7, T8, T9, void, void, void, void, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          case T5::ID: static_cast<T5*>(pmsg)->~T5(); break;
          case T6::ID: static_cast<T6*>(pmsg)->~T6(); break;
          case T7::ID: static_cast<T7*>(pmsg)->~T7(); break;
          case T8::ID: static_cast<T8*>(pmsg)->~T8(); break;
          case T9::ID: static_cast<T9*>(pmsg)->~T9(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<const T5&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<const T6&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<const T7&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<const T8&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<const T9&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<T5&&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<T6&&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<T7&&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<T8&&>(msg)); break;
        case T9::ID: ::new (p) T9(static_cast<T9&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 8 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3, typename T4, 
            typename T5, typename T6, typename T7, typename T8>
  class message_packet<T1, T2, T3, T4, T5, T6, T7, T8, void, void, void, void, void, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4, T5, T6, T7, T8>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          case T5::ID: static_cast<T5*>(pmsg)->~T5(); break;
          case T6::ID: static_cast<T6*>(pmsg)->~T6(); break;
          case T7::ID: static_cast<T7*>(pmsg)->~T7(); break;
          case T8::ID: static_cast<T8*>(pmsg)->~T8(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<const T5&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<const T6&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<const T7&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<const T8&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<T5&&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<T6&&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<T7&&>(msg)); break;
        case T8::ID: ::new (p) T8(static_cast<T8&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 7 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3, typename T4, 
            typename T5, typename T6, typename T7>
  class message_packet<T1, T2, T3, T4, T5, T6, T7, void, void, void, void, void, void, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4, T5, T6, T7>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4, T5, T6, T7>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          case T5::ID: static_cast<T5*>(pmsg)->~T5(); break;
          case T6::ID: static_cast<T6*>(pmsg)->~T6(); break;
          case T7::ID: static_cast<T7*>(pmsg)->~T7(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<const T5&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<const T6&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<const T7&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<T5&&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<T6&&>(msg)); break;
        case T7::ID: ::new (p) T7(static_cast<T7&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 6 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3, typename T4, 
            typename T5, typename T6>
  class message_packet<T1, T2, T3, T4, T5, T6, void, void, void, void, void, void, void, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4, T5, T6>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4, T5, T6>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          case T5::ID: static_cast<T5*>(pmsg)->~T5(); break;
          case T6::ID: static_cast<T6*>(pmsg)->~T6(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<const T5&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<const T6&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<T5&&>(msg)); break;
        case T6::ID: ::new (p) T6(static_cast<T6&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 5 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3, typename T4, 
            typename T5>
  class message_packet<T1, T2, T3, T4, T5, void, void, void, void, void, void, void, void, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4, T5>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4, T5>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          case T5::ID: static_cast<T5*>(pmsg)->~T5(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<const T5&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        case T5::ID: ::new (p) T5(static_cast<T5&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 4 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3, typename T4>
  class message_packet<T1, T2, T3, T4, void, void, void, void, void, void, void, void, void, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3, T4>::size,
      ALIGNMENT = etl::largest<T1, T2, T3, T4>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          case T4::ID: static_cast<T4*>(pmsg)->~T4(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<const T4&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        case T4::ID: ::new (p) T4(static_cast<T4&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 3 message types.
  //***************************************************************************
  template <typename T1, typename T2, typename T3>
  class message_packet<T1, T2, T3, void, void, void, void, void, void, void, void, void, void, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2, T3>::size,
      ALIGNMENT = etl::largest<T1, T2, T3>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          case T3::ID: static_cast<T3*>(pmsg)->~T3(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<const T3&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        case T3::ID: ::new (p) T3(static_cast<T3&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 2 message types.
  //***************************************************************************
  template <typename T1, typename T2>
  class message_packet<T1, T2, void, void, void, void, void, void, void, void, void, void, void, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1, T2>::size,
      ALIGNMENT = etl::largest<T1, T2>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          case T2::ID: static_cast<T2*>(pmsg)->~T2(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<const T2&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        case T2::ID: ::new (p) T2(static_cast<T2&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };

  //***************************************************************************
  // Specialisation for 1 message type.
  //***************************************************************************
  template <typename T1>
  class message_packet<T1, void, void, void, void, void, void, void, void, void, void, void, void, void, void, void>
  {
  public:

    //********************************************
    message_packet()
      : valid(false)
    {
    }

    //********************************************
    explicit message_packet(const etl::imessage& msg)
      : valid(true)
    {
      add_new_message(msg);
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    explicit message_packet(etl::imessage&& msg)
      : valid(true)
    {
      add_new_message(etl::move(msg));
    }
  #endif

    //**********************************************
    message_packet(const message_packet& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(other.get());
      }
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet(message_packet&& other)
      : valid(other.is_valid())
    {
      if (valid)
      {
        add_new_message(etl::move(other.get()));
      }
    }
  #endif

    //**********************************************
    message_packet& operator =(const message_packet& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(rhs.get());
      }

      return *this;
    }

  #if ETL_CPP11_SUPPORTED
    //**********************************************
    message_packet& operator =(message_packet&& rhs)
    {
      delete_current_message();
      valid = rhs.is_valid();
      if (valid)
      {
        add_new_message(etl::move(rhs.get()));
      }

      return *this;
    }
  #endif

    //********************************************
    ~message_packet()
    {
      delete_current_message();
    }

    //********************************************
    etl::imessage& get() ETL_NOEXCEPT
    {
      return *static_cast<etl::imessage*>(data);
    }

    //********************************************
    const etl::imessage& get() const ETL_NOEXCEPT
    {
      return *static_cast<const etl::imessage*>(data);
    }

    //********************************************
    bool is_valid() const
    {
      return valid;
    }

    enum
    {
      SIZE      = etl::largest<T1>::size,
      ALIGNMENT = etl::largest<T1>::alignment
    };

  private:

    //********************************************
    void delete_current_message()
    {
      if (valid)
      {
        etl::imessage* pmsg = static_cast<etl::imessage*>(data);

  #if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)
        pmsg->~imessage();
  #else
        size_t id = pmsg->message_id;

        switch (id)
        {
          case T1::ID: static_cast<T1*>(pmsg)->~T1(); break;
          default: assert(false); break;
        }
    #endif
      }
    }

    //********************************************
    void add_new_message(const etl::imessage& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<const T1&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }

  #if ETL_CPP11_SUPPORTED
    //********************************************
    void add_new_message(etl::imessage&& msg)
    {
      const size_t id = msg.message_id;
      void* p = data;

      switch (id)
      {
        case T1::ID: ::new (p) T1(static_cast<T1&&>(msg)); break;
        default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;
      }
    }
  #endif

    typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
    bool valid;
  };
}

#undef ETL_FILE

#endif
