Generic queue template for C + +

Source: Internet
Author: User

Function: The format of the queue data storage is diversified by using the template on Qtcreator.

Templates are implemented in two ways:

1, the implementation of the template directly in the header file declaration and definition, and other classes through # include "*.h" for inclusion, you can implement calling the class and instantiation.

2, you can directly define the template in the CPP file specific functions, other classes need to call the class, only through the # include "*.cpp" file, if in the form of "*.h", there will be an error.

#ifndef Templaterqueue_h#defineTemplaterqueue_h#include<QQueue>#include<QMutex>#include<QMutexLocker>Template<classT>classtemplaterqueue{ Public: Templaterqueue (); BOOLSetqueuedata (ConstT tmp); BOOLGetqueuedata (T *tmp); intgetqueuelength (); BOOLclearqueue ();Private: Qqueue<T> *M_prqueuedata; BOOLLockflag; Qmutex M_mutex;}; Template<classT>Templaterqueue<T>:: Templaterqueue () {M_prqueuedata=NewQqueue<t>; Lockflag=false;} Template<classT>BOOLTemplaterqueue<t>::setqueuedata (ConstT tmp) {Qmutexlocker Locker (&M_mutex); if(Lockflag = =true)        return false; Else{Lockflag=true; M_prqueuedata-Enqueue (TMP); Lockflag=false; return true; }}template<classT>BOOLTemplaterqueue<t>::getqueuedata (T *tmp) {Qmutexlocker Locker (&M_mutex); if(Lockflag = =true)      return false; Else{Lockflag=true; if(m_prqueuedata->IsEmpty ()) {Lockflag=false; return false; }      Else      {        *tmp = m_prqueuedata->dequeue (); Lockflag=false; return true; }}}template<classT>intTemplaterqueue<t>:: Getqueuelength () {Qmutexlocker locker (&M_mutex); if(Lockflag = =true)      return false; Else    {      returnM_prqueuedata->length (); }}template<classT>BOOLTemplaterqueue<t>:: Clearqueue () {Qmutexlocker locker (&M_mutex); if(Lockflag = =true)       return false; Else{m_prqueuedata-Clear (); return true; }}#endif //Templaterqueue_h

Generic queue template for C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.