J
Johannes Bauer
Hi folks,
I'm writing a microcontroller abstraction library in C++0x, mainly to
try out how well suited it is for embedded purposes and play around with
some of the new exciting 0x-features.
I have a construction of nested classes, where the outermost defines a
memory map (e.g. a memory-mapped UART representation), then that memory
map has registers (e.g. a baudrate register), those nest bitfields (e.g.
a enum for some clock divider values).
This works actually very beautifully and allows me to do some neat
tricks. There is one small thing, however, where I am not sure how to
solve it more nicely using 0x's features. Consider this innermost class
representing some imaginary clock divider bitfield:
class Bf_CLKDIV {
public:
enum class Enum : uint32_t {
DIVBY2 = 0 << 16,
DIVBY4 = 1 << 16,
DIVBY8 = 2 << 16,
DIVBY16 = 3 << 16,
};
void operator=(enum class Enum aValue) {
myPeriphMemory.TESTDIR = (myPeriphMemory.TESTIS &
~(0x30000)) | (uint32_t)aValue;
}
} CLKDIV;
For the sake of simplicity, consider myPeriphMemory a global variable.
Now in code, I can assign values to that enum quite nicely:
portFoo.TESTDIR.CLKDIV = D_TEST_APB::Reg_TESTDIR::Bf_CLKDIV::Enum:IVBY4;
However, since "D_TEST_APB::Reg_TESTDIR::Bf_CLKDIV::Enum::" is the only
type which matches operator= "DIVBY4" by itself would be unambiguous.
The compiler obviously rejects
portFoo.TESTDIR.CLKDIV = DIVBY4;
Is there some way, maybe by using the auto keyword to make the above
syntax look nicer? To make to compiler more willing to choose the scope
itself, if it is unambiguous?
And, by the way, since I declared my enum like this
enum class Enum : uint32_t
Is there a way to extract the underlying integer type during compile
time without having it to restate again as I do in:
myPeriphMemory.TESTDIR = (myPeriphMemory.TESTIS & ~(0x30000)) |
(uint32_t)aValue;
with the explicit cast? Something like (imaginary syntax here)
myPeriphMemory.TESTDIR = (myPeriphMemory.TESTIS & ~(0x30000)) |
(typeof(enum class Enum))aValue;
Best regards,
Johannes
--
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos über Rüdiger Thomas in dsa <[email protected]>
I'm writing a microcontroller abstraction library in C++0x, mainly to
try out how well suited it is for embedded purposes and play around with
some of the new exciting 0x-features.
I have a construction of nested classes, where the outermost defines a
memory map (e.g. a memory-mapped UART representation), then that memory
map has registers (e.g. a baudrate register), those nest bitfields (e.g.
a enum for some clock divider values).
This works actually very beautifully and allows me to do some neat
tricks. There is one small thing, however, where I am not sure how to
solve it more nicely using 0x's features. Consider this innermost class
representing some imaginary clock divider bitfield:
class Bf_CLKDIV {
public:
enum class Enum : uint32_t {
DIVBY2 = 0 << 16,
DIVBY4 = 1 << 16,
DIVBY8 = 2 << 16,
DIVBY16 = 3 << 16,
};
void operator=(enum class Enum aValue) {
myPeriphMemory.TESTDIR = (myPeriphMemory.TESTIS &
~(0x30000)) | (uint32_t)aValue;
}
} CLKDIV;
For the sake of simplicity, consider myPeriphMemory a global variable.
Now in code, I can assign values to that enum quite nicely:
portFoo.TESTDIR.CLKDIV = D_TEST_APB::Reg_TESTDIR::Bf_CLKDIV::Enum:IVBY4;
However, since "D_TEST_APB::Reg_TESTDIR::Bf_CLKDIV::Enum::" is the only
type which matches operator= "DIVBY4" by itself would be unambiguous.
The compiler obviously rejects
portFoo.TESTDIR.CLKDIV = DIVBY4;
Is there some way, maybe by using the auto keyword to make the above
syntax look nicer? To make to compiler more willing to choose the scope
itself, if it is unambiguous?
And, by the way, since I declared my enum like this
enum class Enum : uint32_t
Is there a way to extract the underlying integer type during compile
time without having it to restate again as I do in:
myPeriphMemory.TESTDIR = (myPeriphMemory.TESTIS & ~(0x30000)) |
(uint32_t)aValue;
with the explicit cast? Something like (imaginary syntax here)
myPeriphMemory.TESTDIR = (myPeriphMemory.TESTIS & ~(0x30000)) |
(typeof(enum class Enum))aValue;
Best regards,
Johannes
--
Ah, der neueste und bis heute genialste Streich unsere großenZumindest nicht öffentlich!
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos über Rüdiger Thomas in dsa <[email protected]>