Jump to content
  • Advertisement
Sign in to follow this  
belfegor

IDirect3DTexture9 wrapper class "move capable"

This topic is 2337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!