Program Listing for File Event.hpp

Return to documentation for file (/tmp/ws/src/rmf_task/rmf_task_sequence/include/rmf_task_sequence/Event.hpp)

/*
 * Copyright (C) 2021 Open Source Robotics Foundation
 *
 * 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 RMF_TASK_SEQUENCE__EVENT_HPP
#define RMF_TASK_SEQUENCE__EVENT_HPP

#include <rmf_task/Constraints.hpp>
#include <rmf_task/Parameters.hpp>
#include <rmf_task/Estimate.hpp>
#include <rmf_task/Event.hpp>

#include <rmf_task/detail/Resume.hpp>

#include <rmf_task_sequence/Activity.hpp>

#include <nlohmann/json.hpp>

namespace rmf_task_sequence {

//==============================================================================
class Event : public rmf_task::Event
{
public:

  // Event::Active simply uses the Activity::Active API
  class Active;
  using ActivePtr = std::shared_ptr<Active>;

  // Event::Description simply uses the Activity::Description API
  using Description = Activity::Description;
  using ConstDescriptionPtr = std::shared_ptr<const Description>;

  class Standby;
  using StandbyPtr = std::shared_ptr<Standby>;

  class Initializer;
  using InitializerPtr = std::shared_ptr<Initializer>;
  using ConstInitializerPtr = std::shared_ptr<const Initializer>;
};

//==============================================================================
class Event::Standby
{
public:

  virtual ConstStatePtr state() const = 0;

  virtual rmf_traffic::Duration duration_estimate() const = 0;

  virtual ActivePtr begin(
    std::function<void()> checkpoint,
    std::function<void()> finished) = 0;

  // Virtual destructor
  virtual ~Standby() = default;
};

//==============================================================================
class Event::Active : public Activity::Active
{
public:

  virtual ConstStatePtr state() const = 0;

  virtual rmf_traffic::Duration remaining_time_estimate() const = 0;

  // Virtual destructor
  virtual ~Active() = default;
};

//==============================================================================
class Event::Initializer
{
public:

  Initializer();

  template<typename Description>
  using Initialize =
    std::function<
    StandbyPtr(
      const AssignIDPtr& id,
      const std::function<rmf_task::State()>& get_state,
      const ConstParametersPtr& parameters,
      const Description& description,
      std::function<void()> update)
    >;

  template<typename Description>
  using Restore =
    std::function<
    ActivePtr(
      const AssignIDPtr& id,
      const std::function<rmf_task::State()>& get_state,
      const ConstParametersPtr& parameters,
      const Description& description,
      const nlohmann::json& backup_state,
      std::function<void()> update,
      std::function<void()> checkpoint,
      std::function<void()> finished)
    >;

  template<typename Desc>
  void add(
    Initialize<Desc> initializer,
    Restore<Desc> restorer);

  StandbyPtr initialize(
    const AssignIDPtr& id,
    const std::function<rmf_task::State()>& get_state,
    const ConstParametersPtr& parameters,
    const Event::Description& description,
    std::function<void()> update) const;

  ActivePtr restore(
    const AssignIDPtr& id,
    const std::function<rmf_task::State()>& get_state,
    const ConstParametersPtr& parameters,
    const Event::Description& description,
    const nlohmann::json& backup,
    std::function<void()> update,
    std::function<void()> checkpoint,
    std::function<void()> finished) const;

  class Implementation;
private:
  void _add(
    std::type_index type,
    Initialize<Event::Description> initializer,
    Restore<Event::Description> restorer);

  rmf_utils::impl_ptr<Implementation> _pimpl;
};

} // namespace rmf_task_sequence

#include <rmf_task_sequence/detail/impl_Event.hpp>

#endif // RMF_TASK_SEQUENCE__EVENT_HPP