Loading include/ftl/concat.h 0 → 100644 +84 −0 Original line number Diff line number Diff line /* * Copyright 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include <ftl/details/concat.h> namespace android::ftl { // Lightweight (not allocating nor sprintf-based) concatenation. // // std::string_view name = "Volume"; // ftl::Concat string(ftl::truncated<3>(name), ": ", -3, " dB"); // // assert(string.str() == "Vol: -3 dB"); // assert(string.c_str()[string.size()] == '\0'); // template <std::size_t, typename... Ts> struct Concat; template <std::size_t N, typename T, typename... Ts> struct Concat<N, T, Ts...> : Concat<N + details::StaticString<T>::N, Ts...> { explicit constexpr Concat(T v, Ts... args) { append(v, args...); } protected: constexpr Concat() = default; constexpr void append(T v, Ts... args) { using Str = details::StaticString<T>; const Str str(v); // TODO: Replace with constexpr std::copy in C++20. for (auto it = str.view.begin(); it != str.view.end();) { *this->end_++ = *it++; } using Base = Concat<N + Str::N, Ts...>; this->Base::append(args...); } }; template <std::size_t N> struct Concat<N> { static constexpr std::size_t max_size() { return N; } constexpr std::size_t size() const { return end_ - buffer_; } constexpr const char* c_str() const { return buffer_; } constexpr std::string_view str() const { // TODO: Replace with {buffer_, end_} in C++20. return {buffer_, size()}; } protected: constexpr Concat() : end_(buffer_) {} constexpr void append() { *end_ = '\0'; } char buffer_[N + 1]; char* end_; }; // Deduction guide. template <typename... Ts> Concat(Ts&&...) -> Concat<0, Ts...>; template <std::size_t N> constexpr auto truncated(std::string_view v) { return details::Truncated<N>{v}; } } // namespace android::ftl services/surfaceflinger/Scheduler/SchedulerUtils.cpp→include/ftl/details/concat.h +62 −0 Original line number Diff line number Diff line /* * Copyright 2018 The Android Open Source Project * Copyright 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. Loading @@ -14,26 +14,49 @@ * limitations under the License. */ #include "SchedulerUtils.h" #pragma once #include <cinttypes> #include <numeric> #include <unordered_map> #include <vector> #include <functional> #include <string_view> namespace android { namespace scheduler { #include <ftl/string.h> int64_t calculate_median(std::vector<int64_t>* v) { if (!v || v->empty()) { return 0; } namespace android::ftl::details { size_t n = v->size() / 2; nth_element(v->begin(), v->begin() + static_cast<long>(n), v->end()); return v->at(n); } template <typename T, typename = void> struct StaticString; } // namespace scheduler } // namespace android template <typename T> struct StaticString<T, std::enable_if_t<std::is_integral_v<T>>> { static constexpr std::size_t N = to_chars_length_v<T>; explicit StaticString(T v) : view(to_chars(buffer, v)) {} to_chars_buffer_t<T> buffer; const std::string_view view; }; template <std::size_t M> struct StaticString<const char (&)[M], void> { static constexpr std::size_t N = M - 1; explicit constexpr StaticString(const char (&str)[M]) : view(str, N) {} const std::string_view view; }; template <std::size_t N> struct Truncated { std::string_view view; }; template <std::size_t M> struct StaticString<Truncated<M>, void> { static constexpr std::size_t N = M; explicit constexpr StaticString(Truncated<M> str) : view(str.view.substr(0, N)) {} const std::string_view view; }; } // namespace android::ftl::details include/input/Input.h +5 −7 Original line number Diff line number Diff line Loading @@ -31,10 +31,8 @@ #include <stdint.h> #include <ui/Transform.h> #include <utils/BitSet.h> #include <utils/KeyedVector.h> #include <utils/RefBase.h> #include <utils/Timers.h> #include <utils/Vector.h> #include <array> #include <limits> #include <queue> Loading Loading @@ -88,7 +86,7 @@ enum { */ AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE = 0x40, #ifdef __linux__ #if defined(__linux__) /** * This event was generated or modified by accessibility service. */ Loading Loading @@ -799,11 +797,11 @@ public: // Low-level accessors. inline const PointerProperties* getPointerProperties() const { return mPointerProperties.array(); return mPointerProperties.data(); } inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.data(); } inline const PointerCoords* getSamplePointerCoords() const { return mSamplePointerCoords.array(); return mSamplePointerCoords.data(); } static const char* getLabel(int32_t axis); Loading Loading @@ -834,9 +832,9 @@ protected: float mRawYCursorPosition; ui::Transform mRawTransform; nsecs_t mDownTime; Vector<PointerProperties> mPointerProperties; std::vector<PointerProperties> mPointerProperties; std::vector<nsecs_t> mSampleEventTimes; Vector<PointerCoords> mSamplePointerCoords; std::vector<PointerCoords> mSamplePointerCoords; }; /* Loading libs/binder/ndk/include_cpp/android/binder_auto_utils.h +9 −0 Original line number Diff line number Diff line Loading @@ -162,6 +162,15 @@ class ScopedAResource { */ const T get() const { return mT; } /** * Release the underlying resource. */ [[nodiscard]] T release() { T a = mT; mT = DEFAULT; return a; } /** * This allows the value in this class to be set from beneath it. If you call this method and * then change the value of T*, you must take ownership of the value you are replacing and add Loading libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp +23 −0 Original line number Diff line number Diff line Loading @@ -752,6 +752,29 @@ TEST(NdkBinder, GetClassInterfaceDescriptor) { ASSERT_STREQ(IFoo::kIFooDescriptor, AIBinder_Class_getDescriptor(IFoo::kClass)); } static void addOne(int* to) { if (!to) return; ++(*to); } struct FakeResource : public ndk::impl::ScopedAResource<int*, addOne, nullptr> { explicit FakeResource(int* a) : ScopedAResource(a) {} }; TEST(NdkBinder_ScopedAResource, GetDelete) { int deleteCount = 0; { FakeResource resource(&deleteCount); } EXPECT_EQ(deleteCount, 1); } TEST(NdkBinder_ScopedAResource, Release) { int deleteCount = 0; { FakeResource resource(&deleteCount); (void)resource.release(); } EXPECT_EQ(deleteCount, 0); } int main(int argc, char* argv[]) { ::testing::InitGoogleTest(&argc, argv); Loading Loading
include/ftl/concat.h 0 → 100644 +84 −0 Original line number Diff line number Diff line /* * Copyright 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include <ftl/details/concat.h> namespace android::ftl { // Lightweight (not allocating nor sprintf-based) concatenation. // // std::string_view name = "Volume"; // ftl::Concat string(ftl::truncated<3>(name), ": ", -3, " dB"); // // assert(string.str() == "Vol: -3 dB"); // assert(string.c_str()[string.size()] == '\0'); // template <std::size_t, typename... Ts> struct Concat; template <std::size_t N, typename T, typename... Ts> struct Concat<N, T, Ts...> : Concat<N + details::StaticString<T>::N, Ts...> { explicit constexpr Concat(T v, Ts... args) { append(v, args...); } protected: constexpr Concat() = default; constexpr void append(T v, Ts... args) { using Str = details::StaticString<T>; const Str str(v); // TODO: Replace with constexpr std::copy in C++20. for (auto it = str.view.begin(); it != str.view.end();) { *this->end_++ = *it++; } using Base = Concat<N + Str::N, Ts...>; this->Base::append(args...); } }; template <std::size_t N> struct Concat<N> { static constexpr std::size_t max_size() { return N; } constexpr std::size_t size() const { return end_ - buffer_; } constexpr const char* c_str() const { return buffer_; } constexpr std::string_view str() const { // TODO: Replace with {buffer_, end_} in C++20. return {buffer_, size()}; } protected: constexpr Concat() : end_(buffer_) {} constexpr void append() { *end_ = '\0'; } char buffer_[N + 1]; char* end_; }; // Deduction guide. template <typename... Ts> Concat(Ts&&...) -> Concat<0, Ts...>; template <std::size_t N> constexpr auto truncated(std::string_view v) { return details::Truncated<N>{v}; } } // namespace android::ftl
services/surfaceflinger/Scheduler/SchedulerUtils.cpp→include/ftl/details/concat.h +62 −0 Original line number Diff line number Diff line /* * Copyright 2018 The Android Open Source Project * Copyright 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. Loading @@ -14,26 +14,49 @@ * limitations under the License. */ #include "SchedulerUtils.h" #pragma once #include <cinttypes> #include <numeric> #include <unordered_map> #include <vector> #include <functional> #include <string_view> namespace android { namespace scheduler { #include <ftl/string.h> int64_t calculate_median(std::vector<int64_t>* v) { if (!v || v->empty()) { return 0; } namespace android::ftl::details { size_t n = v->size() / 2; nth_element(v->begin(), v->begin() + static_cast<long>(n), v->end()); return v->at(n); } template <typename T, typename = void> struct StaticString; } // namespace scheduler } // namespace android template <typename T> struct StaticString<T, std::enable_if_t<std::is_integral_v<T>>> { static constexpr std::size_t N = to_chars_length_v<T>; explicit StaticString(T v) : view(to_chars(buffer, v)) {} to_chars_buffer_t<T> buffer; const std::string_view view; }; template <std::size_t M> struct StaticString<const char (&)[M], void> { static constexpr std::size_t N = M - 1; explicit constexpr StaticString(const char (&str)[M]) : view(str, N) {} const std::string_view view; }; template <std::size_t N> struct Truncated { std::string_view view; }; template <std::size_t M> struct StaticString<Truncated<M>, void> { static constexpr std::size_t N = M; explicit constexpr StaticString(Truncated<M> str) : view(str.view.substr(0, N)) {} const std::string_view view; }; } // namespace android::ftl::details
include/input/Input.h +5 −7 Original line number Diff line number Diff line Loading @@ -31,10 +31,8 @@ #include <stdint.h> #include <ui/Transform.h> #include <utils/BitSet.h> #include <utils/KeyedVector.h> #include <utils/RefBase.h> #include <utils/Timers.h> #include <utils/Vector.h> #include <array> #include <limits> #include <queue> Loading Loading @@ -88,7 +86,7 @@ enum { */ AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE = 0x40, #ifdef __linux__ #if defined(__linux__) /** * This event was generated or modified by accessibility service. */ Loading Loading @@ -799,11 +797,11 @@ public: // Low-level accessors. inline const PointerProperties* getPointerProperties() const { return mPointerProperties.array(); return mPointerProperties.data(); } inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.data(); } inline const PointerCoords* getSamplePointerCoords() const { return mSamplePointerCoords.array(); return mSamplePointerCoords.data(); } static const char* getLabel(int32_t axis); Loading Loading @@ -834,9 +832,9 @@ protected: float mRawYCursorPosition; ui::Transform mRawTransform; nsecs_t mDownTime; Vector<PointerProperties> mPointerProperties; std::vector<PointerProperties> mPointerProperties; std::vector<nsecs_t> mSampleEventTimes; Vector<PointerCoords> mSamplePointerCoords; std::vector<PointerCoords> mSamplePointerCoords; }; /* Loading
libs/binder/ndk/include_cpp/android/binder_auto_utils.h +9 −0 Original line number Diff line number Diff line Loading @@ -162,6 +162,15 @@ class ScopedAResource { */ const T get() const { return mT; } /** * Release the underlying resource. */ [[nodiscard]] T release() { T a = mT; mT = DEFAULT; return a; } /** * This allows the value in this class to be set from beneath it. If you call this method and * then change the value of T*, you must take ownership of the value you are replacing and add Loading
libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp +23 −0 Original line number Diff line number Diff line Loading @@ -752,6 +752,29 @@ TEST(NdkBinder, GetClassInterfaceDescriptor) { ASSERT_STREQ(IFoo::kIFooDescriptor, AIBinder_Class_getDescriptor(IFoo::kClass)); } static void addOne(int* to) { if (!to) return; ++(*to); } struct FakeResource : public ndk::impl::ScopedAResource<int*, addOne, nullptr> { explicit FakeResource(int* a) : ScopedAResource(a) {} }; TEST(NdkBinder_ScopedAResource, GetDelete) { int deleteCount = 0; { FakeResource resource(&deleteCount); } EXPECT_EQ(deleteCount, 1); } TEST(NdkBinder_ScopedAResource, Release) { int deleteCount = 0; { FakeResource resource(&deleteCount); (void)resource.release(); } EXPECT_EQ(deleteCount, 0); } int main(int argc, char* argv[]) { ::testing::InitGoogleTest(&argc, argv); Loading