|
So I figured out awhile ago how to prepare a massive amount of information about a pixel format based on a few inputs about it, where the compiler computes the rest at compile time. It's all powered by C++ templates. It implements code, to for example, shift a 6 bit value out of the middle of a 16 bit big endian word, give it you you, and then allow you to change it.
You define the bit depth of every channel, and what type of channel it is, and it does the rest:
using rgb565be = pixel<
color_model::rgb,
endian_mode::big_endian,
channel_traits<channel_name::R,5>, channel_traits<channel_name::G,6>, channel_traits<channel_name::B,5> >;
and then you can get the channel info from a pixel type like this:
using ch = typename PixelType::channel_type_by_index<Index>;
printf("channel %s\r\n",ch::name_());
printf("\tindex: %d\r\n",(int)ch::index_);
printf("\tbit-depth: %d\r\n",(int)ch::bit_depth_);
printf("\tmin: %llu, max: %llu\r\n",(unsigned long long)ch::min,(unsigned long long)ch::max);
printf("\tscale: %llu\r\n",(unsigned long long)ch::scale);
printf("\r\n");
It computes all those values above (the trailing underscores are to avoid conflicts with virtual instance methods of the same name that allow you to get the information at runtime rather than compile time like above)
Pretty slick, if I do say so myself, but...
I want to be able to automatically generate code to convert pixel formats of the same color model to each other, so you can for example, from 24-bit RGB to 16-bit BGR format without missing a beat or writing code to do it yourself.
So I needed a template to fetch a channel by name at compile time.
I can get it, but I can't populate the index of the channel with a meaningful value if I don't retrieve it by index. Nor can I count the bits to the left of it which means that nifty shifting code I generate is incorrect if I fetch the channel type by name.
I'm stumped by the seemingly simple task of convincing the C++ compiler to give me the index of type (within a template argument parameter pack) that has a particular string associated with it.
I've been working on this all day.
I love C++.
I hate C++.
Real programmers use butterflies
|
|
|
|
|
Any progress? I'm no template wizard so can't help.
You're clever regardless of how this turns out.
|
|
|
|
|
Update: I fixed it. Eureka!
template<size_t Count,typename Name, typename ...ChannelTraits>
struct channel_index_by_name_impl;
template<size_t Count,typename Name>
struct channel_index_by_name_impl<Count,Name> {
static constexpr size_t value=-1;
};
template<size_t Count,typename Name, typename ChannelTrait, typename ...ChannelTraits>
struct channel_index_by_name_impl<Count,Name, ChannelTrait, ChannelTraits...> {
static constexpr size_t value = is_same<Name, typename ChannelTrait::name_type>::value ? Count : channel_index_by_name_impl<Count+1,Name, ChannelTraits...>::value;
};
Above is the magic I needed
I still haven't figured it out. It's not catching my specializations that I set up for some reason.
This is my i don't know which iteration now.
#define GFX_CHANNEL_NAME(x) struct x { static inline constexpr const char* value() { return #x; } };
...
template<bool Value>
struct boolean_type {
constexpr static const bool value = Value;
};
template<typename T, typename U>
struct is_same : boolean_type<false> {};
template<typename T>
struct is_same<T, T> : boolean_type<true> {};
template<typename PixelType,typename Name,size_t Count, bool Match, typename ChannelTrait,typename... ChannelTraits>
struct channel_index_by_name_impl {};
template<typename PixelType,typename Name,size_t Count, typename ChannelTrait,typename... ChannelTraits>
struct channel_index_by_name_impl<PixelType,Name,Count,false,ChannelTrait,ChannelTraits...> {
constexpr static const size_t value = Count;
using next = channel_index_by_name_impl<PixelType,Name,Count+1,is_same<Name,typename PixelType::channel_type_by_index<Count+1>::name_type >::value,ChannelTraits...>;
};
template<typename PixelType,typename Name,size_t Count, typename ChannelTrait,typename... ChannelTraits>
struct channel_index_by_name_impl<PixelType,Name,Count,true,ChannelTrait,ChannelTraits...> {
constexpr static const size_t value = Count;
};
Here the ChannelTraits parameter pack has channel_traits definitions in it, and each of them has a name_type which is a type alias for one of those #define d "strings" above.
The idea is it keeps instantiating the next one in the series until is_same for the next template's name_type is true.
The problem is it always seems to stop at the first one.
Real programmers use butterflies
modified 21-Mar-21 21:08pm.
|
|
|
|
|
Definitely worth a bookmark in case I need a good template example to look at.
|
|
|
|
|
I'm in the middle of working on an article titled Metaprogramming in C++: A Gentle Introduction
It will cover this, and several other techniques.
Real programmers use butterflies
|
|
|
|
|
honey the codewitch wrote: I love C++.
I hate C++ Best explanation so far.
Ditto, on Delphi. It is wonderfull, except for those few lines of assembly with the comment "magic".
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I figured out my issue though and unlike with other languages, it feels like I accomplished something significant.
I was just talking to a friend about how C++ development is a field unto itself. Creating a program in C++ is like writing a dissertation. You could spend a lifetime studying C++ and never master it. Just when you think you have, someone figures out a novel way to hack the compiler and opens up an entire new subfield like happened with metaprogramming.
I can't say the same of any other language I've used, at least not to that degree. It's in a class by itself. The standard C++ compiler is pure magic. Oh, the things it can do. It boggles the mind.
Real programmers use butterflies
|
|
|
|
|
Sophia, Rose, Blanche and Dorothy.
Some of you will understand.
Real programmers use butterflies
|
|
|
|
|
SRBD ?
"I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP
|
|
|
|
|
No, it's not an acronym, it's an old American pop culture reference. You'd have had to watch American television in the 1980s and 1990s.
Real programmers use butterflies
|
|
|
|
|
honey the codewitch wrote: You'd have had to watch American television in the 1980s and 1990s. I didn't have american TV but I did know them.
My favourite was the granny...
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
that was Sophia. We named the smarter hen after her.
Real programmers use butterflies
|
|
|
|
|
Nelek wrote: My favourite was the granny...
Weren't they all???
|
|
|
|
|
fair enough...
I meant the oldest (the mother of one of them)
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
How old are these chickens?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
They're still small but they already act like little old ladies.
Real programmers use butterflies
|
|
|
|
|
So did Sophia, when she wanted something.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Have you considered printing some of these?
3D Printed T-Rex Arms For Chickens[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Well since they weren't eggs over the weekend, we know they are Spring chickens.
I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.
|
|
|
|
|
Silence is golden
|
|
|
|
|
|
Any of those would be a good name for the goose from Jack and the Beanstalk.
But for chickens??!?!?!
If you can't laugh at yourself - ask me and I will do it for you.
|
|
|
|
|
We did too. Jaba the Hen, Hen Solo, Cluck Vader, Princess Lay-a.
Most of you will understand..
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Epic is giving "the Fall" for free. A darky puzzle game where you play as an AI-suit trying to bring your hurt human pilot to a hospital, not tested it yet, but looks entertaining.
and
GoG is giving "Deadlight: Director's cut" for free. A shorty zombie slayer & puzzle game (around 4 or 5 hous gameplay). Old comments say not worth the price back then, but being free...
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Nelek wrote: GoG is giving "Deadlight: Director's cut" for free. €11.99 for me
Coincidentally, I'm looking for some Good Old Games at GOG right now, but lots of games just aren't running that well anymore.
They worked nice when they were first released on GOG, but GOG itself is becoming old and a lot of oldies have become just trying out compatibilities and hoping for the best
|
|
|
|
|