[PATCH] D59142: YAMLIO: Improve template arg deduction for mapOptional

Pavel Labath via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 14 08:24:22 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL356157: YAMLIO: Improve template arg deduction for mapOptional (authored by labath, committed by ).

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D59142/new/

https://reviews.llvm.org/D59142

Files:
  llvm/trunk/include/llvm/Support/YAMLTraits.h
  llvm/trunk/unittests/Support/YAMLIOTest.cpp


Index: llvm/trunk/include/llvm/Support/YAMLTraits.h
===================================================================
--- llvm/trunk/include/llvm/Support/YAMLTraits.h
+++ llvm/trunk/include/llvm/Support/YAMLTraits.h
@@ -863,8 +863,8 @@
     mapOptionalWithContext(Key, Val, Ctx);
   }
 
-  template <typename T>
-  void mapOptional(const char *Key, T &Val, const T &Default) {
+  template <typename T, typename DefaultT>
+  void mapOptional(const char *Key, T &Val, const DefaultT &Default) {
     EmptyContext Ctx;
     mapOptionalWithContext(Key, Val, Default, Ctx);
   }
@@ -890,10 +890,13 @@
     this->processKey(Key, Val, false, Ctx);
   }
 
-  template <typename T, typename Context>
-  void mapOptionalWithContext(const char *Key, T &Val, const T &Default,
+  template <typename T, typename Context, typename DefaultT>
+  void mapOptionalWithContext(const char *Key, T &Val, const DefaultT &Default,
                               Context &Ctx) {
-    this->processKeyWithDefault(Key, Val, Default, false, Ctx);
+    static_assert(std::is_convertible<DefaultT, T>::value,
+                  "Default type must be implicitly convertible to value type!");
+    this->processKeyWithDefault(Key, Val, static_cast<const T &>(Default),
+                                false, Ctx);
   }
 
 private:
Index: llvm/trunk/unittests/Support/YAMLIOTest.cpp
===================================================================
--- llvm/trunk/unittests/Support/YAMLIOTest.cpp
+++ llvm/trunk/unittests/Support/YAMLIOTest.cpp
@@ -823,7 +823,7 @@
       io.mapRequired("f1", c.f1);
       io.mapRequired("f2", c.f2);
       io.mapRequired("f3", c.f3);
-      io.mapOptional("f4", c.f4, MyFlags(flagRound));
+      io.mapOptional("f4", c.f4, flagRound);
      }
   };
 }
@@ -1327,8 +1327,8 @@
     static void mapping(IO &io, TotalSeconds &secs) {
       MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
 
-      io.mapOptional("hours",    keys->hours,    (uint32_t)0);
-      io.mapOptional("minutes",  keys->minutes,  (uint8_t)0);
+      io.mapOptional("hours", keys->hours, 0);
+      io.mapOptional("minutes", keys->minutes, 0);
       io.mapRequired("seconds",  keys->seconds);
     }
   };


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59142.190636.patch
Type: text/x-patch
Size: 2207 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190314/629ef010/attachment.bin>


More information about the llvm-commits mailing list