Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(285)

Unified Diff: tools/gn/label_ptr.h

Issue 48523006: GN: Separately track labels and origins for lists of stuff (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gn/gyp_binary_target_writer.cc ('k') | tools/gn/ninja_binary_target_writer.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gn/label_ptr.h
diff --git a/tools/gn/label_ptr.h b/tools/gn/label_ptr.h
new file mode 100644
index 0000000000000000000000000000000000000000..cf08bf01e0c1a8a4397be6e852e52508eadb38de
--- /dev/null
+++ b/tools/gn/label_ptr.h
@@ -0,0 +1,80 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef TOOLS_GN_LABEL_PTR_H_
+#define TOOLS_GN_LABEL_PTR_H_
+
+#include <functional>
+
+class ParseNode;
+
+// Structure that holds a labeled "thing". This is used for various places
+// where we need to store lists of targets or configs. We sometimes populate
+// the pointers on another thread from where we compute the labels, so this
+// structure lets us save them separately. This also allows us to store the
+// location of the thing that added this dependency.
+template<typename T>
+struct LabelPtrPair {
+ typedef T DestType;
+
+ LabelPtrPair() : label(), ptr(NULL), origin(NULL) {}
+
+ // This contructor is typically used in unit tests, it extracts the label
+ // automatically from a given pointer.
+ explicit LabelPtrPair(const T* p) : label(p->label()), ptr(p), origin(NULL) {
+ }
+
+ ~LabelPtrPair() {}
+
+ Label label;
+ const T* ptr; // May be NULL.
+ const ParseNode* origin; // May be NULL.
+};
+
+typedef LabelPtrPair<Config> LabelConfigPair;
+typedef LabelPtrPair<Target> LabelTargetPair;
+
+typedef std::vector<LabelConfigPair> LabelConfigVector;
+typedef std::vector<LabelTargetPair> LabelTargetVector;
+
+// Comparison and search functions ---------------------------------------------
+
+// To do a brute-force search by label:
+// std::find_if(vect.begin(), vect.end(), LabelPtrLabelEquals<Config>(label));
+template<typename T>
+struct LabelPtrLabelEquals : public std::unary_function<Label, bool> {
+ explicit LabelPtrLabelEquals(const Label& l) : label(l) {}
+
+ bool operator()(const LabelPtrPair<T>& arg) const {
+ return arg.label == label;
+ }
+
+ const Label& label;
+};
+
+// To do a brute-force search by object pointer:
+// std::find_if(vect.begin(), vect.end(), LabelPtrLabelEquals<Config>(config));
+template<typename T>
+struct LabelPtrPtrEquals : public std::unary_function<T, bool> {
+ explicit LabelPtrPtrEquals(const T* p) : ptr(p) {}
+
+ bool operator()(const LabelPtrPair<T>& arg) const {
+ return arg.ptr == ptr;
+ }
+
+ const T* ptr;
+};
+
+// To sort by label:
+// std::sort(vect.begin(), vect.end(), LabelPtrLabelLess<Config>());
+template<typename T>
+struct LabelPtrLabelLess : public std::binary_function<LabelPtrPair<T>,
+ LabelPtrPair<T>,
+ bool> {
+ bool operator()(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) const {
+ return a.label < b.label;
+ }
+};
+
+#endif // TOOLS_GN_LABEL_PTR_H_
« no previous file with comments | « tools/gn/gyp_binary_target_writer.cc ('k') | tools/gn/ninja_binary_target_writer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698