[SCM] kodi-pvr-hts/master: add a utility file with a method to erase elements of a map using a lambda. This makes removing dirty entities much more concise.

tiber-guest at users.alioth.debian.org tiber-guest at users.alioth.debian.org
Wed Mar 2 23:01:43 UTC 2016


The following commit has been merged in the master branch:
commit 871ce068cdfe499e4316c005f44b80fc64ba2a6c
Author: Sam Stenvall <neggelandia at gmail.com>
Date:   Thu Jul 23 14:33:04 2015 +0300

    add a utility file with a method to erase elements of a map using
    a lambda. This makes removing dirty entities much more concise.

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8318881..0d18327 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -48,9 +48,13 @@ set(HTS_SOURCES_TVHEADEND_ENTITY
                 src/tvheadend/entity/Tag.cpp
                 src/tvheadend/entity/TimeRecording.h
                 src/tvheadend/entity/TimeRecording.cpp)
+
+set(HTS_SOURCES_TVHEADEND_UTILITIES
+				src/tvheadend/utilities/Utilities.h)
                 
 source_group("Source Files" FILES ${HTS_SOURCES})
 source_group("Source Files\\tvheadend\\entity" FILES ${HTS_SOURCES_TVHEADEND_ENTITY})
+source_group("Source Files\\tvheadend\\utilities" FILES ${HTS_SOURCES_TVHEADEND_UTILITIES})
 
 # Resource files
 set(HTS_RESOURCES 
@@ -66,6 +70,7 @@ source_group("Resource Files" FILES ${HTS_RESOURCES})
 # Combine the file lists
 list(APPEND HTS_SOURCES
             ${HTS_SOURCES_TVHEADEND_ENTITY}
+            ${HTS_SOURCES_TVHEADEND_UTILITIES}
             ${HTS_RESOURCES})
 
 add_subdirectory(lib/libhts)
diff --git a/src/AutoRecordings.cpp b/src/AutoRecordings.cpp
index 65d60c3..1886580 100644
--- a/src/AutoRecordings.cpp
+++ b/src/AutoRecordings.cpp
@@ -22,8 +22,10 @@
 #include "AutoRecordings.h"
 
 #include "Tvheadend.h"
+#include "tvheadend/utilities/Utilities.h"
 
 using namespace PLATFORM;
+using namespace tvheadend;
 using namespace tvheadend::entity;
 
 AutoRecordings::AutoRecordings(CHTSPConnection &conn) :
@@ -42,22 +44,12 @@ void AutoRecordings::Connected()
     it->second.SetDirty(true);
 }
 
-bool AutoRecordings::SyncDvrCompleted()
+void AutoRecordings::SyncDvrCompleted()
 {
-  bool update(false);
-
-  auto it = m_autoRecordings.begin();
-  while (it != m_autoRecordings.end())
+  utilities::erase_if(m_autoRecordings, [](const AutoRecordingMapEntry &entry)
   {
-    if (it->second.IsDirty())
-    {
-      update = true;
-      m_autoRecordings.erase(it++);
-    }
-    else
-      ++it;
-  }
-  return update;
+    return entry.second.IsDirty();
+  });
 }
 
 int AutoRecordings::GetAutorecTimerCount() const
diff --git a/src/AutoRecordings.h b/src/AutoRecordings.h
index f3d06b3..a51e523 100644
--- a/src/AutoRecordings.h
+++ b/src/AutoRecordings.h
@@ -42,7 +42,7 @@ public:
 
   /* state updates */
   void Connected();
-  bool SyncDvrCompleted();
+  void SyncDvrCompleted();
 
   /* data access */
   int  GetAutorecTimerCount() const;
diff --git a/src/TimeRecordings.cpp b/src/TimeRecordings.cpp
index 545a4e4..861ab3b 100644
--- a/src/TimeRecordings.cpp
+++ b/src/TimeRecordings.cpp
@@ -22,8 +22,10 @@
 #include "TimeRecordings.h"
 
 #include "Tvheadend.h"
+#include "tvheadend/utilities/Utilities.h"
 
 using namespace PLATFORM;
+using namespace tvheadend;
 using namespace tvheadend::entity;
 
 TimeRecordings::TimeRecordings(CHTSPConnection &conn) :
@@ -42,22 +44,12 @@ void TimeRecordings::Connected()
     it->second.SetDirty(true);
 }
 
-bool TimeRecordings::SyncDvrCompleted()
+void TimeRecordings::SyncDvrCompleted()
 {
-  bool update(false);
-
-  auto it = m_timeRecordings.begin();
-  while (it != m_timeRecordings.end())
+  utilities::erase_if(m_timeRecordings, [](const TimeRecordingMapEntry &entry)
   {
-    if (it->second.IsDirty())
-    {
-      update = true;
-      m_timeRecordings.erase(it++);
-    }
-    else
-      ++it;
-  }
-  return update;
+    return entry.second.IsDirty();
+  });
 }
 
 int TimeRecordings::GetTimerecTimerCount() const
diff --git a/src/TimeRecordings.h b/src/TimeRecordings.h
index e163ce8..8d0bf7d 100644
--- a/src/TimeRecordings.h
+++ b/src/TimeRecordings.h
@@ -42,7 +42,7 @@ public:
 
   /* state updates */
   void Connected();
-  bool SyncDvrCompleted();
+  void SyncDvrCompleted();
 
   /* data access */
   int  GetTimerecTimerCount() const;
diff --git a/src/Tvheadend.cpp b/src/Tvheadend.cpp
index 7ac559a..b6959f4 100644
--- a/src/Tvheadend.cpp
+++ b/src/Tvheadend.cpp
@@ -23,6 +23,7 @@
 #include <ctime>
 #include <memory>
 #include "Tvheadend.h"
+#include "tvheadend/utilities/Utilities.h"
 
 #include "platform/util/util.h"
 #include "platform/threads/atomics.h"
@@ -41,6 +42,7 @@ if ((x) != (y))\
 using namespace std;
 using namespace ADDON;
 using namespace PLATFORM;
+using namespace tvheadend;
 using namespace tvheadend::entity;
 
 CTvheadend::CTvheadend(tvheadend::Settings settings)
@@ -1373,23 +1375,19 @@ void CTvheadend::SyncChannelsCompleted ( void )
   Tags::iterator tit = m_tags.begin();
 
   /* Tags */
-  while (tit != m_tags.end())
+  utilities::erase_if(m_tags, [](const TagMapEntry &entry)
   {
-    if (tit->second.IsDirty())
-      m_tags.erase(tit++);
-    else
-      ++tit;
-  }
+    return entry.second.IsDirty();
+  });
+
   TriggerChannelGroupsUpdate();
 
   /* Channels */
-  while (cit != m_channels.end())
+  utilities::erase_if(m_channels, [](const ChannelMapEntry &entry)
   {
-    if (cit->second.IsDirty())
-      m_channels.erase(cit++);
-    else
-      ++cit;
-  }
+    return entry.second.IsDirty();
+  });
+
   TriggerChannelUpdate();
   
   /* Next */
@@ -1402,32 +1400,20 @@ void CTvheadend::SyncDvrCompleted ( void )
   if (m_asyncState.GetState() > ASYNC_DVR)
     return;
 
-  bool update;
-  Recordings::iterator rit = m_recordings.begin();
-
   /* Recordings */
-  update = false;
-  while (rit != m_recordings.end())
+  utilities::erase_if(m_recordings, [](const RecordingMapEntry &entry)
   {
-    if (rit->second.IsDirty())
-    {
-      update = true;
-      m_recordings.erase(rit++);
-    }
-    else
-      ++rit;
-  }
+    return entry.second.IsDirty();
+  });
 
   /* Time-based repeating timers */
-  update |= m_timeRecordings.SyncDvrCompleted();
+  m_timeRecordings.SyncDvrCompleted();
 
   /* EPG-query-based repeating timers */
-  update |= m_autoRecordings.SyncDvrCompleted();
+  m_autoRecordings.SyncDvrCompleted();
 
   TriggerRecordingUpdate();
   TriggerTimerUpdate();
-  if (update)
-    tvhinfo("recordings updated");
 
   /* Next */
   m_asyncState.SetState(ASYNC_EPG);
@@ -1438,43 +1424,25 @@ void CTvheadend::SyncEpgCompleted ( void )
   /* Done */
   if (!m_settings.bAsyncEpg || m_asyncState.GetState() > ASYNC_EPG)
     return;
-  
-  bool update;
-  Schedules::iterator  sit = m_schedules.begin();
-  Events::iterator     eit;
 
   /* Events */
-  update = false;
-  while (sit != m_schedules.end())
+  for (auto &entry : m_schedules)
   {
-    uint32_t channelId = sit->second.channel;
-    
-    if (sit->second.IsDirty())
-    {
-      update = true;
-      m_schedules.erase(sit++);
-    }
-    else
+    utilities::erase_if(entry.second.events, [](const EventMapEntry &entry)
     {
-      eit = sit->second.events.begin();
-      while (eit != sit->second.events.end())
-      {
-        if (eit->second.IsDirty())
-        {
-          update = true;
-          sit->second.events.erase(eit++);
-        }
-        else
-          ++eit;
-      }
-      ++sit;
-    }
-
-    TriggerEpgUpdate(channelId);
+      return entry.second.IsDirty();
+    });
   }
+  
+  /* Schedules */
+  utilities::erase_if(m_schedules, [](const ScheduleMapEntry &entry)
+  {
+    return entry.second.IsDirty();
+  });
 
-  if (update)
-    tvhinfo("epg updated");
+  /* Trigger updates */
+  for (const auto &entry : m_schedules)
+    TriggerEpgUpdate(entry.second.channel);
 }
 
 void CTvheadend::ParseTagAddOrUpdate ( htsmsg_t *msg, bool bAdd )
diff --git a/src/tvheadend/entity/AutoRecording.h b/src/tvheadend/entity/AutoRecording.h
index 7ae3a00..9f5ea16 100644
--- a/src/tvheadend/entity/AutoRecording.h
+++ b/src/tvheadend/entity/AutoRecording.h
@@ -66,5 +66,6 @@ namespace tvheadend
     };
 
     typedef std::map<std::string, AutoRecording> AutoRecordingsMap;
+    typedef std::pair<std::string, AutoRecording> AutoRecordingMapEntry;
   }
 }
diff --git a/src/tvheadend/entity/TimeRecording.h b/src/tvheadend/entity/TimeRecording.h
index f46ee6c..78a760e 100644
--- a/src/tvheadend/entity/TimeRecording.h
+++ b/src/tvheadend/entity/TimeRecording.h
@@ -48,5 +48,6 @@ namespace tvheadend
     };
 
     typedef std::map<std::string, TimeRecording> TimeRecordingsMap;
+    typedef std::pair<std::string, TimeRecording> TimeRecordingMapEntry;
   }
 }
diff --git a/src/tvheadend/entity/Entity.h b/src/tvheadend/utilities/Utilities.h
similarity index 64%
copy from src/tvheadend/entity/Entity.h
copy to src/tvheadend/utilities/Utilities.h
index 677faca..fed8100 100644
--- a/src/tvheadend/entity/Entity.h
+++ b/src/tvheadend/utilities/Utilities.h
@@ -23,37 +23,23 @@
 
 namespace tvheadend
 {
-  namespace entity
+  namespace utilities
   {
 
     /**
-     * Abstract entity. An entity can be dirty or clean
+     * std::remove_if() for maps. Borrowed from:
+     * http://stackoverflow.com/questions/800955/remove-if-equivalent-for-stdmap
      */
-    class Entity
+    template< typename ContainerT, typename PredicateT >
+    void erase_if(ContainerT& items, const PredicateT& predicate)
     {
-    public:
-      Entity() : m_dirty(false) {};
-      virtual ~Entity() = default;
-
-      /**
-       * @return if the entity is dirty
-       */
-      virtual bool IsDirty() const
+      for (auto it = items.begin(); it != items.end();)
       {
-        return m_dirty;
+        if (predicate(*it))
+          it = items.erase(it);
+        else
+          ++it;
       }
-
-      /**
-       * Marks the entity as dirty or not
-       * @param dirty
-       */
-      virtual void SetDirty(bool dirty)
-      {
-        m_dirty = dirty;
-      }
-
-    private:
-      bool m_dirty;
     };
   }
 }

-- 
kodi-pvr-hts packaging



More information about the pkg-multimedia-commits mailing list