Jump to content
  • Advertisement
Sign in to follow this  
choffstein

typedef question

This topic is 4869 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 created a smart pointer that takes a class template, and a class name string and an ObjectFactory pointer in the constructor. I typedef the smart pointers like this:
typedef ObjectFactorySmartPtr<A> SmrtPtrA;
typedef ObjectFactorySmartPtr<B> SmrtPtrB;
However, each smart pointer has to have a constructor, so they have to be created like this:
ObjectFactory<A> of;
SmrtPtrA a1("A", &of);
SmrtPtrB b1("B", (ObjectFactory<B>*)&of);
The issue is, all SmrtPtrAs will have the exact same constructor, so its just a pain and a waste of time to put all that in the constructor. Is there anyway I can have:
SmrtPtrA a1;
and have an understood constructor through the typedef? Any ideas? Maybe some #define trickery? EDIT: I tried this:
#define SMRT_PTRA(a) SmrtPtrA a("A", &of)
But I don't really like the way it looks. I mean, I have to use:
SMRT_PTRA(a0)
to create a smart pointer called "a0." Just...ugly.

Share this post


Link to post
Share on other sites
Advertisement
Could you use inheritance and an overriding constructor? Eg:


class SmrtPtrA: public ObjectFactorySmartPtr<A> {
public:
SmrtPtrA(A* a): ObjectFactorySmartPtr<A>("A", a) {
}
}



(My syntax may be slightly off, but you should get the idea.)

Share this post


Link to post
Share on other sites
Yeah, but then none of my operator overloading works...and then when you type all that in, you might as well create a class for each pointer anyway...

Any other thoughts?
Thanks.

Share this post


Link to post
Share on other sites
Good point. Well, I'm all out of ideas. [smile] I don't think this is going to work.

Share this post


Link to post
Share on other sites

template<class T>
class ptr_traits{
ptr_traits(){} //private constructor because we shouldn't get here
};

#define SPECIALISE_LE_PTR(ty) template<>struct ptr_traits<ty>{ static const char* name(){ return #ty ; } private: ptr_traits(){}};
SPECIALISE_LE_PTR(A)
SPECIALISE_LE_PTR(B)

#undef SPECIALISE_LE_PTR

template<class T>
struct ObjectFactorySmartPtrFactory{
ObjectFactory<T>& of;
const char* class_name;
ObjectFactorySmartPtrFactory(ObjectFactory<T>& of):of(of){
class_name = ptr_traits<T>::name();
}
ObjectFactorySmartPtr<T> operator(){
return ObjectFactorySmartPtr(class_name, &of);
}
};

int main(){
ObjectFactory<A> of;
ObjectFactorySmartPtrFactory<A> spf;
SmartPtrA ap = spf();
}




Disclaimer: I am posting from work, on a laptop keyboard, and my lunch hour is now probably overdue, so I cannot gaurantee the accuracy, compilability, portability, readibility, feasability, etc. of this code. Also, the board messed up my formatting when I edited it; it had backslashes in the macro.



p.s.: Sorry about the "ObjectFactorySmartPtrFactory", I thought it rather amusing at the time

Share this post


Link to post
Share on other sites
Addendum
I totally forgot about RTTI. It depends what you need the type's name for. If you simply need a unique identifier for that type, typeid will work fine. However, if you need the actual type name, in non-mangled frm, (e.g. to use as output), you're out of luck.
The standard doesn't say anything about what typeid(A) should return. Borland C++ returns a nicely formatted string, as does MSVC, but GCC returns a horrible mangled string such as NS6nstestRirpiblah.


#include <typeinfo>
template<class T>
class ptr_traits{
ptr_traits(){} //private constructor because we shouldn't get here
static const char* name_;
public:
const char* name(){
return name_;
}
};
template<typename T> ptr_traits<T>::name_ = typeid(T).name();


Share this post


Link to post
Share on other sites
you could use something like:

template <typename T>
struct ObjectFactorySmartPtr
{
T ptr;
ObjectFactorySmartPtr(const char *thing){ std::cout << thing << "\n";}
};


template <typename T, const char* (*Name)(void)>
struct SmartHack : public ObjectFactorySmartPtr<T>
{
SmartHack(void): ObjectFactorySmartPtr<T>(Name()){}
};

const char* SayInt(void) { return "int";}
typedef SmartHack<int, SayInt> IntPtr;

class A{};
const char* SayA(void) { return "A";}
typedef SmartHack<A, SayA> APtr;

int main(void)
{
IntPtr ip;
APtr ap;
}


only unresolved issue is passing the factory, I leave that as an exercise to the reader.

Share this post


Link to post
Share on other sites
Interesting; I didn't know you could pass function pointers as template arguments. I've tried passing char* as a template parameter before, and it didn't work (mind you, it was gcc 3.3.3). Comeau C++ seems to allow it.




Share this post


Link to post
Share on other sites
Quote:
Original post by MrEvil
Addendum
I totally forgot about RTTI. It depends what you need the type's name for. If you simply need a unique identifier for that type, typeid will work fine. However, if you need the actual type name, in non-mangled frm, (e.g. to use as output), you're out of luck.
The standard doesn't say anything about what typeid(A) should return. Borland C++ returns a nicely formatted string, as does MSVC, but GCC returns a horrible mangled string such as "St4pairIKiMNK6nstest4evntPFvvEE".

*** Source Snippet Removed ***


I've just written a parser for GCC's horribly mangled RTTI strings. It's got a few bugs at the moment (mainly in the area of local classes), but I'll upload it somewhere soon. Furthermore, long long and long long double have the same mangled type name ("x"), as do unsigned int and unsigned wchar_t ("j").

As an example, "St4pairIS_IKiMNK6nstest4evntPFvvEEbE" is translated to std::pair<std::pair<int const, void(nstest::evnt::*)(void) const>, bool>.

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!