123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #ifndef NANXING_BASIC_SIGNAL_LINKLIST_
- #define NANXING_BASIC_SIGNAL_LINKLIST_
- #include"nanxing_defalloc.h"
- #include<mutex>
- #include<deque>
- namespace nanxing{
- template<typename T>
- struct link_node
- {
- T* data;
- link_node<T>* next;
- link_node(T _data):data(_data),next(nullptr){}
- link_node<T>& operator=(link_node<T>& _data)
- {
- data=std::move(_data.data);
- return *this;
- }
- link_node(T&& _data):data(_data){}
- };
- template<typename T,typename Alloc=nanxing::allocator<link_node<T>>>
- class basic_signal_linklist //注意这个和list的区别,链表会返回值而不是引用,在某些地方我们需要std::move而不是仅仅获得一个引用
- {
- private:
- std::mutex t;
- using pointer=link_node<T>*;
- int size=0;
- pointer head=nullptr; //头节点指针
- pointer end =nullptr; //这个相当于钻规则的空子,end是晚于head初始化的
- Alloc Creator;
- public:
- explicit basic_signal_linklist(){}
- int push(T&& data)
- {
-
- pointer ptr=Creator.allocate(sizeof(link_node<T>));
- if(ptr==nullptr)
- {
- return -1;
- }
- //Creator.construct(ptr,std::move(data));
- ptr->data=new std::packaged_task<void()>(std::move(data));
- //(ptr->data)();
- ptr->next=nullptr;
- if(head==nullptr)
- {
- head=ptr;
- end=ptr;
- }
- else
- {
- end->next=ptr;
- end=end->next;
- }
- size++;
- ptr=nullptr;
- return 0;
- }
- int list_size()
- {
- return size;
- }
- pointer pop()
- {
- std::unique_lock<std::mutex> lk(t);
- if(size==0)
- {
- return nullptr;
- }
- pointer ptr=head;
- head=head->next;
- size--;
- if(size==0)
- {
- end=nullptr;
- }
- return ptr;
- }
- ~basic_signal_linklist()
- {
- while(head!=end)
- {
- pointer ptr=head;
- head=head->next;
- Creator.destory(ptr);
- }
- if(head==end&&head!=nullptr)
- {
- Creator.destory(head);
- }
- }
- };
- }
- #endif
|