Index: tools/gn/value.h |
diff --git a/tools/gn/value.h b/tools/gn/value.h |
index ee201c45e7b6a8065f3d014724eade3f79eccb8b..543b6e98cca3a4969009d5619bd3c4a21bc7a3b3 100644 |
--- a/tools/gn/value.h |
+++ b/tools/gn/value.h |
@@ -5,6 +5,8 @@ |
#ifndef TOOLS_GN_VALUE_H_ |
#define TOOLS_GN_VALUE_H_ |
+#include <map> |
+ |
#include "base/basictypes.h" |
#include "base/logging.h" |
#include "base/strings/string_piece.h" |
@@ -22,7 +24,23 @@ class Value { |
INTEGER, |
STRING, |
LIST, |
- SCOPE |
+ |
+ // Scopes and dictionaries act equivalently from the calling code |
+ // perspective, but are created differently. The scope type is used in |
+ // templates to refer to the invoker. Dictionaries are used when reading |
+ // scopes from files and we actually want to copy and own the values. Both |
scottmg
2014/04/03 20:44:40
Adding a second almost-identical seems pretty awkw
brettw
2014/04/03 23:19:24
I went around and around on this and at times had
|
+ // are read-only from script and are read using "value.key" syntax. |
+ // |
+ // GN specifically doesn't expose a general writable dictionary type to the |
+ // script because of the additional complexity, lack of a strong use case, |
+ // and potential for abuse. We can add this if there is a very strong use |
+ // case and we can come up with syntax we like. |
+ // |
+ // If we wanted to use this more generally, an alternative approach would |
+ // be to refcount the Scope object so value could own a reference to it, |
+ // and then delete the dictionary type. |
+ SCOPE, |
+ DICT, |
}; |
Value(); |
@@ -93,6 +111,15 @@ class Value { |
return scope_value_; |
} |
+ std::map<std::string, Value>& dict_value() { |
+ DCHECK(type_ == DICT); |
+ return dict_value_; |
+ } |
+ const std::map<std::string, Value>& dict_value() const { |
+ DCHECK(type_ == DICT); |
+ return dict_value_; |
+ } |
+ |
// Converts the given value to a string. Returns true if strings should be |
// quoted or the ToString of a string should be the string itself. |
std::string ToString(bool quote_strings) const; |
@@ -106,12 +133,17 @@ class Value { |
bool operator!=(const Value& other) const; |
private: |
+ // This is a lot of objects associated with every Value that need |
+ // initialization and tear down every time. It might be more efficient to |
+ // create a union of ManualConstructor objects (see SmallMap) and only |
+ // use the one we care about. |
Type type_; |
std::string string_value_; |
bool boolean_value_; |
int64 int_value_; |
std::vector<Value> list_value_; |
Scope* scope_value_; // Non-owning. |
+ std::map<std::string, Value> dict_value_; |
const ParseNode* origin_; |
}; |