refs #245 & #210 moved TupleConverter's parsing of the stringified macro argument into a base class, ready for further extension

This commit is contained in:
Mathew Sutcliffe
2014-06-13 12:11:19 +01:00
parent 9766b5c63a
commit 45260258d6
2 changed files with 73 additions and 7 deletions

35
src/blackmisc/tuple.cpp Normal file
View File

@@ -0,0 +1,35 @@
/* Copyright (C) 2014 VATSIM Community / authors
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "tuple.h"
namespace BlackMisc
{
TupleConverterBase::Parser::Parser(QString string)
{
string.remove(QRegExp("^\\s*\\(\\s*")); // remove first '('
string.remove(QRegExp("\\s*\\)\\s*$")); // remove last ')'
QString current;
int level = 0;
for (const auto c : string)
{
if (c == '(') { level++; }
if (c == ')') { level++; }
if (c == ',' && level == 0) { m_raw.push_back(current.trimmed()); current.clear(); }
else { current += c; }
}
if (! current.trimmed().isEmpty()) { m_raw.push_back(current.trimmed()); current.clear(); }
for (const auto &member : m_raw)
{
QRegExp simple("^o\\.(\\w+)$");
if (member.contains(simple)) { m_names.push_back(simple.cap(1)); continue; }
qFatal("BLACK_DECLARE_TUPLE_CONVERSION: Parser couldn't extract member name from \"%s\"", qPrintable(member));
}
for (auto &name : m_names) { name.remove(QRegExp("^m_")); }
}
} // namespace

View File

@@ -43,7 +43,7 @@
#define BLACK_DECLARE_TUPLE_CONVERSION(T, MEMBERS) \
namespace BlackMisc \
{ \
template <> class TupleConverter<T> \
template <> class TupleConverter<T> : TupleConverterBase \
{ \
friend class T; \
static_assert(Private::HasEnabledTupleConversion<T>::value, \
@@ -56,10 +56,14 @@
{ \
return BlackMisc::tie MEMBERS; \
} \
static const Parser &parser() \
{ \
static const Parser p(#MEMBERS); \
return p; \
} \
static const QStringList &jsonMembers() \
{ \
static QStringList members = QString(#MEMBERS).replace("tie(","").replace("(","").replace(")","").replace(" ","").replace("o.","").split(","); \
return members; \
return parser().m_names; \
} \
public: \
static auto constToTuple(const T &o) -> decltype(BlackMisc::tie MEMBERS) \
@@ -77,7 +81,7 @@
#define BLACK_DECLARE_TUPLE_CONVERSION_TEMPLATE(T, MEMBERS) \
namespace BlackMisc \
{ \
template <class... U> class TupleConverter<T<U...>> \
template <class... U> class TupleConverter<T<U...>> : TupleConverterBase \
{ \
friend class T<U...>; \
static_assert(Private::HasEnabledTupleConversion<T<U...>>::value, \
@@ -90,10 +94,14 @@
{ \
return BlackMisc::tie MEMBERS; \
} \
static const Parser &parser() \
{ \
static const Parser p(#MEMBERS); \
return p; \
} \
static const QStringList &jsonMembers() \
{ \
static QStringList members = QString(#MEMBERS).replace("tie(","").replace("(","").replace(")","").replace(" ","").replace("o.","").split(","); \
return members; \
return parser().m_names; \
} \
public: \
static auto constToTuple(const T<U...> &o) -> decltype(BlackMisc::tie MEMBERS) \
@@ -106,6 +114,23 @@
namespace BlackMisc
{
/*!
* \brief Base class for TupleConverter<T>.
* \details Defines common types and functions which can be used inside the BLACK_DECLARE_TUPLE_CONVERSION() macro.
* \ingroup Tuples
*/
class TupleConverterBase
{
protected:
//! \brief Helper class which parses the stringified macro argument.
struct Parser
{
Parser(QString); //!< Constructor.
QStringList m_raw; //!< The raw macro argument, split by top-level commas.
QStringList m_names; //!< The names of the tuple members, stripped of any o.m_ prefix.
};
};
/*!
* \brief Class template for converting class objects to tuples
* \details If a class T uses the BLACK_ENABLE_TUPLE_CONVERSION() and BLACK_DECLARE_TUPLE_CONVERSION() macros, and <CODE>object</CODE>
@@ -116,7 +141,7 @@ namespace BlackMisc
* \nosubgrouping
* \ingroup Tuples
*/
template <class T> class TupleConverter
template <class T> class TupleConverter : private TupleConverterBase
{
// BLACK_DECLARE_TUPLE_CONVERSION generates an explicit specialization of TupleConverter,
// so this unspecialized template will only be used if the macro is missing. It is also
@@ -137,6 +162,12 @@ namespace BlackMisc
static std::tuple<> constToTuple(const T &object);
//! @}
/*!
* \name Static Private Member Functions
* \brief Returns an object with information extracted from the stringified macro argument.
*/
static const Parser &parser();
/*!
* \name Static Private Member Functions
* \brief Returns a list of the names of the tuple members.