Hastable String/pointeur de fonction
Hello,
Je me demandais s'il était possible de faire une hashtable qui contiennent un pointeur de fonction.
Le but serait de pouvoir appeler une fonction en fonction d'un string...
j'ai essayé ca :
header.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
typedef void (*pointer_fct)();
public ref class PrerollTest :ITest {
private:
System::Collections::Hashtable^ map;
public:
PrerollTest(TestManager^ p_testManager);
~PrerollTest();
void run(String^ name);
//Test Method Declaration
void FileSourceToWindowsDest();
}; |
code.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
PrerollTest::PrerollTest(TestManager^ p_testManager)
{
//Create the map to store the function pointer and is name
this->map = gcnew System::Collections::Hashtable();
//get the string
String^ s = "File Source To Windows Dest";
//Add the function pointer into the map
this->map->Add(s,this->FileSourceToWindowsDest);
}
PrerollTest::~PrerollTest()
{
}
/*****************************************
* Virtual method declaration
*****************************************/
void PrerollTest::run(String^ name)
{
//name is the name of the pointer function that we want to launch
bool runAll;
if(name->Equals("Run all"))
{
for each(*pointer_fct p in map->Values)
{
p();
}
}
else
{
System::Collections::IDictionaryEnumerator^ e = map->GetEnumerator();
while ( e->MoveNext() )
if(e->Key->Equals(name))
{
*pointer_fct p = e->Value;
p();
}
}
}
/******************************************
* fonction
******************************************/
void PrerollTest::FileSourceToWindowsDest()
{
} |
Je trouve rien sur le net et je sèche pas mal sur comment résoudre ce problème... une solution serait d'utiliser la map<string *, pointer_fct> de std mais je voulais rester en CLI pour faire ca...
Si vous avez une idée ou repéré une erreur ou eu plus de chance que moi lors de vos recherche... bref... merci d'avance