如何在 C++ 中将远程设备映射为数组?

发布于 2024-09-07 08:02:53 字数 501 浏览 2 评论 0原文

我想创建一个 C++ 对象来包装外部外围设备的 RAM。我正在尝试设置如下内容:

Peripheral p;

p[4] = 10;

int n = p[5];

为此,我需要在访问数组元素时读取或写入外围设备。 我无法弄清楚如何使用运算符重载等来做到这一点。我可以返回一个“访问器”对象,该对象可以用作第二行中的左值:

PeripheralAccessor Peripheral::operator[](int i);

或者我可以定义一个“简单”运算符,可用于从第三行的外设读取 int:

int Peripheral::operator[](int i);

但我无法让两者共存以提供对外围设备的读写访问权限。我可以将第二个运算符定义为 const (右值)运算符,但只会为该类的 const 实例调用它,这不足以满足我的需求......

希望我已经解释了我正在尝试的内容明确地实现这里;有人可以建议我应该如何做(或者实际上是否可能)?

I would like to create a C++ object to wrap the RAM of an external peripheral device. I'm trying to set up something like the following:

Peripheral p;

p[4] = 10;

int n = p[5];

To do this I need to read or write to the peripheral whenever an array element is accessed.
I cannot work out how to do this using operator overloading etc. I can return an "accessor" object that can be used as an lvalue in the second line:

PeripheralAccessor Peripheral::operator[](int i);

or I can define a "simple" operator that can be used to read an int from the peripheral on the third line:

int Peripheral::operator[](int i);

but I cannot get the two to coexist to give both read and write access to the peripheral. I can define this second operator as a const (rvalue) operator, but it will ONLY then be called for a const instance of the class, which isn't enough for my needs...

Hopefully I've explained what I'm trying to achieve here clearly; can anybody suggest how I should be doing it (or indeed whether it is possible)?

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

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

发布评论

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

评论(3

月下凄凉 2024-09-14 08:02:53

处理这个问题的常用方法是使用代理对象:

class register_proxy { 
public:
    register_proxy &operator=(int value) { 
        write_value(value);
    }

    operator int() {
        return read_value();
    }
};

class peripheral { 
    register_proxy registers[4];
public:

    register_proxy &operator[](int reg_num) { return registers[reg_num]; }
};

我省略了一些细节(例如如何告诉每个 register_proxy 要读/写的实际寄存器号 - 通常通过将参数传递给演员)。无论如何,总体思路非常简单:operator[] 返回对代理对象的引用。当您在赋值的右侧使用代理对象的operator int(当然,或者它采用的任何类型)时,它会被调用。当代理对象的 operator= 位于赋值的左侧时,它会被调用,因此您尝试在那里写入一个值。

The usual way to handle this is to use a proxy object:

class register_proxy { 
public:
    register_proxy &operator=(int value) { 
        write_value(value);
    }

    operator int() {
        return read_value();
    }
};

class peripheral { 
    register_proxy registers[4];
public:

    register_proxy &operator[](int reg_num) { return registers[reg_num]; }
};

I've left out some details (like how you tell each register_proxy what actual register number to read/write -- usually by passing an argument to the ctor). In any case, the general idea is pretty simple: operator[] returns a reference to a proxy object. The proxy object's operator int (or whatever type it takes, of course) gets invoked when you use it on the right side of an assignment. The proxy object's operator= gets invoked when it's on the left side of the assignment, so you're trying to write a value there.

只有一腔孤勇 2024-09-14 08:02:53

为您的“PeripheralAccessor”提供一个 int 的隐式转换运算符:

operator int () const;

Give your "PeripheralAccessor" an implicit conversion operator for int:

operator int () const;
黑白记忆 2024-09-14 08:02:53

由于您无法根据返回类型进行重载,因此您必须选择其中之一(并且不能选择 int,因为这样您就无法获得左值行为)。但是,您可以将 PeripheralAccessor 自动转换为 int。

编辑:杰瑞打败了我。甚至还有代码。

Since you can't overload based on return type, you have to choose one or the other (and you can't choose int, 'cause then you don't get the lvalue behavior). However, you can give PeripheralAccessor an automatic conversion to int.

Edit: Jerry beat me. With code even.

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