ICU-20138 Macros for C and C++ FormattedValue subclass boilerplate.

- Includes macros for implementation only. Headers should be spelled out.
This commit is contained in:
Shane F. Carr 2019-02-04 18:47:04 -08:00
parent 1624176dd9
commit 57d07d3ec3
4 changed files with 125 additions and 15 deletions

View File

@ -14,9 +14,10 @@
// dependencies more modularly.
#include "unicode/formattedvalue.h"
#include "capi_helper.h"
#include "fphdlimp.h"
#include "uvectr32.h"
#include "util.h"
#include "uvectr32.h"
U_NAMESPACE_BEGIN
@ -49,6 +50,125 @@ private:
};
// C API Helpers for FormattedValue
// Magic number as ASCII == "UFV"
struct UFormattedValueImpl;
typedef IcuCApiHelper<UFormattedValue, UFormattedValueImpl, 0x55465600> UFormattedValueApiHelper;
struct UFormattedValueImpl : public UMemory, public UFormattedValueApiHelper {
// This pointer should be set by the child class.
FormattedValue* fFormattedValue = nullptr;
};
/** Implementation of the methods from U_FORMATTED_VALUE_SUBCLASS_AUTO. */
#define UPRV_FORMATTED_VALUE_SUBCLASS_AUTO_IMPL(Name) \
Name::Name(Name&& src) U_NOEXCEPT { \
delete fData; \
fData = src.fData; \
src.fData = nullptr; \
} \
Name::~Name() { \
delete fData; \
fData = nullptr; \
} \
Name& Name::operator=(Name&& src) U_NOEXCEPT { \
delete fData; \
fData = src.fData; \
src.fData = nullptr; \
return *this; \
} \
UnicodeString Name::toString(UErrorCode& status) const { \
if (U_FAILURE(status)) { \
return ICU_Utility::makeBogusString(); \
} \
if (fData == nullptr) { \
status = fErrorCode; \
return ICU_Utility::makeBogusString(); \
} \
return fData->toString(status); \
} \
UnicodeString Name::toTempString(UErrorCode& status) const { \
if (U_FAILURE(status)) { \
return ICU_Utility::makeBogusString(); \
} \
if (fData == nullptr) { \
status = fErrorCode; \
return ICU_Utility::makeBogusString(); \
} \
return fData->toTempString(status); \
} \
Appendable& Name::appendTo(Appendable& appendable, UErrorCode& status) const { \
if (U_FAILURE(status)) { \
return appendable; \
} \
if (fData == nullptr) { \
status = fErrorCode; \
return appendable; \
} \
return fData->appendTo(appendable, status); \
} \
UBool Name::nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const { \
if (U_FAILURE(status)) { \
return FALSE; \
} \
if (fData == nullptr) { \
status = fErrorCode; \
return FALSE; \
} \
return fData->nextPosition(cfpos, status); \
}
/**
* Implementation of the standard methods for a UFormattedValue "subclass" C API.
* @param CPPType The public C++ type, like FormattedList
* @param CType The public C type, like UFormattedList
* @param ImplType A name to use for the implementation class
* @param HelperType A name to use for the "mixin" typedef for C API conversion
* @param Prefix The C API prefix, like ulistfmt
* @param MagicNumber A unique 32-bit number to use to identify this type
*/
#define UPRV_FORMATTED_VALUE_CAPI_AUTO_IMPL(CPPType, CType, ImplType, HelperType, Prefix, MagicNumber) \
U_NAMESPACE_BEGIN \
class ImplType; \
typedef IcuCApiHelper<CType, ImplType, MagicNumber> HelperType; \
class ImplType : public UFormattedValueImpl, public HelperType { \
public: \
ImplType(); \
~ImplType(); \
CPPType fImpl; \
}; \
ImplType::ImplType() { \
fFormattedValue = &fImpl; \
} \
ImplType::~ImplType() {} \
U_NAMESPACE_END \
U_CAPI CType* U_EXPORT2 \
Prefix ## _openResult (UErrorCode* ec) { \
if (U_FAILURE(*ec)) { \
return nullptr; \
} \
ImplType* impl = new ImplType(); \
if (impl == nullptr) { \
*ec = U_MEMORY_ALLOCATION_ERROR; \
return nullptr; \
} \
return static_cast<HelperType*>(impl)->exportForC(); \
} \
U_DRAFT const UFormattedValue* U_EXPORT2 \
Prefix ## _resultAsValue (const CType* uresult, UErrorCode* ec) { \
const ImplType* result = HelperType::validate(uresult, *ec); \
if (U_FAILURE(*ec)) { return nullptr; } \
return static_cast<const UFormattedValueApiHelper*>(result)->exportConstForC(); \
} \
U_CAPI void U_EXPORT2 \
Prefix ## _closeResult (CType* uresult) { \
UErrorCode localStatus = U_ZERO_ERROR; \
const ImplType* impl = HelperType::validate(uresult, localStatus); \
delete impl; \
}
U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_FORMATTING */

View File

@ -6,7 +6,7 @@
#if !UCONFIG_NO_FORMATTING
#include "unicode/formattedvalue.h"
#include "number_utypes.h"
#include "formattedval_impl.h"
#include "capi_helper.h"
U_NAMESPACE_BEGIN
@ -198,7 +198,7 @@ ufmtval_getString(
const UFormattedValue* ufmtval,
int32_t* pLength,
UErrorCode* ec) {
const auto* impl = number::impl::UFormattedValueApiHelper::validate(ufmtval, *ec);
const auto* impl = UFormattedValueApiHelper::validate(ufmtval, *ec);
if (U_FAILURE(*ec)) {
return nullptr;
}
@ -218,7 +218,7 @@ ufmtval_nextPosition(
const UFormattedValue* ufmtval,
UConstrainedFieldPosition* ucfpos,
UErrorCode* ec) {
const auto* fmtval = number::impl::UFormattedValueApiHelper::validate(ufmtval, *ec);
const auto* fmtval = UFormattedValueApiHelper::validate(ufmtval, *ec);
auto* cfpos = UConstrainedFieldPositionImpl::validate(ucfpos, *ec);
if (U_FAILURE(*ec)) {
return FALSE;

View File

@ -12,6 +12,7 @@
#include "fphdlimp.h"
#include "number_utypes.h"
#include "numparse_types.h"
#include "formattedval_impl.h"
#include "unicode/numberformatter.h"
#include "unicode/unumberformatter.h"

View File

@ -11,22 +11,11 @@
#include "number_types.h"
#include "number_decimalquantity.h"
#include "number_stringbuilder.h"
#include "capi_helper.h"
U_NAMESPACE_BEGIN namespace number {
namespace impl {
struct UFormattedValueImpl;
// Magic number as ASCII == "UFV"
typedef IcuCApiHelper<UFormattedValue, UFormattedValueImpl, 0x55465600> UFormattedValueApiHelper;
struct UFormattedValueImpl : public UMemory, public UFormattedValueApiHelper {
FormattedValue* fFormattedValue = nullptr;
};
/** Helper function used in upluralrules.cpp */
const DecimalQuantity* validateUFormattedNumberToDecimalQuantity(
const UFormattedNumber* uresult, UErrorCode& status);