1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
/*
* Copyright (C) 2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <tuple>
#include <unicode/utypes.h>
#include <wtf/Forward.h>
#include <wtf/FunctionTraits.h>
namespace WTF {
constexpr bool needsToGrowToProduceBuffer(UErrorCode);
constexpr bool needsToGrowToProduceCString(UErrorCode);
// Use this to call a function from ICU that has the following properties:
// - Takes a buffer pointer and capacity.
// - Returns the length of the buffer needed.
// - Takes a UErrorCode* as its last argument, returning the status, including U_BUFFER_OVERFLOW_ERROR.
// Pass the arguments, but pass a Vector in place of the buffer pointer and capacity, and don't pass a UErrorCode*.
// This will call the function, once or twice as needed, resizing the buffer as needed.
//
// Example:
//
// Vector<UChar, 32> buffer;
// auto status = callBufferProducingFunction(ucal_getDefaultTimeZone, buffer);
//
template<typename FunctionType, typename ...ArgumentTypes> UErrorCode callBufferProducingFunction(const FunctionType&, ArgumentTypes&&...);
// Implementations of the functions declared above.
constexpr bool needsToGrowToProduceBuffer(UErrorCode errorCode)
{
return errorCode == U_BUFFER_OVERFLOW_ERROR;
}
constexpr bool needsToGrowToProduceCString(UErrorCode errorCode)
{
return needsToGrowToProduceBuffer(errorCode) || errorCode == U_STRING_NOT_TERMINATED_WARNING;
}
namespace CallBufferProducingFunction {
template<typename CharacterType, size_t inlineCapacity, typename ...ArgumentTypes> auto& findVector(Vector<CharacterType, inlineCapacity>& buffer, ArgumentTypes&&...)
{
return buffer;
}
template<typename FirstArgumentType, typename ...ArgumentTypes> auto& findVector(FirstArgumentType&&, ArgumentTypes&&... arguments)
{
return findVector(std::forward<ArgumentTypes>(arguments)...);
}
constexpr std::tuple<> argumentTuple() { return { }; }
template<typename FirstArgumentType, typename ...OtherArgumentTypes> auto argumentTuple(FirstArgumentType&&, OtherArgumentTypes&&...);
template<typename CharacterType, size_t inlineCapacity, typename ...OtherArgumentTypes> auto argumentTuple(Vector<CharacterType, inlineCapacity>& buffer, OtherArgumentTypes&&... otherArguments)
{
return tuple_cat(std::make_tuple(buffer.data(), buffer.size()), argumentTuple(std::forward<OtherArgumentTypes>(otherArguments)...));
}
template<typename FirstArgumentType, typename ...OtherArgumentTypes> auto argumentTuple(FirstArgumentType&& firstArgument, OtherArgumentTypes&&... otherArguments)
{
// This technique of building a tuple and passing it twice does not work well for complex types, so assert this is a relatively simple one.
static_assert(std::is_trivial_v<std::remove_reference_t<FirstArgumentType>>);
return tuple_cat(std::make_tuple(firstArgument), argumentTuple(std::forward<OtherArgumentTypes>(otherArguments)...));
}
}
template<typename FunctionType, typename ...ArgumentTypes> UErrorCode callBufferProducingFunction(const FunctionType& function, ArgumentTypes&&... arguments)
{
auto& buffer = CallBufferProducingFunction::findVector(std::forward<ArgumentTypes>(arguments)...);
buffer.grow(buffer.capacity());
auto status = U_ZERO_ERROR;
auto resultLength = std::apply(function, CallBufferProducingFunction::argumentTuple(std::forward<ArgumentTypes>(arguments)..., &status));
if (U_SUCCESS(status))
buffer.shrink(resultLength);
else if (needsToGrowToProduceBuffer(status)) {
status = U_ZERO_ERROR;
buffer.grow(resultLength);
std::apply(function, CallBufferProducingFunction::argumentTuple(std::forward<ArgumentTypes>(arguments)..., &status));
ASSERT(U_SUCCESS(status));
}
return status;
}
template<auto deleteFunction>
struct ICUDeleter {
void operator()(typename FunctionTraits<decltype(deleteFunction)>::template ArgumentType<0> pointer)
{
if (pointer)
deleteFunction(pointer);
}
};
namespace ICU {
WTF_EXPORT_PRIVATE unsigned majorVersion();
WTF_EXPORT_PRIVATE unsigned minorVersion();
} // namespace ICU
} // namespace WTF
using WTF::callBufferProducingFunction;
using WTF::needsToGrowToProduceCString;
using WTF::needsToGrowToProduceBuffer;
using WTF::ICUDeleter;
|