diff options
author | scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-04-23 15:38:09 +0000 |
---|---|---|
committer | scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-04-23 15:38:09 +0000 |
commit | b7dbf633a69408fef33105e39b60c442771dba07 (patch) | |
tree | 61d34f61e99f6c353a05c0cd5db1631afb1f868f | |
parent | d42aca31b9ddc1cb9a81522b4c73a9fe550450bc (diff) |
Fix an SkCommandLineFlags bug.
Previously I was storing an SkTDArray of const char*,
which fails if the strings go out of scope.
Instead, store an SkTArray of SkString, and copy the
strings, so we do not depend on the strings sticking
around. Using an SkTArray because it is smart enough
to call the destructors, so the copies can be destroyed
on program exit.
BUG=https://code.google.com/p/skia/issues/detail?id=1237
Review URL: https://codereview.chromium.org/14414008
git-svn-id: http://skia.googlecode.com/svn/trunk@8829 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | gm/gmmain.cpp | 2 | ||||
-rw-r--r-- | tools/flags/SkCommandLineFlags.cpp | 1 | ||||
-rw-r--r-- | tools/flags/SkCommandLineFlags.h | 47 |
3 files changed, 40 insertions, 10 deletions
diff --git a/gm/gmmain.cpp b/gm/gmmain.cpp index 4979d79a54..65058d6198 100644 --- a/gm/gmmain.cpp +++ b/gm/gmmain.cpp @@ -1256,7 +1256,7 @@ static int findConfig(const char config[]) { return -1; } -static bool skip_name(const SkTDArray<const char*> array, const char name[]) { +static bool skip_name(SkCommandLineFlags::StringArray array, const char name[]) { if (0 == array.count()) { // no names, so don't skip anything return false; diff --git a/tools/flags/SkCommandLineFlags.cpp b/tools/flags/SkCommandLineFlags.cpp index 451daf7f6f..d634edb311 100644 --- a/tools/flags/SkCommandLineFlags.cpp +++ b/tools/flags/SkCommandLineFlags.cpp @@ -6,6 +6,7 @@ */ #include "SkCommandLineFlags.h" +#include "SkTDArray.h" static bool string_is_in(const char* target, const char* set[], size_t len) { for (size_t i = 0; i < len; i++) { diff --git a/tools/flags/SkCommandLineFlags.h b/tools/flags/SkCommandLineFlags.h index c705948348..08fd3a2ba5 100644 --- a/tools/flags/SkCommandLineFlags.h +++ b/tools/flags/SkCommandLineFlags.h @@ -9,7 +9,7 @@ #define SK_COMMAND_LINE_FLAGS_H #include "SkString.h" -#include "SkTDArray.h" +#include "SkTArray.h" /** * Including this file (and compiling SkCommandLineFlags.cpp) provides command line @@ -68,7 +68,7 @@ * * creates an array: * - * SkTDArray<const char*> FLAGS_args; + * SkCommandLineFlags::StringArray FLAGS_args; * * If the default value is the empty string, FLAGS_args will default to a size * of zero. Otherwise it will default to a size of 1 with the default string @@ -108,6 +108,35 @@ public: */ static void Parse(int argc, char** argv); + /** + * Custom class for holding the arguments for a string flag. + * Publicly only has accessors so the strings cannot be modified. + */ + class StringArray { + public: + const char* operator[](int i) const { + SkASSERT(i >= 0 && i < fStrings.count()); + return fStrings[i].c_str(); + } + + int count() const { + return fStrings.count(); + } + + bool isEmpty() const { return this->count() == 0; } + + private: + void reset() { fStrings.reset(); } + + void append(const char* string) { + fStrings.push_back().set(string); + } + + SkTArray<SkString> fStrings; + + friend class SkFlagInfo; + }; + private: static SkFlagInfo* gHead; static SkString gUsage; @@ -140,7 +169,7 @@ static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ #define DECLARE_bool(name) extern bool FLAGS_##name; #define DEFINE_string(name, defaultValue, helpString) \ -SkTDArray<const char*> FLAGS_##name; \ +SkCommandLineFlags::StringArray FLAGS_##name; \ static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ NULL, \ &FLAGS_##name, \ @@ -150,14 +179,14 @@ static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ // string2 allows specifying a short name. There is an assert that shortName // is only 1 character. #define DEFINE_string2(name, shortName, defaultValue, helpString) \ -SkTDArray<const char*> FLAGS_##name; \ +SkCommandLineFlags::StringArray FLAGS_##name; \ static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ TO_STRING(shortName), \ &FLAGS_##name, \ defaultValue, \ helpString) -#define DECLARE_string(name) extern SkTDArray<const char*> FLAGS_##name; +#define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name; #define DEFINE_int32(name, defaultValue, helpString) \ int32_t FLAGS_##name; \ @@ -197,7 +226,7 @@ public: } static bool CreateStringFlag(const char* name, const char* shortName, - SkTDArray<const char*>* pStrings, + SkCommandLineFlags::StringArray* pStrings, const char* defaultValue, const char* helpString) { SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kString_FlagType, helpString)); info->fDefaultString.set(defaultValue); @@ -206,7 +235,7 @@ public: info->fStrings->reset(); // If default is "", leave the array empty. if (info->fDefaultString.size() > 0) { - info->fStrings->append(1, &defaultValue); + info->fStrings->append(defaultValue); } return true; } @@ -254,7 +283,7 @@ public: void append(const char* string) { if (kString_FlagType == fFlagType) { - fStrings->append(1, &string); + fStrings->append(string); } else { SkASSERT(!"Can only append to kString_FlagType"); } @@ -357,7 +386,7 @@ private: int32_t fDefaultInt; double* fDoubleValue; double fDefaultDouble; - SkTDArray<const char*>* fStrings; + SkCommandLineFlags::StringArray* fStrings; // Both for the help string and in case fStrings is empty. SkString fDefaultString; |