C ++ member function pointer for_each

I am developing a small virtual machine in C ++ for a school project that should work as a dc command and consists of an Input Output element, a chipset, a processor, and a Ram. I am currently working on a chipset in which I implemented a small parsing class to be able to retrieve ASM instructions from standard input or a file and then push these instructions onto the CPU.

The problem is that my instructions are sorted into std :: list, and I would like each of them to be able to click on the foreach command on them. To do this, I need to be able to call my member function "push_instruction" as a pointer to the F for_each function; and I could not find a trick to do this ...

Any ideas? here is my code:

/*
** Function which will supervise
** the lexing and parsing of the input (whether it std_input or a file descriptor)
** the memory pushing of operands and operators
** and will command the execution of instructions to the Cpu
*/
void                    Chipset::startExecution()
{
    /*
    ** My parsing 
    ** Instructions
    **
    */

    for_each(this->_instructList.begin(), this->_instructList.end(), this->pushInstruction);
}


void                    Chipset::pushInstruction(instructionList* instruction)
{
    if (instruction->opCode == PUSH)
      this->_processor->pushOperand(instruction->value, Memory::OPERAND, Memory::BACK);
    else if (instruction->opCode == ASSERT)
      this->_processor->pushOperand(instruction->value, Memory::ASSERT, Memory::BACK);
    else
      this->_processor->pushOperation(instruction->opCode);
}
+5
4
std::for_each(
    _instructList.begin(), 
    _instructList.end(), 
    std::bind1st(std::mem_fun(&Chipset::pushInstruction), this));
+11

std::bind, , -:

struct PushInstruction {
  PushInstruction(Chipset& self) : self(self) {} 

  void operator()(instructionList* instruction) {
    self.pushInstruction(instruction);
  }    

  Chipset& self;    
};

std::for_each(_instructList.begin(), _instructList.end(), PushInstruction(*this));

:

, , pushInstruction on, .

operator(), , , pushInstruction.

for_each this , , for_each, operator() .

+3

boost::bind ,

std::for_each(/*..*/, /*..*/,
            boost::bind(&Chipset::pushInstruction, this, _1));
+2

You can get a bind1stpointer thisfunctor applied to your processor:

std::foreach( instructList.begin(), instructList.end(),
      std::bind1st
          ( std::mem_fun( &CChipSet::pushInstruction )
          , this 
          )
      );

(Note. I have intentionally removed the underline from yours _instructionList. This is not permitted.)

+2
source

All Articles