On 04.07.2016 16:12, bitrex wrote:
>
> [snip]
> So essentially what I'm asking is that say in the setup code the user
> wants an "Event<int, foo>" (where "foo" is a string designator), an
> "Event<char, bar>", and an Event<float, baz>". Since the types of the
> subclasses of AbstractEvent required by a specific implementation are
> all known at compile time, I'm wondering if there's a way that I can
> have a memory pool generated at compile time for them, with cells large
> enough to hold any subclass instance, such that the compiler will inform
> me how much data memory is used by the pool (it's a Harvard architecture
> uP.) Instead of having the pool instantiated at runtime.
>
> If there's a better way to go about this I'm open to suggestions as
> well. Thanks.
You could use Boost solution, but since this is a microprocessor with a
limited compiler you may not have Boost available. And anyway, with a
Boost dependency the code may need to be updated every third year or so
just in order to still compile. I used to love Boost but it's big and
hairy, it has at least two competing date-time sub libraries neither of
which is up to my standard of usability, and it's forever changing.
For the DIY C++ has two mechanisms for generating a "cell" that can hold
any of N specific types:
* an array of bytes used via placement new and support for alignment, and
* a union.
The union (keyword) is nice but there is no direct support for a
/discriminated union/, a union where there is a member that specifies
the main type stored in there, like a Pascal variant record.
One way is to store a pointer to dummy polymorphic type representative.
This approach leverages default generated copying for the union, while
supporting dynamic_cast, which is nice. In the other direction, if you
find that client code is frequently discriminating on type then that's a
common anti-pattern, something to avoid; it's so bad that it was the
main reason why Bertrand Meyers did not include enumerations in Eiffel.
Cheers & hth.,
- Alf