Skip to main content
GameDev.net gamedev.net
🔒 Locked

IDirect3DTexture9 wrapper class "move capable"

Started by belfegor Feb 23, 2012 at 9:32 PM 3 replies 1.2k views
Original Post
belfegor
belfegor
I waant to wrap IDirect3DTexture9 object and make it "move capable", to be able to store this into some STL containers.
How i should do this properly as IDirect3DTexture9 is a COM objets witch inherits from IUnknown witch does some reference counting?


class MyTexture
{
private:
IDirect3DTexture9* tex;

MyTexture(const MyTexture& o) {} // no copies allowed
MyTexture& operator = (const MyTexture& o) { return *this; }// no copies allowed

public:
MyTexture()
{
D3DXCreateTextureFromFile(... &tex);
}

~MyTexture()
{
tex->Relase();
tex = nullptr;
}

MyTexture(MyTexture&& o) : tex(o.tex) { o.tex = nullptr; }

MyTexture& operator = (MyTexture&& o)
{
if(this != &o)
{
tex->Release();// Should release here???
tex = o.tex;
o.tex = nullptr;
}
return *this;
}
};


Thanks for your time.
belfegor
belfegor
This seems fine to me:
#include <iostream>
#include <vector>
#include <algorithm>

typedef unsigned long ulong;

class IUnknown
{
private:
ulong refCnt;
public:
IUnknown()
{}

ulong AddRef()
{
return ++refCnt;
}

ulong Release()
{
if (--refCnt == 0)
{
delete this;
return 0;
}
return refCnt;
}
};

void CreateCOMLikeObject(IUnknown** pp)
{
*pp = new IUnknown;
(*pp)->AddRef();
}

class Foo
{
private:
friend struct smallerData;
IUnknown* p;
int data;

Foo(const Foo& o) {}
Foo& operator = (const Foo& o) { return *this; }
Foo() {}

public:
explicit Foo(int d) : p(nullptr), data(d)
{
CreateCOMLikeObject(&p);
}

~Foo()
{
if(nullptr != p)
{
p->Release();
p = nullptr;
}
}

Foo(Foo&& o) : p(o.p), data(o.data)
{
o.p = nullptr;
}

Foo& operator = (Foo&& o)
{
if(this != &o)
{
p = o.p;
data = o.data;
o.p = nullptr;
}
return *this;
}

int get_data() const { return data; }
};

struct smallerData
{
public:
bool operator () (const Foo& l, const Foo& r) const
{
return l.data < r.data;
}
};

int main()
{
{
std::vector<Foo> v;

for(int i = 0; i < 10; ++i)
{
v.push_back(Foo(10 - i));
}

std::random_shuffle(v.begin(), v.end());

std::cout << "shuffled :\t";
std::for_each(v.begin(), v.end(), [](const Foo& f) { std::cout << f.get_data() << ' '; });
std::cout << std::endl;

std::sort(v.begin(), v.end(), smallerData());

std::cout << "sorted :\t";
std::for_each(v.begin(), v.end(), [](const Foo& f) { std::cout << f.get_data() << ' '; });
std::cout << std::endl;
}

std::cout << "Press enter to exit...";
std::cin.ignore();
return 0;
}
SiCrane
SiCrane
Why not use an existing smart pointer like CComPtr?
belfegor
belfegor
I don't like COM blink.png , it makes me dizzy. I am trying to see if i can get away from using pointers in STL containers.
See any problems with my latest code?

Thanks for your time.
bubu LV
bubu LV
If you are using IDirect3DTexture9, then you are using COM - or more precisely - you are using COM-like object.
CComPtr has only one thing to do - to mange COM-like objects. So I really don't see any disadvantage of using CComPtr class.

Alternatively you can use boost::intrusive_ptr to do the same job - http://www.gamedev.n...ost__p__4573797

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.