|
This can be handled in a number of ways that depend on what you want to do with these objects later. Probably the most obvious solution is to write an abstract class from which all X instantiations derive from, and use this as the type of the pointers stored in te container:
class X_base
{
virtual foo()=0;
...
}
template <class T>
class X: public X_base
{
...
};
vector<X_base*> X_vector; Is this suitable for you?
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Yes, making an abstract base class solves my problem.
But I'm curiuos what other solutions might exist.
Andras
|
|
|
|
|
Hello Joo,
I think Joaquin's solution is really cool. But I think we may have missed your earlier concern : how to obtain the actual derived class object contained within the vector.
If we used a vector of pointers to X_base objects, we will obtain only pointers to X_base objects when we iterate through the vector.
We need to be able to determine at runtime the following information :
1. What is the X_base-derived-template-class of each vector item.
2. What is the type that is actually used to instantiate the X_base-derived template class (in your earlier example, this is actually 'int' or 'double').
Inspired by Joaquin, I went on to refine his example source codes and produced the following :
#include <vector>
#include <iostream>
#include <typeinfo.h>
using namespace std;
class X_base
{
public :
X_base()
{
}
virtual operator const type_info&() = 0;
};
template <class T>
class X: public X_base
{
public :
typedef T MyType;
X(MyType t) :
m_t(t),
m_ti(typeid(t))
{
}
virtual operator const type_info&()
{
return m_ti;
}
MyType& GetValue()
{
return m_t;
}
protected :
MyType m_t;
const type_info& m_ti;
};
typedef X<int> X_int;
typedef X<double> X_double;
typedef std::vector<X_base*> X_base_vector;
int _tmain(int argc, _TCHAR* argv[])
{
X_int* pxi = new X_int(5);
X_double* pxd = new X_double(0.85);
X_base_vector x_base_vector;
X_base_vector::iterator theIterator;
const type_info& ti_int = typeid(int);
const type_info& ti_double = typeid(double);
int i = 0;
cout << ti_int.name() << endl;
cout << ti_double.name() << endl;
x_base_vector.push_back(pxi);
x_base_vector.push_back(pxd);
for (theIterator = x_base_vector.begin(); theIterator != x_base_vector.end(); theIterator++)
{
X_base* px = *theIterator;
if (ti_int == (const type_info&)(*px))
{
X_int* pxi = dynamic_cast<X_int*>(px);
cout << "px == X<int>" << endl;
cout << "Value : " << pxi -> GetValue() << endl;
}
if (ti_double == (const type_info&)(*px))
{
X_double* pxd = dynamic_cast<X_double*>(px);
cout << "px == X<double>" << endl;
cout << "Value : " << pxd -> GetValue() << endl;
}
cout << ((const type_info&)(*px)).name() << endl;
}
return 0;
}
Essentially, I used Run-Time-Type-Information (RTTI) to help determine the class (X_base-derived) of each vector item as well as to determine the type that is actually used to instantiate the X_base-derived template class.
Hope the above code will be helpful to you and Joaquin.
Thanks,
Bio.
|
|
|
|
|
You could also use the boost::any as container item type. Please see http://www.boost.org/doc/html/any.html[^] for more information.
If you think this is something you would like to use, then you do not need to download the entire boost source code. You can download any.hpp and use it. No link libraries required.
--
Seraphim Shock. Gold for your ears.
|
|
|
|
|
Hi,
Consider the following template
template<typename t="">
class X
{
//...
};
Now, I would like to put pointers to the different instantiations of the above template for ex
X<int>* xi;
X<double> *xd;
in a single stl container, let's say a vector. Can I do it in an elegant way ?
(Well, actually I can, using a vector<void*>. But after that how do I get back my objects ?)
Thank you,
Andras
|
|
|
|
|
Hello everyone
How to load a MFC dll to a WTL project? If having some sample, could you send me by email?
Thank you .
-Freehawk
|
|
|
|
|
If it's an MFC extension DLL, you can't. An extension DLL can only be used by an MFC app. (You could add MFC support to the WTL app, though).
If it's an MFC regular DLL, there's nothing special you have to do to use it.
--Mike--
Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0.1! | RightClick-Encrypt
"Just because the box has 2 gigabytes of memory doesn't mean you get to use it all!"
-- Rico Mariani, CLR perf guy
|
|
|
|
|
Hello.
Could you teach me the details about "add MFC support to the WTL app"?
Thank you.
-Freehawk
|
|
|
|
|
It'd be easier if you made a dummy ATL project with the appwizard, and check the "support MFC" checkbox so you can see where the CWinApp object is created & initialized.
--Mike--
Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0.1! | RightClick-Encrypt
Pinky, are you pondering what I'm pondering?
I think so Brain, but if we shaved our heads, we'd look like weasels!
|
|
|
|
|
Hello
Thank you for the reply.
Now I am using WTL7.1, and I cannot find "check the "support MFC" checkbox ".
how to do?
-Freehawk
|
|
|
|
|
|
Hello.
Thank you very much. I want to know a way by which I can use MFC in a WTL project.
If know it, please tell me.
-Freehawk.
|
|
|
|
|
You have to ask yourself this question; If I need MFC support, then why am I not writing an MFC based application?
--
Seraphim Shock. Gold for your ears.
|
|
|
|
|
I need some docking, auto-hide tool windows, till now I only realized them by WTL, at same time I need to call a lot of Dll created by MFC, so.......
-Freehawk.
|
|
|
|
|
hi all,
how can i match AfxGetMainWnd() in WTL code?
the issue is that from child windows or from some other classes i need to get a pointer to MainFrame. AfxXXXXXXXXX does bot work... what does then?
debug - is my life style
|
|
|
|
|
There is no such function. Store the pointer to your main window where it is created (typically in the ProjectName.cpp file if you are using the WTL-wizard). Either make a global variable or a function, say WtlGetMainWnd().
--
Seraphim Shock. Gold for your ears.
|
|
|
|
|
Iam currently trying to code a small graphics viewer using the ATL in VC. Ive had some success modifying the onDraw code so I can use the setPixelV command to display the pixel data I need to the screen, but I have yet to find a way to change the size that the control appears in IE or the test container. Iam pretty new to VC and the ATL but have a very strong java background, and i finding this ATL stuff hard? Can n e 1 help me change the size of my control????! Also sometimes when i use setPixelV I get some kind of throw back or 'echo' where it draws some pixels at the bottom of the screen in a unspecified position?!
[] /\ () X
|
|
|
|
|
I can't speak to your exact SetPixelV issues, but you should be sure to limit the area to ATL_DRAWINFO::prcBounds in your OnDraw.
I think that I can provide some insight into the size issue.
Recently I made an ATL ActiveX control for IE & in order to set the default size of the control, I modified _Module.m_sizeExtent in my constructor. When I needed to re-adjust the size of the control, I told IE to do it for me
To do that, I had to get the IHTMLDocument that my control was in & find my exact IHTMLObjectElement & then do a put_height to adjust it.
Bad Psudo Code<br />
this->GetClientSite(IOleClientSite)<br />
IOleClientSite::QI(IID_ServiceProvider)<br />
IServiceProvider->QueryService(SID_SWebBrowserApp)<br />
IWebBrowser2->get_Document()<br />
--find the IHTML Object that you want to adjust<br />
IHTMLObjectElement->put_Height(VT_I4)<br />
done
It's probably something similar for the test container - but I've never investigated it.
|
|
|
|
|
Hi..
I make an activeX control (ATL 3.0 - VC++6.0 SP5)
I want to make a property read only at runtime...
what I did was :
BOOL blnMode=TRUE;
GetAmbientUserMode(blnMode);
if(blnMode)
return Error("Property not available at runtime\n",IID_INumberBox,E_FAIL);
the problem with this :
When the value of the property is being loaded (when I run the peogram) the put_property() is invoked and the error is raised..
So I did this :
BOOL blnMode=TRUE;
GetAmbientUserMode(blnMode);
if(blnMode && IsWindow())
return Error("Property not available at runtime\n",IID_INumberBox,E_FAIL);
this checks if the window of the control has been created .. if not the control is loading properties so no need to fire raise the error..
this works .. but I wonder if there is a way to check if the properies are being loaded/saved ?!! Something like : IsLoadingProperties() , IsSavingProperties()...
Or is there a way to override the loading / saving actions ?
Any help / comments are appreciated
|
|
|
|
|
I have added an ATL object with connection points to my MFC project so I can expose methods and events to another application. How would I go about starting an instance of my object, so I can fire events from, say, a button click? I'm new to C++ and ATL programming, so please bear with me if my question is stupid.
Regards,
NotBear
|
|
|
|
|
Hello!
Hope someone can help me.
First some explanation:
I have a function (from the DX9 C++ Help) that grabs a video frame from any DirectShow supported Video file. It works fine so far, no problems.
BUT (as always )
if i call this function in a thread, CoCreateInstance failes (using ATL Smart Pointers with IMediaDet).
Here is the begin of the function to the point where FAILED is true:
HRESULT GetBitmap(LPTSTR pszFileName, BITMAPINFOHEADER** ppbmih)
{
HRESULT hr;
CComPtr<imediadet> pDet;
hr = pDet.CoCreateInstance(__uuidof(MediaDet));
if (FAILED(hr))
return hr;
The function where i am calling GetBitmap() looks this way:
unsigned __stdcall DoStuffThreaded( void *pArguments )
{
for( ;; )
GetBitmap()
.. do stuff etc
_endthreadex(1);
}
and the thread function is started with:
hThread = _beginthreadex(NULL, 0, DoStuffThreaded, (void*)pParam, NULL, &uiID);
Again, if i call GetBitmap() the usual way (not in a thread) it works perfect.
Do i need to initialize ATL in some way to make it work in a thread?
Thanks for any help/hints/solutions!
Nik
|
|
|
|
|
Ok, i am stupid.
Doing a
CoInitialize(NULL);
at the beginning
and a
CoUnitialize();
at the end
solves the problem...
Nik
|
|
|
|
|
Hello Virtualnik,
The reason why CoInitialize() and CoUninitialize() are required is to enable COM to support Apartment Threaded Models. In an Apartment Threaded Model, each thread owns the COM objects that are created in the thread.
When a thread wants to access the COM objects created in another thread, the COM object must be marshalled to the thread that wants the access. In order that threads be able to perform marshalling, CoInitialize() and CoUninitialize() must be called in each thread.
Hope the above will shed more light.
Thanks,
Bio.
|
|
|
|
|
Thank you for the explanation!
Nik
|
|
|
|
|
I am Using
CComPtr<IHTMLElement> m_pBody;
as a Class Member
and in the DocumentComplete() Handler of BHO
hr = spHTML->get_body(&m_pBody); <-ASSERTION FAILED 2nd Time
if (FAILED(hr))
return hr;
When the Code Executes second time this assertion fails
Excerpts from CComQIPtr class
T** operator&()
{
ATLASSERT(p==NULL);
return &p;
}
if i don't use CComQIPtr then everything is ok but it sometimes crashes
what does this assert means what am i doing wrong ?
Live as if your were to die tomorrow. Learn as if you were to live forever.
-Mahatma Gandhi
|
|
|
|