C++ 中的成员函数指针for_each

发布于 2024-10-18 10:32:05 字数 1210 浏览 2 评论 0原文

我正在为一个学校项目开发一个 C++ 小型虚拟机,它应该像 dc 命令一样工作,由输入输出元件、芯片组、CPU 和 RAM 组成。我目前正在研究芯片组,其中我实现了一个小的解析类,以便能够从标准输入或文件中获取一些 Asm 指令,然后将这些指令推送到 CPU。

问题是:我的指令在 std::list 中排序,我希望能够使用 foreach 指令逐个推送它们。为此,我需要能够调用我的成员函数“push_instruction”作为 for_each 的函数指针 F;我找不到做到这一点的技巧......

有什么想法吗? 这是我的代码:

/*
** Function which will supervise
** the lexing and parsing of the input (whether it's 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);
}

I'm developing a small Virtual Machine in C++ for a school project, which should work like dc command, and is composed of a Input Output element, a Chipset, a Cpu and Ram. I'm currently working on the chipset, in which I've implemented a little parsing class in order to be able to get some Asm instructions from standard input or file, and then push this instructions to the Cpu.

The problem is: my instructions are sorted in a std::list, and I'd like to be able to push them each by each with a foreach instruction. To do that I need to be able to call my member function "push_instruction" as the function pointer F of for_each; and I wasn't able to find the trick to do that...

Any ideas?
here's my code:

/*
** Function which will supervise
** the lexing and parsing of the input (whether it's 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦在深巷 2024-10-25 10:32:05
std::for_each(
    _instructList.begin(), 
    _instructList.end(), 
    std::bind1st(std::mem_fun(&Chipset::pushInstruction), this));
std::for_each(
    _instructList.begin(), 
    _instructList.end(), 
    std::bind1st(std::mem_fun(&Chipset::pushInstruction), this));
吖咩 2024-10-25 10:32:05

当您不记得 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 的对象,并存储对该对象的引用。

然后,我定义一个 operator(),它接受您想要推送的指令,并调用 pushInstruction

for_each 循环中,我只需将 this 传递给我的构造函数,创建仿函数的一个实例,该实例将传递给 for_each,然后调用每个元素的 operator()

When you can't remember the syntax for the std::bind functions, it is sometimes easier to write a functor which just forwards to the member function:

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));

A bit of explanation:

I define a class which, in its constructor, takes the object we want to call pushInstruction on, and which stores a reference to the object.

Then I define an operator() hich takes the instruction you wish to push, and calls pushInstruction.

In the for_each loop, I just pass this to my constructor, creating an instance of the functor, which is passed to for_each, which then calls operator() on it for each element.

尘曦 2024-10-25 10:32:05

使用 boost::bind 作为,

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

Use boost::bind as,

std::for_each(/*..*/, /*..*/,
            boost::bind(&Chipset::pushInstruction, this, _1));
池予 2024-10-25 10:32:05

您可以bind1st您的this指针来获取应用于您的CPU的函子:(

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

注意:我故意在您的_instructionList中省略了下划线。它是不允许。)

You can bind1st your this pointer to obtain a functor applied on your cpu:

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

(Note: I deliberately left away the underscore from your _instructionList. It's not allowed.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文