I'm writing a 'skeleton' front-end in native C++ for other users where the users are creating functions that I will 'call' depending on arguments that are passed in. For example:
skeleton.exe /foo param1
would be used to call the function "int doFoo(param1){return 0;}" inside my skeleton. As more team members write functions, I would need to add those functions as well.
A stray thought I had - and I'm not even certain if this would be possible - would be to have a resource file - or, maybe, a resource xml file - that would list the command line arguments, the number of parameters and then the function I need to call for that parameter. So my resource file for the above would look like:
foo 1 doFoo
This way, as people create their functions, all they have to do is add it to the resource file.
The problem I am running into - and what I'm wondering if it is even possible - is whether I can 'interpret' that 'doFoo' as a function if it is read from the resource file. Is there anything that would allow me to 'interpret' a string as a function I can call?
You will need to map strings to function pointers.
One method is to create a lookup table another is to use std::map.
Search the internet for "c++ dispatch" and maybe "c++ double dispatch".
I think I've got it! I just need to ensure that each added function is declared with extern "C" __declspec(dllexport)! Example:
extern "C" __declspec(dllexport) int square(int x)
{
return (x*x);
}
typedef int(__cdecl *MYPROC)(int);
int _tmain(int argc, _TCHAR* argv[])
{
LPCSTR funcName = "square";
HMODULE hMod = GetModuleHandle(NULL);
if (hMod != NULL)
{
cout << "hMod is not NULL!" << endl;
MYPROC abstractSquare = (MYPROC)GetProcAddress(hMod, funcName);
if (NULL != abstractSquare)
{
cout << "abstractSquare is not NULL!" << endl;
int y = (abstractSquare)(10);
cout << "y = " << y << "!" << endl;
}
else
{
cout << "abstractSquare is NULL!" << endl;
}
}
else
{
cout << "hMod is NULL!" << endl;
}
return 0;
}
Related
I'm making a terminal app just to increase my OOP skills. I have this RunApplication()function and LoginCustomer()function to make a customer log in to the application.
This code is my RunApplicationfunction:
void RunApplication(){
while (key!='q'){
printUI();
std::cin >> key;
if (key == '1'){
LoginCustomer();
}
..... // There are many other if statements but not included here since there are many and irrelevant.
and this is my LoginCustomerfunction:
void LoginCustomer(){
std::cout << "***************************" << std::endl;
std::cout << "1 ----> Show Available Cars" << std::endl;
std::cout << "2 ----> Show Available Motors" << std::endl;
std::cout << "3 ----> Show Available Trucks" << std::endl;
std::cout << "always 'l' ----> Logout" << std::endl;
std::cout << "always 'q' ----> Exit" << std::endl;
std::cout << "***************************" << std::endl;
std::cin >> key;
if (key == 'l') //if you want to go back main page which was loaded into terminal with RunApplication()
return;
else if(key == 'q')
exit(EXIT_FAILURE);
}
return; // What if I just call RunApplication() here again instead a simple return, wouldn't program be the most inefficient program ?
else if(key == 'q')
exit(EXIT_FAILURE);
My question is can I forbid a function to be called more than one time in a program(or main) ? Because if you think about it, in the LoginCustomer If you press "l" it gets you to the main page by simply ending the LoginCustomer()using returncommand. But what if I would just use RunApplication()there again instead of returncommand wouldn't be there a inefficiency in program ? So I was thinking is there any keywordto forbid a function to be called more than one time ?
My question is can I forbid a function to be called more than one time in a program(or main) ?
You can use local static variables to have a guarantee that the code is called only once.
Example:
struct FunctionObject
{
FunctionObject()
{
std::cout << "I will be called only once" << std::endl;
}
};
void Do()
{
static FunctionObject fo;
}
int main()
{
std::cout << "First" << std::endl;
Do();
std::cout << "Second" << std::endl;
Do();
}
But for your UI example I would prefer a design with simple state machine which take care that states can only be activated in a given order.
I'm making a terminal app just to increase my OOP skills.
Your example code did not have any object nor any OOP design. Maybe you have some classes/objects elsewhere, but I can't see any OOP design in your code.
You can use std::call_once: https://en.cppreference.com/w/cpp/thread/call_once
static std::once_flag flag;
std::call_once(flag, []{
//code here..
});
Local variables are stored and created on the stack. Static local variables are constant and remain between calls so:
bool my_one_off_function ()
{
static int iCount = 0; // static only intialized at start of program
if (! iCount++) // First time only will be zero
{
do_first_call_stuff ();
return true;
}
else
return false;
}
Alternatively, in your specific case, when the login request is received, you could just check if the login credentials already exist before calling the login function. This would be the more "elegant" solution because it allows logout etc.
making random actions in a game makes it really look like real...
so if a character has many capabilities like move, work, study... so in programming a function of those is called depending on some conditions. what we want is a more random and real-looking like action where no condition is there but depending on a random condition the character takes a random actions..
I thought to make actions (functions) in an array then declare a pointer to function and the program can randomly generate an index which on which the pointer to function will be assigned the corresponding function name from the array:
#include <iostream>
void Foo() { std::cout << "Foo" << std::endl; }
void Bar() { std::cout << "Bar" << std::endl; }
void FooBar(){ std::cout << "FooBar" << std::endl; }
void Baz() { std::cout << "Baz" << std::endl; }
void FooBaz(){ std::cout << "FooBaz" << std::endl; }
int main()
{
void (*pFunc)();
void* pvArray[5] = {(void*)Foo, (void*)Bar, (void*)FooBar, (void*)Baz, (void*)FooBaz};
int choice;
std::cout << "Which function: ";
std::cin >> choice;
std::cout << std::endl;
// or random index: choice = rand() % 5;
pFunc = (void(*)())pvArray[choice];
(*pFunc)();
// or iteratley call them all:
std::cout << "calling functions iteraely:" << std::endl;
for(int i(0); i < 5; i++)
{
pFunc = (void(*)())pvArray[i];
(*pFunc)();
}
std::cout << std::endl;
return 0;
}
the program works fine but I only is it good or there's an alternative. every comment is welcome
There is absolutely no point in converting function pointers to void* and back. Define an array of function pointers, and use it as a normal array. The syntax for the declaration is described in this Q&A (it is for C, but the syntax remains the same in C++). The syntax for the call is a straightforward () application after the indexer [].
void (*pFunc[])() = {Foo, Bar, FooBar, Baz, FooBaz};
...
pFunc[choice]();
Demo.
Note: Although function pointers work in C++, a more flexible approach is to use std::function objects instead.
In C++, I'm trying to write a function with function pointers. I want to be able to throw an exception if a function pointer is passed for a function that does not exist. I tried to handle the function pointer like a normal pointer and check if it is null
#include <cstddef>
#include <iostream>
using namespace std;
int add_1(const int& x) {
return x + 1;
}
int foo(const int& x, int (*funcPtr)(const int& x)) {
if (funcPtr != NULL) {
return funcPtr(x);
} else {
throw "not a valid function pointer";
}
}
int main(int argc, char** argv) {
try {
int x = 5;
cout << "add_1 result is " << add_1(x) << endl;
cout << "foo add_1 result is " << foo(x, add_1) << endl;
cout << "foo add_2 result is " << foo(x, add_2) << endl; //should produce an error
}
catch (const char* strException) {
cerr << "Error: " << strException << endl;
}
catch (...) {
cerr << "We caught an exception of an undetermined type" << endl;
}
return 0;
}
but that doesn't seem to work. What is the best way to do this?
Checking for NULL is ok. But it is not possible to pass a pointer to a function that does not exist in the first place. So you don't have to worry about this. Although it is possible to just declare a function without defining it and pass the address of it. In that case you will get linker error.
It will automatically throw an error if you are passing pointer which does not exist, if you are declaring a pointer then you have to initialize it with null to avoid garbage value, so comparing with null will not serve any purpose.
you still you want to check then try to assign some function(like add, sub etc.), if it takes then ok , if not then it will show again error as previously mentioned.
#include<cstddef>
#include <iostream>
using namespace std;
int foo(const int& x, int (*funcPtr)(const int& x)) {
if (*funcPtr != NULL) {
return funcPtr(x);
}
else
{
cout << "not a valid function pointer";
}
}
If you want to 'throw' exception then you need to 'catch' it as well.
Your code is failing because of two reasons in short,
1) You are not checking value of function pointer.
2) You are not properly catching the thrown exception.
I'm developing a DLL (in Visual Studio 2013) to read TIFF (satellite images), using GDAL library, and having an issue to get back my variable with data - currently it empty (returns NULL).
In my DLL I have my funcion defined in the "RasterFuncs.h" like this:
namespace RasterFuncs
{
// This class is exported from the RasterFuncs.dll
class MyRasterFuncs
{
public:
// Open a raster file
static RASTERFUNCS_API int Open(char* rname, GDALDataset *poDataset);
};
}
and in my DLL cpp I have the following:
namespace RasterFuncs
{
int MyRasterFuncs::Open(char* rname, GDALDataset *poDataset)
{
poDataset = (GDALDataset *) GDALOpen(rname, GA_ReadOnly);
if (poDataset != NULL)
{
cout << "RasterXSize 1:" << poDataset->GetRasterXSize() << endl;
cout << "RasterYSize 1:" << poDataset->GetRasterYSize() << endl;
cout << "RasterCount 1:" << poDataset->GetRasterCount() << endl;
}
return 0;
}
}
at this point I have the poDataset with all image data.
However, I call this DLL form another CPP using the following code:
rfileName = "C:/Image1.tif";
// Open raster satelitte image
GDALDataset *poDataset = NULL;
GDALAllRegister();
RasterFuncs::MyRasterFuncs::Open(rfileName, poDataset);
if (poDataset != NULL)
{
cout << "RasterXSize:" << poDataset->GetRasterXSize() << endl;
cout << "RasterYSize:" << poDataset->GetRasterYSize() << endl;
cout << "RasterCount:" << poDataset->GetRasterCount() << endl;
}
and when I test poDataset that come back, shows NULL.
Anybody could help in this issue?
Thanks in advance and best regards!
Remember that by default arguments to functions are passed by value, meaning the function have a copy of the value.
When you assign to the poDataset variable in the function, you're only changing your local copy. The calling function will not see this modification. You need to pass the argument by reference:
static RASTERFUNCS_API int Open(char* rname, GDALDataset*& poDataset);
Joachim is spot on, but on the flip side you could pass a pointer to the pointer rather than a reference to the pointer. I won't go into the pointers vs references battle, as both are good to know and understand.
static RASTERFUNCS_API int Open(char* rname, GDALDataset **poDataset);
and
RasterFuncs::MyRasterFuncs::Open(rfileName, &poDataset);
("**" means "pointer to a pointer" and "&" means "address of")
I have tried to adapt my knowledge of modularity to Visual C++ however, upon what seems to be an endless search scouring for syntax, I simply can't get this right. Basically in this code, the menu is called first, once the user enters their choice (only coded option 1 thus far) to return that value to the main, which then steps into the if statement and calls fahrenheit. I am requesting the syntax for passing by reference, I know C#'s syntax for this, but not Visual C++
Here's the code.
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void Celsius()
{
}
void fahrenheit()
{
cout << "Success!" << endl; //....Outputs this just to see if the module is being called properly.
}
int menu(int Mystring) //....I was testing this syntax to pass the variable.
{
cout << "What would you like to do : " << endl;
cout << "1) Fanreheit to Celsius" << endl;
cout << "2) Celsius to Fahrenheit" << endl;
cout << "Choice : " ;
cin >> Mystring;
return Mystring;
}
int main()
{
int celsius = 0;
int fahrenheit = 0;
int Mystring = 0;
menu(Mystring); //....Testing this syntax to pass Mystring to menu.
if (Mystring == 1) //....I was hoping the menu would return Mystring as value = 1.
{
fahrenheit(); //.......I want this to call fahrenheit module if Mystring = 1
}
}
The "things" you're talking about aren't called modules, but functions. That's a pretty big difference and I think you should know it, since you won't understand nearly any article without that knowledge.
That being cleared, the problem in your code is, that you pass the variable by value (int menu(int Mystring)), while - in order to change it inside the function - you need to pass it by reference or pointer:
int menu(int &Mystring)
There are plenty of articles about functions in C++. You should check them out probably.