|
|
|
#define OLC_PGE_APPLICATION
|
|
|
|
#include "pixelGameEngine.h"
|
|
|
|
#include <memory>
|
|
|
|
using namespace olc;
|
|
|
|
|
|
|
|
struct myStruct{
|
|
|
|
std::string name="";
|
|
|
|
int level=1;
|
|
|
|
};
|
|
|
|
std::vector<std::weak_ptr<myStruct>> vals;
|
|
|
|
|
|
|
|
void AddVal(std::weak_ptr<myStruct>newVal) {
|
|
|
|
auto findVal=std::find_if(vals.begin(),vals.end(),
|
|
|
|
[&newVal](std::weak_ptr<myStruct>&val)
|
|
|
|
{
|
|
|
|
return val.lock()->name==newVal.lock()->name;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (findVal!=vals.end()) {
|
|
|
|
std::cout<<"Value "<<newVal.lock()->name<<" already exists."<<std::endl;
|
|
|
|
findVal->lock()->level++;
|
|
|
|
std::cout<<findVal->lock()->name+" is now level "<<findVal->lock()->level<<std::endl;
|
|
|
|
} else {
|
|
|
|
std::cout<<"Value "<<newVal.lock()->name<<" does not exist."<<std::endl;
|
|
|
|
vals.push_back(newVal);
|
|
|
|
std::cout<<newVal.lock()->name+" is now level "<<vals[vals.size()-1].lock()->level<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
std::shared_ptr<myStruct> myPtr1=std::make_shared<myStruct>();
|
|
|
|
std::shared_ptr<myStruct> myPtr2=std::make_shared<myStruct>();
|
|
|
|
std::shared_ptr<myStruct> myPtr3=std::make_shared<myStruct>();
|
|
|
|
std::shared_ptr<myStruct> myPtr4=std::make_shared<myStruct>();
|
|
|
|
|
|
|
|
std::weak_ptr<myStruct> refPtr1=myPtr1;
|
|
|
|
std::weak_ptr<myStruct> refPtr2=myPtr2;
|
|
|
|
std::weak_ptr<myStruct> refPtr3=myPtr3;
|
|
|
|
std::weak_ptr<myStruct> refPtr4=myPtr2;
|
|
|
|
|
|
|
|
refPtr1.lock()->name="Test Upgrade 1";
|
|
|
|
refPtr2.lock()->name="Test Upgrade 2";
|
|
|
|
refPtr3.lock()->name="Test Upgrade 3";
|
|
|
|
|
|
|
|
std::cout<<(refPtr2.lock()==refPtr4.lock())<<std::endl;
|
|
|
|
|
|
|
|
AddVal(refPtr1);
|
|
|
|
AddVal(refPtr2);
|
|
|
|
AddVal(refPtr3);
|
|
|
|
AddVal(refPtr4);
|
|
|
|
}
|