int main(){ std::cout << std::endl; { std::shared_ptr<int> sharedPtr1( new int,IntDeleter() ); std::shared_ptr<int> sharedPtr2( new int,IntDeleter() ); auto intDeleter= std::get_deleter<IntDeleter>(sharedPtr1); intDeleter->getInfo(); sharedPtr2.reset(); intDeleter->getInfo(); } createRandomNumbers(); IntDeleter().getInfo(); { // create three Smart pointer for doubles std::unique_ptr<double,DoubleDeleter > uniquePtr( new double, DoubleDeleter() ); std::unique_ptr<double,DoubleDeleter > uniquePtr1( new double, DoubleDeleter() ); std::shared_ptr<double> sharedPtr( new double, DoubleDeleter() ); std::shared_ptr<double> sharedPtr4(std::move(uniquePtr)); std::shared_ptr<double> sharedPtr5= std::move(uniquePtr1); DoubleDeleter().getInfo(); } DoubleDeleter().getInfo(); }
static void CALLBACK workFunc(PTP_CALLBACK_INSTANCE callbackInstance, PVOID threadParam, PTP_WORK work) { // Aquire ownership of the pointer as a unique_ptr so it is automatically // deleted. std::unique_ptr<std::function<void()>> uniquePtr( (std::function<void()>*)threadParam); // Run the function (*uniquePtr)(); }
void runAsync(std::function<void()> callback) { // A std::function can be of varrying size, but we need to pass a pointer. // Allocate a space for it with new and wrap it in a unique_ptr for // automatic cleanup. In the case where the thread starts, we let the // thread delete it. std::unique_ptr<std::function<void()>> uniquePtr( new std::function<void()>(std::move(callback))); PTP_WORK ptpWork = ::CreateThreadpoolWork(workFunc, (void*)uniquePtr.get(), NULL); if (ptpWork == NULL) { // TODO: Log errors? Probably only fails due to alloc failure. throw std::bad_alloc(); } ::SubmitThreadpoolWork(ptpWork); // Disable the normal unique_ptr automatic deletion as the thread // will delete it. uniquePtr.release(); }
inline static std::unique_ptr<T> makeUnique(T* ptr) { std::unique_ptr<T> uniquePtr(ptr); return std::move(uniquePtr); }
KernelVariable::uniquePtr KernelVariable::createFromFloat(const std::string& varName, const float* data, unsigned int floatCount) { cl_float dummy; return uniquePtr(new KernelVariable(varName, TYPE_FLOAT, data, floatCount, &dummy)); }
KernelVariable::uniquePtr KernelVariable::createFromUint(const std::string& varName, const unsigned int* data, unsigned int uintCount) { cl_uint dummy; return uniquePtr(new KernelVariable(varName, TYPE_UINT, data, uintCount, &dummy)); }