/*
 * SPDX-FileCopyrightText: 2004 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#ifndef TANGO_SERVER_TIMEDCMDDATA_H
#define TANGO_SERVER_TIMEDCMDDATA_H

#include <tango/server/Except.h>
#include <tango/server/TangoTimestamp.h>

#include <chrono>

namespace Tango
{
//=============================================================================
//
//            The TimedCmdData class
//
//
// description :     This class is used to store all the data needed to build
//            a command value plus a date.
//
//=============================================================================

/**
 * This class is used to store one element of a command history stack.
 *
 *
 * @headerfile tango.h
 * @ingroup Server
 */

template <typename T>
class TimedCmdData
{
  public:
    /**@name Constructors
     * Miscellaneous constructors
     */
    //@{
    /**
     * Create a new TimedCmdData object.
     *
     * The memory pointed to by the <i>p_data</i> parameter will not be freed
     *
     * @param p_data Pointer to the command result data
     * @param when The date
     */
    TimedCmdData(T *p_data, const TangoTimestamp &when) :
        TimedCmdData(p_data, false, when)
    {
    }

    /**
     * Create a new TimedCmdData object with memory management.
     *
     * @param p_data Pointer to the command result data
     * @param rel Set to true if the memory pointed to by the <i>p_data</i> parameter must be freed
     * @param when The date
     */
    TimedCmdData(T *p_data, bool rel, const TangoTimestamp &when) :
        ptr(p_data),
        tp(when),
        release(rel)
    {
    }

    /**
     * Create a new TimedCmdData object for errors.
     *
     * The created TimedCmdData is used to store command errors
     * in the command history stack
     *
     * @param errs The error stack
     * @param when The date
     */
    TimedCmdData(DevErrorList errs, const TangoTimestamp &when) :
        ptr(nullptr),
        err(errs),
        tp(when),
        release(false)
    {
    }

    //@}

    /// @privatesection

    T *ptr;
    DevErrorList err;
    std::chrono::system_clock::time_point tp;
    bool release;
};
} // namespace Tango
#endif
