// NewProcess.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <stdarg.h> using namespace std; typedef unsigned int(*_do_sth)(); typedef void(*_fail_call)(unsigned int); typedef void(*_success_call)(unsigned int); unsigned int gResult = 0; void _process_start(); void _transation_start(); void _fail_rollback(_success_call a, _fail_call b, unsigned int result); void _fail_rollback_call(unsigned int result); void _success_submit_call(unsigned int result); void _transation_end(void(*_fail_rollback)(_success_call, _fail_call, unsigned int), _success_call a, _fail_call b, unsigned int result); void _sequence(_do_sth a, ...); unsigned int _do_A(); unsigned int _do_B(); #define _RESULTBITMAP gResult #define __process_start _process_start(); #define __fail_rollback _fail_rollback #define __transation_start _transation_start(); #define __transation_end(submitcall,rollbackcall) _transation_end(__fail_rollback,##submitcall,##rollbackcall,_RESULTBITMAP); #define __sequence _sequence #define __sequence_end ; #define __process_end ; /* DSL */ int _tmain(int argc, _TCHAR* argv[]) { __process_start { __transation_start { __sequence ( _do_A, _do_B ) __sequence_end } __transation_end(_success_submit_call, _fail_rollback_call) } __process_end return 0; } /* 业务函数 */ unsigned int _do_A() { cout << "Do A." << endl; return 0; /* ok */ //return 1; /* fail */ } unsigned int _do_B() { cout << "Do B." << endl; return 0; /* ok */ //return 1; /* fail */ } /* 执行框架相关函数 */ void _process_start() { cout << "Process start." << endl; } void _transation_start() { cout << "Transation start!" << endl; } void _transation_end(void(*_fail_rollback)(_success_call,_fail_call, unsigned int), _success_call a, _fail_call b, unsigned int result) { _fail_rollback(a, b, result); cout << "Transation end!" << endl; } void _fail_rollback(_success_call a, _fail_call b, unsigned int result) { /* 0-ok !0-fail */ if ((result>>31)) { cout << "Transation fail rollback..." << endl; b(result); } else { a(result); cout << "Transation ok!" << result << endl; } } void _fail_rollback_call(unsigned int result) { cout << "Trancation fail and rollback called." << endl; } void _success_submit_call(unsigned int result) { cout << "Trancation success and submit called." << endl; } void _sequence(_do_sth a, ...) { va_list ap; _do_sth do_it; va_start(ap, a); if (!a) { return; } if (!a()) { _RESULTBITMAP |= 1; } else { _RESULTBITMAP |= (1 << 31); return; } do_it = va_arg(ap, _do_sth); if (!do_it) { va_end(ap); return; } if (!do_it()) { _RESULTBITMAP |= (1 << 1); } else { _RESULTBITMAP |= (1 << 31); return; } do_it = va_arg(ap, _do_sth); if (!do_it) { va_end(ap); return; } if (!do_it()) { _RESULTBITMAP |= (1 << 2); } else { _RESULTBITMAP |= (1 << 31); return; } do_it = va_arg(ap, _do_sth); if (!do_it) { va_end(ap); return; } if (!do_it()) { _RESULTBITMAP |= (1 << 3); } else { _RESULTBITMAP |= (1 << 31); return; } do_it = va_arg(ap, _do_sth); if (!do_it) { va_end(ap); return; } if (!do_it()) { _RESULTBITMAP |= (1 << 4); } else { _RESULTBITMAP |= (1 << 31); return; } do_it = va_arg(ap, _do_sth); if (!do_it) { va_end(ap); return; } if (!do_it()) { _RESULTBITMAP |= (1 << 5); } else { _RESULTBITMAP |= (1 << 31); return; } do_it = va_arg(ap, _do_sth); if (!do_it) { va_end(ap); return; } if (!do_it()) { _RESULTBITMAP |= (1 << 6); } else { _RESULTBITMAP |= (1 << 31); return; } va_end(ap); cout << "Sequence do!" << endl; }