Improve C++ example -- MOS_MIGRATED_REVID=87514947
diff --git a/base_workspace/examples/cpp/README.md b/base_workspace/examples/cpp/README.md new file mode 100644 index 0000000..fba7143 --- /dev/null +++ b/base_workspace/examples/cpp/README.md
@@ -0,0 +1,4 @@ +C++ Examples +============ + +These examples demonstrate building C++ binaries, libraries, and tests.
diff --git a/base_workspace/examples/cpp/hello-fail.cc b/base_workspace/examples/cpp/hello-fail.cc index 9b0b94a..5b561cb 100644 --- a/base_workspace/examples/cpp/hello-fail.cc +++ b/base_workspace/examples/cpp/hello-fail.cc
@@ -1,11 +1,22 @@ #include "examples/cpp/hello-lib.h" +#include <string> + +using hello::HelloLib; +using std::string; + +/** + * This is a fake test that prints "Hello barf" and then exits with exit code 1. + * If run as a test (cc_test), the non-0 exit code indicates to Bazel that the + * "test" has failed. + */ int main(int argc, char** argv) { - const char* obj = "barf"; + HelloLib lib; + string thing = "barf"; if (argc > 1) { - obj = argv[1]; + thing = argv[1]; } - greet(obj); + lib.greet(thing); return 1; }
diff --git a/base_workspace/examples/cpp/hello-lib.cc b/base_workspace/examples/cpp/hello-lib.cc index aee43ba..8e40921 100644 --- a/base_workspace/examples/cpp/hello-lib.cc +++ b/base_workspace/examples/cpp/hello-lib.cc
@@ -1,5 +1,21 @@ #include "examples/cpp/hello-lib.h" -#include <stdio.h> +#include <iostream> -void greet(const char* obj) { printf("hello %s\n", obj); } +using std::cout; +using std::endl; +using std::string; + +namespace hello { + +HelloLib::HelloLib() : HelloLib("Hello") { +} + +HelloLib::HelloLib(const string& greeting) : greeting_(new string(greeting)) { +} + +void HelloLib::greet(const string& thing) { + cout << *greeting_ << " " << thing << endl; +} + +} // namespace hello
diff --git a/base_workspace/examples/cpp/hello-lib.h b/base_workspace/examples/cpp/hello-lib.h index 3e74c2b..555d177 100644 --- a/base_workspace/examples/cpp/hello-lib.h +++ b/base_workspace/examples/cpp/hello-lib.h
@@ -1,6 +1,23 @@ -#ifndef EXAMPLE_WORKSPACE_CPP_HELLO_LIB_H_ -#define EXAMPLE_WORKSPACE_CPP_HELLO_LIB_H_ +#ifndef EXAMPLES_CPP_HELLO_LIB_H_ +#define EXAMPLES_CPP_HELLO_LIB_H_ -void greet(const char* object); +#include <string> +#include <memory> -#endif // EXAMPLE_WORKSPACE_CPP_HELLO_LIB_H_ +namespace hello { + +class HelloLib { + public: + HelloLib(); + + explicit HelloLib(const std::string &greeting); + + void greet(const std::string &thing); + + private: + std::unique_ptr<const std::string> greeting_; +}; + +} // namespace hello + +#endif // EXAMPLES_CPP_HELLO_LIB_H_
diff --git a/base_workspace/examples/cpp/hello-world.cc b/base_workspace/examples/cpp/hello-world.cc index 305b17f..be435c3 100644 --- a/base_workspace/examples/cpp/hello-world.cc +++ b/base_workspace/examples/cpp/hello-world.cc
@@ -1,11 +1,26 @@ #include "examples/cpp/hello-lib.h" -int main(int argc, char** argv) { - const char* obj = "world"; - if (argc > 1) { - obj = argv[1]; - } +#include <string> - greet(obj); +using hello::HelloLib; +using std::string; + +/** + * This prints "Hello world". If it is run with arguments, it will use the first + * argument instead of "world". Build and run //examples/cpp:hello-world to see + * this program in action. + * + * This file does double-duty as a "test." It is a cc_binary, so it can also be + * compiled as a cc_test and Bazel will decide on whether it passed or failed + * based on exit code (which is always 0 here, so the test "passes"). See + * hello-fail.cc for an example of making a test fail. + */ +int main(int argc, char** argv) { + HelloLib lib("Hello"); + string thing = "world"; + if (argc > 1) { + thing = argv[1]; + } + lib.greet(thing); return 0; }