/******************************************************************************
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.
******************************************************************************/

/*[[[cog
import cog
cog.outl("#if 0")
]]]*/
/*[[[end]]]*/
#error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
/*[[[cog
import cog
cog.outl("#endif")
]]]*/
/*[[[end]]]*/

/*[[[cog
import cog
cog.outl("//***************************************************************************")
cog.outl("// THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.")
cog.outl("//***************************************************************************")
]]]*/
/*[[[end]]]*/

//***************************************************************************
// 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
{
  /*[[[cog
    import cog
    ################################################
    # The first definition for all of the messages.
    ################################################
    cog.outl("//***************************************************************************")
    cog.outl("// The definition for all %s message types." % Handlers)
    cog.outl("//***************************************************************************")
    cog.out("template <")
    cog.out("typename T1, ")
    for n in range(2, int(Handlers)):
        cog.out("typename T%s = void, " % n)
        if n % 4 == 0:
            cog.outl("")
            cog.out("          ")
    cog.outl("typename T%s = void>" % int(Handlers))
    cog.outl("class message_packet")
    cog.outl("{")
    cog.outl("public:")
    cog.outl("")
    cog.outl("  //********************************************")
    cog.outl("  message_packet()")
    cog.outl("    : valid(false)")
    cog.outl("  {")
    cog.outl("  }")
    cog.outl("")
    cog.outl("  //********************************************")
    cog.outl("  explicit message_packet(const etl::imessage& msg)")
    cog.outl("    : valid(true)")
    cog.outl("  {")
    cog.outl("    add_new_message(msg);")
    cog.outl("  }")
    cog.outl("")
    cog.outl("#if ETL_CPP11_SUPPORTED")
    cog.outl("  //********************************************")
    cog.outl("  explicit message_packet(etl::imessage&& msg)")
    cog.outl("    : valid(true)")
    cog.outl("  {")
    cog.outl("    add_new_message(etl::move(msg));")
    cog.outl("  }")
    cog.outl("#endif")
    cog.outl("")
    cog.outl("  //**********************************************")
    cog.outl("  message_packet(const message_packet& other)")
    cog.outl("    : valid(other.is_valid())")
    cog.outl("  {")
    cog.outl("    if (valid)")
    cog.outl("    {")
    cog.outl("      add_new_message(other.get());")
    cog.outl("    }")
    cog.outl("  }")
    cog.outl("")
    cog.outl("#if ETL_CPP11_SUPPORTED")
    cog.outl("  //**********************************************")
    cog.outl("  message_packet(message_packet&& other)")
    cog.outl("    : valid(other.is_valid())")
    cog.outl("  {")
    cog.outl("    if (valid)")
    cog.outl("    {")
    cog.outl("      add_new_message(etl::move(other.get()));")
    cog.outl("    }")
    cog.outl("  }")
    cog.outl("#endif")
    cog.outl("")
    cog.outl("  //**********************************************")
    cog.outl("  message_packet& operator =(const message_packet& rhs)")
    cog.outl("  {")
    cog.outl("    delete_current_message();")
    cog.outl("    valid = rhs.is_valid();")
    cog.outl("    if (valid)")
    cog.outl("    {")
    cog.outl("      add_new_message(rhs.get());")
    cog.outl("    }")
    cog.outl("")
    cog.outl("    return *this;")
    cog.outl("  }")
    cog.outl("")
    cog.outl("#if ETL_CPP11_SUPPORTED")
    cog.outl("  //**********************************************")
    cog.outl("  message_packet& operator =(message_packet&& rhs)")
    cog.outl("  {")
    cog.outl("    delete_current_message();")
    cog.outl("    valid = rhs.is_valid();")
    cog.outl("    if (valid)")
    cog.outl("    {")
    cog.outl("      add_new_message(etl::move(rhs.get()));")
    cog.outl("    }")
    cog.outl("")
    cog.outl("    return *this;")
    cog.outl("  }")
    cog.outl("#endif")
    cog.outl("")
    cog.outl("  //********************************************")
    cog.outl("  ~message_packet()")
    cog.outl("  {")
    cog.outl("    delete_current_message();")
    cog.outl("  }")
    cog.outl("")
    cog.outl("  //********************************************")
    cog.outl("  etl::imessage& get() ETL_NOEXCEPT")
    cog.outl("  {")
    cog.outl("    return *static_cast<etl::imessage*>(data);")
    cog.outl("  }")
    cog.outl("")
    cog.outl("  //********************************************")
    cog.outl("  const etl::imessage& get() const ETL_NOEXCEPT")
    cog.outl("  {")
    cog.outl("    return *static_cast<const etl::imessage*>(data);")
    cog.outl("  }")
    cog.outl("")
    cog.outl("  //********************************************")
    cog.outl("  bool is_valid() const")
    cog.outl("  {")
    cog.outl("    return valid;")
    cog.outl("  }")
    cog.outl("")
    cog.outl("  enum")
    cog.outl("  {")
    cog.out("    SIZE      = etl::largest<")
    for n in range(1, int(Handlers)):
        cog.out("T%d, " % n)
    cog.outl("T%s>::size," % int(Handlers))
    cog.out("    ALIGNMENT = etl::largest<")
    for n in range(1, int(Handlers)):
        cog.out("T%d, " % n)
    cog.outl("T%s>::alignment" % int(Handlers))
    cog.outl("  };")
    cog.outl("")
    cog.outl("private:")
    cog.outl("")
    cog.outl("  //********************************************")
    cog.outl("  void delete_current_message()")
    cog.outl("  {")
    cog.outl("    if (valid)")
    cog.outl("    {")
    cog.outl("      etl::imessage* pmsg = static_cast<etl::imessage*>(data);")
    cog.outl("")
    cog.outl("#if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)")
    cog.outl("      pmsg->~imessage();")
    cog.outl("#else")
    cog.outl("      size_t id = pmsg->message_id;")
    cog.outl("")
    cog.outl("      switch (id)")
    cog.outl("      {")
    for n in range(1, int(Handlers) + 1):
        cog.outl("        case T%s::ID: static_cast<T%s*>(pmsg)->~T%s(); break;" % (n, n, n))
    cog.outl("        default: assert(false); break;")
    cog.outl("      }")
    cog.outl("  #endif")
    cog.outl("    }")
    cog.outl("  }")
    cog.outl("")
    cog.outl("  //********************************************")
    cog.outl("  void add_new_message(const etl::imessage& msg)")
    cog.outl("  {")
    cog.outl("    const size_t id = msg.message_id;")
    cog.outl("    void* p = data;")
    cog.outl("")
    cog.outl("    switch (id)")
    cog.outl("    {")
    for n in range(1, int(Handlers) + 1):
        cog.outl("      case T%d::ID: ::new (p) T%d(static_cast<const T%d&>(msg)); break;" %(n, n, n))
    cog.outl("      default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;")
    cog.outl("    }")
    cog.outl("  }")
    cog.outl("")
    cog.outl("#if ETL_CPP11_SUPPORTED")
    cog.outl("  //********************************************")
    cog.outl("  void add_new_message(etl::imessage&& msg)")
    cog.outl("  {")
    cog.outl("    const size_t id = msg.message_id;")
    cog.outl("    void* p = data;")
    cog.outl("")
    cog.outl("    switch (id)")
    cog.outl("    {")
    for n in range(1, int(Handlers) + 1):
        cog.outl("      case T%d::ID: ::new (p) T%d(static_cast<T%d&&>(msg)); break;" %(n, n, n))
    cog.outl("      default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;")
    cog.outl("    }")
    cog.outl("  }")
    cog.outl("#endif")
    cog.outl("")
    cog.outl("  typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;")
    cog.outl("  bool valid;")
    cog.outl("};")

    ####################################
    # All of the other specialisations.
    ####################################
    for n in range(int(Handlers) - 1, 0, -1):
        cog.outl("")
        cog.outl("//***************************************************************************")
        if n == 1:
            cog.outl("// Specialisation for %d message type." % n)
        else:
            cog.outl("// Specialisation for %d message types." % n)
        cog.outl("//***************************************************************************")
        cog.out("template <")
        for t in range(1, n):
            cog.out("typename T%s, " % t)
            if t % 4 == 0:
                cog.outl("")
                cog.out("          ")
        cog.outl("typename T%s>" % n)
        cog.out("class message_packet<")
        for t in range(1, n + 1):
            cog.out("T%d, " % t)
            if t % 16 == 0:
                cog.outl("")
                cog.out("               ")
        for t in range(n + 1, int(Handlers)):
            cog.out("void, ")
            if t % 16 == 0:
                cog.outl("")
                cog.out("               ")
        cog.outl("void>")
        cog.outl("{")
        cog.outl("public:")
        cog.outl("")
        cog.outl("  //********************************************")
        cog.outl("  message_packet()")
        cog.outl("    : valid(false)")
        cog.outl("  {")
        cog.outl("  }")
        cog.outl("")
        cog.outl("  //********************************************")
        cog.outl("  explicit message_packet(const etl::imessage& msg)")
        cog.outl("    : valid(true)")
        cog.outl("  {")
        cog.outl("    add_new_message(msg);")
        cog.outl("  }")
        cog.outl("")
        cog.outl("#if ETL_CPP11_SUPPORTED")
        cog.outl("  //********************************************")
        cog.outl("  explicit message_packet(etl::imessage&& msg)")
        cog.outl("    : valid(true)")
        cog.outl("  {")
        cog.outl("    add_new_message(etl::move(msg));")
        cog.outl("  }")
        cog.outl("#endif")
        cog.outl("")
        cog.outl("  //**********************************************")
        cog.outl("  message_packet(const message_packet& other)")
        cog.outl("    : valid(other.is_valid())")
        cog.outl("  {")
        cog.outl("    if (valid)")
        cog.outl("    {")
        cog.outl("      add_new_message(other.get());")
        cog.outl("    }")
        cog.outl("  }")
        cog.outl("")
        cog.outl("#if ETL_CPP11_SUPPORTED")
        cog.outl("  //**********************************************")
        cog.outl("  message_packet(message_packet&& other)")
        cog.outl("    : valid(other.is_valid())")
        cog.outl("  {")
        cog.outl("    if (valid)")
        cog.outl("    {")
        cog.outl("      add_new_message(etl::move(other.get()));")
        cog.outl("    }")
        cog.outl("  }")
        cog.outl("#endif")
        cog.outl("")
        cog.outl("  //**********************************************")
        cog.outl("  message_packet& operator =(const message_packet& rhs)")
        cog.outl("  {")
        cog.outl("    delete_current_message();")
        cog.outl("    valid = rhs.is_valid();")
        cog.outl("    if (valid)")
        cog.outl("    {")
        cog.outl("      add_new_message(rhs.get());")
        cog.outl("    }")
        cog.outl("")
        cog.outl("    return *this;")
        cog.outl("  }")
        cog.outl("")
        cog.outl("#if ETL_CPP11_SUPPORTED")
        cog.outl("  //**********************************************")
        cog.outl("  message_packet& operator =(message_packet&& rhs)")
        cog.outl("  {")
        cog.outl("    delete_current_message();")
        cog.outl("    valid = rhs.is_valid();")
        cog.outl("    if (valid)")
        cog.outl("    {")
        cog.outl("      add_new_message(etl::move(rhs.get()));")
        cog.outl("    }")
        cog.outl("")
        cog.outl("    return *this;")
        cog.outl("  }")
        cog.outl("#endif")
        cog.outl("")
        cog.outl("  //********************************************")
        cog.outl("  ~message_packet()")
        cog.outl("  {")
        cog.outl("    delete_current_message();")
        cog.outl("  }")
        cog.outl("")
        cog.outl("  //********************************************")
        cog.outl("  etl::imessage& get() ETL_NOEXCEPT")
        cog.outl("  {")
        cog.outl("    return *static_cast<etl::imessage*>(data);")
        cog.outl("  }")
        cog.outl("")
        cog.outl("  //********************************************")
        cog.outl("  const etl::imessage& get() const ETL_NOEXCEPT")
        cog.outl("  {")
        cog.outl("    return *static_cast<const etl::imessage*>(data);")
        cog.outl("  }")
        cog.outl("")
        cog.outl("  //********************************************")
        cog.outl("  bool is_valid() const")
        cog.outl("  {")
        cog.outl("    return valid;")
        cog.outl("  }")
        cog.outl("")
        cog.outl("  enum")
        cog.outl("  {")
        cog.out("    SIZE      = etl::largest<")
        for t in range(1, n):
            cog.out("T%d, " % t)
        cog.outl("T%s>::size," % n)
        cog.out("    ALIGNMENT = etl::largest<")
        for t in range(1, n):
            cog.out("T%d, " % t)
        cog.outl("T%s>::alignment" % n)
        cog.outl("  };")
        cog.outl("")
        cog.outl("private:")
        cog.outl("")
        cog.outl("  //********************************************")
        cog.outl("  void delete_current_message()")
        cog.outl("  {")
        cog.outl("    if (valid)")
        cog.outl("    {")
        cog.outl("      etl::imessage* pmsg = static_cast<etl::imessage*>(data);")
        cog.outl("")
        cog.outl("#if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)")
        cog.outl("      pmsg->~imessage();")
        cog.outl("#else")
        cog.outl("      size_t id = pmsg->message_id;")
        cog.outl("")
        cog.outl("      switch (id)")
        cog.outl("      {")
        for t in range(1, n + 1):
            cog.outl("        case T%s::ID: static_cast<T%s*>(pmsg)->~T%s(); break;" % (t, t, t))
        cog.outl("        default: assert(false); break;")
        cog.outl("      }")
        cog.outl("  #endif")
        cog.outl("    }")
        cog.outl("  }")
        cog.outl("")
        cog.outl("  //********************************************")
        cog.outl("  void add_new_message(const etl::imessage& msg)")
        cog.outl("  {")
        cog.outl("    const size_t id = msg.message_id;")
        cog.outl("    void* p = data;")
        cog.outl("")
        cog.outl("    switch (id)")
        cog.outl("    {")
        for t in range(1, n + 1):
            cog.outl("      case T%d::ID: ::new (p) T%d(static_cast<const T%d&>(msg)); break;" %(t, t, t))
        cog.outl("      default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;")
        cog.outl("    }")
        cog.outl("  }")
        cog.outl("")
        cog.outl("#if ETL_CPP11_SUPPORTED")
        cog.outl("  //********************************************")
        cog.outl("  void add_new_message(etl::imessage&& msg)")
        cog.outl("  {")
        cog.outl("    const size_t id = msg.message_id;")
        cog.outl("    void* p = data;")
        cog.outl("")
        cog.outl("    switch (id)")
        cog.outl("    {")
        for t in range(1, n + 1):
            cog.outl("      case T%d::ID: ::new (p) T%d(static_cast<T%d&&>(msg)); break;" %(t, t, t))
        cog.outl("      default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;")
        cog.outl("    }")
        cog.outl("  }")
        cog.outl("#endif")
        cog.outl("")
        cog.outl("  typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;")
        cog.outl("  bool valid;")
        cog.outl("};")
  ]]]*/
  /*[[[end]]]*/
}

#undef ETL_FILE

#endif
