Board 0.9.6
ShapeList.h
Go to the documentation of this file.
1/* -*- mode: c++ -*- */
27#ifndef BOARD_SHAPELIST_H
28#define BOARD_SHAPELIST_H
29
30#include <board/Exception.h>
31#include <board/Shape.h>
32#include <board/Tools.h>
33#include <queue>
34#include <stack>
35#include <typeinfo>
36#include <vector>
37
38namespace LibBoard
39{
40
41struct Group;
42
47struct ShapeList : public Shape {
48
49 typedef std::vector<Shape *>::size_type size_type;
50
51 inline ShapeList();
52
53 ShapeList(const ShapeList & other);
54
55 ShapeList & operator=(const ShapeList & other);
56
57 ShapeList(ShapeList && other);
58
59 ShapeList & operator=(ShapeList && other);
60
69 ShapeList(const Shape & shape, unsigned int times, double dx, double dy, double scale);
70
81 ShapeList(const Shape & shape, unsigned int times, double dx, double dy, double scaleX, double scaleY, double angle);
82
87 template <typename S> ShapeList(const std::vector<S> & shapes);
88
89 ~ShapeList() override;
90
96 const std::string & name() const override;
97
102 ShapeList & clear();
103
104 ShapeList & rotate(double angle, const Point & center) override;
105
112 ShapeList rotated(double angle, const Point & center);
113
119 ShapeList & rotate(double angle) override;
120
126 ShapeList rotated(double angle);
127
134 ShapeList & translate(double dx, double dy) override;
135
142 ShapeList translated(double dx, double dy);
143
150 ShapeList & scale(double sx, double sy) override;
151
157 ShapeList & scale(double s) override;
158
165 ShapeList scaled(double sx, double sy) const;
166
172 ShapeList scaled(double s) const;
173
180 void scaleAll(double s) override;
181
182 void flushPostscript(std::ostream & stream, const TransformEPS & transform) const override;
183
184 void flushFIG(std::ostream & stream, const TransformFIG & transform, std::map<Color, int> & colormap) const override;
185
186 void flushSVG(std::ostream & stream, const TransformSVG & transform) const override;
187
188 void flushTikZ(std::ostream & stream, const TransformTikZ & transform) const override;
189
190 Rect boundingBox(LineWidthFlag) const override;
191
192 ShapeList * clone() const override;
193
200 ShapeList & push_back(Shape * shape);
201
211 ShapeList & operator<<(const Shape & shape);
212
216 template <typename S> //
217 ShapeList & operator<<(const std::vector<S> & shapes);
218
226 ShapeList & operator+=(const Shape & shape);
227
238 ShapeList & append(const Shape & shape, Direction direction = Direction::Right, Alignment alignment = Alignment::Center, double margin = 0.0, LineWidthFlag lineWidthFlag = UseLineWidth);
239
250 void addRepeated(const Shape & shape, unsigned int times, double dx, double dy, double scaleX = 1.0, double scaleY = 1.0, double angle = 0.0);
251
256 ShapeList & duplicateLast(std::size_t copies = 1);
257
264 template <typename T> T & last(const std::size_t position = 0);
265
272 Shape & last(const std::size_t position = 0);
273
279 template <typename T> T & topLevelFindLast(std::size_t position = 0);
280
285 Shape & top();
286
291 void accept(ShapeVisitor & visitor) override;
292
298 void accept(const ShapeVisitor & visitor) override;
299
305 void accept(ConstShapeVisitor & visitor) const override;
306
312 void accept(const ConstShapeVisitor & visitor) const override;
313
319 Shape * accept(CompositeShapeTransform & transform) const override;
320
326 Shape * accept(const CompositeShapeTransform & transform) const override;
327
334 inline TopLevelIterator(std::vector<Shape *>::iterator it);
335 inline Shape & operator*();
336 inline Shape * operator->();
337 inline Shape * pointer();
338 inline TopLevelIterator & operator++();
339 inline TopLevelIterator operator++(int);
340 inline bool operator==(const TopLevelIterator & other);
341 inline bool operator!=(const TopLevelIterator & other);
342
343 private:
344 std::vector<Shape *>::iterator _it;
345 };
346
353 inline TopLevelConstIterator(std::vector<Shape *>::const_iterator it);
354 inline const Shape & operator*();
355 inline const Shape * operator->();
356 inline const Shape * pointer();
359 inline bool operator==(const TopLevelConstIterator & other);
360 inline bool operator!=(const TopLevelConstIterator & other);
361
362 private:
363 std::vector<Shape *>::const_iterator _it;
364 };
365
371 inline DepthFirstIterator();
372 inline DepthFirstIterator(ShapeList * list);
373 inline Shape & operator*();
374 inline Shape * operator->();
375 inline Shape * pointer();
376 inline bool operator==(const DepthFirstIterator & other);
377 inline bool operator!=(const DepthFirstIterator & other);
379 inline DepthFirstIterator operator++(int);
380
381 private:
382 void moveToFirstActuelShape();
383 void moveToNextActualShape();
384 std::stack<ShapeList *> _shapeListsStack;
385 std::stack<TopLevelIterator> _iteratorsStack;
386 };
387
393 inline BreadthFirstIterator();
394 inline BreadthFirstIterator(ShapeList * list);
395 inline Shape & operator*();
396 inline Shape * operator->();
397 inline Shape * pointer();
398 inline bool operator==(const BreadthFirstIterator & other);
399 inline bool operator!=(const BreadthFirstIterator & other);
402
403 private:
404 void moveToFirstActuelShape();
405 void moveToNextActualShape();
406 std::queue<ShapeList *> _shapeListsQueue;
407 std::queue<TopLevelIterator> _iteratorsQueue;
408 };
409
414 inline TopLevelIterator begin();
415
420 inline TopLevelIterator end();
421
426 inline TopLevelConstIterator begin() const;
427
432 inline TopLevelConstIterator cbegin() const;
433
438 inline TopLevelConstIterator end() const;
439
444 inline TopLevelConstIterator cend() const;
445
451
457
463
469
474 std::size_t deepSize() const;
475
480 inline std::size_t size() const;
481
482private:
483 static const std::string _name;
485protected:
486 void addShape(const Shape & shape, double scaleFactor);
487
488 std::vector<Shape *> _shapes;
493 void deleteShapes();
494};
495
496// Inline methods and functions
497
498#if defined(max)
499#undef max
500#define HAS_MSVC_MAX true
501#endif
502
504
505template <typename T> T & ShapeList::last(const std::size_t position)
506{
507 if (position < _shapes.size()) {
508 std::vector<Shape *>::reverse_iterator it = _shapes.rbegin() + static_cast<std::vector<Shape *>::difference_type>(position);
509 Shape * pshape = *it;
510 T * result = dynamic_cast<T *>(pshape);
511 if (!result) {
512 std::cerr << "Error: ShapeList::last<> called with invalid result type\n";
513 exit(-1);
514 }
515 return dynamic_cast<T &>(*result);
516 } else {
517 Tools::error << "Trying to access an element that does not exist (" << position << "/" << _shapes.size() << ").\n";
518 throw -1;
519 }
520}
521
522template <typename T> T & ShapeList::topLevelFindLast(std::size_t position)
523{
524 std::vector<Shape *>::reverse_iterator it = _shapes.rbegin();
525 while (it != _shapes.rend()) {
526 T * shape = dynamic_cast<T *>(*it);
527 if (shape) {
528 if (!position) {
529 return *shape;
530 } else {
531 --position;
532 }
533 }
534 ++it;
535 }
536 throw Exception("topLevelFindLast<T>(): no such shape type found (" + std::string(typeid(T).name()) + ")");
537}
538
539ShapeList::TopLevelIterator::TopLevelIterator(std::vector<Shape *>::iterator it) : _it(it) {}
540
542{
543 return **_it;
544}
545
547{
548 return *_it;
549}
550
552{
553 return *_it;
554}
555
557{
558 ++_it;
559 return *this;
560}
561
563{
564 TopLevelIterator previous(*this);
565 ++_it;
566 return previous;
567}
568
570{
571 return _it == other._it;
572}
573
575{
576 return _it != other._it;
577}
578
579ShapeList::TopLevelConstIterator::TopLevelConstIterator(std::vector<Shape *>::const_iterator it) : _it(it) {}
580
582{
583 return **_it;
584}
585
587{
588 return *_it;
589}
590
592{
593 return *_it;
594}
595
597{
598 ++_it;
599 return *this;
600}
601
603{
604 TopLevelConstIterator previous(*this);
605 ++_it;
606 return previous;
607}
608
610{
611 return _it == other._it;
612}
613
615{
616 return _it != other._it;
617}
618
620{
621 return TopLevelIterator(_shapes.begin());
622}
623
625{
626 return TopLevelIterator(_shapes.end());
627}
628
630{
631 return TopLevelConstIterator(_shapes.begin());
632}
633
635{
636 return TopLevelConstIterator(_shapes.begin());
637}
638
640{
641 return TopLevelConstIterator(_shapes.end());
642}
643
645{
646 return TopLevelConstIterator(_shapes.end());
647}
648
650{
651 return DepthFirstIterator(this);
652}
653
655{
656 return DepthFirstIterator();
657}
658
660{
661 return BreadthFirstIterator(this);
662}
663
665{
666 return BreadthFirstIterator();
667}
668
669std::size_t ShapeList::size() const
670{
671 return _shapes.size();
672}
673
675
677{
678 _shapeListsStack.push(list);
679 _iteratorsStack.push(list->begin());
680 moveToFirstActuelShape();
681}
682
684{
685 return *(_iteratorsStack.top());
686}
687
689{
690 return pointer();
691}
692
694{
695 return _iteratorsStack.top().pointer();
696}
697
699{
700 return (_shapeListsStack.empty() && other._shapeListsStack.empty()) ||
701 ((!_shapeListsStack.empty() && !other._shapeListsStack.empty()) && (_shapeListsStack.top() == other._shapeListsStack.top()) && (_iteratorsStack.top() == other._iteratorsStack.top()));
702}
703
705{
706 return !(*this == other);
707}
708
710{
711 moveToNextActualShape();
712 return *this;
713}
714
716{
717 DepthFirstIterator currentValue(*this);
718 moveToNextActualShape();
719 return currentValue;
720}
721
723
725{
726 _shapeListsQueue.push(list);
727 _iteratorsQueue.push(list->begin());
728 moveToFirstActuelShape();
729}
730
732{
733 return *(_iteratorsQueue.front());
734}
735
737{
738 return pointer();
739}
740
742{
743 return _iteratorsQueue.front().pointer();
744}
745
747{
748 return (_shapeListsQueue.empty() && other._shapeListsQueue.empty()) ||
749 ((!_shapeListsQueue.empty() && !other._shapeListsQueue.empty()) && (_shapeListsQueue.front() == other._shapeListsQueue.front()) && (_iteratorsQueue.front() == other._iteratorsQueue.front()));
750}
751
753{
754 return !(*this == other);
755}
756
758{
759 moveToNextActualShape();
760 return *this;
761}
762
764{
765 BreadthFirstIterator currentValue(*this);
766 moveToNextActualShape();
767 return currentValue;
768}
769
770template <typename S> //
771ShapeList::ShapeList(const std::vector<S> & shapes) //
772 : ShapeList()
773{
774 (*this) << shapes;
775}
776
777template <typename S> //
778ShapeList & ShapeList::operator<<(const std::vector<S> & shapes) //
779{
780 for (const S & shape : shapes) {
781 (*this) << shape;
782 }
783 return *this;
784}
785
786#if defined(HAS_MSVC_MAX)
787#define max(A, B) ((A) > (B) ? (A) : (B))
788#endif
789
790} // namespace LibBoard
791
792#endif /* BOARD_SHAPELIST_H */
The Exception type. @copyright This source code is part of the Board project, a C++ library whose pur...
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
Definition Exception.h:36
MessageStream error
Definition Board.h:55
Direction
The Direction enum.
Definition Globals.h:36
Alignment
The Alignment enum.
Definition Globals.h:47
@ Center
Definition Globals.h:50
LineWidthFlag
Definition Style.h:57
@ UseLineWidth
Definition Style.h:59
A CompositeShapeTransform may be used to duplicate/transform a composite shape tree.
Definition ShapeVisitor.h:104
A ConstShapeVisitor may visit const shapes of a composite shape tree in back-to-front order.
Definition ShapeVisitor.h:79
Struct representing a 2D point.
Definition Point.h:42
Struct representing a rectangle on the plane.
Definition Rect.h:40
The BreadthFirstIterator struct allows to traverse the shape tree using a breadth-first strategy.
Definition ShapeList.h:392
BreadthFirstIterator & operator++()
Definition ShapeList.h:757
Shape * pointer()
Definition ShapeList.h:741
Shape & operator*()
Definition ShapeList.h:731
Shape * operator->()
Definition ShapeList.h:736
bool operator==(const BreadthFirstIterator &other)
Definition ShapeList.h:746
bool operator!=(const BreadthFirstIterator &other)
Definition ShapeList.h:752
BreadthFirstIterator()
Definition ShapeList.h:722
The DepthFirstIterator struct allows to traverse the shape tree using a depth-first strategy.
Definition ShapeList.h:370
Shape & operator*()
Definition ShapeList.h:683
Shape * operator->()
Definition ShapeList.h:688
bool operator!=(const DepthFirstIterator &other)
Definition ShapeList.h:704
DepthFirstIterator & operator++()
Definition ShapeList.h:709
bool operator==(const DepthFirstIterator &other)
Definition ShapeList.h:698
Shape * pointer()
Definition ShapeList.h:693
DepthFirstIterator()
Definition ShapeList.h:674
The TopLevelConstIterator struct.
Definition ShapeList.h:352
TopLevelConstIterator(std::vector< Shape * >::const_iterator it)
Definition ShapeList.h:579
TopLevelConstIterator & operator++()
Definition ShapeList.h:596
const Shape & operator*()
Definition ShapeList.h:581
const Shape * pointer()
Definition ShapeList.h:591
bool operator!=(const TopLevelConstIterator &other)
Definition ShapeList.h:614
const Shape * operator->()
Definition ShapeList.h:586
bool operator==(const TopLevelConstIterator &other)
Definition ShapeList.h:609
The TopLevelIterator struct.
Definition ShapeList.h:333
TopLevelIterator(std::vector< Shape * >::iterator it)
Definition ShapeList.h:539
TopLevelIterator & operator++()
Definition ShapeList.h:556
bool operator!=(const TopLevelIterator &other)
Definition ShapeList.h:574
Shape & operator*()
Definition ShapeList.h:541
Shape * operator->()
Definition ShapeList.h:546
bool operator==(const TopLevelIterator &other)
Definition ShapeList.h:569
Shape * pointer()
Definition ShapeList.h:551
A group of shapes.
Definition ShapeList.h:47
TopLevelConstIterator cbegin() const
cbegin
Definition ShapeList.h:634
void scaleAll(double s) override
Definition ShapeList.cpp:362
void addShape(const Shape &shape, double scaleFactor)
Definition ShapeList.cpp:164
ShapeList & push_back(Shape *shape)
Definition ShapeList.cpp:427
~ShapeList() override
Definition ShapeList.cpp:85
BreadthFirstIterator breadthFirstBegin()
breadthFirstBegin
Definition ShapeList.h:659
ShapeList & duplicateLast(std::size_t copies=1)
Definition ShapeList.cpp:185
BreadthFirstIterator breadthFirstEnd()
breadthFirstEnd
Definition ShapeList.h:664
Rect boundingBox(LineWidthFlag) const override
Definition ShapeList.cpp:405
void addRepeated(const Shape &shape, unsigned int times, double dx, double dy, double scaleX=1.0, double scaleY=1.0, double angle=0.0)
Definition ShapeList.cpp:214
Shape & top()
Definition ShapeList.cpp:438
TopLevelIterator begin()
begin
Definition ShapeList.h:619
const std::string & name() const override
Definition ShapeList.cpp:49
ShapeList scaled(double sx, double sy) const
Definition ShapeList.cpp:352
TopLevelConstIterator cend() const
cend
Definition ShapeList.h:644
TopLevelIterator end()
end
Definition ShapeList.h:624
DepthFirstIterator depthFirstBegin()
depthFirstBegin
Definition ShapeList.h:649
void flushTikZ(std::ostream &stream, const TransformTikZ &transform) const override
Definition ShapeList.cpp:396
ShapeList & rotate(double angle, const Point &center) override
Definition ShapeList.cpp:287
void flushFIG(std::ostream &stream, const TransformFIG &transform, std::map< Color, int > &colormap) const override
Definition ShapeList.cpp:380
ShapeList & append(const Shape &shape, Direction direction=Direction::Right, Alignment alignment=Alignment::Center, double margin=0.0, LineWidthFlag lineWidthFlag=UseLineWidth)
Definition ShapeList.cpp:232
std::size_t size() const
The number of shapes in the list (at top level).
Definition ShapeList.h:669
void flushPostscript(std::ostream &stream, const TransformEPS &transform) const override
Definition ShapeList.cpp:371
ShapeList()
Definition ShapeList.h:503
std::size_t deepSize() const
Recursively counts the number of shapes in the list.
Definition ShapeList.cpp:501
ShapeList translated(double dx, double dy)
Definition ShapeList.cpp:325
DepthFirstIterator depthFirstEnd()
depthFirstEnd
Definition ShapeList.h:654
ShapeList rotated(double angle, const Point &center)
Definition ShapeList.cpp:298
std::vector< Shape * >::size_type size_type
Definition ShapeList.h:49
ShapeList & scale(double sx, double sy) override
Definition ShapeList.cpp:330
ShapeList & operator<<(const Shape &shape)
Definition ShapeList.cpp:150
ShapeList & operator+=(const Shape &shape)
Definition ShapeList.cpp:197
T & topLevelFindLast(std::size_t position=0)
Definition ShapeList.h:522
void flushSVG(std::ostream &stream, const TransformSVG &transform) const override
Definition ShapeList.cpp:387
ShapeList & operator=(const ShapeList &other)
Definition ShapeList.cpp:120
ShapeList * clone() const override
Definition ShapeList.cpp:422
ShapeList & translate(double dx, double dy) override
Definition ShapeList.cpp:314
void deleteShapes()
Definition ShapeList.cpp:97
void accept(ShapeVisitor &visitor) override
Accepts a visitor object.
Definition ShapeList.cpp:443
std::vector< Shape * > _shapes
Definition ShapeList.h:488
ShapeList & clear()
Definition ShapeList.cpp:90
T & last(const std::size_t position=0)
Definition ShapeList.h:505
A ShapeVisitor visits all shapes in a composite shape tree in back-to-front order.
Definition ShapeVisitor.h:54
Abstract structure for a 2D shape.
Definition Shape.h:64
virtual Point center(LineWidthFlag lineWidthFlag=IgnoreLineWidth) const
Definition Shape.cpp:59
Structure representing a scaling and translation suitable for an EPS output.
Definition Transforms.h:71
Structure representing a scaling and translation suitable for an XFig output.
Definition Transforms.h:87
Structure representing a scaling and translation suitable for an SVG output.
Definition Transforms.h:109
Structure representing a scaling and translation suitable for an TikZ output.
Definition Transforms.h:126