mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-05-01 22:55:41 +08:00
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:
35
src/blackmisc/tuple.cpp
Normal file
35
src/blackmisc/tuple.cpp
Normal 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
|
||||||
@@ -43,7 +43,7 @@
|
|||||||
#define BLACK_DECLARE_TUPLE_CONVERSION(T, MEMBERS) \
|
#define BLACK_DECLARE_TUPLE_CONVERSION(T, MEMBERS) \
|
||||||
namespace BlackMisc \
|
namespace BlackMisc \
|
||||||
{ \
|
{ \
|
||||||
template <> class TupleConverter<T> \
|
template <> class TupleConverter<T> : TupleConverterBase \
|
||||||
{ \
|
{ \
|
||||||
friend class T; \
|
friend class T; \
|
||||||
static_assert(Private::HasEnabledTupleConversion<T>::value, \
|
static_assert(Private::HasEnabledTupleConversion<T>::value, \
|
||||||
@@ -56,10 +56,14 @@
|
|||||||
{ \
|
{ \
|
||||||
return BlackMisc::tie MEMBERS; \
|
return BlackMisc::tie MEMBERS; \
|
||||||
} \
|
} \
|
||||||
|
static const Parser &parser() \
|
||||||
|
{ \
|
||||||
|
static const Parser p(#MEMBERS); \
|
||||||
|
return p; \
|
||||||
|
} \
|
||||||
static const QStringList &jsonMembers() \
|
static const QStringList &jsonMembers() \
|
||||||
{ \
|
{ \
|
||||||
static QStringList members = QString(#MEMBERS).replace("tie(","").replace("(","").replace(")","").replace(" ","").replace("o.","").split(","); \
|
return parser().m_names; \
|
||||||
return members; \
|
|
||||||
} \
|
} \
|
||||||
public: \
|
public: \
|
||||||
static auto constToTuple(const T &o) -> decltype(BlackMisc::tie MEMBERS) \
|
static auto constToTuple(const T &o) -> decltype(BlackMisc::tie MEMBERS) \
|
||||||
@@ -77,7 +81,7 @@
|
|||||||
#define BLACK_DECLARE_TUPLE_CONVERSION_TEMPLATE(T, MEMBERS) \
|
#define BLACK_DECLARE_TUPLE_CONVERSION_TEMPLATE(T, MEMBERS) \
|
||||||
namespace BlackMisc \
|
namespace BlackMisc \
|
||||||
{ \
|
{ \
|
||||||
template <class... U> class TupleConverter<T<U...>> \
|
template <class... U> class TupleConverter<T<U...>> : TupleConverterBase \
|
||||||
{ \
|
{ \
|
||||||
friend class T<U...>; \
|
friend class T<U...>; \
|
||||||
static_assert(Private::HasEnabledTupleConversion<T<U...>>::value, \
|
static_assert(Private::HasEnabledTupleConversion<T<U...>>::value, \
|
||||||
@@ -90,10 +94,14 @@
|
|||||||
{ \
|
{ \
|
||||||
return BlackMisc::tie MEMBERS; \
|
return BlackMisc::tie MEMBERS; \
|
||||||
} \
|
} \
|
||||||
|
static const Parser &parser() \
|
||||||
|
{ \
|
||||||
|
static const Parser p(#MEMBERS); \
|
||||||
|
return p; \
|
||||||
|
} \
|
||||||
static const QStringList &jsonMembers() \
|
static const QStringList &jsonMembers() \
|
||||||
{ \
|
{ \
|
||||||
static QStringList members = QString(#MEMBERS).replace("tie(","").replace("(","").replace(")","").replace(" ","").replace("o.","").split(","); \
|
return parser().m_names; \
|
||||||
return members; \
|
|
||||||
} \
|
} \
|
||||||
public: \
|
public: \
|
||||||
static auto constToTuple(const T<U...> &o) -> decltype(BlackMisc::tie MEMBERS) \
|
static auto constToTuple(const T<U...> &o) -> decltype(BlackMisc::tie MEMBERS) \
|
||||||
@@ -106,6 +114,23 @@
|
|||||||
namespace BlackMisc
|
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
|
* \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>
|
* \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
|
* \nosubgrouping
|
||||||
* \ingroup Tuples
|
* \ingroup Tuples
|
||||||
*/
|
*/
|
||||||
template <class T> class TupleConverter
|
template <class T> class TupleConverter : private TupleConverterBase
|
||||||
{
|
{
|
||||||
// BLACK_DECLARE_TUPLE_CONVERSION generates an explicit specialization of TupleConverter,
|
// 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
|
// 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);
|
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
|
* \name Static Private Member Functions
|
||||||
* \brief Returns a list of the names of the tuple members.
|
* \brief Returns a list of the names of the tuple members.
|
||||||
|
|||||||
Reference in New Issue
Block a user