📄 sense.h
字号:
return p;}/************************************************************************ * This function first creates a new packet obtained from the allocator. It then * sets @p->pld@ to null if @p->pld@ is a pointer, and the reference count to 1. ************************************************************************/template <class H, class P>void smart_packet_t<H,P>::free(){#ifdef SENSE_DEBUG packet_trait<P>::check_ref(pld,refcount);#endif packet_trait<P>::free(pld); refcount--; if(refcount==0) { packet_trait<H>::free(hdr); m_allocator.free(this); }}/************************************************************************ * This function destroys the packet without looking at its reference count. ************************************************************************/template <class H, class P>void smart_packet_t<H,P>::destroy(){ packet_trait<H>::free(hdr); packet_trait<P>::free(pld); m_allocator.free(this);}/************************************************************************ * This function increases the reference count. * It must also increase the reference count of the payload packet. ************************************************************************/template <class H, class P>void smart_packet_t<H,P>::inc_ref(){ packet_trait<P>::inc_ref(pld); refcount++; return;}/************************************************************************ * This function is called before forwarding the * encapsulated payload packet to a higher layer. The reference count in @pld@ * is increased if @pld@ is a packet pointer, otherwise nothing * happens. ************************************************************************/template <class H, class P>void smart_packet_t<H,P>::inc_pld_ref() const{ packet_trait<P>::inc_ref(pld);}/************************************************************************ * This function is called to show the content of the packet. It calls * the @dump()@ function of the header, then, depending on the return value, * it may or may not call the @dump()@ function of the payload packet. ************************************************************************/template <class H, class P>std::string smart_packet_t<H,P>::dump() const{ std::string str="["; std::string h; if( hdr.dump(h) ) { std::string m; packet_trait<P>::dump(m,pld); str = str + h + ", " + m + "]"; } else { str = str + h + ", NULL]"; } return str;}/************************************************************************ * This is the partial specialization of @packet_trait@ for smart * packets. Notice that there is no specialization for general packet * pointers. The design decision here is, the specialization for * pointers other than that of smart packets should be left to * users. When a user wants to pass a pointer of a certain packet, he * must implements required member functions, such as alloc(), free(), * as well as a partial or complete specialization of @packet_trait@ * for that pointer ************************************************************************/template <class H, class P>class packet_trait< smart_packet_t<H,P>* >{ public: typedef smart_packet_t<H,P> nonpointer_t; static void free(nonpointer_t* const &p) { if(p!=NULL) p->free(); }; static void inc_ref(nonpointer_t* const &p) { if(p!=NULL) p->inc_ref(); }; static void init(nonpointer_t* &p) { p=NULL; } static void dump(std::string& str,nonpointer_t* const &p) { if(p!=NULL) str= p->dump(); else str="NULL"; } static void check_ref(nonpointer_t* const &p, int ref) { if(p!=NULL&&!p->check_ref(ref)) printf("Payload refcount is smaller than the current refcount\n"); }};/************************************************************************ * @<h2>coordinate_t</h2>@ * * This class defines the coordination of a position in a two dimensional space. ************************************************************************/class coordinate_t{ public: coordinate_t () : x(0.0), y(0.0) { } coordinate_t ( double _x, double _y ) : x(_x), y(_y) { } double x,y;};/************************************************************************ * In COST, timers can only pass one argument due to the * limitation of C++ template (we cannot declare two template classes * with the same name but with different numbers of template * parameters). Therefore, in case we need two arguments, we have to * use @pair@ in STL. ************************************************************************/#include <utility>using std::make_pair;using std::pair;/************************************************************************ * @<h2>triple</h2>@ * * This class is similar to @pair@ in STL, except that it takes three * template parameters. ************************************************************************/template <class T1, class T2, class T3>class triple{ public: typedef T1 first_type; typedef T2 second_type; typedef T3 third_type; T1 first; T2 second; T3 third; triple() : first(T1()), second(T2()), third(T3()) {} triple(const T1& a, const T2& b, const T3& c) : first(a), second(b), third(c) {} template <class U1, class U2, class U3> triple(const triple<U1, U2, U3>& t) : first(t.first), second(t.second), third(t.third) {}};template <class T1, class T2, class T3>inline bool operator == (const triple<T1, T2, T3>& x, const triple<T1, T2, T3>& y){ return x.first == y.first && x.second == y.second && x.third == y.third;}template <class T1, class T2, class T3>inline triple<T1, T2, T3> make_triple(const T1& x, const T2& y, const T3& z){ return triple<T1, T2, T3>(x, y, z);}/************************************************************************ * @<h2>quadruple</h2>@ * We may also need a template class to combine four variables, but unfortunately * the name 'quadruple' sometimes is already defined as long double. So we * just comment this class out. ************************************************************************//*template <class T1, class T2, class T3, class T4>class quadruple{ public: typedef T1 first_type; typedef T2 second_type; typedef T3 third_type; typedef T4 fourth_type; T1 first; T2 second; T3 third; T4 fourth; quadruple() : first(T1()), second(T2()), third(T3()), fourth(T4()) {} quadruple(const T1& a, const T2& b, const T3& c, const T4& d) : first(a), second(b), third(c), fourth(d) {} template <class U1, class U2, class U3, class U4> quadruple(const quadruple<U1, U2, U3, U4>& t) : first(t.first), second(t.second), third(t.third), fourth(t.fourth) {}};template <class T1, class T2, class T3, class T4>inline bool operator == (const quadruple<T1, T2, T3, T4>& x, const quadruple<T1, T2, T3, T4>& y){ return x.first == y.first && x.second == y.second && x.third == y.third && x.fourth == y.fourth; }template <class T1, class T2, class T3, class T4>inline quadruple<T1, T2, T3, T4> make_quadruple(const T1& a, const T2& b, const T3& c, const T4& d){ return quadruple<T1, T2, T3, T4>(a, b, c, d);}*/#endif /* SENSE_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -