Unsolved How to create attribute like python flask's route?
-
I have a struct list for functions and i use those for input a switch case method.
But i add functions by manually. How can i create a attribute for auto recognizing like flask?typedef struct { int a; void (*print)(int x); } _w_md; QList<_w_md> tasklist; // Some macro/attribute here void my_print_func(int x){ //***** } int main(){ _w_md job = { 1, my_print_func }; tasklist.push_back(job); //switch-case and if statements for running true function //like run if(_w_md.a == job.a) job.print(1234) }
-
Hi
I have no idea what flask does.
However, if u need a list of tasks with the same function like DoJob
and different implementations, you can use base and subclasses
http://www.cplusplus.com/doc/tutorial/polymorphism/ -
Hi,
If you are thinking about the decorator pattern from Python, AFAIK, there's no direct equivalent in C++.
-
@mrjj said in How to create attribute like python flask's route?:
I have no idea what flask does.
Flask is python web framework. http://flask.pocoo.org/
-
@Taz742
Thank you but it was more what the route thing did. -
@mrjj
Not that I know anything having never used Flask(!), but I think@app.route('/hello') def hello(): return 'Hello, World!'
The
@app.route('/hello')
directive tells a Flask application that a web request for url/hello
should be responded to by routing the request to the method with that attribute, i.e. thedef hello()
in this case.This allows the programmer to produce an interface by declaratively decorating desired functions with
@app.route()
s, which I suppose is "neat".The OP is saying that to achieve that at present he is instead maintaining a large
struct
of all the desired functions to call, and writing largeswitch
/if
s to call the corresponding function in response to a given list of urls. He maintains that struct list manually, does not like doing so, and would like "Some macro/attribute" he can put on his functions instead which will magically do the job for him.So unless you can think of a way he can emulate this in C++/Qt (e.g. via its metatypes??), he is going to have soldier on manually....
-
@JonB
ah. thank you.
so basically it tags code for a given URL
/xxx -> func a
/aa/yy -> func b
or is it more complex ? -
@mrjj
Who knows? Like I said I've never used Flask!app.route
doesself.add_url_rule(rule, endpoint, f, **options)
[
f
is the decorated function] so that gives an idea of what it does.The gist is along the line of http://flask.pocoo.org/docs/1.0/patterns/viewdecorators/
View Decorators
Python has a really interesting feature called function decorators. This allows some really neat things for web applications. Because each view in Flask is a function, decorators can be used to inject additional functionality to one or more functions. The route() decorator is the one you probably used already.
So I suspect the behaviour is tied to using Python for this kind of feature....
-
@JonB
oh. so basically its a decorator pattern.
Just much smoother to look at than anything possible with c++ :)