l>heavencrossrecords.net - Yield in C++
*
This section of the archives stores heavencrossrecords.net"s complete Developer Toolbox collection,featuring a variety of mini-articles & source code contributions from our readers.
*
Yield in C++ Submitted by Chris Pergrossi
*
I"m currently working on an engine that relies heavily on the scripting language Python. Unfortunately, I"ve discovered major bottlenecks in relying on Python (mostly because it"s interpreted), & so am switching khổng lồ scripts written in C++ (compiled, but seperate lớn my engine, for ease of development). One of the major disadvantages to lớn doing this would be lớn loose some of the "cool" features of Python, such as generators, which allow you khổng lồ resume functions by using a "yield" keyword to signal you"re ready to quit, và would lượt thích to resume from that spot when the function is called again. Today, I solved the problem (took me about 15 minutes actually, so I can only assume many others have discovered this cool feature) by using a bit of ASM (sorry, windows only, though you could probably vày something similar on other systems) & some compiler generosity. I developed this using VC++ 6.0, though with minimal changes, it should work on other compilers as well. Well, here"s the code. It has two examples, one with a function with a return value, and the other has a function without one (the yield command is a macro, and doesn"t use up ANY parameter/return value space). I tried to phản hồi the macros and program as best I could, and one improvement that might be necessary (I don"t need it) is the ability lớn "reset" the function, to lớn execute from the beginning of the function (instead of wherever the last place you left off is). A cool side-effect of the way I did this was that the calling function doesn"t know or care if the function it"s calling is a "generator" or not, it just calls it more than once (same way) lớn yield/resume! Enjoy Chris PergrossiMy Realm email me at only_jolly_roger at hotmail dot com if you have questions, comments, improvements, or have used the tip in any of your projects.


Bạn đang xem: Yield c# là gì


Xem thêm: Bác Sĩ Chuyên Khoa Cấp 1 Là Gì, Bác Sĩ Chuyên Khoa 1 Là Gì

Feel không tính tiền to use the code any way you like, I"m just being curious.
#include #include #include //// marks a location in the program for resume// does not return control, exits function from inside macro//// yield( x, ret )// x : the "name" of the yield, cannot be ambiguous in the// function namespace// ret : the return value for when yield() exits the function;// must match function return type (leave blank for no return type)#define yield(x,ret) /* store the resume location */ __asm mov _myStaticMkr,offset label_##x /* return the supplied value */ return ret; /* our offset in the function */ label_##x://// resumes function from the stored offset, or// continues without notice if there"s not one// stored//// resume()// #define resume() /* our stored offset */ static _myStaticMkr=0; /* demo for no offset */ if( _myStaticMkr ) /* resume from offset */ __asm jmp _myStaticMkr // example demonstrating a function with an int return type// using the yield() & resume() macros//// myFunc()// int myFunc() resume(); cout yield(1,1); cout yield(2,1); cout yield(3,1); cout return 0;// main function//// main()// void main( void ) cout myFunc(); vày cout while( myFunc() ); cout.flush(); getch();/*// example demonstrating a function with no return type// using the yield() & resume() macros//// myFunc()// void myFunc() resume(); cout yield(1); cout yield(2); cout yield(3); cout return;// main function//// main()// void main( void ) cout myFunc(); for( int k = 0; k myFunc(); cout.flush(); getch();*/