first completed the LifetimeTest, it can run very good.

This commit is contained in:
2023-08-02 01:21:53 +08:00
parent 77cc31bcda
commit 3ab6197086
2 changed files with 57 additions and 12 deletions

View File

@@ -35,7 +35,7 @@ public:
public: public:
TestArray(RawLifetimeCallback<T> *cb) TestArray(TCB *cb)
{ {
life_cb=cb; life_cb=cb;
@@ -96,21 +96,58 @@ template<typename T> class TestObject
public: public:
TestObject(){value=0;} TestObject()
{
value=0;
std::cout<<"TestObject()"<<std::endl;
}
TestObject(int v)
{
value=v;
std::cout<<"TestObject("<<value<<")"<<std::endl;
}
~TestObject() ~TestObject()
{ {
std::cout<<"~TestObject("<<value<<")"<<std::endl; std::cout<<"~TestObject("<<value<<")"<<std::endl;
} }
void Set(int v)
{
value=v;
std::cout<<"TestObject::Set("<<value<<")"<<std::endl;
}
int Get()const{return value;}
}; };
template<typename T> class TestObjectArray:public Test<T,ObjectLifetimeCallback<T>> template<typename T> class TestObjectArray:public TestArray<T *,ObjectLifetimeCallback<T>>
{ {
DefaultObjectLifetimeCallback life_cb;; DefaultObjectLifetimeCallback<T> life_cb;
public: public:
TestObjectArray():TestArray<T,ObjectLifetimeCallback<T>>(&life_cb){} TestObjectArray():TestArray<T *,ObjectLifetimeCallback<T>>(&life_cb){}
~TestObjectArray()=default; virtual ~TestObjectArray()
{
for(T *p:*this)
life_cb.Clear(&p);
}
T *Create()
{
T *p;
if(!life_cb.Create(&p))
return nullptr;
TestArray<T *,ObjectLifetimeCallback<T>>::Add(p);
return p;
}
}; };
void main() void main()
@@ -170,14 +207,22 @@ void main()
{ {
TestObjectArray<TestObject<int>> ta_obj; TestObjectArray<TestObject<int>> ta_obj;
ta_obj.Add(); ta_obj.Add(new TestObject<int>(1));
ta_obj.Add(); ta_obj.Add(new TestObject<int>(2));
ta_obj.Add(); ta_obj.Add(new TestObject<int>(3));
TestObject<int> *p=ta_obj.Create();
p->Set(4);
TestObject<int> *two[2]={new TestObject<int>(9),new TestObject<int>(0)};
ta_obj.Add(two,2);
std::cout<<"TestArray<TestObject<int>>: "; std::cout<<"TestArray<TestObject<int>>: ";
for(const TestObject<int> &obj:ta_obj) for(TestObject<int> *obj:ta_obj)
std::cout<<"{"<<obj.value<<"} "; std::cout<<"{"<<obj->Get()<<"} ";
std::cout<<std::endl; std::cout<<std::endl;
} }

View File

@@ -1,4 +1,4 @@
#include<hgl/type/Queue.H> #include<hgl/type/Queue.h>
#include<iostream> #include<iostream>
using namespace hgl; using namespace hgl;