Board  0.9.5
ShapeVisitor.h
Go to the documentation of this file.
1 /* -*- mode: c++ -*- */
26 #ifndef BOARD_SHAPE_VISITOR_H
27 #define BOARD_SHAPE_VISITOR_H
28 
29 #include <board/Color.h>
30 #include <board/Style.h>
31 #include <cstddef> // For std::size_t
32 #include <functional>
33 #include <set>
34 
35 namespace LibBoard
36 {
37 
38 struct Shape;
39 struct ShapeWithStyle;
40 struct ShapeList;
41 struct Board;
42 struct Dot;
43 struct Line;
44 struct Arrow;
45 struct Polyline;
46 struct Ellipse;
47 struct Text;
48 struct Bezier;
49 
53 struct ShapeVisitor {
54  virtual void visit(Shape & shape) = 0;
55  virtual void visit(Shape & shape) const = 0;
56  virtual void visit(ShapeWithStyle & shape);
57  virtual void visit(ShapeWithStyle & shape) const;
58  virtual void visit(Dot &);
59  virtual void visit(Dot &) const;
60  virtual void visit(Line &);
61  virtual void visit(Line &) const;
62  virtual void visit(Arrow &);
63  virtual void visit(Arrow & arrow) const;
64  virtual void visit(Polyline &);
65  virtual void visit(Polyline &) const;
66  virtual void visit(Ellipse &);
67  virtual void visit(Ellipse &) const;
68  virtual void visit(Text &);
69  virtual void visit(Text &) const;
70  virtual void visit(Bezier &);
71  virtual void visit(Bezier &) const;
72  virtual ~ShapeVisitor();
73 };
74 
79  virtual void visit(const Shape & shape) = 0;
80  virtual void visit(const Shape & shape) const = 0;
81  virtual void visit(const ShapeWithStyle & shape);
82  virtual void visit(const ShapeWithStyle & shape) const;
83  virtual void visit(const Dot &);
84  virtual void visit(const Dot &) const;
85  virtual void visit(const Line &);
86  virtual void visit(const Line &) const;
87  virtual void visit(const Arrow &);
88  virtual void visit(const Arrow & arrow) const;
89  virtual void visit(const Polyline &);
90  virtual void visit(const Polyline &) const;
91  virtual void visit(const Ellipse &);
92  virtual void visit(const Ellipse &) const;
93  virtual void visit(const Text &);
94  virtual void visit(const Text &) const;
95  virtual void visit(const Bezier &);
96  virtual void visit(const Bezier &) const;
97  virtual ~ConstShapeVisitor();
98 };
99 
104  virtual Shape * map(const Shape &) = 0;
105  virtual Shape * map(const Shape &) const = 0;
106  virtual Shape * map(const Dot &);
107  virtual Shape * map(const Dot &) const;
108  virtual Shape * map(const Line &);
109  virtual Shape * map(const Line &) const;
110  virtual Shape * map(const Arrow &);
111  virtual Shape * map(const Arrow &) const;
112  virtual Shape * map(const Polyline &);
113  virtual Shape * map(const Polyline &) const;
114  virtual Shape * map(const Ellipse &);
115  virtual Shape * map(const Ellipse &) const;
116  virtual Shape * map(const Text &);
117  virtual Shape * map(const Text &) const;
118  virtual Shape * map(const Bezier &);
119  virtual Shape * map(const Bezier &) const;
120  virtual ~CompositeShapeTransform();
121 };
122 
129  void visit(const Shape &) override;
130  void visit(const Shape &) const override;
131  const ShapeList & shapeList() const;
132 
133 private:
134  ShapeList & _shapeList;
135 };
136 
142  void visit(const Shape &) override;
143  void visit(const Shape &) const override;
144 };
145 
151  ShapeCounter();
152  void clear();
153  std::size_t value() const;
154  void visit(const Shape &) override;
155  void visit(const Shape &) const override;
156 
157 private:
158  std::size_t _count;
159 };
160 
167  void clear();
168  void visit(const Shape &) override;
169  void visit(const Shape &) const override;
170  void visit(const Dot & d) override;
171  void visit(const Line &) override;
172  void visit(const Arrow &) override;
173  void visit(const Polyline &) override;
174  void visit(const Ellipse &) override;
175  void visit(const Text &) override;
176  void visit(const Bezier &) override;
177  const std::set<Color> & colors() const;
178 
179 private:
180  std::set<Color> _colors;
181 };
182 
188  void clear();
189  void visit(const Shape &) override;
190  void visit(const Shape &) const override;
191  void visit(const Dot &) override;
192  void visit(const Line &) override;
193  void visit(const Arrow &) override;
194  void visit(const Polyline &) override;
195  void visit(const Ellipse &) override;
196  void visit(const Text &) override;
197  void visit(const Bezier &) override;
198  const std::set<Color> & colors() const;
199 
200 private:
201  std::set<Color> _colors;
202 };
203 
208  typedef std::function<void(const Shape &)> Function;
209  ConstLeafVisitor(Function f);
210  void visit(const Shape &) override;
211  void visit(const Shape &) const override;
212  void visit(const Dot &) override;
213  void visit(const Line &) override;
214  void visit(const Arrow &) override;
215  void visit(const Polyline &) override;
216  void visit(const Ellipse &) override;
217  void visit(const Text &) override;
218  void visit(const Bezier &) override;
219 
220 private:
221  Function _f;
222 };
223 
227 struct LeafVisitor : public ShapeVisitor {
228  typedef std::function<void(Shape &)> Function;
229  LeafVisitor(Function f);
230  void visit(Shape &) override;
231  void visit(Shape &) const override;
232  void visit(Dot &) override;
233  void visit(Line &) override;
234  void visit(Arrow &) override;
235  void visit(Polyline &) override;
236  void visit(Ellipse &) override;
237  void visit(Text &) override;
238  void visit(Bezier &) override;
239 
240 private:
241  Function _f;
242 };
243 
248  enum ColorSpecification
249  {
250  PenColor,
251  FillColor
252  };
253  typedef std::function<void(ShapeWithStyle &)> Function;
254  ShapeWithStyleVisitor(Function f);
256  ShapeWithStyleVisitor(Color color, ColorSpecification spec);
257  ShapeWithStyleVisitor(Color penColor, Color fillColor);
258  ShapeWithStyleVisitor(double lineWidth);
259  ShapeWithStyleVisitor(LineStyle lineStyle);
260  ShapeWithStyleVisitor(LineCap lineCap);
261  ShapeWithStyleVisitor(LineJoin lineJoin);
262  void visit(Shape &) override;
263  void visit(Shape &) const override;
264  void visit(ShapeWithStyle &) override;
265  void visit(ShapeWithStyle &) const override;
266  void visit(Dot &) override;
267  void visit(Dot &) const override;
268  void visit(Line &) override;
269  void visit(Line &) const override;
270  void visit(Arrow &) override;
271  void visit(Arrow &) const override;
272  void visit(Polyline &) override;
273  void visit(Polyline &) const override;
274  void visit(Ellipse &) override;
275  void visit(Ellipse &) const override;
276  void visit(Text &) override;
277  void visit(Text &) const override;
278  void visit(Bezier &) override;
279  void visit(Bezier &) const override;
280 
281 private:
282  Function _f;
283 };
284 
285 } // namespace LibBoard
286 
287 #endif /* BOARD_SHAPE_VISITOR_H */
LibBoard::ConstLeafVisitor
Leaf visitor may be used to apply a function on each leaf, in back-to-front order.
Definition: ShapeVisitor.h:207
Style.h
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
LibBoard::Style
Definition: Style.h:69
LibBoard::BoundingBoxViewer
The BoundingBoxViewer struct may be used to display the bounding boxes of the shapes in a composite t...
Definition: ShapeVisitor.h:141
LibBoard::Polyline
A polygonal line described by a series of 2D points.
Definition: Polyline.h:38
LibBoard::Arrow
A line between two points with an arrow at one extremity.
Definition: Arrow.h:38
LibBoard::PenColorExtractor
The pen-color extractor may be used to retrieve all pen colors.
Definition: ShapeVisitor.h:165
LibBoard::LeafVisitor
Leaf visitor may be used to apply a function on each leaf, in back-to-front order.
Definition: ShapeVisitor.h:227
LibBoard::CompositeShapeTransform
A CompositeShapeTransform may be used to duplicate/transform a composite shape tree.
Definition: ShapeVisitor.h:103
LibBoard::Dot
A line between two points.
Definition: Dot.h:42
LibBoard::BoundingBoxExtractor
The BoundingBoxExtractor struct may be used to compute the bounding box of a composite shape tree.
Definition: ShapeVisitor.h:127
LibBoard::Text
A piece of text.
Definition: Text.h:40
LibBoard::ShapeCounter
The ShapeCounter struct may be used to count shapes in the composite shapes tree.
Definition: ShapeVisitor.h:150
LibBoard::Line
A line between two points.
Definition: Line.h:38
LibBoard::Bezier
A cubice polygonal line described by a series of 2D points.
Definition: Bezier.h:40
LibBoard::Shape
Abstract structure for a 2D shape.
Definition: Shape.h:63
LibBoard::FillColorExtractor
The fill-color extractor may be used to retrieve all fill colors.
Definition: ShapeVisitor.h:186
LibBoard::ConstShapeVisitor
A ConstShapeVisitor may visit const shapes of a composite shape tree in back-to-front order.
Definition: ShapeVisitor.h:78
LibBoard::Ellipse
An ellipse.
Definition: Ellipse.h:38
LibBoard::ShapeList
A group of shapes.
Definition: ShapeList.h:46
Color.h
The Color structure. @copyright This source code is part of the Board project, a C++ library whose pu...
LibBoard::ShapeWithStyleVisitor
Leaf visitor may be used to apply a function on each Shape with style, in back-to-front order.
Definition: ShapeVisitor.h:247
LibBoard::ShapeVisitor
A ShapeVisitor visits all shapes in a composite shape tree in back-to-front order.
Definition: ShapeVisitor.h:53
LibBoard::Color
Structure representing an RGB triple.
Definition: Color.h:43
LibBoard::ShapeWithStyle
Abstract structure for a 2D shape.
Definition: ShapeWithStyle.h:38