MCPcopy Create free account
hub / github.com/docopt/docopt.cpp / BranchPattern

Class BranchPattern

docopt_private.h:140–216  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

138 };
139
140 class BranchPattern
141 : public Pattern {
142 public:
143 BranchPattern(PatternList children = {})
144 : fChildren(std::move(children))
145 {}
146
147 Pattern& fix() {
148 UniquePatternSet patterns;
149 fix_identities(patterns);
150 fix_repeating_arguments();
151 return *this;
152 }
153
154 virtual std::string const& name() const override {
155 throw std::runtime_error("Logic error: name() shouldnt be called on a BranchPattern");
156 }
157
158 virtual value const& getValue() const {
159 throw std::runtime_error("Logic error: name() shouldnt be called on a BranchPattern");
160 }
161
162 virtual std::vector<Pattern*> flat(bool (*filter)(Pattern const*)) override {
163 if (filter(this)) {
164 return {this};
165 }
166
167 std::vector<Pattern*> ret;
168 for(auto& child : fChildren) {
169 auto sublist = child->flat(filter);
170 ret.insert(ret.end(), sublist.begin(), sublist.end());
171 }
172 return ret;
173 }
174
175 virtual void collect_leaves(std::vector<LeafPattern*>& lst) override final {
176 for(auto& child : fChildren) {
177 child->collect_leaves(lst);
178 }
179 }
180
181 void setChildren(PatternList children) {
182 fChildren = std::move(children);
183 }
184
185 PatternList const& children() const { return fChildren; }
186
187 virtual void fix_identities(UniquePatternSet& patterns) {
188 for(auto& child : fChildren) {
189 // this will fix up all its children, if needed
190 if (auto bp = dynamic_cast<BranchPattern*>(child.get())) {
191 bp->fix_identities(patterns);
192 }
193
194 // then we try to add it to the list
195 auto inserted = patterns.insert(child);
196 if (!inserted.second) {
197 // already there? then reuse the existing shared_ptr for that thing

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected