Struct containing unusual templated function in C++ code - visual-c++

In the following C++ code (from a Microsoft COM header file), what is the part that starts with template<class Q>...?
I am thoroughly confused for other reasons as well, as although a struct is in use, it has class-like elements; for example, the public keyword.
extern "C++" {
struct IUnknown {
public:
virtual HRESULT WINAPI QueryInterface(REFIID riid,void **ppvObject) = 0;
virtual ULONG WINAPI AddRef(void) = 0;
virtual ULONG WINAPI Release(void) = 0;
template<class Q> HRESULT WINAPI QueryInterface(Q **pp) { return QueryInterface(__uuidof(*pp),(void **)pp); }
};
}

The part that starts with template<class Q> HRESULT WINAPI QueryInterface is a template member function. In other words, it's a function template that's a member of a class (or struct, in this case).
Being a template means you can pass any interface type as its parameter, and the compiler will generate a function to query an object for an interface of that type:
IFoo *x;
IBar *y;
if (foo.QueryInterface(&x) != S_OK) {
// use x->whatever to invoke members of IFoo
}
if (foo.QueryInterface(&y) != S_OK) {
// use y->whatever to invoke members of IBar
}
Since it's a function template, the compiler deduces the type for Q from the type of the parameter you pass so when you pass an IFoo **, Q has the type IFoo, and when you pass an IBar **, Q has the type IBar.
In C++, the only difference between a class and struct is that member visibility in a class defaults to private, but in a struct defaults to public (so the public: tag isn't accomplishing anything in this case).

Related

C++11 thread pool - tasks with input parameters

I am trying to use a simple thread pool example from the book of Anthony Williams "C++ Concurrency in Action". I have even found the code here (the class thread_pool) in one of the posts:
Synchronizing tasks
but I have a different question. I would like to submit a task (a member function) to the queue with the following signature:
class A;
class B;
bool MyClass::Func(A*, B*);
How would I need to change the thread_pool class, or how do I pack my function in some void F(), which is assumed to be used as a task in this example?
Here is the most relevant part of the class for me (for the details please see the link above):
class thread_pool
{
thread_safe_queue<std::function<void()> work_queue; // bool MyClass::Func(a,b) ??
void worker_thread() {
while(!done) {
std::function<void()> task;
if(work_queue.try_pop(task)) {
task(); // how should my function MyClass::Func(a,b) be called here?
}
else {
std::this_thread::yield();
}
}
}
// -- Submit a task to the thread pool
template <typename FunctionType>
void submit(FunctionType f) {
work_queue.push(std::function<void()>(f)); // how should bool MyClassFunc(A*, B*) be submitted here
}
}
And finally, how can I call the submit Function in my code?
Thank you very much for your help (unfortunatelly I am not very experienced yet in using all the C++11 features, which is probably also why I need help here, but an answer to this question would be something to start with :)).
You have to bind the parameters to a value when you insert a task into the queue. That means that you have to create a wrapper for your function that stores the values for this and the values for the two function parameters. There are many ways to do this, e.g. lambda functions or std::bind.
work_queue.push_back( [obj, a, b]() {obj->Func(a,b)} );
work_queue.push_back( std::bind(&MyClass::Func, obj, a, b) );
Your submit function must take these parameters and create the binding, e.g.
template<typename F, typename... Args>
void submit(F f, Args&&... args) {
work_queue.push_back( std::bind(f, std::forward<Args>(args)...) );
}
It may be convenient to create a special overload for member functions and objects.
I've written something that does something (very) similar to this before. I'll post the code here and you can have a look. GenCmd is the function wrapper. The queue looks like this, and is used/defined in Impl (code omitted). You only need to look at implementation of GenCmd, as this contains the necessary work.
ConcurrentQueue<std::unique_ptr<Cmd>> cqueue_;
I've wrapped std::function<> to be polymorphic in queue. std_utility contains make_index_sequence, that is used to extract values from a tuple (google make_index_sequence to find an implementation somewhere if this is not already part of your std library).
#include <functional>
#include <memory>
#include <iostream>
#include <utility>
#include <boost/noncopyable.hpp>
class CmdExecutor : public boost::noncopyable
{
public:
CmdExecutor(std::ostream& errorOutputStream);
~CmdExecutor();
template <class Receiver, class ... FArgs, class ... CArgs >
void process(Receiver& receiver, void (Receiver::*f)(FArgs...), CArgs&&... args)
{
process(std::unique_ptr<Cmd>(new GenCmd<void(Receiver,FArgs...)>(f, receiver, std::forward<CArgs>(args)...)));
}
private:
class Cmd
{
public:
virtual void execute() = 0;
virtual ~Cmd(){}
};
template <class T> class GenCmd;
template <class Receiver, class ... Args>
class GenCmd<void(Receiver, Args...)> : public Cmd
{
public:
template <class FuncT, class ... CArgs>
GenCmd(FuncT&& f, Receiver& receiver, CArgs&&... args)
: call_(std::move(f)),
receiver_(receiver),
args_(args...)
{
}
//We must convert references to values...
virtual void execute()
{
executeImpl(std::make_index_sequence<sizeof...(Args)>{});
}
private:
template <std::size_t ... Is>
void executeImpl(std::index_sequence<Is...>)
{
// We cast the values in the tuple to the original type (of Args...)
call_(receiver_, static_cast<Args>(std::get<Is>(args_))...);
}
std::function<void(Receiver&, Args...)> call_;
Receiver& receiver_;
// NOTE:
// References converted to values for safety sake, as they are likely
// to not be around when this is executed in other context.
std::tuple<typename std::remove_reference<Args>::type...> args_;
};
void process(std::unique_ptr<Cmd> command);
class Impl;
Impl* pimpl_;
};
It's basically used as follows:
...
CmdExecutor context_;
...
void MyClass::myFunction()
{
ArgX x;
ArgY y;
context_.process(*this, &MyClass::someFunction, x, y);
}
You can see from this that process does the wrapping of member function type and converts it to the underlying type for storage on queue. This allows for multiple argument types. I've opted for using runtime polymorphism to store the function types, hence the GenCmd derivative.
Note: If the invoked function receives an rvalue (Arg&&), the stored type is casted to the original type, therefore causing a move, and rendering the applicable command argument (which would only be invoked once) empty (that's the intent, at least - untested...)

C# - Method's type signature is not PInvoke compatible

I am trying to use the VC++ (2003) dll in C# (2010)
When am calling the method of dll from c# am getting this error
"Method's type signature is not PInvoke compatible"
I am returning the structure from VC++
Code:
struct SLFData
{
public:
char ByLat[10];
char ByLong[10];
};
And I am marshalling in C#
Code:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct SLFData
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public char[] ByLat;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public char[] ByLong;
};
Yet again I get the same error!
What am I missing here?
Can anybody help me Plzz
You say that you are using the struct as a return value. The documentation says:
Structures that are returned from platform invoke calls must be blittable types. Platform invoke does not support non-blittable structures as return types.
Your struct is not blittable. So you cannot use it as a function return type.
The simplest way to proceed is to return the struct via a parameter of the function. Change the C++ function to accept a parameter of type SLFData* and have the caller pass in the address of a struct which the C++ function populates. On the C# side you pass the struct as an out parameter.
FWIW, your struct declaration is wrong. For a start C# char is two bytes wide. It's best done like this:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct SLFData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string ByLat;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string ByLong;
};

VC++ 2010 can access class variable vector of struct from Main() but not from class function

I have written a header file with one basic data structure.
ProdList.h
#ifndef LISTOFITEMS_H
#define LISTOFITEMS_H
struct ListOfItems
{
public:
std::string fdcustid;
std::string fdstkid;
std::string fdordisquantity;
std::string fdordsstatus; // <> 'H'
std::string fdordhtype; // <> 'A'
};
#endif /* GRANDFATHER_H */
now that I have a data structure I include it in the class definition and use the data structure "ListOfItems" in the class "ProdContainer".
ProdContainer.h
#include "ProdList.h"
class ProdContainer
{
public:
ProdContainer(void);
~ProdContainer(void);
static void SetNumberOfElements(int Elements);
std::vector<ListOfItems> Items;
}
now when i write the following in Main.
int _tmain(int argc, _TCHAR* argv[])
{
ProdContainer myobject;
myobject.Items.resize(12);
printf("The size of Items is %i \n", myobject.Items.size());
return 0;
}
All goes as expected and I get the following output.
The size of Items is 12
Which is all fine and good. However, I want to encapsulate the data within the class and only allow access through class functions.
The problem arises when I add the following code to "SetNumberOfElements" implementation.
void ProdContainer::SetNumberOfElements(int Elements)
{
Items.resize(Elements);
}
When I try to compile this "error C2228 left of '.resize' must have class/struct/union" appears and I am at a loss at what to do next.
I have searched high and low and cant seem to find any posts matching this particular problem, it's probably a schoolboy error. I've checked the MSDN site on error C2228 and as far as I can see Items is a substantiated variable of struct type ListOfItems, so I can't see why this error is appearing.
Is there a method for accessing a vector of a struct or some other aspect that I just can't see.
Please help, I am just about ready to explode.
You can only access static data from static functions. So change this
static void SetNumberOfElements(int Elements);
To this
void SetNumberOfElements(int Elements);
You cannot access non-static class members from the static function.
What you can do is to "switch" from static to non-static.
static void SetNumberOfElements( void * lParam, int Elements)
{
((ProdContainer*)lParam)->Items.resize( Elements );
}
Use it like this inside your class:
SetNumberOfELements( this, 10 );

c++ this parameter through System::Threading:ThreadStart to class member function

I'm writing in c++ a graphic user interfaced program and I need to create a threads in the program.
so I'm using System::Threading namespace to get my goal.
The function that I want to use as thread is a class member function, so here is what I've done:
Server::Server() // constructor
{
System::Threading::Thread^ T = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this, this->RunServer)); // Server::RunServer
T->Start();
}
since it gave me those errors:
Error 2 error C3350: 'System::Threading::ThreadStart' : a delegate
constructor expects 2 argument(s)
Error 1 error C3867: 'Server::RunServer': function call missing
argument list; use '&Server::RunServer' to create a pointer to
member
I tried this call:
Server::Server() // constructor
{
System::Threading::Thread^ T = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(&Server::RunServer));
T->Start();
}
and received this errors:
Error 1 error C3364: 'System::Threading::ThreadStart' : invalid
argument for delegate constructor; delegate target needs to be a
pointer to a member
function
2 IntelliSense: invalid delegate initializer -- function is not a
member of a managed
class
as far as I know the second try did not succeed because Server::RunServer doesn't have address, so it's like to do &1.
by the way I tried to use the ThreadStart to create thread of none class memeber function and it worked fine.
I'm using win7 - visual studio 2012. How to make it work?
EDIT:
Server declaration:
class Server
{
public:
/* Fields */
std::string Port;
std::string Host;
WSADATA wsaData;
int ListenResult;
SOCKET ListenSocket;
SOCKET* ClientSocket;
SOCKADDR_IN* ADDR;
int ADDRSize;
struct addrinfo *result;
struct addrinfo hints;
std::vector<Client> Clients;
/* Methods */
Server();
std::wstring StringW(char* String);
void Print(std::wstring String);
std::wstring CurrentTime();
void ParseServerIni();
void RunServer();
void PartToString(Part* _Part);
void InsertListItem(std::string String);
void ClientHandler(SOCKET* _Sock, SOCKADDR_IN* _ADDR);
int ParsePacket(Packet &_Packet, int _Bytes, Byte** _PacketBlock);
};
You almost got the syntax right.
Assuming the declaration is:
public ref class Server
{
void RunServer();
};
Then you should combine your two approaches, by specifying both the object to invoke the method on, and the address of the method, with the name of the declaring class.
gcnew System::Threading::ThreadStart(this, &Server::RunServer)

how to create a thread using a non static method in vc++ mfc

I am creating a thread using this call:
m_pThread=AfxBeginThread(read_data,(LPVOID)hSerial);
read_data is a static method in my class.
But I want to call a non static method and make a thread.
As I want to share a variable between this thread and one of my class method.
I tried taking a static variable but it gave some errors.
You cannot create a thread using a non-static member of a function as the thread procedure: the reason is all non-static methods of a class have an implicit first argument, this is pointer this.
This
class foo
{
void dosomething();
};
is actually
class foo
{
void dosomething(foo* this);
};
Because of that, the function signature does not match the one you need for the thread procedure. You can use a static method as thread procedure and pass the this pointer to it. Here is an example:
class foo
{
CWindThread* m_pThread;
HANDLE hSerial;
static UINT MyThreadProc(LPVOID pData);
void Start();
};
void foo::Start()
{
m_pThread=AfxBeginThread(MyThreadProc,(LPVOID)this);
}
UINT foo::MyThreadProc(LPVOID pData)
{
foo* self = (foo*)pData;
// now you can use self as it was this
ReadFile(self->hSerial, ...);
return 0;
}
I won't repeat what Marius said, but will add that I use the following:
class foo
{
CWindThread* m_pThread;
HANDLE hSerial;
static UINT _threadProc(LPVOID pData);
UINT MemberThreadProc();
void Start();
};
void foo::Start()
{
m_pThread=AfxBeginThread(_threadProc,(LPVOID)this);
}
UINT foo::MyThreadProc(LPVOID pData)
{
foo* self = (foo*)pData;
// call class instance member
return self->MemberThreadProc();
}
UINT foo::MemberThreadProc()
{
// do work
ReadFile(hSerial, ...);
return 0;
}
I follow this pattern every time I use threads in classes in MFC apps. That way I have the convenience of having all the members like I am in the class itself.

Resources