util: add ndn::scheduler namespace alias

This is the new preferred way to refer to Scheduler and related
classes. The Scheduler class itself is also available as ndn::Scheduler
like before. ndn::util::Scheduler and ndn::EventId are deprecated.

Refs: #4883
Change-Id: I825c2a0911a959caa60d7d8a547ca7199c58f61e
diff --git a/ndn-cxx/impl/face-impl.hpp b/ndn-cxx/impl/face-impl.hpp
index 5b5c39e..e8219ed 100644
--- a/ndn-cxx/impl/face-impl.hpp
+++ b/ndn-cxx/impl/face-impl.hpp
@@ -424,8 +424,8 @@
 
 private:
   Face& m_face;
-  util::Scheduler m_scheduler;
-  util::scheduler::ScopedEventId m_processEventsTimeoutEvent;
+  Scheduler m_scheduler;
+  scheduler::ScopedEventId m_processEventsTimeoutEvent;
 
   std::atomic_uintptr_t m_lastPendingInterestId;
   PendingInterestTable m_pendingInterestTable;
diff --git a/ndn-cxx/impl/pending-interest.hpp b/ndn-cxx/impl/pending-interest.hpp
index ac14304..a139fc2 100644
--- a/ndn-cxx/impl/pending-interest.hpp
+++ b/ndn-cxx/impl/pending-interest.hpp
@@ -210,7 +210,7 @@
   DataCallback m_dataCallback;
   NackCallback m_nackCallback;
   TimeoutCallback m_timeoutCallback;
-  util::scheduler::ScopedEventId m_timeoutEvent;
+  scheduler::ScopedEventId m_timeoutEvent;
   int m_nNotNacked; ///< number of Interest destinations that have not Nacked
   optional<lp::Nack> m_leastSevereNack;
   std::function<void()> m_deleter;
diff --git a/ndn-cxx/ims/in-memory-storage-entry.cpp b/ndn-cxx/ims/in-memory-storage-entry.cpp
index 148aee1..7b04271 100644
--- a/ndn-cxx/ims/in-memory-storage-entry.cpp
+++ b/ndn-cxx/ims/in-memory-storage-entry.cpp
@@ -43,7 +43,7 @@
 }
 
 void
-InMemoryStorageEntry::scheduleMarkStale(util::Scheduler& sched, time::nanoseconds after)
+InMemoryStorageEntry::scheduleMarkStale(Scheduler& sched, time::nanoseconds after)
 {
   m_markStaleEventId = sched.scheduleEvent(after, [this] { m_isFresh = false; });
 }
diff --git a/ndn-cxx/ims/in-memory-storage-entry.hpp b/ndn-cxx/ims/in-memory-storage-entry.hpp
index 1640c02..b257709 100644
--- a/ndn-cxx/ims/in-memory-storage-entry.hpp
+++ b/ndn-cxx/ims/in-memory-storage-entry.hpp
@@ -77,7 +77,7 @@
   /** @brief Schedule an event to mark this entry as non-fresh.
    */
   void
-  scheduleMarkStale(util::Scheduler& sched, time::nanoseconds after);
+  scheduleMarkStale(Scheduler& sched, time::nanoseconds after);
 
   /** @brief Check if the data can satisfy an interest with MustBeFresh
    */
@@ -91,7 +91,7 @@
   shared_ptr<const Data> m_dataPacket;
 
   bool m_isFresh;
-  util::scheduler::ScopedEventId m_markStaleEventId;
+  scheduler::ScopedEventId m_markStaleEventId;
 };
 
 } // namespace ndn
diff --git a/ndn-cxx/net/dns.cpp b/ndn-cxx/net/dns.cpp
index 54d21e4..55f0822 100644
--- a/ndn-cxx/net/dns.cpp
+++ b/ndn-cxx/net/dns.cpp
@@ -124,8 +124,8 @@
   SuccessCallback m_onSuccess;
   ErrorCallback m_onError;
 
-  util::scheduler::Scheduler m_scheduler;
-  util::scheduler::EventId m_resolveTimeout;
+  Scheduler m_scheduler;
+  scheduler::EventId m_resolveTimeout;
 };
 
 void
diff --git a/ndn-cxx/net/impl/network-monitor-impl-osx.hpp b/ndn-cxx/net/impl/network-monitor-impl-osx.hpp
index c287faa..f3a4cdc 100644
--- a/ndn-cxx/net/impl/network-monitor-impl-osx.hpp
+++ b/ndn-cxx/net/impl/network-monitor-impl-osx.hpp
@@ -101,8 +101,8 @@
 private:
   std::map<std::string, shared_ptr<NetworkInterface>> m_interfaces; ///< ifname => interface
 
-  util::Scheduler m_scheduler;
-  util::scheduler::ScopedEventId m_cfLoopEvent;
+  Scheduler m_scheduler;
+  scheduler::ScopedEventId m_cfLoopEvent;
 
   SCDynamicStoreContext m_context;
   detail::CFReleaser<SCDynamicStoreRef> m_scStore;
diff --git a/ndn-cxx/util/notification-subscriber.hpp b/ndn-cxx/util/notification-subscriber.hpp
index 2cc5f42..340eec7 100644
--- a/ndn-cxx/util/notification-subscriber.hpp
+++ b/ndn-cxx/util/notification-subscriber.hpp
@@ -137,8 +137,8 @@
   uint64_t m_lastSequenceNum;
   uint64_t m_lastNackSequenceNum;
   uint64_t m_attempts;
-  util::scheduler::Scheduler m_scheduler;
-  util::scheduler::ScopedEventId m_nackEvent;
+  Scheduler m_scheduler;
+  scheduler::ScopedEventId m_nackEvent;
   ScopedPendingInterestHandle m_lastInterest;
   time::milliseconds m_interestLifetime;
 };
diff --git a/ndn-cxx/util/scheduler.cpp b/ndn-cxx/util/scheduler.cpp
index 36a662c..f4b3a10 100644
--- a/ndn-cxx/util/scheduler.cpp
+++ b/ndn-cxx/util/scheduler.cpp
@@ -33,10 +33,9 @@
 class EventInfo : noncopyable
 {
 public:
-  EventInfo(time::nanoseconds after, const EventCallback& callback)
-    : expireTime(time::steady_clock::now() + after)
-    , isExpired(false)
-    , callback(callback)
+  EventInfo(time::nanoseconds after, EventCallback&& cb)
+    : callback(std::move(cb))
+    , expireTime(time::steady_clock::now() + after)
   {
   }
 
@@ -47,10 +46,10 @@
   }
 
 public:
-  time::steady_clock::TimePoint expireTime;
-  bool isExpired;
   EventCallback callback;
-  EventQueue::const_iterator queueIt;
+  Scheduler::EventQueue::const_iterator queueIt;
+  time::steady_clock::TimePoint expireTime;
+  bool isExpired = false;
 };
 
 EventId::EventId(Scheduler& sched, weak_ptr<EventInfo> info)
@@ -85,25 +84,25 @@
 }
 
 bool
-EventQueueCompare::operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const noexcept
+Scheduler::EventQueueCompare::operator()(const shared_ptr<EventInfo>& a,
+                                         const shared_ptr<EventInfo>& b) const noexcept
 {
   return a->expireTime < b->expireTime;
 }
 
 Scheduler::Scheduler(boost::asio::io_service& ioService)
   : m_timer(make_unique<detail::SteadyTimer>(ioService))
-  , m_isEventExecuting(false)
 {
 }
 
 Scheduler::~Scheduler() = default;
 
 EventId
-Scheduler::scheduleEvent(time::nanoseconds after, const EventCallback& callback)
+Scheduler::scheduleEvent(time::nanoseconds after, EventCallback callback)
 {
   BOOST_ASSERT(callback != nullptr);
 
-  EventQueue::iterator i = m_queue.insert(make_shared<EventInfo>(after, callback));
+  auto i = m_queue.insert(make_shared<EventInfo>(after, std::move(callback)));
   (*i)->queueIt = i;
 
   if (!m_isEventExecuting && i == m_queue.begin()) {
diff --git a/ndn-cxx/util/scheduler.hpp b/ndn-cxx/util/scheduler.hpp
index 578d8ca..f1a38f7 100644
--- a/ndn-cxx/util/scheduler.hpp
+++ b/ndn-cxx/util/scheduler.hpp
@@ -135,21 +135,12 @@
    */
   [[deprecated]]
   explicit
-  ScopedEventId(Scheduler& scheduler) noexcept
+  ScopedEventId(Scheduler&) noexcept
   {
   }
 };
 
-class EventQueueCompare
-{
-public:
-  bool
-  operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const noexcept;
-};
-
-using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
-
-/** \brief Generic scheduler
+/** \brief Generic time-based scheduler
  */
 class Scheduler : noncopyable
 {
@@ -163,7 +154,7 @@
    *  \return EventId that can be used to cancel the scheduled event
    */
   EventId
-  scheduleEvent(time::nanoseconds after, const EventCallback& callback);
+  scheduleEvent(time::nanoseconds after, EventCallback callback);
 
   /** \brief Cancel a scheduled event
    *
@@ -198,22 +189,35 @@
   executeEvent(const boost::system::error_code& code);
 
 private:
-  unique_ptr<detail::SteadyTimer> m_timer;
+  class EventQueueCompare
+  {
+  public:
+    bool
+    operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const noexcept;
+  };
+
+  using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
   EventQueue m_queue;
-  bool m_isEventExecuting;
+
+  unique_ptr<util::detail::SteadyTimer> m_timer;
+  bool m_isEventExecuting = false;
 
   friend EventId;
+  friend EventInfo;
 };
 
 } // namespace scheduler
 
-using util::scheduler::Scheduler;
+// for backwards compatibility
+using Scheduler [[deprecated]] = scheduler::Scheduler;
 
 } // namespace util
 
+namespace scheduler = util::scheduler;
+using scheduler::Scheduler;
+
 // for backwards compatibility
-using util::scheduler::Scheduler;
-using util::scheduler::EventId;
+using EventId [[deprecated]] = util::scheduler::EventId;
 
 } // namespace ndn
 
diff --git a/tests/integrated/face.cpp b/tests/integrated/face.cpp
index 99d5a20..5798b0f 100644
--- a/tests/integrated/face.cpp
+++ b/tests/integrated/face.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2018 Regents of the University of California.
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -79,7 +79,7 @@
    *  \param[out] outcome the response, initially '?', 'D' for Data, 'N' for Nack, 'T' for timeout
    *  \return scheduled event id
    */
-  EventId
+  scheduler::EventId
   sendInterest(time::nanoseconds delay, const Interest& interest, char& outcome)
   {
     if (face2 == nullptr) {
@@ -95,7 +95,7 @@
     });
   }
 
-  EventId
+  scheduler::EventId
   sendInterest(time::nanoseconds delay, const Interest& interest)
   {
     static char ignoredOutcome;
@@ -105,7 +105,7 @@
   /** \brief Stop io_service after a delay
    *  \return scheduled event id
    */
-  EventId
+  scheduler::EventId
   terminateAfter(time::nanoseconds delay)
   {
     return sched.scheduleEvent(delay, [this] { face.getIoService().stop(); });
@@ -114,7 +114,6 @@
 protected:
   Face face;
   Scheduler sched;
-
   unique_ptr<Face> face2;
 };
 
diff --git a/tests/unit/util/scheduler.t.cpp b/tests/unit/util/scheduler.t.cpp
index d8335c4..77b60a5 100644
--- a/tests/unit/util/scheduler.t.cpp
+++ b/tests/unit/util/scheduler.t.cpp
@@ -31,9 +31,7 @@
 namespace scheduler {
 namespace tests {
 
-using namespace ndn::tests;
-
-class SchedulerFixture : public UnitTestTimeFixture
+class SchedulerFixture : public ndn::tests::UnitTestTimeFixture
 {
 public:
   SchedulerFixture()