summaryrefslogtreecommitdiffstats
path: root/llvm/docs
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2015-05-14 23:08:22 +0000
committerAlex Lorenz <arphaman@gmail.com>2015-05-14 23:08:22 +0000
commit68e787bdb0bfe493bd11f493d4e81299fa0845cd (patch)
treee008af40db5c4fb3e61515de2beb622af2510125 /llvm/docs
parentb3faa900bdc193b8732a3c1d6722353084410008 (diff)
downloadbcm5719-llvm-68e787bdb0bfe493bd11f493d4e81299fa0845cd.tar.gz
bcm5719-llvm-68e787bdb0bfe493bd11f493d4e81299fa0845cd.zip
YAML: Add support for literal block scalar I/O.
This commit gives the users of the YAML Traits I/O library the ability to serialize scalars using the YAML literal block scalar notation by allowing them to implement a specialization of the `BlockScalarTraits` struct for their custom types. Reviewers: Duncan P. N. Exon Smith Differential Revision: http://reviews.llvm.org/D9613 llvm-svn: 237404
Diffstat (limited to 'llvm/docs')
-rw-r--r--llvm/docs/YamlIO.rst50
1 files changed, 50 insertions, 0 deletions
diff --git a/llvm/docs/YamlIO.rst b/llvm/docs/YamlIO.rst
index ab8b19ac2a5..aa4bae35d1a 100644
--- a/llvm/docs/YamlIO.rst
+++ b/llvm/docs/YamlIO.rst
@@ -467,6 +467,56 @@ looks like:
// Determine if this scalar needs quotes.
static bool mustQuote(StringRef) { return true; }
};
+
+Block Scalars
+-------------
+
+YAML block scalars are string literals that are represented in YAML using the
+literal block notation, just like the example shown below:
+
+.. code-block:: yaml
+
+ text: |
+ First line
+ Second line
+
+The YAML I/O library provides support for translating between YAML block scalars
+and specific C++ types by allowing you to specialize BlockScalarTraits<> on
+your data type. The library doesn't provide any built-in support for block
+scalar I/O for types like std::string and llvm::StringRef as they are already
+supported by YAML I/O and use the ordinary scalar notation by default.
+
+BlockScalarTraits specializations are very similar to the
+ScalarTraits specialization - YAML I/O will provide the native type and your
+specialization must create a temporary llvm::StringRef when writing, and
+it will also provide an llvm::StringRef that has the value of that block scalar
+and your specialization must convert that to your native data type when reading.
+An example of a custom type with an appropriate specialization of
+BlockScalarTraits is shown below:
+
+.. code-block:: c++
+
+ using llvm::yaml::BlockScalarTraits;
+ using llvm::yaml::IO;
+
+ struct MyStringType {
+ std::string Str;
+ };
+
+ template <>
+ struct BlockScalarTraits<MyStringType> {
+ static void output(const MyStringType &Value, void *Ctxt,
+ llvm::raw_ostream &OS) {
+ OS << Value.Str;
+ }
+
+ static StringRef input(StringRef Scalar, void *Ctxt,
+ MyStringType &Value) {
+ Value.Str = Scalar.str();
+ return StringRef();
+ }
+ };
+
Mappings
OpenPOWER on IntegriCloud