Clang-formatted platform/settings.*

This commit is contained in:
Alex Zolotarev 2016-02-29 14:18:29 +03:00 committed by Sergey Yershov
parent 8f89176809
commit 00ce4641f1
2 changed files with 406 additions and 381 deletions

View file

@ -17,7 +17,6 @@
#include "std/iostream.hpp"
#include "std/sstream.hpp"
#define FIRST_LAUNCH_KEY "FirstLaunchOnDate"
namespace
@ -29,369 +28,396 @@ char const * Settings::kLocationStateMode = "LastLocationStateMode";
namespace Settings
{
StringStorage::StringStorage()
{
lock_guard<mutex> guard(m_mutex);
StringStorage::StringStorage()
try
{
lock_guard<mutex> guard(m_mutex);
string settingsPath = GetPlatform().SettingsPathForFile(SETTINGS_FILE_NAME);
LOG(LINFO, ("Settings path:", settingsPath));
ReaderStreamBuf buffer(make_unique<FileReader>(settingsPath));
istream stream(&buffer);
try
string line;
while (stream.good())
{
string settingsPath = GetPlatform().SettingsPathForFile(SETTINGS_FILE_NAME);
LOG(LINFO, ("Settings path:", settingsPath));
ReaderStreamBuf buffer(make_unique<FileReader>(settingsPath));
istream stream(&buffer);
std::getline(stream, line);
if (line.empty())
continue;
string line;
while (stream.good())
{
std::getline(stream, line);
if (line.empty())
continue;
size_t const delimPos = line.find(DELIM_CHAR);
if (delimPos == string::npos)
continue;
size_t const delimPos = line.find(DELIM_CHAR);
if (delimPos == string::npos)
continue;
string const key = line.substr(0, delimPos);
string const value = line.substr(delimPos + 1);
if (!key.empty() && !value.empty())
m_values[key] = value;
}
}
catch (RootException const & ex)
{
LOG(LWARNING, ("Loading settings:", ex.Msg()));
string const key = line.substr(0, delimPos);
string const value = line.substr(delimPos + 1);
if (!key.empty() && !value.empty())
m_values[key] = value;
}
}
void StringStorage::Save() const
catch (RootException const & ex)
{
try
LOG(LWARNING, ("Loading settings:", ex.Msg()));
}
}
void StringStorage::Save() const
{
try
{
FileWriter file(GetPlatform().SettingsPathForFile(SETTINGS_FILE_NAME));
for (auto const & value : m_values)
{
FileWriter file(GetPlatform().SettingsPathForFile(SETTINGS_FILE_NAME));
for (auto const & value : m_values)
{
string line(value.first);
line += DELIM_CHAR;
line += value.second;
line += "\n";
file.Write(line.data(), line.size());
}
}
catch (RootException const & ex)
{
// Ignore all settings saving exceptions.
LOG(LWARNING, ("Saving settings:", ex.Msg()));
string line(value.first);
line += DELIM_CHAR;
line += value.second;
line += "\n";
file.Write(line.data(), line.size());
}
}
StringStorage & StringStorage::Instance()
catch (RootException const & ex)
{
static StringStorage inst;
return inst;
// Ignore all settings saving exceptions.
LOG(LWARNING, ("Saving settings:", ex.Msg()));
}
}
void StringStorage::Clear()
StringStorage & StringStorage::Instance()
{
static StringStorage inst;
return inst;
}
void StringStorage::Clear()
{
lock_guard<mutex> guard(m_mutex);
m_values.clear();
Save();
}
bool StringStorage::GetValue(string const & key, string & outValue) const
{
lock_guard<mutex> guard(m_mutex);
auto const found = m_values.find(key);
if (found == m_values.end())
return false;
outValue = found->second;
return true;
}
void StringStorage::SetValue(string const & key, string && value)
{
lock_guard<mutex> guard(m_mutex);
m_values[key] = move(value);
Save();
}
void StringStorage::DeleteKeyAndValue(string const & key)
{
lock_guard<mutex> guard(m_mutex);
auto const found = m_values.find(key);
if (found != m_values.end())
{
lock_guard<mutex> guard(m_mutex);
m_values.clear();
m_values.erase(found);
Save();
}
bool StringStorage::GetValue(string const & key, string & outValue) const
{
lock_guard<mutex> guard(m_mutex);
auto const found = m_values.find(key);
if (found == m_values.end())
return false;
outValue = found->second;
return true;
}
void StringStorage::SetValue(string const & key, string && value)
{
lock_guard<mutex> guard(m_mutex);
m_values[key] = move(value);
Save();
}
void StringStorage::DeleteKeyAndValue(string const & key)
{
lock_guard<mutex> guard(m_mutex);
auto const found = m_values.find(key);
if (found != m_values.end())
{
m_values.erase(found);
Save();
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////
template <> string ToString<string>(string const & str)
template <>
string ToString<string>(string const & str)
{
return str;
}
template <>
bool FromString<string>(string const & strIn, string & strOut)
{
strOut = strIn;
return true;
}
namespace impl
{
template <class T, size_t N>
bool FromStringArray(string const & s, T(&arr)[N])
{
istringstream in(s);
size_t count = 0;
while (in.good() && count < N)
{
return str;
in >> arr[count];
if (!std::isfinite(arr[count]))
return false;
++count;
}
template <> bool FromString<string>(string const & strIn, string & strOut)
{
strOut = strIn;
return true;
}
return (!in.fail() && count == N);
}
}
namespace impl
template <>
string ToString<m2::AnyRectD>(m2::AnyRectD const & rect)
{
ostringstream out;
out.precision(12);
m2::PointD glbZero(rect.GlobalZero());
out << glbZero.x << " " << glbZero.y << " ";
out << rect.Angle().val() << " ";
m2::RectD const & r = rect.GetLocalRect();
out << r.minX() << " " << r.minY() << " " << r.maxX() << " " << r.maxY();
return out.str();
}
template <>
bool FromString<m2::AnyRectD>(string const & str, m2::AnyRectD & rect)
{
double val[7];
if (!impl::FromStringArray(str, val))
return false;
// Will get an assertion in DEBUG and false return in RELEASE.
m2::RectD const r(val[3], val[4], val[5], val[6]);
if (!r.IsValid())
return false;
rect = m2::AnyRectD(m2::PointD(val[0], val[1]), ang::AngleD(val[2]), r);
return true;
}
template <>
string ToString<m2::RectD>(m2::RectD const & rect)
{
ostringstream stream;
stream.precision(12);
stream << rect.minX() << " " << rect.minY() << " " << rect.maxX() << " " << rect.maxY();
return stream.str();
}
template <>
bool FromString<m2::RectD>(string const & str, m2::RectD & rect)
{
double val[4];
if (!impl::FromStringArray(str, val))
return false;
// Will get an assertion in DEBUG and false return in RELEASE.
rect = m2::RectD(val[0], val[1], val[2], val[3]);
return rect.IsValid();
}
template <>
string ToString<bool>(bool const & v)
{
return v ? "true" : "false";
}
template <>
bool FromString<bool>(string const & str, bool & v)
{
if (str == "true")
v = true;
else if (str == "false")
v = false;
else
return false;
return true;
}
namespace impl
{
template <typename T>
string ToStringScalar(T const & v)
{
ostringstream stream;
stream.precision(12);
stream << v;
return stream.str();
}
template <typename T>
bool FromStringScalar(string const & str, T & v)
{
istringstream stream(str);
if (stream.good())
{
template <class T, size_t N> bool FromStringArray(string const & s, T (&arr)[N])
stream >> v;
return !stream.fail();
}
else
return false;
}
}
template <>
string ToString<double>(double const & v)
{
return impl::ToStringScalar<double>(v);
}
template <>
bool FromString<double>(string const & str, double & v)
{
return impl::FromStringScalar<double>(str, v);
}
template <>
string ToString<int32_t>(int32_t const & v)
{
return impl::ToStringScalar<int32_t>(v);
}
template <>
bool FromString<int32_t>(string const & str, int32_t & v)
{
return impl::FromStringScalar<int32_t>(str, v);
}
template <>
string ToString<int64_t>(int64_t const & v)
{
return impl::ToStringScalar<int64_t>(v);
}
template <>
bool FromString<int64_t>(string const & str, int64_t & v)
{
return impl::FromStringScalar<int64_t>(str, v);
}
template <>
string ToString<uint32_t>(uint32_t const & v)
{
return impl::ToStringScalar<uint32_t>(v);
}
template <>
bool FromString<uint32_t>(string const & str, uint32_t & v)
{
return impl::FromStringScalar<uint32_t>(str, v);
}
namespace impl
{
template <class TPair>
string ToStringPair(TPair const & value)
{
ostringstream stream;
stream.precision(12);
stream << value.first << " " << value.second;
return stream.str();
}
template <class TPair>
bool FromStringPair(string const & str, TPair & value)
{
istringstream stream(str);
if (stream.good())
{
stream >> value.first;
if (stream.good())
{
istringstream in(s);
size_t count = 0;
while (in.good() && count < N)
{
in >> arr[count];
if (!std::isfinite(arr[count]))
return false;
++count;
}
return (!in.fail() && count == N);
stream >> value.second;
return !stream.fail();
}
}
return false;
}
}
template <> string ToString<m2::AnyRectD>(m2::AnyRectD const & rect)
typedef pair<int, int> IPairT;
typedef pair<double, double> DPairT;
template <>
string ToString<IPairT>(IPairT const & v)
{
return impl::ToStringPair(v);
}
template <>
bool FromString<IPairT>(string const & s, IPairT & v)
{
return impl::FromStringPair(s, v);
}
template <>
string ToString<DPairT>(DPairT const & v)
{
return impl::ToStringPair(v);
}
template <>
bool FromString<DPairT>(string const & s, DPairT & v)
{
return impl::FromStringPair(s, v);
}
template <>
string ToString<Units>(Units const & v)
{
switch (v)
{
ostringstream out;
out.precision(12);
m2::PointD glbZero(rect.GlobalZero());
out << glbZero.x << " " << glbZero.y << " ";
out << rect.Angle().val() << " ";
m2::RectD const & r = rect.GetLocalRect();
out << r.minX() << " " << r.minY() << " " << r.maxX() << " " << r.maxY();
return out.str();
}
template <> bool FromString<m2::AnyRectD>(string const & str, m2::AnyRectD & rect)
{
double val[7];
if (!impl::FromStringArray(str, val))
return false;
// Will get an assertion in DEBUG and false return in RELEASE.
m2::RectD const r(val[3], val[4], val[5], val[6]);
if (!r.IsValid())
return false;
rect = m2::AnyRectD(m2::PointD(val[0], val[1]), ang::AngleD(val[2]), r);
return true;
}
template <> string ToString<m2::RectD>(m2::RectD const & rect)
{
ostringstream stream;
stream.precision(12);
stream << rect.minX() << " " << rect.minY() << " " << rect.maxX() << " " << rect.maxY();
return stream.str();
}
template <> bool FromString<m2::RectD>(string const & str, m2::RectD & rect)
{
double val[4];
if (!impl::FromStringArray(str, val))
return false;
// Will get an assertion in DEBUG and false return in RELEASE.
rect = m2::RectD(val[0], val[1], val[2], val[3]);
return rect.IsValid();
}
template <> string ToString<bool>(bool const & v)
{
return v ? "true" : "false";
}
template <> bool FromString<bool>(string const & str, bool & v)
{
if (str == "true")
v = true;
else if (str == "false")
v = false;
else
return false;
return true;
}
namespace impl
{
template <typename T>
string ToStringScalar(T const & v)
{
ostringstream stream;
stream.precision(12);
stream << v;
return stream.str();
}
template <typename T>
bool FromStringScalar(string const & str, T & v)
{
istringstream stream(str);
if (stream.good())
{
stream >> v;
return !stream.fail();
}
else return false;
}
}
template <> string ToString<double>(double const & v)
{
return impl::ToStringScalar<double>(v);
}
template <> bool FromString<double>(string const & str, double & v)
{
return impl::FromStringScalar<double>(str, v);
}
template <> string ToString<int32_t>(int32_t const & v)
{
return impl::ToStringScalar<int32_t>(v);
}
template <> bool FromString<int32_t>(string const & str, int32_t & v)
{
return impl::FromStringScalar<int32_t>(str, v);
}
template <> string ToString<int64_t>(int64_t const & v)
{
return impl::ToStringScalar<int64_t>(v);
}
template <> bool FromString<int64_t>(string const & str, int64_t & v)
{
return impl::FromStringScalar<int64_t>(str, v);
}
template <> string ToString<uint32_t>(uint32_t const & v)
{
return impl::ToStringScalar<uint32_t>(v);
}
template <> bool FromString<uint32_t>(string const & str, uint32_t & v)
{
return impl::FromStringScalar<uint32_t>(str, v);
}
namespace impl
{
template <class TPair> string ToStringPair(TPair const & value)
{
ostringstream stream;
stream.precision(12);
stream << value.first << " " << value.second;
return stream.str();
}
template <class TPair> bool FromStringPair(string const & str, TPair & value)
{
istringstream stream(str);
if (stream.good())
{
stream >> value.first;
if (stream.good())
{
stream >> value.second;
return !stream.fail();
}
}
return false;
}
}
typedef pair<int, int> IPairT;
typedef pair<double, double> DPairT;
template <> string ToString<IPairT>(IPairT const & v)
{
return impl::ToStringPair(v);
}
template <> bool FromString<IPairT>(string const & s, IPairT & v)
{
return impl::FromStringPair(s, v);
}
template <> string ToString<DPairT>(DPairT const & v)
{
return impl::ToStringPair(v);
}
template <> bool FromString<DPairT>(string const & s, DPairT & v)
{
return impl::FromStringPair(s, v);
}
template <> string ToString<Units>(Units const & v)
{
switch (v)
{
case Foot: return "Foot";
default: return "Metric";
}
}
template <> bool FromString<Units>(string const & s, Units & v)
{
if (s == "Metric")
v = Metric;
else if (s == "Foot")
v = Foot;
else
return false;
return true;
}
template <> string ToString<location::EMyPositionMode>(location::EMyPositionMode const & v)
{
switch (v)
{
case location::MODE_UNKNOWN_POSITION: return "Unknown";
case location::MODE_PENDING_POSITION: return "Pending";
case location::MODE_NOT_FOLLOW: return "NotFollow";
case location::MODE_FOLLOW: return "Follow";
case location::MODE_ROTATE_AND_FOLLOW: return "RotateAndFollow";
default: return "Unknown";
}
}
template <> bool FromString<location::EMyPositionMode>(string const & s, location::EMyPositionMode & v)
{
if (s == "Unknown")
v = location::MODE_UNKNOWN_POSITION;
else if (s == "Pending")
v = location::MODE_PENDING_POSITION;
else if (s == "NotFollow")
v = location::MODE_NOT_FOLLOW;
else if (s == "Follow")
v = location::MODE_FOLLOW;
else if (s == "RotateAndFollow")
v = location::MODE_ROTATE_AND_FOLLOW;
else
return false;
return true;
}
bool IsFirstLaunchForDate(int date)
{
int savedDate;
if (!Get(FIRST_LAUNCH_KEY, savedDate) || savedDate < date)
{
Set(FIRST_LAUNCH_KEY, date);
return true;
}
else
return false;
case Foot: return "Foot";
default: return "Metric";
}
}
template <>
bool FromString<Units>(string const & s, Units & v)
{
if (s == "Metric")
v = Metric;
else if (s == "Foot")
v = Foot;
else
return false;
return true;
}
template <>
string ToString<location::EMyPositionMode>(location::EMyPositionMode const & v)
{
switch (v)
{
case location::MODE_UNKNOWN_POSITION: return "Unknown";
case location::MODE_PENDING_POSITION: return "Pending";
case location::MODE_NOT_FOLLOW: return "NotFollow";
case location::MODE_FOLLOW: return "Follow";
case location::MODE_ROTATE_AND_FOLLOW: return "RotateAndFollow";
default: return "Unknown";
}
}
template <>
bool FromString<location::EMyPositionMode>(string const & s, location::EMyPositionMode & v)
{
if (s == "Unknown")
v = location::MODE_UNKNOWN_POSITION;
else if (s == "Pending")
v = location::MODE_PENDING_POSITION;
else if (s == "NotFollow")
v = location::MODE_NOT_FOLLOW;
else if (s == "Follow")
v = location::MODE_FOLLOW;
else if (s == "RotateAndFollow")
v = location::MODE_ROTATE_AND_FOLLOW;
else
return false;
return true;
}
bool IsFirstLaunchForDate(int date)
{
int savedDate;
if (!Get(FIRST_LAUNCH_KEY, savedDate) || savedDate < date)
{
Set(FIRST_LAUNCH_KEY, date);
return true;
}
else
return false;
}
}

View file

@ -6,58 +6,57 @@
namespace Settings
{
// Current location state mode. @See location::EMyPositionMode.
extern char const * kLocationStateMode;
// Current location state mode. @See location::EMyPositionMode.
extern char const * kLocationStateMode;
template <class T> bool FromString(string const & str, T & outValue);
template <class T> string ToString(T const & value);
template <class T>
bool FromString(string const & str, T & outValue);
template <class T>
string ToString(T const & value);
class StringStorage
{
typedef map<string, string> ContainerT;
ContainerT m_values;
class StringStorage
{
typedef map<string, string> ContainerT;
ContainerT m_values;
mutable mutex m_mutex;
mutable mutex m_mutex;
StringStorage();
void Save() const;
StringStorage();
void Save() const;
public:
static StringStorage & Instance();
public:
static StringStorage & Instance();
void Clear();
bool GetValue(string const & key, string & outValue) const;
void SetValue(string const & key, string && value);
void DeleteKeyAndValue(string const & key);
};
void Clear();
bool GetValue(string const & key, string & outValue) const;
void SetValue(string const & key, string && value);
void DeleteKeyAndValue(string const & key);
};
/// Retrieve setting
/// @return false if setting is absent
template <class ValueT> bool Get(string const & key, ValueT & outValue)
{
string strVal;
return StringStorage::Instance().GetValue(key, strVal)
&& FromString(strVal, outValue);
}
/// Automatically saves setting to external file
template <class ValueT> void Set(string const & key, ValueT const & value)
{
StringStorage::Instance().SetValue(key, ToString(value));
}
inline void Delete(string const & key)
{
StringStorage::Instance().DeleteKeyAndValue(key);
}
inline void Clear()
{
StringStorage::Instance().Clear();
}
enum Units { Metric = 0, Foot };
/// Use this function for running some stuff once according to date.
/// @param[in] date Current date in format yymmdd.
bool IsFirstLaunchForDate(int date);
/// Retrieve setting
/// @return false if setting is absent
template <class ValueT>
bool Get(string const & key, ValueT & outValue)
{
string strVal;
return StringStorage::Instance().GetValue(key, strVal) && FromString(strVal, outValue);
}
/// Automatically saves setting to external file
template <class ValueT>
void Set(string const & key, ValueT const & value)
{
StringStorage::Instance().SetValue(key, ToString(value));
}
inline void Delete(string const & key) { StringStorage::Instance().DeleteKeyAndValue(key); }
inline void Clear() { StringStorage::Instance().Clear(); }
enum Units
{
Metric = 0,
Foot
};
/// Use this function for running some stuff once according to date.
/// @param[in] date Current date in format yymmdd.
bool IsFirstLaunchForDate(int date);
}