Evoplex  0.2.1
edge_p.h
1 /* Evoplex <https://evoplex.org>
2  * Copyright (C) 2016-present - Marcos Cardinot <marcos@cardinot.net>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef EDGE_P_H
18 #define EDGE_P_H
19 
20 #include <memory>
21 #include <unordered_map>
22 
23 #include "attributes.h"
24 
25 namespace evoplex {
26 
27 class Node;
28 class BaseEdge;
29 using EdgePtr = std::shared_ptr<BaseEdge>;
30 
35 class BaseEdge
36 {
37  friend class AbstractGraph;
38  friend class TestEdge;
39 
40 private:
41  struct constructor_key { /* this is a private key accessible only to friends */ };
42 
43 public:
44  explicit BaseEdge(const constructor_key&, int id, const Node& origin,
45  const Node& neighbour, Attributes* attrs=new Attributes(), bool ownsAttrs=true);
46 
47  ~BaseEdge();
48 
52  inline const Attributes* attrs() const;
54  inline const Value& attr(int id) const;
56  inline Value attr(const QString& name, Value defaultValue=Value()) const;
58  inline void setAttr(int id, const Value& value);
60  inline void addAttr(QString name, Value value);
61 
65  inline int id() const;
69  inline const Node& origin() const;
73  inline const Node& neighbour() const;
74 
75 private:
76  const int m_id;
77  const Node& m_origin;
78  const Node& m_neighbour;
79  Attributes* m_attrs;
80  const bool m_ownsAttrs;
81 };
82 
83 /************************************************************************
84  BaseEdge: Inline member functions
85  ************************************************************************/
86 
87 inline int BaseEdge::id() const
88 { return m_id; }
89 
90 inline const Node& BaseEdge::origin() const
91 { return m_origin; }
92 
93 inline const Node& BaseEdge::neighbour() const
94 { return m_neighbour; }
95 
96 inline const Attributes* BaseEdge::attrs() const
97 { return m_attrs; }
98 
99 inline const Value& BaseEdge::attr(int id) const
100 { return m_attrs->value(id); }
101 
102 inline Value BaseEdge::attr(const QString& name, Value defaultValue) const
103 { return m_attrs->value(name, defaultValue); }
104 
105 inline void BaseEdge::setAttr(int id, const Value& value)
106 { m_attrs->setValue(id, value); }
107 
108 inline void BaseEdge::addAttr(QString name, Value value)
109 { m_attrs->push_back(name, value); }
110 
111 } // evoplex
112 #endif // EDGE_H
A class for variant data types (tagged union).
Definition: value.h:56
It wraps a std::shared_ptr<BaseNode>.
Definition: node.h:35
void addAttr(QString name, Value value)
Appends the attribute name with the value value.
Definition: edge_p.h:108
const Node & neighbour() const
Gets the target Node.
Definition: edge_p.h:93
void setValue(int id, const Value &value)
Sets the value at id.
Definition: attributes.h:221
const Value & value(int id) const
Gets the value of the attribute at id.
Definition: attributes.h:213
const Node & origin() const
Gets the source Node.
Definition: edge_p.h:90
int id() const
Gets the edge&#39;s id.
Definition: edge_p.h:87
void setAttr(int id, const Value &value)
Sets the value at id.
Definition: edge_p.h:105
const Value & attr(int id) const
Gets the value of the attribute at id.
Definition: edge_p.h:99
Definition: abstractgraph.h:29
const Attributes * attrs() const
Gets the edge&#39;s attributes.
Definition: edge_p.h:96
A container of labeled values.
Definition: attributes.h:39
Abstract base class for graph plugins.
Definition: abstractgraph.h:54
An Edge connects a node to itself or to another node.
Definition: edge_p.h:35
void push_back(QString name, Value value)
Appends the attribute name with the value value.
Definition: attributes.h:197