C++11 and whitespace cleanups.
[dcpomatic.git] / src / lib / active_text.cc
index de3dc816563cb8b3c0e46807dbab4bb27218ba1f..1e0fd6adbcc1bc80b4d90c08a293893bfaf48370 100644 (file)
 
 */
 
+
 #include "active_text.h"
 #include "text_content.h"
-#include <boost/shared_ptr.hpp>
-#include <boost/weak_ptr.hpp>
+
 
 using std::list;
 using std::pair;
 using std::make_pair;
-using boost::weak_ptr;
-using boost::shared_ptr;
+using std::weak_ptr;
+using std::shared_ptr;
 using boost::optional;
+using namespace dcpomatic;
 
-void
-ActiveText::add (DCPTimePeriod period, list<PlayerText>& pc, list<Period> p) const
-{
-       BOOST_FOREACH (Period i, p) {
-               DCPTimePeriod test (i.from, i.to.get_value_or(DCPTime::max()));
-               optional<DCPTimePeriod> overlap = period.overlap (test);
-               if (overlap && overlap->duration() > DCPTime(period.duration().get() / 2)) {
-                       pc.push_back (i.subs);
-               }
-       }
-}
-
-list<PlayerText>
-ActiveText::get (DCPTimePeriod period) const
-{
-       list<PlayerText> ps;
-
-       for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
-
-               shared_ptr<const TextContent> caption = i->first.lock ();
-               if (!caption || !caption->use()) {
-                       continue;
-               }
-
-               add (period, ps, i->second);
-       }
-
-       return ps;
-}
 
 /** Get the open captions that should be burnt into a given period.
  *  @param period Period of interest.
@@ -67,11 +39,13 @@ ActiveText::get (DCPTimePeriod period) const
 list<PlayerText>
 ActiveText::get_burnt (DCPTimePeriod period, bool always_burn_captions) const
 {
+       boost::mutex::scoped_lock lm (_mutex);
+
        list<PlayerText> ps;
 
-       for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
+       for (auto const& i: _data) {
 
-               shared_ptr<const TextContent> caption = i->first.lock ();
+               auto caption = i.first.lock ();
                if (!caption) {
                        continue;
                }
@@ -81,33 +55,43 @@ ActiveText::get_burnt (DCPTimePeriod period, bool always_burn_captions) const
                        continue;
                }
 
-               add (period, ps, i->second);
+               for (auto j: i.second) {
+                       DCPTimePeriod test (j.from, j.to.get_value_or(DCPTime::max()));
+                       auto overlap = period.overlap (test);
+                       if (overlap && overlap->duration() > DCPTime(period.duration().get() / 2)) {
+                               ps.push_back (j.subs);
+                       }
+               }
        }
 
        return ps;
 }
 
+
 /** Remove subtitles that finish before a given time from our list.
  *  @param time Time to remove before.
  */
 void
 ActiveText::clear_before (DCPTime time)
 {
+       boost::mutex::scoped_lock lm (_mutex);
+
        Map updated;
-       for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
+       for (auto const& i: _data) {
                list<Period> as;
-               BOOST_FOREACH (Period j, i->second) {
+               for (auto j: i.second) {
                        if (!j.to || j.to.get() >= time) {
                                as.push_back (j);
                        }
                }
                if (!as.empty ()) {
-                       updated[i->first] = as;
+                       updated[i.first] = as;
                }
        }
        _data = updated;
 }
 
+
 /** Add a new subtitle with a from time.
  *  @param content Content that the subtitle is from.
  *  @param ps Subtitles.
@@ -116,12 +100,15 @@ ActiveText::clear_before (DCPTime time)
 void
 ActiveText::add_from (weak_ptr<const TextContent> content, PlayerText ps, DCPTime from)
 {
+       boost::mutex::scoped_lock lm (_mutex);
+
        if (_data.find(content) == _data.end()) {
                _data[content] = list<Period>();
        }
        _data[content].push_back (Period (ps, from));
 }
 
+
 /** Add the to time for the last subtitle added from a piece of content.
  *  @param content Content that the subtitle is from.
  *  @param to To time for the last subtitle submitted to add_from for this content.
@@ -130,24 +117,29 @@ ActiveText::add_from (weak_ptr<const TextContent> content, PlayerText ps, DCPTim
 pair<PlayerText, DCPTime>
 ActiveText::add_to (weak_ptr<const TextContent> content, DCPTime to)
 {
+       boost::mutex::scoped_lock lm (_mutex);
+
        DCPOMATIC_ASSERT (_data.find(content) != _data.end());
 
        _data[content].back().to = to;
 
-       BOOST_FOREACH (StringText& i, _data[content].back().subs.text) {
+       for (auto& i: _data[content].back().subs.string) {
                i.set_out (dcp::Time(to.seconds(), 1000));
        }
 
        return make_pair (_data[content].back().subs, _data[content].back().from);
 }
 
+
 /** @param content Some content.
  *  @return true if we have any active subtitles from this content.
  */
 bool
 ActiveText::have (weak_ptr<const TextContent> content) const
 {
-       Map::const_iterator i = _data.find(content);
+       boost::mutex::scoped_lock lm (_mutex);
+
+       auto i = _data.find(content);
        if (i == _data.end()) {
                return false;
        }
@@ -155,8 +147,10 @@ ActiveText::have (weak_ptr<const TextContent> content) const
        return !i->second.empty();
 }
 
+
 void
 ActiveText::clear ()
 {
+       boost::mutex::scoped_lock lm (_mutex);
        _data.clear ();
 }