Yield C# Là Gì

  -  
l>luyenkimmau.com.vn - Yield in C++
*
This section of the archives stores luyenkimmau.com.vn"s complete Developer Toolbox collection,featuring a variety of mini-articles and 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 Pyhẹp. Unfortunately, I"ve sầu discovered major bottlenecks in relying on Pykhiêm tốn (mostly because it"s interpreted), và so am switching khổng lồ scripts written in C++ (compiled, but seperate khổng lồ my engine, for ease of development). One of the major disadvantages khổng lồ doing this would be lớn loose some of the "cool" features of Python thả, such as generators, which allow you khổng lồ resume functions by using a "yield" từ khóa to lớn signal you"re ready khổng lồ quit, & would like 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 sầu discovered this cool feature) by using a bit of ASM (sorry, windows only, though you could probably vì something similar on other systems) and some compiler generosity. I developed this using VC++ 6.0, though with minimal changes, it should work on other compilers as well.


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


Xem thêm: Làm Quen Với Retrofit Là Gì, Làm Quen Với Retrofit 2 Http Client

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, và doesn"t use up ANY parameter/return value space). I tried to lớn bình luận the macros and program as best I could, và one improvement that might be necessary (I don"t need it) is the ability lớn "reset" the function, to lớn exeđáng yêu 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) to yield/resume! Enjoy Chris PergrossiMy Realm Thư điện tử 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. Feel không lấy phí lớn use the code any way you like, I"m just being curious.


Xem thêm: Bạn Đang Tìm Hiểu Agile Là Gì Và Các Phương Pháp Kiểm Thử Agile

#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 sầu blank for no return type)#define yield(x,ret) /* store the resume location */ __asm mov _myStaticMkr,offmix label_##x /* return the supplied value */ return ret; /* our offphối 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 offphối */ static _myStaticMkr=0; /* kiểm tra for no offset */ if( _myStaticMkr ) /* resume from offmix */ __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();*/