Converting old code to std::move semantics

91 Views Asked by At

There is some old code that I wanted to improve, because it looks bad. I think what it does is accomplishable with std::move, am I right? The whole code was posted on this GitHub. However, I only need to improve that particular struct (Resource).

The usage is:

std::vector<Resource> ResourceHelper::resources;

bool ResourceHelper::EnumNamesFunc(HMODULE hModule, LPCWSTR lpType, LPWSTR lpName, LONG_PTR lParam)
{
    if (!IS_INTRESOURCE(lpName))
    {
        std::wcout << lpName << std::endl;
    }
    else if (IS_INTRESOURCE(lpName))
    {
        std::cout << reinterpret_cast<int>(lpName) << std::endl;
    }

    HRSRC hResource = hResource = FindResourceW(hModule, lpName, lpType);

    if (!hResource)
    {
        return false;
    }

    DWORD dwSize = SizeofResource(hModule, hResource);
    HGLOBAL hGlobal = LoadResource(hModule, hResource);

    if (!hGlobal)
    {
        return false;
    }

    LPVOID lpResource = LockResource(hGlobal);

    if (!lpResource)
    {
        return false;
    }

    resources.push_back(Resource(lpType, lpName, hGlobal, dwSize));

    FreeResource(lpResource);

    return true;
}

Snippet

struct resource
{
    LPCWSTR lpType;
    LPWSTR lpName;
    LPVOID lpData;
    DWORD dwSize;

    LPWSTR copy_string(LPCWSTR value)
    {
        if (IS_INTRESOURCE(value))
            return const_cast<LPWSTR>(value);

        int len = wcslen(value) + 1;
        LPWSTR copy = new WCHAR[len];
        memcpy(copy, value, sizeof(WCHAR) * len);
        return copy;
    }
    
    resource(LPCWSTR lpType, const wchar_t* lpName, LPVOID lpData, DWORD dwSize)
    {
        this->lpType = copy_string(lpType);
        this->lpName = copy_string(lpName);
        this->lpData = lpData;
        this->dwSize = dwSize;
    }
    
    resource(const resource& src)
    {
        this->lpType = copy_string(src.lpType);
        this->lpName = copy_string(src.lpName);
        this->lpData = src.lpData;
        this->dwSize = src.dwSize;
    }
    
    ~resource()
    {
        if (!IS_INTRESOURCE(lpType))
            delete[] lpType;

        if (!IS_INTRESOURCE(lpName))
            delete[] lpName;
    }
    
    resource& operator=(const resource& rhs)
    {
        if (&rhs != this)
        {
            resource copy(rhs);

            using std::swap;
            swap(this->lpType, copy.lpType);
            swap(this->lpName, copy.lpName);
            swap(this->lpData, copy.lpData);
            swap(this->dwSize, copy.dwSize);
        }

        return *this;
    }
};

My attempt

class resource
{
public:
    resource() = default;

    resource(const resource&) = delete;

    resource(resource&& obj) noexcept
    {
        *this = std::move(obj);
    }

    ~resource()
    {
    }

    resource& operator=(const resource&) = delete;

    resource& operator=(resource&& rhs) noexcept
    {
        std::swap(type, rhs.type);
        std::swap(name, rhs.name);
        std::swap(lpData, rhs.lpData);
        std::swap(dwSize, rhs.dwSize);
        
        return *this;
    }

private:
    LPCWSTR type;
    LPWSTR name;
    LPVOID lpData;
    DWORD dwSize;
};
0

There are 0 best solutions below