Files
Sourcetrail/src/lib/data/graph/Node.h
T
Eberhard Graether 5ceb7a4b43 data: added Graph data structure for storing code semantics in Storage
This change adds graph data structures Token, Node, Edge and Graph for holding code semantics in the Storage and fills
them with the parsed data. Things to note:
* Token is the base for the classes Edge and Node and holds a unique id.
* Graph is the owner of all Nodes and Edges.
* A Node saves the Edges it is endpoint of.
* An Edge saves the Nodes that are endpoints.
* Nodes and Edges are not subclassed. The different Node and Edge types are distinguished by the enums NodeType and
  EdgeType. Type specific fields are therefore available for all types, a check when assigning them prohibits faulty
  usage.
* Storage uses the Graph::getNodeHierarchy method, which is not strict about knowing type names beforehand. E.g. if A::B
  is passed in and A doesn't exist so far, then node A gets created as NODE_UNDEFINED with B as it's child.
* The Graph methods addNodeAsPlainCopy and addEdgeAsPlainCopy can be used to create a sub-graph without reference to the
  original, but with the same ids.

review id = 19

fortune cookie message = Your cheerful outlook is one of your best assets.
2014-06-30 14:08:01 +02:00

84 lines
1.7 KiB
C++

#ifndef NODE_H
#define NODE_H
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
#include "data/graph/Edge.h"
#include "data/graph/Token.h"
class Node: public Token
{
public:
enum NodeType
{
NODE_UNDEFINED,
NODE_CLASS,
NODE_STRUCT,
NODE_GLOBAL,
NODE_FIELD,
NODE_FUNCTION,
NODE_METHOD,
NODE_NAMESPACE,
NODE_ENUM
};
Node(NodeType type, const std::string& name);
virtual ~Node();
std::shared_ptr<Node> createPlainCopy() const;
NodeType getType() const;
void setType(NodeType type);
const std::string& getName() const;
const std::vector<Edge*>& getEdges() const;
void addEdge(Edge* edge);
void removeEdge(Edge* edge);
Edge* getMemberEdge() const;
Edge* findEdge(std::function<bool(Edge*)> func) const;
Edge* findEdgeOfType(Edge::EdgeType type, std::function<bool(Edge*)> func) const;
void forEachEdge(std::function<void(Edge*)> func) const;
void forEachEdgeOfType(Edge::EdgeType type, std::function<void(Edge*)> func) const;
// Token implementation.
virtual bool isNode() const;
virtual bool isEdge() const;
// Additional field accessors for different NodeTypes.
void setAccess(Edge::AccessType access);
bool isConst() const;
void setConst(bool isConst);
bool isStatic() const;
void setStatic(bool isStatic);
// Logging.
std::string getTypeString(NodeType type) const;
std::string getAsString() const;
private:
// Constructor for plain copies.
Node(Id id, NodeType type, const std::string& name);
NodeType m_type;
std::string m_name;
std::vector<Edge*> m_edges;
// Additional fields for different NodeTypes.
bool m_isConst;
bool m_isStatic;
};
std::ostream& operator<<(std::ostream& ostream, const Node& node);
#endif // NODE_H