Program Listing for File Viewer.hpp

Return to documentation for file (include/rmf_traffic/schedule/Viewer.hpp)

/*
 * Copyright (C) 2019 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_TRAFFIC__SCHEDULE__VIEWER_HPP
#define RMF_TRAFFIC__SCHEDULE__VIEWER_HPP

#include <rmf_traffic/detail/bidirectional_iterator.hpp>

#include <rmf_traffic/schedule/Query.hpp>
#include <rmf_traffic/schedule/Participant.hpp>
#include <rmf_traffic/schedule/Itinerary.hpp>

#include <rmf_utils/impl_ptr.hpp>
#include <rmf_utils/macros.hpp>
#include <rmf_utils/optional.hpp>

namespace rmf_traffic {
namespace schedule {

//==============================================================================
class Viewer
{
public:

  class View
  {
  public:

    template<typename E, typename I, typename F>
    using base_iterator = rmf_traffic::detail::bidirectional_iterator<E, I, F>;

    // TODO(MXG): Replace this with a PIMPL class
    struct Element
    {
      const ParticipantId participant;
      const PlanId plan_id;
      const RouteId route_id;
      const std::shared_ptr<const Route> route;
      const ParticipantDescription& description;
    };

    class IterImpl;
    using const_iterator = base_iterator<const Element, IterImpl, View>;
    using iterator = const_iterator;

    const_iterator begin() const;

    const_iterator end() const;

    std::size_t size() const;

    class Implementation;
  private:
    rmf_utils::impl_ptr<Implementation> _pimpl;
  };

  virtual View query(const Query& parameters) const = 0;

  virtual View query(
    const Query::Spacetime& spacetime,
    const Query::Participants& participants) const = 0;

  // TODO(MXG): Consider providing an iterator-style API to view participant IDs
  // and participant descriptions.

  virtual const std::unordered_set<ParticipantId>& participant_ids() const = 0;

  virtual std::shared_ptr<const ParticipantDescription> get_participant(
    ParticipantId participant_id) const = 0;

//  virtual Version latest_version() const = 0;

  // Virtual destructor
  virtual ~Viewer() = default;

  // The Debug class is for internal testing use only. Its definition is not
  // visible to downstream users.
  class Debug;
  class Implementation;
};

//==============================================================================
class ItineraryViewer : public virtual Viewer
{
public:

  virtual std::optional<ItineraryView> get_itinerary(
    ParticipantId participant_id) const = 0;

  virtual std::optional<PlanId> get_current_plan_id(
    ParticipantId participant_id) const = 0;

  virtual const std::vector<CheckpointId>* get_current_progress(
    ParticipantId participant_id) const = 0;

  virtual ProgressVersion get_current_progress_version(
    ParticipantId participant_id) const = 0;

  class DependencySubscription
  {
  public:

    bool reached() const;

    bool deprecated() const;

    bool finished() const;

    Dependency dependency() const;

    // TODO(MXG): Should this class allow the user to change the callbacks for
    // on_reached and on_changed? I'm concerned that could lead to race
    // conditions or confusing behavior.

    class Implementation;
  private:
    DependencySubscription();
    rmf_utils::unique_impl_ptr<Implementation> _pimpl;
  };

  virtual DependencySubscription watch_dependency(
    Dependency dependency,
    std::function<void()> on_reached,
    std::function<void()> on_deprecated) const = 0;

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

} // namespace schedule

namespace detail {

extern template class bidirectional_iterator<
    const schedule::Viewer::View::Element,
    schedule::Viewer::View::IterImpl,
    schedule::Viewer::View
>;

} // namespace detail
} // namespace rmf_traffic

#endif // RMF_TRAFFIC__SCHEDULE__VIEWER_HPP