在C++中,是否可以将任意指针作为输入参数

Is it possible to take an arbitrary pointer as input argument in C++?

本文关键字:指针 任意 参数 输入 C++ 是否      更新时间:2023-10-16

我想创建一个类来管理Cortex-M3设备的位带功能。对于那些不知道的人:处理器将特定区域内的每一位映射到一个完整的单词。这允许对特定位进行原子集操作。类本身使用std::uintptr_t。对于构造函数,我希望允许一个任意指针,因为我不在乎它指向什么。它可能是设备头中定义的某个结构。

我当前的实现为构造函数提供了:

Bitband(uintptr_t address, uint32_t bitNumber);
Bitband(void * ptr, uint32_t bitNumber);

我的应用程序调用构造函数如下:

Bitband foo(reinterpret_cast<uintptr_t>(&gpioPort->IDR), pin);

如果我去掉他reinterpret_cast,我就不会得到"uintptr_t"answers"void*"的已知转换。有没有一种干净的方法可以消除每次调用的repret_cast,并将任意指针作为构造函数的参数?

编辑:这是我当前的Bitband类代码,以及我打开或关闭led的用途:

位带.hpp

#pragma once
#include <stdint.h>
class Bitband
{
public:
Bitband(uintptr_t address, uint32_t bitNumber);
Bitband(void * address, uint32_t bitNumber);
inline void Set(bool val) const
{
uint32_t * const pData = reinterpret_cast<uint32_t *>(this->bbAddress);
*pData = val;
}
inline bool Get() const 
{
uint32_t * const pData = reinterpret_cast<uint32_t *>(this->bbAddress);
return *pData;
}
private:
static uintptr_t GetBitBandAddress(uintptr_t address, uint32_t bit);
static bool IsSramAddress(uintptr_t address);
static bool IsPeripheralAddress(uintptr_t address);
uintptr_t const bbAddress;
/* Constants for bit band calculation for SRAM */
static uintptr_t const sramStartAddress = 0x20000000;
static uintptr_t const sramEndAddress   = 0x200FFFFF;
static uintptr_t const sramBbBaseAddress = 0x22000000;
/* Constants for bit band calculation for Peripherals */
static uintptr_t const peripheralsStartAddress = 0x40000000;
static uintptr_t const peripheralsEndAddress   = 0x400FFFFF;
static uintptr_t const peripheralsBbBaseAddress = 0x42000000;
};

位带.cpp

#include "bitband.hpp"
#include <cassert>
Bitband::Bitband(uintptr_t address, uint32_t bitNumber) :
bbAddress(GetBitBandAddress(address, bitNumber)) {}
Bitband::Bitband(void * address, uint32_t bitNumber) :
bbAddress(GetBitBandAddress(reinterpret_cast<uintptr_t>(address), bitNumber)) {}
uintptr_t Bitband::GetBitBandAddress(uintptr_t const address,
uint32_t const bitNumber)
{
uintptr_t bbBase;
uintptr_t regionStartAddress;
assert(Bitband::IsPeripheralAddress(address)
|| Bitband::IsSramAddress(address));
/* Set the parameters depending on wether we are in peripherals region or sram
region. */
if(Bitband::IsSramAddress(address))
{
bbBase = Bitband::sramBbBaseAddress;
regionStartAddress = Bitband::sramStartAddress;
}
else if(Bitband::IsPeripheralAddress(address))
{
bbBase = Bitband::peripheralsBbBaseAddress;
regionStartAddress = Bitband::peripheralsStartAddress;
}
else
{
/* Invalid parameter */
__breakpoint(0);
}
uintptr_t byteOffset = address - regionStartAddress;
auto bitWordOffset = (byteOffset * 32) + (bitNumber * sizeof(uint32_t));
auto bitWordAddr = bbBase + bitWordOffset;
return bitWordAddr;
}
bool Bitband::IsSramAddress(uintptr_t address)
{
return (address >= Bitband::sramStartAddress)
&& (address <= Bitband::sramEndAddress);
}
bool Bitband::IsPeripheralAddress(uintptr_t address)
{
return (address >= Bitband::peripheralsStartAddress)
&& (address <= Bitband::peripheralsEndAddress);
}

它被我的班级使用(用于测试,我只打开/关闭一些led)

led.hpp

#pragma once
#include <stdint.h>
#include "stm32l1xx.h"                  // Keil::Device:Startup
#include "bitband.hpp"
class Led
{
public:
Led(GPIO_TypeDef * const ledPort, uint16_t ledPin);
inline void Set(bool newState) { this->ledOutputBitBand.Set(!newState); }
private:
Bitband ledOutputBitBand;
};

led.cpp

#include <led.hpp>
#include <cassert>
Led::Led(GPIO_TypeDef * const port, uint16_t const pin) :
ledOutputBitBand(reinterpret_cast<uintptr_t>(&port->ODR), pin)
{
assert(pin < 16);
/* Set port mode to push pull */
port->MODER |= 1 << ( 2 * pin);
}

在主应用程序中使用

Led greenLed(GPIOD, 0);
greenLed.Set(true);

如果我忽略了interpret_cast,我会得到以下消息:

Src/led.cpp(5): error: no matching constructor for initialization of 'Bitband'
ledOutputBitBand(&port->ODR, pin)
^                ~~~~~~~~~~~~~~~
./Inc/bitband.hpp(9): note: candidate constructor not viable: no known conversion from 'volatile uint16_t *' (aka 'volatile unsigned short *') to 'uintptr_t' (aka 'unsigned int') for 1st argument; remove &
Bitband(uintptr_t address, uint32_t bitNumber);
^
./Inc/bitband.hpp(10): note: candidate constructor not viable: no known conversion from 'volatile uint16_t *' (aka 'volatile unsigned short *') to 'void *' for 1st argument
Bitband(void * address, uint32_t bitNumber);
^
./Inc/bitband.hpp(6): note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
class Bitband
^
./Inc/bitband.hpp(6): note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided
1 error generated.

所以我想,如果我想在另一个上下文中使用类Bitband,我就必须再次使用interpret_cast?

考虑到您提供的需求,实现这一目标的干净方法。

我真的不明白你打算调用哪个构造函数(你没有明确你的目标),但不管怎样,reinterpret_cast似乎最接近于合理的。

它很冗长,目的是提醒您应该重新审视您的设计,并提出一个一开始就不需要这种转换的设计。因为我们不再生活在20世纪70年代

不过,如果你坚持旧的风格,你可以使用C风格的演员阵容:

Bitband foo((void*)&gpioPort->IDR, pin);

我恳求你不要这样做。