int CPos::short_pos(day_price_t &dp, int percent) { int date = dp.date; int price = dp.open; assert(m_status == ST_OPEN); assert(date >= m_begin_date && m_end_date == 0); int count = m_init_count / percent; if (count > m_curr_count) count = m_curr_count; m_curr_count -= count; if (m_curr_count == 0){ m_status = ST_CLOSE; m_end_date = date; } m_curr_cost = price; COperation *op = new COperation(this, date, DI_SELL, count, price); m_ops[date] = op; //we get fix_profit now profit_t *p = &m_fix_profit; p->fee += op->get_fee(); p->cash_profit += count * (price - m_init_cost); p->float_profit = m_curr_count * (price - m_init_cost) + p->cash_profit; p->total_profit = p->cash_profit - p->fee; return p->total_profit; }
CPos:: CPos(CStock *owner, int begin_date, int count, int cost) { m_db = owner->get_db(); m_owner = owner; m_begin_date = begin_date; m_end_date = 0; m_init_count = m_curr_count = count; m_init_cost = m_curr_cost = cost; m_status = ST_OPEN; COperation *op = new COperation(this, begin_date, DI_BUY, count, cost); m_ops[begin_date] = op; m_fix_profit.fee = op->get_fee(); }
/// Process the operation queue in FIFO order. /// /// @param[in] poRunner the execution thread instance invoking this method void CAsynchronous::MakeCallbacks (const CThread *poRunner) { LOGDEBUG (TEXT ("Waiting for thread control semaphore")); m_semThread.Wait (); LOGDEBUG (TEXT ("Thread control semaphore signalled")); while (!m_bPoison) { EnterCriticalSection (); if (poRunner != m_poRunner) goto abandonLoop; while (!m_poHead) { m_bWaiting = true; LeaveCriticalSection (); LOGDEBUG ("List is empty - waiting on semaphore"); if (!m_semQueue.Wait (GetTimeoutInactivity ())) { LOGDEBUG ("Thread inactivity timeout reached"); EnterCriticalSection (); // Abandon if thread recycling has happened if (poRunner != m_poRunner) goto abandonLoop; if (!m_poHead) { LOGINFO ("Terminating idle callback thread"); CThread::Release (m_poRunner); m_poRunner = NULL; m_bWaiting = false; LOGDEBUG (TEXT ("Signalling thread control semaphore")); m_semThread.Signal (); LeaveCriticalSection (); return; } else { LOGDEBUG ("Operation received on idle timeout boundary"); } LeaveCriticalSection (); } if (m_bPoison) { LOGINFO ("Thread poisoned"); goto abortLoop; } EnterCriticalSection (); if (poRunner != m_poRunner) goto abandonLoop; m_bWaiting = false; } COperation *poOperation = m_poHead; if (!(m_poHead = poOperation->m_poNext)) { m_poTail = NULL; } LeaveCriticalSection (); LOGDEBUG ("Calling run on asynchronous operation"); poOperation->m_nMustReschedule = -poOperation->m_nMustReschedule; poOperation->Run (); if (poOperation->m_nMustReschedule > 0) { if (poOperation->m_nMustReschedule >= (GetTimeoutAbortPeriod () / GetTimeoutReschedule ())) { LOGERROR ("Aborting operation after " << poOperation->m_nMustReschedule << " attempts"); goto abortLoop; } else { if ((poOperation->m_nMustReschedule % (GetTimeoutInfoPeriod () / GetTimeoutReschedule ())) == 0) { LOGINFO ("Rescheduling operation " << poOperation->m_nMustReschedule << " times"); } else { LOGDEBUG ("Rescheduling operation " << poOperation->m_nMustReschedule << " time(s)"); } } EnterCriticalSection (); if (poOperation->OnScheduled ()) { if (!(poOperation->m_poNext = m_poHead)) { m_poTail = NULL; } m_poHead = poOperation; } else { LOGDEBUG (TEXT ("Operation rejected reschedule")); delete poOperation; } LeaveCriticalSection (); CThread::Sleep (GetTimeoutReschedule ()); } else { LOGDEBUG ("Deleting asynchronous operation"); delete poOperation; } } abortLoop: LOGDEBUG ("Callback thread aborting"); EnterCriticalSection (); // Fall through to abandonLoop abandonLoop: // Already in critical section when called from above while (m_poHead) { COperation *poOperation = m_poHead; m_poHead = poOperation->m_poNext; if (poOperation->m_bVital) { LOGINFO (TEXT ("Running vital operation")); poOperation->Run (); } else { LOGDEBUG (TEXT ("Discarding operation")); } delete poOperation; } m_poTail = NULL; if (m_poRunner != poRunner) { // Thread recycling has happened LOGDEBUG (TEXT ("Signalling thread control semaphore")); m_semThread.Signal (); LOGINFO (TEXT ("Callback thread abandonned")); } else { CThread::Release (m_poRunner); m_poRunner = NULL; LOGINFO (TEXT ("Callback thread aborted")); } m_bWaiting = false; LeaveCriticalSection (); return; }
double GetResult() { return op->GetResult(); }
int main() { // The number that the variable "x" will point to double_complex x; // Creates a variable named "x" and which value will be x CVar xvar ( "x" , &x ); // Asks for a fomula depending on the variable x, e.g. "sin 2x" char s[500]=""; printf("Enter a formula depending on the variable x:\n"); gets(s); // Creates an operation with this formula. The operation depends on one // variable, which is xvar ; the third argument is an array of pointers // to variables; the previous argument is its size CVar* vararray[1]; vararray[0]=&xvar; COperation op ( s, 1, vararray ); // Affects (indirectly) a value to xvar x=3; // Printfs the value of the formula for x=3; printf("%s = %s for x=3\n\n", op.Expr(), PrettyPrint(op.Val()) ); // Creates a function name which can be used in later functions to refer to the operation op(x) CFunction f (op, &xvar); f.SetName("f"); // Creates a second variable named y, and a formula depending on both x and y double_complex y; CVar yvar ( "y" , &y ); CVar* vararray2[2]; // table of variables containing the adresses of xvar and yvar vararray2[0]=&xvar; vararray2[1]=&yvar; // Asks for a formula using x, y and the already-defined function f, e.g. x+f(3y) printf("Enter a formula using x, y and the function f(x): x -> %s that you just entered, e.g. x+f(3y) :\n", op.Expr()); gets(s); CFunction* funcarray[1]; funcarray[0]=&f; COperation op2 ( (char*)s , 2 , vararray2 , 1, funcarray ); // vararray2 is a CVar* array with two elements // funcarray is a CFunction* array with one element y=5; printf("Value for x=3, y=5 : %s\n", PrettyPrint(op2.Val()) ); // Turns the last expression into a function of x and y CFunction g(op2, 2, vararray2); g.SetName("g"); // Here is another way to do it double_complex z,t; CVar zvar("z", &z), tvar("t", &t); COperation op3,zop,top; zop=zvar; top=tvar; // constructs, from a variable, the operation returning its value op3=g( (zop+top, top^2) ); // Ready-to-use ; needs two pairs of ( ) // Now op3 contains the operation op2 with x replaced with z+t, and y replaced with t^2 z=5;t=7; printf("\nLet g be the function g : x,y -> %s\n", op2.Expr()); printf("Value of %s for z=5,t=7:\n %s\n", op3.Expr(), PrettyPrint(op3.Val()) ); COperation dopdt=op3.Diff(tvar); // Computes the derivative of op3 w.r.t t printf("Value of d/dt (g(z+t,t^2)) = %s for z=5,t=7:\n %s\n", dopdt.Expr(), PrettyPrint(dopdt.Val()) ); COperation dopdtbar=op3.DiffConj(tvar); // Computes the derivative of op3 w.r.t the conjugate of t printf("Value of d/dtbar (g(z+t,t^2)) = %s for z=5,t=7:\n %s\n", dopdtbar.Expr(), PrettyPrint(dopdtbar.Val()) ); return 0; }
void CCommandCore::SetOperation(const COperation& operation) { CXMLElement* p_ele = GetRootCommandElement(); p_ele->SetAttribute("command",operation.GetStringForm()); }
int test_SimplyFactory1() { //CCalculatorFactory* calFactory = new CCalculatorFactory(); int type = 0; double a = 10, b = 2; type = 1; //COperation* operation = calFactory->CreateOperation((PRODUCTTYPE)type, a, b); COperation* operation = CCalculatorFactory::CreateOperation((PRODUCTTYPE)type, a, b); if (operation) { std::cout << operation->GetResult() << std::endl; delete operation; operation = NULL; } type = 2; //operation = calFactory->CreateOperation((PRODUCTTYPE)type, a, b); operation = CCalculatorFactory::CreateOperation((PRODUCTTYPE)type, a, b); if (operation) { std::cout << operation->GetResult() << std::endl; delete operation; operation = NULL; } type = 3; //operation = calFactory->CreateOperation((PRODUCTTYPE)type, a, b); operation = CCalculatorFactory::CreateOperation((PRODUCTTYPE)type, a, b); if (operation) { std::cout << operation->GetResult() << std::endl; delete operation; operation = NULL; } type = 4; //operation = calFactory->CreateOperation((PRODUCTTYPE)type, a, b); operation = CCalculatorFactory::CreateOperation((PRODUCTTYPE)type, a, b); if (operation) { std::cout << operation->GetResult() << std::endl; delete operation; operation = NULL; } /*if (calFactory) { delete calFactory; calFactory = NULL; }*/ /*result COperation constructor CAddOperation constructor 12 CAddOperation destructor COperation destructor COperation constructor CSubOperation constructor 8 CSubOperation destructor COperation destructor COperation constructor CMulOperation constructor 20 CMulOperation destructor COperation destructor COperation constructor CDivOperation constructor 5 CDivOperation destructor COperation destructor */ return 0; }