aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorSteven Moreland <smoreland@google.com>2017-09-25 17:14:10 -0700
committerSteven Moreland <smoreland@google.com>2017-09-26 13:02:49 -0700
commit2f4ee8e8d5b4315646189b128e913e830c9803a8 (patch)
tree0edf53750020a80c91e5dd4661260c2aa48d28c4 /utils
parent3d6e7beee4f180a1cd442775f550059f09d5f7ab (diff)
downloadhidl-2f4ee8e8d5b4315646189b128e913e830c9803a8.tar.gz
Allow Formatter to be invalid.
Rather than CHECKing, we can return a Formatter which is invalid. Bug: 65738957 Test: makefiles/host tests Change-Id: Ic9a7a0ffdf6d0c7d7b84f22e042b372ab9a45d41
Diffstat (limited to 'utils')
-rw-r--r--utils/Formatter.cpp10
-rw-r--r--utils/include/hidl-util/Formatter.h13
2 files changed, 20 insertions, 3 deletions
diff --git a/utils/Formatter.cpp b/utils/Formatter.cpp
index 83caf8d1..7fa0be59 100644
--- a/utils/Formatter.cpp
+++ b/utils/Formatter.cpp
@@ -18,8 +18,12 @@
#include <assert.h>
+#include <android-base/logging.h>
+
namespace android {
+Formatter::Formatter() : mFile(NULL /* invalid */), mIndentDepth(0), mAtStartOfLine(true) {}
+
Formatter::Formatter(FILE *file)
: mFile(file == NULL ? stdout : file),
mIndentDepth(0),
@@ -184,7 +188,13 @@ void Formatter::setNamespace(const std::string &space) {
mSpace = space;
}
+bool Formatter::isValid() const {
+ return mFile != nullptr;
+}
+
void Formatter::output(const std::string &text) const {
+ CHECK(isValid());
+
fprintf(mFile, "%s", text.c_str());
}
diff --git a/utils/include/hidl-util/Formatter.h b/utils/include/hidl-util/Formatter.h
index 2f7c02d1..9e6d728f 100644
--- a/utils/include/hidl-util/Formatter.h
+++ b/utils/include/hidl-util/Formatter.h
@@ -30,9 +30,11 @@ namespace android {
// The other is with chain calls and lambda functions
// out.sIf("good", [&] { out("blah").endl()("blah").endl(); }).endl();
struct Formatter {
+ static Formatter invalid() { return Formatter(); }
// Assumes ownership of file. Directed to stdout if file == NULL.
- Formatter(FILE *file = NULL);
+ Formatter(FILE* file);
+ Formatter(Formatter&&) = default;
~Formatter();
void indent(size_t level = 1);
@@ -138,8 +140,13 @@ struct Formatter {
// Remove the line prefix.
void unsetLinePrefix();
-private:
- FILE *mFile;
+ bool isValid() const;
+
+ private:
+ // Creates an invalid formatter object.
+ Formatter();
+
+ FILE* mFile; // invalid if nullptr
size_t mIndentDepth;
bool mAtStartOfLine;