Use filter as strategy to simplify testing and assembling

This commit is contained in:
Constantin Shalnev 2015-12-17 14:16:09 +03:00
parent c811498ec0
commit e1c641c965
5 changed files with 43 additions and 9 deletions

View file

@ -29,15 +29,20 @@ size_t constexpr kItemBlockSize = 1000;
size_t const GpsTrack::kInvalidId = GpsTrackCollection::kInvalidId;
GpsTrack::GpsTrack(string const & filePath, size_t maxItemCount, hours duration)
GpsTrack::GpsTrack(string const & filePath, size_t maxItemCount, hours duration,
unique_ptr<IGpsTrackFilter> && filter)
: m_maxItemCount(maxItemCount)
, m_filePath(filePath)
, m_duration(duration)
, m_needClear(false)
, m_needSendSnapshop(false)
, m_filter(move(filter))
, m_threadExit(false)
, m_threadWakeup(false)
{
if (!m_filter)
m_filter = make_unique<GpsTrackNullFilter>();
ASSERT_GREATER(m_maxItemCount, 0, ());
ASSERT(!m_filePath.empty(), ());
ASSERT_GREATER(m_duration.count(), 0, ());
@ -179,7 +184,7 @@ void GpsTrack::InitCollection(hours duration)
if (originPoints.size() == originPoints.capacity())
{
vector<location::GpsTrackInfo> points;
m_filter.Process(originPoints, points);
m_filter->Process(originPoints, points);
pair<size_t, size_t> evictedIds;
m_collection->Add(points, evictedIds);
@ -192,7 +197,7 @@ void GpsTrack::InitCollection(hours duration)
if (!originPoints.empty())
{
vector<location::GpsTrackInfo> points;
m_filter.Process(originPoints, points);
m_filter->Process(originPoints, points);
pair<size_t, size_t> evictedIds;
m_collection->Add(points, evictedIds);
@ -233,7 +238,7 @@ void GpsTrack::ProcessPoints()
return;
vector<location::GpsTrackInfo> points;
m_filter.Process(originPoints, points);
m_filter->Process(originPoints, points);
pair<size_t, size_t> addedIds;
pair<size_t, size_t> evictedIds;

View file

@ -17,7 +17,9 @@ public:
/// @param filePath - path to the file on disk to persist track
/// @param maxItemCount - number of points to store on disk
/// @param duration - initial value of track duration
GpsTrack(string const & filePath, size_t maxItemCount, hours duration);
/// @param filter - filter object used for filtering points, GpsTrackNullFilter is created by default
GpsTrack(string const & filePath, size_t maxItemCount, hours duration,
unique_ptr<IGpsTrackFilter> && filter = unique_ptr<IGpsTrackFilter>());
~GpsTrack();
/// Adds point or collection of points to gps tracking
@ -82,7 +84,7 @@ private:
unique_ptr<GpsTrackStorage> m_storage; // used in the worker thread
unique_ptr<GpsTrackCollection> m_collection; // used in the worker thread
GpsTrackFilter m_filter;
unique_ptr<IGpsTrackFilter> m_filter; // used in the worker thread
mutex m_threadGuard;
thread m_thread;

View file

@ -4,6 +4,8 @@
#include "platform/settings.hpp"
#include "std/algorithm.hpp"
namespace
{
@ -17,6 +19,13 @@ double constexpr kClosePointDistanceMeters = 15;
} // namespace
void GpsTrackNullFilter::Process(vector<location::GpsInfo> const & inPoints,
vector<location::GpsTrackInfo> & outPoints)
{
outPoints.reserve(inPoints.size());
copy(inPoints.begin(), inPoints.end(), back_inserter(outPoints));
}
void GpsTrackFilter::StoreMinHorizontalAccuracy(double value)
{
Settings::Set(kMinHorizontalAccuracyKey, value);

View file

@ -6,7 +6,24 @@
#include "std/vector.hpp"
class GpsTrackFilter
class IGpsTrackFilter
{
public:
virtual ~IGpsTrackFilter() = default;
virtual void Process(vector<location::GpsInfo> const & inPoints,
vector<location::GpsTrackInfo> & outPoints) = 0;
};
class GpsTrackNullFilter : public IGpsTrackFilter
{
public:
// IGpsTrackFilter overrides
void Process(vector<location::GpsInfo> const & inPoints,
vector<location::GpsTrackInfo> & outPoints) override;
};
class GpsTrackFilter : public IGpsTrackFilter
{
public:
/// Store setting for minimal horizontal accuracy
@ -14,8 +31,9 @@ public:
GpsTrackFilter();
// IGpsTrackFilter overrides
void Process(vector<location::GpsInfo> const & inPoints,
vector<location::GpsTrackInfo> & outPoints);
vector<location::GpsTrackInfo> & outPoints) override;
private:
double m_minAccuracy;

View file

@ -58,7 +58,7 @@ GpsTracker & GpsTracker::Instance()
GpsTracker::GpsTracker()
: m_enabled(GetSettingsIsEnabled())
, m_track(GetFilePath(), kMaxItemCount, GetSettingsDuration())
, m_track(GetFilePath(), kMaxItemCount, GetSettingsDuration(), make_unique<GpsTrackFilter>())
{
}