diff options
author | Zachary Turner <zturner@google.com> | 2016-09-08 18:22:44 +0000 |
---|---|---|
committer | Zachary Turner <zturner@google.com> | 2016-09-08 18:22:44 +0000 |
commit | 35377f88f583e318495e9b754a9f16e26aaa10da (patch) | |
tree | 50820db066ee83c563167234a328747a46800683 /llvm/unittests/Support/YAMLIOTest.cpp | |
parent | ba7c5cfbbb3dd1de41234e6ed1f97de3192f1172 (diff) | |
download | bcm5719-llvm-35377f88f583e318495e9b754a9f16e26aaa10da.tar.gz bcm5719-llvm-35377f88f583e318495e9b754a9f16e26aaa10da.zip |
[YAMLIO] Add the ability to map with context.
mapping a yaml field to an object in code has always been
a stateless operation. You could still pass state by using the
`setContext` function of the YAMLIO object, but this represented
global state for the entire yaml input. In order to have
context-sensitive state, it is necessary to pass this state in
at the granularity of an individual mapping.
This patch adds support for this type of context-sensitive state.
You simply pass an additional argument of type T to the
`mapRequired` or `mapOptional` functions, and provided you have
specialized a `MappingContextTraits<U, T>` class with the
appropriate mapping function, you can pass this context into
the mapping function.
Reviewed By: chandlerc
Differential Revision: https://reviews.llvm.org/D24162
llvm-svn: 280977
Diffstat (limited to 'llvm/unittests/Support/YAMLIOTest.cpp')
-rw-r--r-- | llvm/unittests/Support/YAMLIOTest.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp index 5f35a802caa..bf70e749b4f 100644 --- a/llvm/unittests/Support/YAMLIOTest.cpp +++ b/llvm/unittests/Support/YAMLIOTest.cpp @@ -2299,3 +2299,72 @@ TEST(YAMLIO, TestWrapFlow) { out.clear(); } } + +struct MappingContext { + int A = 0; +}; +struct SimpleMap { + int B = 0; + int C = 0; +}; + +struct NestedMap { + NestedMap(MappingContext &Context) : Context(Context) {} + SimpleMap Simple; + MappingContext &Context; +}; + +namespace llvm { +namespace yaml { +template <> struct MappingContextTraits<SimpleMap, MappingContext> { + static void mapping(IO &io, SimpleMap &sm, MappingContext &Context) { + io.mapRequired("B", sm.B); + io.mapRequired("C", sm.C); + ++Context.A; + io.mapRequired("Context", Context.A); + } +}; + +template <> struct MappingTraits<NestedMap> { + static void mapping(IO &io, NestedMap &nm) { + io.mapRequired("Simple", nm.Simple, nm.Context); + } +}; +} +} + +TEST(YAMLIO, TestMapWithContext) { + MappingContext Context; + NestedMap Nested(Context); + std::string out; + llvm::raw_string_ostream ostr(out); + + Output yout(ostr, nullptr, 15); + + yout << Nested; + ostr.flush(); + EXPECT_EQ(1, Context.A); + EXPECT_EQ("---\n" + "Simple: \n" + " B: 0\n" + " C: 0\n" + " Context: 1\n" + "...\n", + out); + + out.clear(); + + Nested.Simple.B = 2; + Nested.Simple.C = 3; + yout << Nested; + ostr.flush(); + EXPECT_EQ(2, Context.A); + EXPECT_EQ("---\n" + "Simple: \n" + " B: 2\n" + " C: 3\n" + " Context: 2\n" + "...\n", + out); + out.clear(); +} |