Program Listing for File WriterHistory.h

Return to documentation for file (/tmp/ws/src/fastrtps/include/fastdds/rtps/history/WriterHistory.h)

// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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 _FASTDDS_RTPS_WRITERHISTORY_H_
#define _FASTDDS_RTPS_WRITERHISTORY_H_

#include <fastdds/rtps/history/History.h>
#include <fastdds/dds/log/Log.hpp>

namespace eprosima {
namespace fastrtps {
namespace rtps {

class RTPSWriter;
class WriteParams;

class WriterHistory : public rtps::History
{
    friend class RTPSWriter;
    friend class PersistentWriter;
    friend class IPersistenceService;

    WriterHistory(
            WriterHistory&&) = delete;
    WriterHistory& operator =(
            WriterHistory&&) = delete;

public:

    RTPS_DllAPI WriterHistory(
            const HistoryAttributes&  att);
    RTPS_DllAPI virtual ~WriterHistory() override;

    RTPS_DllAPI bool add_change(
            CacheChange_t* a_change);

    RTPS_DllAPI bool add_change(
            CacheChange_t* a_change,
            WriteParams& wparams);

    RTPS_DllAPI iterator remove_change_nts(
            const_iterator removal,
            bool release = true) override;

    RTPS_DllAPI bool matches_change(
            const CacheChange_t* inner,
            CacheChange_t* outer) override;

    using History::remove_change;

    RTPS_DllAPI virtual bool remove_change_g(
            CacheChange_t* a_change);

    RTPS_DllAPI bool remove_change(
            const SequenceNumber_t& sequence_number);

    RTPS_DllAPI CacheChange_t* remove_change_and_reuse(
            const SequenceNumber_t& sequence_number);

    RTPS_DllAPI bool remove_min_change();

    RTPS_DllAPI SequenceNumber_t next_sequence_number() const
    {
        return m_lastCacheChangeSeqNum + 1;
    }

protected:

    RTPS_DllAPI bool do_reserve_cache(
            CacheChange_t** change,
            uint32_t size) override;

    RTPS_DllAPI void do_release_cache(
            CacheChange_t* ch) override;

    bool add_change_(
            CacheChange_t* a_change,
            WriteParams& wparams,
            std::chrono::time_point<std::chrono::steady_clock> max_blocking_time
            = std::chrono::steady_clock::now() + std::chrono::hours(24));

    template<typename PreCommitHook>
    bool add_change_with_commit_hook(
            CacheChange_t* a_change,
            WriteParams& wparams,
            PreCommitHook pre_commit,
            std::chrono::time_point<std::chrono::steady_clock> max_blocking_time)
    {
        if (mp_writer == nullptr || mp_mutex == nullptr)
        {
            EPROSIMA_LOG_ERROR(RTPS_WRITER_HISTORY,
                    "You need to create a Writer with this History before adding any changes");
            return false;
        }

        std::lock_guard<RecursiveTimedMutex> guard(*mp_mutex);
        if (!prepare_and_add_change(a_change, wparams))
        {
            return false;
        }

        pre_commit(*a_change);
        notify_writer(a_change, max_blocking_time);

        return true;
    }

    SequenceNumber_t m_lastCacheChangeSeqNum;
    RTPSWriter* mp_writer;

    uint32_t high_mark_for_frag_ = 0;

private:

    bool prepare_and_add_change(
            CacheChange_t* a_change,
            WriteParams& wparams);

    void notify_writer(
            CacheChange_t* a_change,
            const std::chrono::time_point<std::chrono::steady_clock>& max_blocking_time);

    void set_fragments(
            CacheChange_t* change);
};

} // namespace rtps
} /* namespace fastrtps */
} /* namespace eprosima */

#endif /* _FASTDDS_RTPS_WRITERHISTORY_H_ */