- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试创建自己的小型 AVR 库。我想在代码中使用伪 OOP,目前我将“类”定义为结构。我正在考虑是否可以实现类似“this”关键字的东西。我想在分配给作为结构成员的函数指针的函数中获取结构对象。
我的代码:
#define __class__ struct
#define __inner_object__ struct
#define ALIAS(cls, stc) typedef __class__ cls stc
typedef uint8_t reg8_t;
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
typedef enum {
PIN_DIR_INPUT = 0,
PIN_DIR_OUTPUT = 1
} pin_direction_t;
typedef struct {
const reg8_t m_pin;
const reg8_t m_ddr;
const reg8_t m_port;
const u1 m_bit;
void (*setDirection)(pin_direction_t);
} pin_t;
void __setDirection(pin_direction_t direction)
{
// how to get struct object here?
pin_t* pin = container_of(__setDirection, pin_t, setDirection);
//uint8_t reg = reg_read(pin->m_ddr);
//if (direction == PIN_DIR_OUTPUT)
//reg_write(pin->m_ddr, reg | (1uL << pin->m_bit));
//else
//reg_write(pin->m_ddr, reg & ~(1uL << pin->m_bit));
}
__class__ at_mega8
{
__inner_object__
{
const reg8_t REG_PIND;
const reg8_t REG_DDRD;
const reg8_t REG_PORTD;
__inner_object__
{
const pin_t PIN_D7;
const pin_t PIN_D6;
const pin_t PIN_D5;
const pin_t PIN_D4;
const pin_t PIN_D3;
const pin_t PIN_D2;
const pin_t PIN_D1;
const pin_t PIN_D0;
} Pins;
} PortD;
__inner_object__
{
const reg8_t REG_PINC;
const reg8_t REG_DDRC;
const reg8_t REG_PORTC;
__inner_object__
{
const pin_t PIN_C6;
const pin_t PIN_C5;
const pin_t PIN_C4;
const pin_t PIN_C3;
const pin_t PIN_C2;
const pin_t PIN_C1;
const pin_t PIN_C0;
} Pins;
} PortC;
__inner_object__
{
const reg8_t REG_PINB;
const reg8_t REG_DDRB;
const reg8_t REG_PORTB;
__inner_object__
{
const pin_t PIN_B7;
const pin_t PIN_B6;
const pin_t PIN_B5;
const pin_t PIN_B4;
const pin_t PIN_B3;
const pin_t PIN_B2;
const pin_t PIN_B1;
const pin_t PIN_B0;
} Pins;
} PortB;
};
ALIAS(at_mega8, at_mega8_t);
#define M8_PIND 0x10
#define M8_DDRD 0x11
#define M8_PORTD 0x12
#define M8_PINC 0x13
#define M8_DDRC 0x14
#define M8_PORTC 0x15
#define M8_PINB 0x16
#define M8_DDRB 0x17
#define M8_PORTB 0x18
const at_mega8_t AtMega8 = {
{
M8_PIND, M8_DDRD, M8_PORTD,
{
{ .m_bit = 7, .m_pin = M8_PIND, .m_ddr = M8_DDRD, .m_port = M8_PORTD, .setDirection = __setDirection },
{ .m_bit = 6, .m_pin = M8_PIND, .m_ddr = M8_DDRD, .m_port = M8_PORTD, .setDirection = __setDirection },
{ .m_bit = 5, .m_pin = M8_PIND, .m_ddr = M8_DDRD, .m_port = M8_PORTD, .setDirection = __setDirection },
{ .m_bit = 4, .m_pin = M8_PIND, .m_ddr = M8_DDRD, .m_port = M8_PORTD, .setDirection = __setDirection },
{ .m_bit = 3, .m_pin = M8_PIND, .m_ddr = M8_DDRD, .m_port = M8_PORTD, .setDirection = __setDirection },
{ .m_bit = 2, .m_pin = M8_PIND, .m_ddr = M8_DDRD, .m_port = M8_PORTD, .setDirection = __setDirection },
{ .m_bit = 1, .m_pin = M8_PIND, .m_ddr = M8_DDRD, .m_port = M8_PORTD, .setDirection = __setDirection },
{ .m_bit = 0, .m_pin = M8_PIND, .m_ddr = M8_DDRD, .m_port = M8_PORTD, .setDirection = __setDirection },
}
},
{
M8_PINC, M8_DDRC, M8_PORTC,
{
{ .m_bit = 6, .m_pin = M8_PINC, .m_ddr = M8_DDRC, .m_port = M8_PORTC, .setDirection = __setDirection },
{ .m_bit = 5, .m_pin = M8_PINC, .m_ddr = M8_DDRC, .m_port = M8_PORTC, .setDirection = __setDirection },
{ .m_bit = 4, .m_pin = M8_PINC, .m_ddr = M8_DDRC, .m_port = M8_PORTC, .setDirection = __setDirection },
{ .m_bit = 3, .m_pin = M8_PINC, .m_ddr = M8_DDRC, .m_port = M8_PORTC, .setDirection = __setDirection },
{ .m_bit = 2, .m_pin = M8_PINC, .m_ddr = M8_DDRC, .m_port = M8_PORTC, .setDirection = __setDirection },
{ .m_bit = 1, .m_pin = M8_PINC, .m_ddr = M8_DDRC, .m_port = M8_PORTC, .setDirection = __setDirection },
{ .m_bit = 0, .m_pin = M8_PINC, .m_ddr = M8_DDRC, .m_port = M8_PORTC, .setDirection = __setDirection },
}
},
{
M8_PINB, M8_DDRB, M8_PORTB,
{
{ .m_bit = 7, .m_pin = M8_PINB, .m_ddr = M8_DDRB, .m_port = M8_PORTB, .setDirection = __setDirection },
{ .m_bit = 6, .m_pin = M8_PINB, .m_ddr = M8_DDRB, .m_port = M8_PORTB, .setDirection = __setDirection },
{ .m_bit = 5, .m_pin = M8_PINB, .m_ddr = M8_DDRB, .m_port = M8_PORTB, .setDirection = __setDirection },
{ .m_bit = 4, .m_pin = M8_PINB, .m_ddr = M8_DDRB, .m_port = M8_PORTB, .setDirection = __setDirection },
{ .m_bit = 3, .m_pin = M8_PINB, .m_ddr = M8_DDRB, .m_port = M8_PORTB, .setDirection = __setDirection },
{ .m_bit = 2, .m_pin = M8_PINB, .m_ddr = M8_DDRB, .m_port = M8_PORTB, .setDirection = __setDirection },
{ .m_bit = 1, .m_pin = M8_PINB, .m_ddr = M8_DDRB, .m_port = M8_PORTB, .setDirection = __setDirection },
{ .m_bit = 0, .m_pin = M8_PINB, .m_ddr = M8_DDRB, .m_port = M8_PORTB, .setDirection = __setDirection },
}
},
};
我可以在下面的例子中使用这段代码:
hd44780.pinout.pinRS = &AtMega8.PortB.Pins.PIN_B1;
hd44780.init();
// and somewhere else in hd44780:
// rsPin.setDirection(PIN_DIR_OUTPUT);
我知道有 container_of
宏允许通过其成员获取容器对象。但是当我尝试使用这个宏时,出现了错误:
error: dereferencing pointer to incomplete type
此错误指向 container_of
宏定义。
那么,我有两个问题。是否有可能在 avr-gcc 上的分配函数中获取结构对象?如果不是,是否可以在 gcc (windows) 上执行此操作?当然,我可以将结构对象作为参数传递,但这会很难看
最佳答案
简短的回答,不 - 要用 container_of
做你想做的事,你需要特定对象中函数指针的地址,而不是它指向的函数的地址,并得到您需要某种指向结构或传递给函数的成员之一的指针。
如果您查看 C++ 等在幕后所做的事情,它基本上也是将指针传递给结构。你只是看不到它的发生,因为语言对你隐藏了它。必须以某种方式告知函数您希望它与哪个对象一起工作这一事实是无法解决的 - 它无法通过魔法知道这一点。
关于c - (伪)C 中的 OOP 从其函数指针获取结构对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35374719/
如标题所示,我正在寻找有关伪/冒号 header 字段用途的一些信息,即我想知道为什么我们有第二种类型的 header 字段... 另外 - 我知道在 http2 中使用伪/冒号 header 字段代
(伪)多线程:借助外力 利用WEB服务器本身的多线程来处理,从WEB服务器多次调用我们需要实现多线程的程序。 QUOTE: 我们知道PHP本身是不支持多线程的, 但是我们的WEB服务器是支持多线程的
您如何在 HDL (verilog) 中实现硬件随机数生成器? 需要考虑哪些选项? 这个问题是在self-answer之后格式。鼓励添加答案和更新。 最佳答案 正如摩根的回答中所指出的,这只会产生一个
我写了这个CSS: div { width: 500px; height:150px; margin-left:150px; background: lightblue; } div:
这是我要解决的问题:从数据库A读取一个字符串,将该字符串转换为Date对象,将Date对象存储到数据库B中。 例)数据库A:从数据库A读入日期字符串“ 2015-03-08 02:00:00”,转换为
我想创建 std::fscanf() 的 sibling (我知道这是一个 C 函数)。所以,我的界面是这样的: template std::size_t ts_scanf(is, format,
运行 PostgreSQL 7.x(是的,我正在升级) 问题: 如果没有返回数据,我有三到四个字段需要设置。 正在考虑这样的事情 SELECT CASE WHEN default_fie
出于某种原因,我很难在 JS 中为我的游戏执行以下代码: 假设我们要求用户在棋盘上移动一个棋子。他们可以做的位置是位置A、位置B或位置C。每个位置一次只能容纳一件。否则为无效移动。 第一个用户决定
我已经毫无问题地编写了霍夫曼树的代码,但现在我希望在文件和树中添加伪 EOF,以便我知道何时停止从文件中读取。 我完全掌握了伪 EOF 的概念。我还了解到没有 ASCII 值 > 255 的字符。 我
给定一个按钮 ::after 当被触发时,伪 :after 类需要有一个类 search-active 切换,为按钮设置背景颜色 .primary .search:after, .primary
我想让第一行的文本像第二行一样缩进 (50px)。有什么办法吗?非常感谢! body{ counter-reset: h2counter; } h1{ counter-reset: h2counter
:before 或 :after 这样的伪元素是否可以从父元素的不同属性继承值? 在我的例子中,我有一个第三方组件设置其元素运行时的背景颜色...我需要继承该颜色并将其设置为伪元素的边框颜色。 最佳答
在并行循环中请求随机数总是返回相同的伪随机数。我怎样才能避免这种情况? % workers initialization: if matlabpool('size') == 0 matlabp
假设最大IP可以包含每个“点”括号中的最大数量999,即999.999.999.999 是最大的可用值。 我已经在计算器中检查了正则表达式 ([0-9]+.){3}[0-9]。那么,为什么程序抛出运行
我对随机数生成的概念非常陌生,我需要为用c编写的工作创建自己的算法(内置的随机数生成器对我不起作用)。 有人能给我介绍一个很好的主题,这样我就可以理解这个概念了吗?到目前为止,我所发现的一切似乎都是用
假设我有一个数字序列:{n, n+1, n+2, ... n+m} 在不提前存储数字的情况下,我想创建一个函数 f(),给定序列 {1,2,3,...m} 将以随机(或至少伪)的方式吐出原始集合随机)
什么是伪 tcp channel ,如何实现? 最佳答案 伪 TCP 是一种协议(protocol),它实现了 TCP 的一些思想,以通过不可靠的、基于数据包的接口(interface)提供可靠的数据
我正在尝试展开一些嵌套循环,以牺牲内存为代价(可能)获得更好的性能。在我的场景中,我最终会得到一个包含大约 3 亿个元素(元组)的列表,我必须以(或多或少)随机顺序产生这些元素。 在这个数量级上,ra
如何在 PHP 中生成(伪)随机字母数字字符串,例如:'d79jd8c'? 最佳答案 首先创建一个包含所有可能字符的字符串: $characters = 'abcdefghijklmnopqrstu
我有一段代码可以为玩家生成迷你任务。这很简单,要获得两个不同的点(起点和终点),我有一个如下所示的算法: std::vector missions; missions.push_bac
我是一名优秀的程序员,十分优秀!