From dbdbfb3a9160d54505356350e0e99ab1ee1d78d2 Mon Sep 17 00:00:00 2001 From: wqking Date: Mon, 11 Dec 2023 19:44:24 +0800 Subject: [PATCH] Fixed typos in document --- doc/tutorial/tutorial_variant.md | 19 +++++++++++++++---- doc/variant.md | 4 ++-- tests/docsrc/doc_variant.cpp | 4 ++-- .../docsrc/tutorial/doc_tutorial_variant.cpp | 12 ++++++++---- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/doc/tutorial/tutorial_variant.md b/doc/tutorial/tutorial_variant.md index f1ccbcca..21ff93ff 100644 --- a/doc/tutorial/tutorial_variant.md +++ b/doc/tutorial/tutorial_variant.md @@ -47,13 +47,12 @@ v = std::string("hello"); ``` Get as std::string will copy the value, that's inefficient. -We should get as reference. ```c++ ASSERT(v.get() == "hello"); ``` -Get as reference to avoid copy. +We should get as reference to avoid copy. ```c++ ASSERT(v.get() == "hello"); @@ -63,6 +62,11 @@ Whether the reference is const, it doesn't matter. ```c++ ASSERT(v.get() == "hello"); +``` + +We can assign to the reference if it's not const. + +```c++ v.get() = "world"; ASSERT(v.get() == "world"); ``` @@ -77,7 +81,14 @@ ASSERT(strcmp(v.get(), "great") == 0); Cast to const char *. ```c++ -metapp::Variant casted = v.cast(); +metapp::Variant casted = v.cast(); +ASSERT(strcmp(casted.get(), "great") == 0); +``` + +Cast to std::string. + +```c++ +casted = v.cast(); ASSERT(casted.get() == "great"); ``` @@ -89,7 +100,7 @@ int array[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; Now v contains reference to int[2][3]. We can't simply assign array to v because the array type will be lost. -We need to call Variant::create to retain the array type. +We need to call Variant::create or Variant::reference to retain the array type. ```c++ v = metapp::Variant::reference(array); diff --git a/doc/variant.md b/doc/variant.md index 481f03f3..298d2aac 100644 --- a/doc/variant.md +++ b/doc/variant.md @@ -272,11 +272,11 @@ This function is not useful in most cases. The only useful case is to use it on **Example** ```c++ -// This is wrong. In this case, var hold the ownership of MyClass +// This is wrong. In this case, var hold the ownership of std::string // takeFrom can't take the ownership from var //metapp::Variant var(std::string()); -// This works, var hold the pointer, it doesn't hold the ownership of MyClass +// This works, var hold the pointer, it doesn't hold the ownership of std::string // Note: if we don't call takeFrom later, var will leak the object // because it doesn't hold the ownership. metapp::Variant var(new std::string()); diff --git a/tests/docsrc/doc_variant.cpp b/tests/docsrc/doc_variant.cpp index 2449a821..8fde4187 100644 --- a/tests/docsrc/doc_variant.cpp +++ b/tests/docsrc/doc_variant.cpp @@ -266,11 +266,11 @@ desc*/ ExampleFunc { //code - // This is wrong. In this case, var hold the ownership of MyClass + // This is wrong. In this case, var hold the ownership of std::string // takeFrom can't take the ownership from var //metapp::Variant var(std::string()); - // This works, var hold the pointer, it doesn't hold the ownership of MyClass + // This works, var hold the pointer, it doesn't hold the ownership of std::string // Note: if we don't call takeFrom later, var will leak the object // because it doesn't hold the ownership. metapp::Variant var(new std::string()); diff --git a/tests/docsrc/tutorial/doc_tutorial_variant.cpp b/tests/docsrc/tutorial/doc_tutorial_variant.cpp index a6e9e8d4..154bd4ed 100644 --- a/tests/docsrc/tutorial/doc_tutorial_variant.cpp +++ b/tests/docsrc/tutorial/doc_tutorial_variant.cpp @@ -53,12 +53,12 @@ TEST_CASE("tutorialVariant") //desc Now v contains std::string. v = std::string("hello"); //desc Get as std::string will copy the value, that's inefficient. - //desc We should get as reference. ASSERT(v.get() == "hello"); - //desc Get as reference to avoid copy. + //desc We should get as reference to avoid copy. ASSERT(v.get() == "hello"); //desc Whether the reference is const, it doesn't matter. ASSERT(v.get() == "hello"); + //desc We can assign to the reference if it's not const. v.get() = "world"; ASSERT(v.get() == "world"); @@ -67,14 +67,18 @@ TEST_CASE("tutorialVariant") ASSERT(strcmp(v.get(), "great") == 0); //desc Cast to const char *. - metapp::Variant casted = v.cast(); + metapp::Variant casted = v.cast(); + ASSERT(strcmp(casted.get(), "great") == 0); + + //desc Cast to std::string. + casted = v.cast(); ASSERT(casted.get() == "great"); //desc Let's see how Variant works with array int array[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; //desc Now v contains reference to int[2][3]. //desc We can't simply assign array to v because the array type will be lost. - //desc We need to call Variant::create to retain the array type. + //desc We need to call Variant::create or Variant::reference to retain the array type. v = metapp::Variant::reference(array); ASSERT(v.get()[1][2] == 6); //desc Since v is a reference to array, modify array will also modify v