Parser des commandes (recv) proprement/efficacement ?
Bonjour, je me permet de poster cette question, j'ai créer un petit programme client /serveur en C++. Cependant je me demandais si il y avait en C++ un moyen de parser proprement les commandes que je réceptionne avec rcv() pour éviter de me retrouver avec des if() else() à rallonge.
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| void MyTCPclient::parseCommande(char * buffer) {
if (strcmp ("com1",buffer) != 0) {
// appel fonction MyTCPclient::com1
}
if (strcmp ("com2",buffer) != 0) {
// appel fonction MyTCPclient::com1
}
if (strcmp ("com3",buffer) != 0) {
// appel fonction MyTCPclient::com1
}
...
} |
Pour avoir plutot un truc du genre (JAVA):
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
|
package test;
import java.lang.reflect.Method;
public class ClassMethodTest {
public static void main(String args[]) throws Exception {
Testing t = new Testing("val1", false);
Class tClass = t.getClass();
Method gs1Method = tClass.getMethod("getString1", new Class[] {});
String str1 = (String) gs1Method.invoke(t, new Object[] {});
System.out.println("getString1 returned: " + str1);
Method ss1Method = tClass.getMethod("setString1", new Class[] { String.class });
System.out.println("calling setString1 with 'val2'");
ss1Method.invoke(t, new Object[] { "val2" });
str1 = (String) gs1Method.invoke(t, new Object[] {});
System.out.println("getString1 returned: " + str1);
}
}
} |
En gros de pouvoir appeler toutes mes méthodes seulement en une condition. (je sais pas si c'est très clair ce que je raconte :aie:)