Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1293)

Unified Diff: src/utils/SkTLogic.h

Issue 258693006: Start using type traits in src/record instead of macros. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: ben Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/record/SkRecordTraits.h ('k') | tests/RecordDrawTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/utils/SkTLogic.h
diff --git a/src/utils/SkTLogic.h b/src/utils/SkTLogic.h
index bf9ee1ab2505d8a772ad8e8cff4760afbcccb791..62952ad13c267d88e911a34a25a34cebc2bd5835 100644
--- a/src/utils/SkTLogic.h
+++ b/src/utils/SkTLogic.h
@@ -58,4 +58,35 @@ struct SkTMux {
typename SkTIf<b, B, Neither>::type>::type type;
};
+/** SkTEnableIf_c::type = (condition) ? T : [does not exist]; */
+template <bool condition, class T = void> struct SkTEnableIf_c { };
+template <class T> struct SkTEnableIf_c<true, T> {
+ typedef T type;
+};
+
+/** SkTEnableIf::type = (Condition::value) ? T : [does not exist]; */
+template <class Condition, class T = void> struct SkTEnableIf
+ : public SkTEnableIf_c<static_cast<bool>(Condition::value), T> { };
+
+/** Use as a return type to enable a function only when cond_type::value is true,
+ * like C++14's std::enable_if_t. E.g. (N.B. this is a dumb example.)
+ * SK_WHEN(SkTrue, int) f(void* ptr) { return 1; }
+ * SK_WHEN(!SkTrue, int) f(void* ptr) { return 2; }
+ */
+#define SK_WHEN(cond_prefix, T) typename SkTEnableIf_c<cond_prefix::value, T>::type
+
+// See http://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector
+#define SK_CREATE_MEMBER_DETECTOR(member) \
+template <typename T> \
+class HasMember_##member { \
+ struct Fallback { int member; }; \
+ struct Derived : T, Fallback {}; \
+ template <typename U, U> struct Check; \
+ template <typename U> static uint8_t func(Check<int Fallback::*, &U::member>*); \
+ template <typename U> static uint16_t func(...); \
+public: \
+ typedef HasMember_##member type; \
+ static const bool value = sizeof(func<Derived>(NULL)) == sizeof(uint16_t); \
+}
+
#endif
« no previous file with comments | « src/record/SkRecordTraits.h ('k') | tests/RecordDrawTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698