📄 node.hpp
字号:
#ifndef NODE_H #define NODE_H #include <limits> #include <iostream> #include <sstream> using namespace std; #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> #include "sim_time.hpp" #include "communication_layer.hpp" #include "location.hpp" class Event; typedef boost::shared_ptr<Event> EventPtr; // Node Id Class class NodeId { public: NodeId(t_uint numericValue) : m_numericValue(numericValue) { } NodeId(const t_uchar* byteArray, t_uint byteCount) { m_numericValue = 0; for(t_uint i = 0; i < byteCount; i++) { if(i > sizeof(t_uint)) assert(byteArray[i] == 0); m_numericValue += (byteArray[i] << (8 * i)); } } virtual ~NodeId() { } inline void writeToByteArray(t_uchar* byteArray, t_uint byteCount) const { t_uint id = getNumericValue(); assert(sizeof(t_uint) <= byteCount); // Reset the data to zero. fill(byteArray, &byteArray[byteCount], 0); t_uint i = 0; while(id > 0) { assert(i < byteCount); byteArray[i++] = id & 0xFF; id >>= 8; } } static inline t_uint broadcastDestination() { return numeric_limits<t_uint>::max(); } virtual string getStringValue() const { ostringstream streamValue; if(m_numericValue == broadcastDestination()) streamValue << "BROADCAST"; else streamValue << m_numericValue; return streamValue.str(); } virtual t_uint getNumericValue() const { return m_numericValue; } inline bool operator== (const NodeId& rhs) const { return m_numericValue == rhs.m_numericValue; } inline bool operator< (const NodeId& rhs) const { return m_numericValue < rhs.m_numericValue; } protected: t_uint m_numericValue; }; class Node : boost::noncopyable, public boost::enable_shared_from_this<Node> { // NOTE: For now, we will make this class noncopyable // since a copyable version would require maintaining // pointers to all added layers and copying each one // when necessary. public: typedef boost::shared_ptr<Node> NodePtr; virtual ~Node(); static inline NodePtr create(const Location& location, const NodeId& nodeId); inline Location getLocation() const; inline NodeId getNodeId() const; SimTime currentTime() const; bool scheduleEvent(EventPtr eventToSchedule, const SimTime& eventDelay); bool cancelEvent(EventPtr eventToCancel); protected: Node(const Location& location, const NodeId& nodeId); private: Location m_location; NodeId m_nodeId; }; typedef boost::shared_ptr<Node> NodePtr; // Inline Functions inline NodePtr Node::create(const Location& location, const NodeId& nodeId) { NodePtr p(new Node(location, nodeId)); return p; } inline Location Node::getLocation() const { return m_location; } inline NodeId Node::getNodeId() const { return m_nodeId; } // Overloaded Operators inline ostream& operator<< (ostream& s, const Node& node) { return s<< "Node state (pointer=" << &node << ")"; } inline ostream& operator<< (ostream& s, const NodeId& nodeId) { return s<< nodeId.getStringValue(); } #endif // NODE_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -