简体   繁体   English

C ++:带有unknow deleter的unique_ptr

[英]C++: unique_ptr with unknow deleter

I need to write a function to retrieve and process some data. 我需要编写一个函数来检索和处理一些数据。 This data may be allocated in several ways (on the data segment, on the heap, on a shared memory segment and so on): 这些数据可以通过多种方式分配(在数据段,堆上,共享内存段等):

T *data;
if( global ) data = &d;
if( heap )   data = new T [ size ];
if( shm )    data = (T*) shmat( id, 0, 0 );
// processing data ...

Since data may be dynamically allocated, I'd think that the best way to handle it is using an unique_ptr or some other kind of smart pointers. 由于data可以动态分配,我认为处理它的最佳方法是使用unique_ptr或其他类型的智能指针。 However it's not always dynamically allocated: I'd need to choose at runtime the deleter for unique_ptr , but that's not possible. 然而,它并不总是动态分配:我需要在运行时选择unique_ptr的删除器,但这是不可能的。

How should I define and handle data ? 我该如何定义和处理data

You could make the custom deleter take a run-time value! 您可以使自定义删除器获取运行时值!

struct MyCustomDeleter
{
   MemoryType type;
   template <typename T>
   void operator()(T* value) const
   {
      switch (type)
      {
         case MemoryType::heap:
             delete[] value;
             break;
         case MemoryType::shm:
             unmap_from_shm(value);
             break;
         // etc.
      }
   }
};

...

std::unique_ptr<T, MyCustomDeleter> ptr (new T[size], 
                                         MyCustomDeleter{MemoryType::heap});

I'm not sure about std::unique_ptr , but you can use std::shared_ptr . 我不确定std::unique_ptr ,但你可以使用std::shared_ptr Its custom deleter doesn't depend on class template parameter. 它的自定义删除器不依赖于类模板参数。

In addition to KennyTm's good answer, another possibility is to use a function pointer as your deleter, and then supply different functions at run time: 除了KennyTm的好答案之外,另一种可能性是使用函数指针作为删除器,然后在运行时提供不同的函数:

typedef std::unique_ptr<T, void(*)(void*)> Ptr;
void delete_global(void*);
void delete_heap(void*);
// etc.

Ptr get(/* whatever */)
{
    if ( ... )
       return Ptr(data, delete_global);
    if (... )
       return Ptr(data, delete_heap);

    // etc.
}

Use your own smart pointer with a deleter you choose: 使用您自己的智能指针和您选择的删除器:

enum DataPointerType
{
    Stack,
    Dynamic,
    SharedMem,
    ...
};

template <class T>
class DataPointer
{
public:
    DataPointer(T* pointer, DataPointerType type)
        : _ptr(pointer), _type(type)
    { }

    ~DataPointer()
    {
        switch (type) {
        case Stack: break;
        case Dynamic: delete _ptr; _ptr = nullptr; break;
        ...
        }
    }

    T& operator*() { return *_ptr; }
    const T& operator*() const { return *ptr; }

    T* operator->() { return _ptr; }
    const T* operator->() const { return ptr; }

private:
   T* _ptr;
    DataPointerType _type;

    // Prevent copying and destruction, dangerous
    DataPointer(const DataPointer& other);
    DataPointer& operator=(const DataPointer& other);
};

If you use a shared shared_ptr<> you can choose the deleter at runtime. 如果使用共享shared_ptr<> ,则可以在运行时选择删除器。 As long as you don't copy/... the shared_ptr it should behave the same as a unique_ptr . 只要你不复制/ ... shared_ptr它应该表现得与unique_ptr相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM