<commit_before>/*
 * Copyright (c) 2016-2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef EVENT_H
#define EVENT_H

#include <utility>
#include "events/EventQueue.h"
#include "platform/mbed_assert.h"

namespace events {
/** \defgroup events-public-api Events
 * \ingroup mbed-os-public
 * @{
 */

/** Event
 *
 *  Representation of an event for fine-grain dispatch control
 */
template <typename F>
class Event;

/**
 * \defgroup events_Event Event<void()> class
 * @{
 */

/** Event
 *
 *  Representation of an event for fine-grain dispatch control
 */
template <typename... ArgTs>
class Event<void(ArgTs...)> {
public:
    using duration = std::chrono::duration<int, std::milli>;

    /** Create an event
     *
     *  Constructs an event bound to the specified event queue. The specified
     *  callback acts as the target for the event and is executed in the
     *  context of the event queue's dispatch loop once posted.
     *
     *  @param q        Event queue to dispatch on
     *  @param f        Function to execute when the event is dispatched
     */
    template <typename F>
    Event(EventQueue *q, F f)
    {
        _event = static_cast<struct event *>(
                     equeue_alloc(&q->_equeue, sizeof(struct event) + sizeof(F)));

        if (_event) {
            _event->equeue = &q->_equeue;
            _event->id = 0;
            _event->delay = duration(0);
            _event->period = duration(-1);

            _event->post = &Event::event_post<F>;
            _event->dtor = &Event::event_dtor<F>;

            new (_event + 1) F(std::move(f));

            _event->ref = 1;
        }
    }

    /** Copy constructor for events
     */
    Event(const Event &e)
    {
        _event = 0;
        if (e._event) {
            _event = e._event;
            _event->ref += 1;
        }
    }

    /** Assignment operator for events
     */
    Event &operator=(const Event &that)
    {
        if (this != &that) {
            this->~Event();
            new (this) Event(that);
        }

        return *this;
    }

    /** Destructor for events
     */
    ~Event()
    {
        if (_event) {
            _event->ref -= 1;
            if (_event->ref == 0) {
                _event->dtor(_event);
                equeue_dealloc(_event->equeue, _event);
            }
        }
    }

    /** Configure the delay of an event
     *
     *  @param d    delay (in milliseconds) before dispatching the event, expressed as a Chrono duration.
     *              E.g. delay(50ms)
     */
    void delay(duration d)
    {
        if (_event) {
            _event->delay = d;
        }
    }

    /** Configure the delay of an event
     *  @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
     *
     *  @param d    Millisecond delay before dispatching the event
     */
    MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
    void delay(int d)
    {
        delay(duration(d));
    }

    /** Configure the period of an event
     *
     *  @param p   period (in milliseconds) for repeatedly dispatching an event, expressed as a Chrono duration.
     *             E.g. period(200ms)
     */
    void period(duration p)
    {
        if (_event) {
            _event->period = p;
        }
    }

    /** Configure the period of an event
     *  @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
     *
     *  @param p   Millisecond period for repeatedly dispatching an event
     */
    MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
    void period(int p)
    {
        period(duration(p));
    }

    /** Posts an event onto the underlying event queue
     *
     *  The event is posted to the underlying queue and is executed in the
     *  context of the event queue's dispatch loop.
     *
     *  The post function is IRQ safe and can act as a mechanism for moving
     *  events out of IRQ contexts.
     *<commit_msg>Review feedback - capitilise argument descriptions<commit_after>/*
 * Copyright (c) 2016-2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef EVENT_H
#define EVENT_H

#include <utility>
#include "events/EventQueue.h"
#include "platform/mbed_assert.h"

namespace events {
/** \defgroup events-public-api Events
 * \ingroup mbed-os-public
 * @{
 */

/** Event
 *
 *  Representation of an event for fine-grain dispatch control
 */
template <typename F>
class Event;

/**
 * \defgroup events_Event Event<void()> class
 * @{
 */

/** Event
 *
 *  Representation of an event for fine-grain dispatch control
 */
template <typename... ArgTs>
class Event<void(ArgTs...)> {
public:
    using duration = std::chrono::duration<int, std::milli>;

    /** Create an event
     *
     *  Constructs an event bound to the specified event queue. The specified
     *  callback acts as the target for the event and is executed in the
     *  context of the event queue's dispatch loop once posted.
     *
     *  @param q        Event queue to dispatch on
     *  @param f        Function to execute when the event is dispatched
     */
    template <typename F>
    Event(EventQueue *q, F f)
    {
        _event = static_cast<struct event *>(
                     equeue_alloc(&q->_equeue, sizeof(struct event) + sizeof(F)));

        if (_event) {
            _event->equeue = &q->_equeue;
            _event->id = 0;
            _event->delay = duration(0);
            _event->period = duration(-1);

            _event->post = &Event::event_post<F>;
            _event->dtor = &Event::event_dtor<F>;

            new (_event + 1) F(std::move(f));

            _event->ref = 1;
        }
    }

    /** Copy constructor for events
     */
    Event(const Event &e)
    {
        _event = 0;
        if (e._event) {
            _event = e._event;
            _event->ref += 1;
        }
    }

    /** Assignment operator for events
     */
    Event &operator=(const Event &that)
    {
        if (this != &that) {
            this->~Event();
            new (this) Event(that);
        }

        return *this;
    }

    /** Destructor for events
     */
    ~Event()
    {
        if (_event) {
            _event->ref -= 1;
            if (_event->ref == 0) {
                _event->dtor(_event);
                equeue_dealloc(_event->equeue, _event);
            }
        }
    }

    /** Configure the delay of an event
     *
     *  @param d    Delay (in milliseconds) before dispatching the event, expressed as a Chrono duration.
     *              E.g. delay(50ms)
     */
    void delay(duration d)
    {
        if (_event) {
            _event->delay = d;
        }
    }

    /** Configure the delay of an event
     *  @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
     *
     *  @param d    Millisecond delay before dispatching the event
     */
    MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
    void delay(int d)
    {
        delay(duration(d));
    }

    /** Configure the period of an event
     *
     *  @param p   Period (in milliseconds) for repeatedly dispatching an event, expressed as a Chrono duration.
     *             E.g. period(200ms)
     */
    void period(duration p)
    {
        if (_event) {
            _event->period = p;
        }
    }

    /** Configure the period of an event
     *  @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
     *
     *  @param p   Millisecond period for repeatedly dispatching an event
     */
    MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
    void period(int p)
    {
        period(duration(p));
    }

    /** Posts an event onto the underlying event queue
     *
     *  The event is posted to the underlying queue and is executed in the
     *  context of the event queue's dispatch loop.
     *
     *  The post function is IRQ safe and can act as a mechanism for moving
     *  events out of IRQ contexts.
     *
{'fraction_non_alphanumeric': 0.1048158640226629, 'fraction_numerical': 0.0113314447592068, 'mean_word_length': 2.071865936190783, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6619542', 'n_tokens_mistral': 2871, 'n_tokens_neox': 2751, 'n_words': 972}