How to remove this assembly code from C ++ code?

My environment is Visual Studio 2008.

I have 3 different libraries. In short, their behavior is equivalent to the following.

Library 1 - offers features for registration

class FunctionRegistry
{
   typedef std::list<int> TListInt;
   TListInt m_Params;
   void * m_FPtr;
public:
   FunctionRegistry(void * fptr):m_FPtr(fptr){}
   FunctionRegistry& Insert(int value){m_Params.push_back(value); return *this;}
   void Call();
};

void FunctionRegistry::Call()
{
    /*this is the area I want to get rid of */
    TListInt::iterator ite = m_Params.begin();
    while (ite != m_Params.end());
    {
       __asm push *ite;//I want to remvoe this assmebly code somehow
       ++ite;//Edited after initial post
    }

    ((void(*)())m_FPtr)();

    ite = m_Params.begin();
    while (ite != m_Params.end());
    {
        int i = 0;
        __asm pop i;//I want to remvoe this assmebly code somehow
        ++ite;//Edited after initial post
    }
    /*end of area to get rid of*/
}

Library 2 . Defines a set of functions such as

void foo(int i, int j)
{
    std::cout << "foo int int " << i << " " << j << std::endl;
}

void bar(int i)
{
    std::cout << "bar int " << i << std::endl;
}

void biz()
{
    std::cout << "biz" << std::endl;
}

FunctionRegistry fRegFoo((void*)&foo);
fRegFoo.Insert(10).Insert(20);

FunctionRegistry fRegBar((void*)&bar);
fRegBar.Insert(10);

FunctionRegistry fRegBiz((void*)&biz);

Library 3 . This user is above two libraries, and an example of use would be

fRegFoo.Call();
fRegBar.Call();
fRegBiz.Call();

, . ( API) 1, . , , ( 3), API 1, .

, 1 API ?

, - API, 3.

+4
4

API, " 3", , , . API , - . Lib 2, Lib 1.

Call. .

, , . API, 3, , / , "", " ". .


: , : API Lib 3 , . , Lib 2 Lib 3 , Lib 1 , , .

, : , . [ Windows/VS, , 20 , Unix. .]

+3

push pop asm, switch-statement, 256 case-statements. ( case-statement?)

void FunctionRegistry::Call()
{
    typedef void (*_F)(...);
    _F f = (_F)m_FPtr;

    auto p=*m_Params.begin();
    switch(m_Params.size())
    {
        case 0: f();break;
        case 1: f(p); break;
        case 2: f(p, p+1); break;
        //...
        //case 256: f( ...please get therapy... ); break
    }
}
+1

100%, , ite i . , , asm push/pop std:: stack. - :

#include <stack>
//...
void FunctionRegistry::Call()
{
    std::stack<TListInt::value_type> astack;
    TListInt::iterator ite = m_Params.begin();
    while (ite != m_Params.end());
    {
       astack.push(*ite);
       ++ite;//Edited after initial post
    }

    ((void(*)())m_FPtr)();

    ite = m_Params.begin();
    while (ite != m_Params.end());
    {
        int i = astack.pop();
        ++ite;//Edited after initial post
    }
}
0

boost:: bind? - :

#include <boost/bind/bind.hpp>
class FunctionRegistry
{
  typedef void tProcedure (void);

  private:
    const tProcedure proc;

  public:
    explicit FunctionRegistry( tProcedure proc ) : proc(proc) {}
    void Call() const { proc(); }

};

const FunctionRegistry fRegFoo( boost::bind( foo, 10, 20 ) );
0

Source: https://habr.com/ru/post/1547236/


All Articles