Bridges-C++  3.4.4
Bridges(C++ API)
Element.h
Go to the documentation of this file.
1 #ifndef ELEMENT_H
2 
3 #define ELEMENT_H
4 
5 #include <unordered_set>
6 #include <unordered_map>
7 #include <cmath>
8 
9 using namespace std;
10 
11 #include "DataStructure.h"
12 #include "ElementVisualizer.h"
13 #include "LinkVisualizer.h"
14 #include <JSONutil.h>
15 
16 namespace bridges {
17  namespace datastructure {
18  // forward Declarations
19  template <typename K, typename E1, typename E2> class GraphAdjList;
20  template <typename K, typename E1, typename E2> class GraphAdjMatrix;
21 
51  template <typename E> class Element {
52  //Used for access to generateJSON() and for links manipulation
53  template <typename K, typename E1, typename E2> friend class GraphAdjList;
54  template <typename K, typename E1, typename E2> friend class GraphAdjMatrix;
55  template <typename K> friend class Array;
56  template <typename K> friend class Array1D;
57  template <typename K> friend class Array2D;
58  template <typename K> friend class Array3D;
59 
60  private:
61  bool debug() const {
62  return false;
63  }
68  static const unordered_map<const Shape, const string, hash<int>>& ShapeNames() {
69 
70  static std::unordered_map<const Shape, const string, hash<int>> sn = {
71  {CIRCLE, "circle"},
72  {SQUARE, "square"},
73  {DIAMOND, "diamond"},
74  {CROSS, "cross"},
75  {TRIANGLE, "triangle"},
76  {WYE, "wye"},
77  {STAR, "star"}
78  };
79  return sn;
80  }
81 
82  //this element's label
83  string label;
84  // appl. specific data stored with element
85  E value = E();
86  // this element's visualizer
87  ElementVisualizer* elvis;
88 
89  protected:
90 
91  // this element's collection of links
92  unordered_map<Element*, LinkVisualizer> links;
93 
94  public:
102  explicit Element(const E& val = E(), const string& lab = string()) :
103  label(lab), value(val) {
104  elvis = new ElementVisualizer;
105  }
106 
113  Element(const Element& e)
114  : label(e.label), value(e.value), elvis(new ElementVisualizer(*(e.elvis))), links(e.links) {
115  }
116 
117  Element& operator= (const Element& e) {
118  this->label = e.label;
119  this->value = e.value;
120  *(this->elvis) = *(e.elvis);
121  this->links = e.links;
122  return *this;
123  }
124 
125  E& operator= (E const& e) {
126  this->value = e;
127  return this->value;
128  }
129 
133  virtual ~Element() {
134  delete elvis; // removes the visualizer
135  }
136 
142  return elvis;
143  }
150  return elvis;
151  }
163  if (links.find(const_cast<Element*>(el)) != links.end()) {
164  return &(links.at(const_cast<Element*>(el)));
165  }
166  if (debug())
167  cerr << "Element " << label << " not linked to Element "
168  << el->getLabel() << ", returning NULL" << endl;
169  return nullptr;
170  }
171 
181  return const_cast<Element*> (this)->getLinkVisualizer(el);
182  }
183 
188  string const & getLabel() const {
189  return label;
190  }
191 
197  void setLabel(const string& lab) {
198  label = lab;
199  }
200 
207  E const & getValue() const {
208  return value;
209  }
210 
217  E & getValue() {
218  return value;
219  }
220 
226  void setValue(const E& val) {
227  value = val;
228  }
229 
230  protected:
235  virtual const string getElementRepresentation() const {
237  //write out ElementVisualizer properties
238 
239  // first check if location is set and needs to be included
240  string loc_str = "";
241  if ( (elvis->getLocationX() != INFINITY) &&
242  (elvis->getLocationY() != INFINITY) ) {
243  loc_str = QUOTE + "location" + QUOTE + COLON +
244  OPEN_BOX +
245  JSONencode(elvis->getLocationX()) + COMMA +
246  JSONencode(elvis->getLocationY()) +
247  CLOSE_BOX + COMMA;
248  }
249  return OPEN_CURLY +
250  QUOTE + "color" + QUOTE + COLON + elvis->getColor().getCSSRepresentation()
251  + COMMA +
252  loc_str +
253  QUOTE + "shape" + QUOTE + COLON + QUOTE +
254  ShapeNames().at(elvis->getShape()) + QUOTE + COMMA +
255  QUOTE + "size" + QUOTE + COLON +
256  JSONencode(elvis->getSize()) + COMMA +
257  QUOTE + "name" + QUOTE + COLON + JSONencode( label) +
258  CLOSE_CURLY;
259  }
270  static const string getLinkRepresentation(const LinkVisualizer& lv,
271  const string& src, const string& dest) {
273 
274  //write out LinkVisualizer properties
275  return OPEN_CURLY +
276  QUOTE + "color" + QUOTE + COLON + lv.getColor().getCSSRepresentation()
277  + COMMA +
278  (!lv.getLabel().empty() ?
279  (QUOTE + "label" + QUOTE + COLON +
280  JSONencode( lv.getLabel()) + COMMA) : "") +
281  QUOTE + "thickness" + QUOTE + COLON +
282  JSONencode(lv.getThickness()) + COMMA +
283  QUOTE + "source" + QUOTE + COLON + JSONencode(src) + COMMA +
284  QUOTE + "target" + QUOTE + COLON + JSONencode(dest) +
285  CLOSE_CURLY;
286  }
287  public:
293  void setSize(const double& sz) {
294  elvis->setSize(sz);
295  }
296 
302  double getSize() const {
303  return elvis->getSize();
304  }
310  void setColor(const Color& col) {
311  elvis->setColor(col);
312  }
319  void setColor(const string col) {
320  elvis->setColor(col);
321  }
322 
328  Color getColor() const {
329  return elvis->getColor();
330  }
331 
337  void setOpacity(double opacity) {
338  elvis->setOpacity(opacity);
339  }
340 
346  double getOpacity() {
347  return elvis->getOpacity();
348  }
356  void setShape(const Shape& shp) {
357  elvis->setShape(shp);
358  }
366  Shape getShape() const {
367  return elvis->getShape();
368  }
375  void setLocation(const double& locX, const double& locY) {
376  elvis->setLocation(locX, locY);
377  }
378 
383  double getLocationX() const {
384  return elvis->getLocationX();
385  }
390  double getLocationY() const {
391  return elvis->getLocationY();
392  }
393  }; //end of Element class
394 
395  }
396 }//end of bridges namespace
397 
398 #endif
A BRIDGES 1D array type.
Definition: Array1D.h:53
A BRIDGES array type.
Definition: Array2D.h:42
A BRIDGES array type.
Definition: Array3D.h:42
The foundation of BRIDGES array types. It is not meant to be used directly by students.
Definition: Array.h:21
This class represents Color, and supports rgba, hexadecimal and named color values.
Definition: Color.h:50
const string getCSSRepresentation() const
Definition: Color.h:403
This is the fundamental building block for all data structures in BRIDGES.
Definition: Element.h:51
static const string getLinkRepresentation(const LinkVisualizer &lv, const string &src, const string &dest)
Definition: Element.h:270
void setColor(const Color &col)
Set the color of the Element.
Definition: Element.h:310
void setShape(const Shape &shp)
Set the shape of the element.
Definition: Element.h:356
Element(const E &val=E(), const string &lab=string())
Definition: Element.h:102
void setValue(const E &val)
Sets generic object to "val".
Definition: Element.h:226
virtual const string getElementRepresentation() const
Gets the JSON string of the element representation.
Definition: Element.h:235
void setSize(const double &sz)
Sets size of the element.
Definition: Element.h:293
void setColor(const string col)
Set the color by name.
Definition: Element.h:319
void setLabel(const string &lab)
Sets label of this element.
Definition: Element.h:197
string const & getLabel() const
Gets the label of this element.
Definition: Element.h:188
double getLocationX() const
Gets the X coordinate of the location.
Definition: Element.h:383
LinkVisualizer * getLinkVisualizer(const Element *el)
Returns the LinkVisualizer of element.
Definition: Element.h:162
LinkVisualizer * getLinkVisualizer(const Element *el) const
Returns the LinkVisualizer of element.
Definition: Element.h:180
double getLocationY() const
Gets the Y coordinate of the location.
Definition: Element.h:390
const ElementVisualizer * getVisualizer() const
Get the element visualizer object - constant version.
Definition: Element.h:149
Element(const Element &e)
Definition: Element.h:113
double getSize() const
Get element size.
Definition: Element.h:302
E & getValue()
Gets the object held in the generic object E.
Definition: Element.h:217
unordered_map< Element *, LinkVisualizer > links
Definition: Element.h:92
void setOpacity(double opacity)
Set opacity of element - use the 4th color component.
Definition: Element.h:337
Shape getShape() const
Returns the shape of the element.
Definition: Element.h:366
double getOpacity()
Definition: Element.h:346
E const & getValue() const
Gets the object held in the generic object E.
Definition: Element.h:207
ElementVisualizer * getVisualizer()
Get the element visualizer object.
Definition: Element.h:141
virtual ~Element()
Definition: Element.h:133
Color getColor() const
Get the current color of the element.
Definition: Element.h:328
void setLocation(const double &locX, const double &locY)
Sets the location attributes of an element.
Definition: Element.h:375
This class maintains the visual properties of the a Bridges element.
Definition: ElementVisualizer.h:31
This class provides methods to represent adjacency list based graphs.
Definition: GraphAdjList.h:110
This class provides methods to represent adjacency matrix based graphs.
Definition: GraphAdjMatrix.h:37
std::string JSONencode(const T &d)
Definition: JSONutil.h:36
Shape
Definition: ElementVisualizer.h:10
@ DIAMOND
Definition: ElementVisualizer.h:10
@ CIRCLE
Definition: ElementVisualizer.h:10
@ SQUARE
Definition: ElementVisualizer.h:10
@ STAR
Definition: ElementVisualizer.h:10
@ WYE
Definition: ElementVisualizer.h:10
@ TRIANGLE
Definition: ElementVisualizer.h:10
@ CROSS
Definition: ElementVisualizer.h:10
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
const string COLON
Definition: DataStructure.h:51
const string OPEN_BOX
Definition: DataStructure.h:54
const string COMMA
Definition: DataStructure.h:50
const string OPEN_CURLY
Definition: DataStructure.h:52
const string CLOSE_BOX
Definition: DataStructure.h:55
const string CLOSE_CURLY
Definition: DataStructure.h:53
const string QUOTE
Definition: DataStructure.h:49