473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Queue of function calls

Hello,

I need implementation advice:
I am writing a program that should call some API functions in a
specific order. The APIs are for another application, not Windows
APIs. The order and type of the calls is determined at runtime. There
are a couple of hundred of those APIs that can be called, and they all
take different parameters. What I need is somehow queue the calls with
their parameters and then dequeue and execute them one by one... Now I
really have no idea on how to acomplish anything like this. Could
someone please advice me? I can provide more details if needed.

Please reply to the group.

Thank you!
Jul 22 '05 #1
6 6050
"Richard Berg" <bi***@mail.com> wrote...
I need implementation advice:
I am writing a program that should call some API functions in a
specific order. The APIs are for another application, not Windows
APIs. The order and type of the calls is determined at runtime. There
are a couple of hundred of those APIs that can be called, and they all
take different parameters. What I need is somehow queue the calls with
their parameters and then dequeue and execute them one by one... Now I
really have no idea on how to acomplish anything like this. Could
someone please advice me? I can provide more details if needed.


Wrap the queuing part and dequeuing into some kind of interpreter
of strings -- basically a mechanism that would read a given string
and extract the name of the function to call and values of the args
for that function. Then it would call it. If the function throws
or returns an error, you'll need some mechanism to account for that.

To create a queue, the user would call your "queue stuffer" with
a string, something like

Queue.add("myfunction_1, 123, 3.14, 'a'");
Queue.add("myfunction_2, \"string\", 42"); // need to use \"

then

Queue.execute();

which would go through the strings and execute them one by one.
You can add "syntax" checking to the 'add' method.

Honestly, I don't see any language issue here. Perhaps you should
try comp.programming for more advice on interpreters.

Victor
Jul 22 '05 #2

"Samuele Armondi" <sa****************@hotmail.com> wrote in message
news:3f********@mk-nntp-1.news.uk.worldonline.com...
"Richard Berg" <bi***@mail.com> wrote in message
news:ff**************************@posting.google.c om...
Hello,

I need implementation advice:
I am writing a program that should call some API functions in a
specific order. The APIs are for another application, not Windows
APIs. The order and type of the calls is determined at runtime. There
are a couple of hundred of those APIs that can be called, and they all
take different parameters. What I need is somehow queue the calls with
their parameters and then dequeue and execute them one by one... Now I
really have no idea on how to acomplish anything like this. Could
someone please advice me? I can provide more details if needed.

Please reply to the group.

Thank you!
I'm not sure if this is the best solution or indeed what you are asking

for, but here goes... create an Argument base class, and derive all your argument classes from it. using polymorphism, you will be able to treat all these
arguments the same, even thought they will be of different types (provided
of course you pass them around by pointer or reference).
Now create an class to hold the arguments (say, ArgumentContainer). This
class will hold the number of arguments and an array of _pointers_ to these arguments (otherwise polymorphism goes through the window). Then wrap your
api functions so that they take your ArgumentContainer as an argument, and
store them in a map, like this:
typedef bool (*ApiPointer)(ArgumentContainer)
std::map <std::string, ApiPointer> FunctionMap;
where the string is the function name. now you can use a std::queue <string> or std::deque<string> to queue these calls up and then look them up by name in the map.
I hope it makes sense!
HTH,
S. Armondi

BTW, I have got some code that does something like what I set out above if
you want to take a look
S
Jul 22 '05 #3
"Samuele Armondi" <sa****************@hotmail.com> wrote in message news:<3f********@mk-nntp-1.news.uk.worldonline.com>...
"Samuele Armondi" <sa****************@hotmail.com> wrote in message
news:3f********@mk-nntp-1.news.uk.worldonline.com...
"Richard Berg" <bi***@mail.com> wrote in message
news:ff**************************@posting.google.c om...
Hello,

I need implementation advice:
I am writing a program that should call some API functions in a
specific order. The APIs are for another application, not Windows
APIs. The order and type of the calls is determined at runtime. There
are a couple of hundred of those APIs that can be called, and they all
take different parameters. What I need is somehow queue the calls with
their parameters and then dequeue and execute them one by one... Now I
really have no idea on how to acomplish anything like this. Could
someone please advice me? I can provide more details if needed.

Please reply to the group.

Thank you!


I'm not sure if this is the best solution or indeed what you are asking

for,
but here goes... create an Argument base class, and derive all your

argument
classes from it. using polymorphism, you will be able to treat all these
arguments the same, even thought they will be of different types (provided
of course you pass them around by pointer or reference).
Now create an class to hold the arguments (say, ArgumentContainer). This
class will hold the number of arguments and an array of _pointers_ to

these
arguments (otherwise polymorphism goes through the window). Then wrap your
api functions so that they take your ArgumentContainer as an argument, and
store them in a map, like this:
typedef bool (*ApiPointer)(ArgumentContainer)
std::map <std::string, ApiPointer> FunctionMap;
where the string is the function name. now you can use a std::queue

<string>
or std::deque<string> to queue these calls up and then look them up by

name
in the map.
I hope it makes sense!
HTH,
S. Armondi

BTW, I have got some code that does something like what I set out above if
you want to take a look
S


Thanks for your reply. I was thinking along the same lines... What I
don't really understand is how to call the API function once I have
its name (as string) and a list of arguments (wrapped in classes). Of
course I could make a gigantic if-statement like:

if(strcmp(name, "DoSomething")) {
DoSomething();
}
else if(strcmp(name, "DoOther")) {
DoOther();
}

but this would be terribly ugly when I have hundreds of
possibilities...
Do you see what I mean?

Thanks!
Jul 22 '05 #4

Richard Berg wrote:
I am writing a program that should call some API functions in a
specific order. The APIs are for another application, not Windows
APIs. The order and type of the calls is determined at runtime. There
are a couple of hundred of those APIs that can be called, and they all
take different parameters. What I need is somehow queue the calls with
their parameters and then dequeue and execute them one by one...


Check out the functor object implementation(s) in Andrei Alexandrescu's
book "Modern C++ Design"; I'm suggesting here a "Command object"-based
implementation using templated functor objects, which you'll find at least
one example of in the book.

The drawback of that type-safe method is that you'll need to define one
functor class for each distinct function signature. Whether to define each
functor class once or e.g. via some template mechanism at each usage point
is one design decision you'll need to address. Probably a combination of
predefined functors + support for ad-hoc definitions would be best.

If you're actually going to use all those API-functions then the
"Command object" solution is probably not too much work in comparision with
other methods and in comparision with the client code.

Jul 22 '05 #5
On 6 Jan 2004 13:47:31 -0800, bi***@mail.com (Richard Berg) wrote:
Hello,

I need implementation advice:
I am writing a program that should call some API functions in a
specific order. The APIs are for another application, not Windows
APIs. The order and type of the calls is determined at runtime. There
are a couple of hundred of those APIs that can be called, and they all
take different parameters. What I need is somehow queue the calls with
their parameters and then dequeue and execute them one by one... Now I
really have no idea on how to acomplish anything like this. Could
someone please advice me? I can provide more details if needed.

Please reply to the group.


Download boost from www.boost.org. Then:

#include <boost/function.hpp>
#include <boost/bind.hpp>

typedef boost::function<void()> void_function_t;

std::deque<void_function_t> queue;

void execute_queue()
{
for(std::deque<void_function_t>::iterator i = queue.begin(),
end = queue.end();
i != end;
++i)
{
(*i)();
}
queue.clear();
}

Then to add a call to the queue, just do:

queue.push_back(boost::bind(myapifunction, param1, param2, param3));

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #6

"Richard Berg" <bi***@mail.com> wrote in message
news:ff**************************@posting.google.c om...
"Samuele Armondi" <sa****************@hotmail.com> wrote in message news:3ffb36d7_2@mk-nntp- Do you see what I mean? [SNIP] Thanks!


Ok, I'll go through how i did it...I didn't use polymorphism since I did not
need it, but the bare bones is here.

This is your abstract base class, from which all the argument types will
inherit.
class ArgBase
{
//whatever
}

This is the class that holds all the arguments for a function
class FunctionArguments
{
private:
int NumArgs;
ArgBase *Args; //This could be a std::vector rather than an array
const ArgTypes operator [] (const int) const;
public:
FunctionArguments();
FunctionArguments(int);
FunctionArguments(const FunctionArguments&); // throws
MemCopyError
~FunctionArguments();
FunctionArguments& operator = (const FunctionArguments&); // Need to
be defined because the class allocates memory

const int GetNumArgs() const;
// all the functions to set/retrieve the argument values
};

typedef bool (*FunctionPointer) (const FunctionArguments&);

This class holds the information about a function, such as its name, how
many arguments it takes, ecc. I used a string to represent the argument
sequence, for example a string "ii" meant that a function expected two
integer arguments, "fs" meant float and string, ecc...
class FunctionRecord
{
private:
std::string FunctionName;
std::string ArgumentSequence;
FunctionPointer Function; // Pointer to the actual function
int NumArgs;

bool CheckArgSequence(std::string&);
public:
FunctionRecord(std::string, std::string, int, FunctionPointer);
//throws InvalidArgSequence exception
FunctionRecord();
//This function executes the API call. It inderects FunctionPointer
and passes all the arguments to the actual API call
bool Do(const FunctionArguments&) const;
const std::string& GetArgSequence() const;
const std::string& GetFunctionName() const;
const int GetArgNum() const;
};

Then, you can hold everything in a map:
typedef std::map<std::string, FunctionRecord> FunctionMap;

I added most of the functions at compile time, but I did also make it
possible to add them at runtime. This code comes from the bare bones for an
in-game console I wrote for a friend, but it can very easily be adapted to
your needs.

Once you have the name of the function, you can look it up in the map and
call FunctionMap::second.Do(Arguments) to execute the call.
HTH!!
S. Armondi
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dmitry Akselrod | last post by:
Hello Everyone, I have an almost well working Print Queue Monitoring software written. The software works quite well with typical Windows printing situations. However, there's a problem when a...
7
by: Shailesh Humbad | last post by:
I wrote a simple, but proprietary queue class that efficiently enqueues and dequeues arbitrary length byte arrays. I would like to replace it with an STL container if the performance overhead is...
2
by: Chuck | last post by:
I have not been able to find a good answer about the System.Collections.Queue.Synchronized() method and how it actually works or is to be used. If I create my Queue in the following manner: ...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
9
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
13
by: Jonathan Amsterdam | last post by:
I think there's a slight design flaw in the Queue class that makes it hard to avoid nested monitor deadlock. The problem is that the mutex used by the Queue is not easy to change. You can then...
6
by: les | last post by:
Here's a class which uses 2.0 generics to implement an inter-thread message queue in C#. Any number of threads can post and read from the queue simultaneously, and the message object can be any...
4
by: | last post by:
I am trying to create a 'queue', or something like it. I am storing 1 to 6 integers in a list and need to move backwards and forwards in the list (like the back and forward arrows do in internet...
12
by: Paul Rubin | last post by:
I'd like to suggest adding a new operation Queue.finish() This puts a special sentinel object on the queue. The sentinel travels through the queue like any other object, however, when...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.