Click here to Skip to main content
16,004,761 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: C++, epiphanies, article content ideas Pin
honey the codewitch8-Jun-24 3:40
mvahoney the codewitch8-Jun-24 3:40 
GeneralRe: C++, epiphanies, article content ideas Pin
trønderen8-Jun-24 5:05
trønderen8-Jun-24 5:05 
AnswerRe: C++, epiphanies, article content ideas Pin
jschell12-Jun-24 14:59
jschell12-Jun-24 14:59 
GeneralRe: C++, epiphanies, article content ideas Pin
honey the codewitch12-Jun-24 15:03
mvahoney the codewitch12-Jun-24 15:03 
GeneralRe: C++, epiphanies, article content ideas Pin
jschell13-Jun-24 12:33
jschell13-Jun-24 12:33 
GeneralRe: C++, epiphanies, article content ideas Pin
honey the codewitch13-Jun-24 12:37
mvahoney the codewitch13-Jun-24 12:37 
GeneralRe: C++, epiphanies, article content ideas Pin
jschell14-Jun-24 13:15
jschell14-Jun-24 13:15 
PraiseI never fail to be impressed by this Pin
honey the codewitch7-Jun-24 5:44
mvahoney the codewitch7-Jun-24 5:44 
Disclaimer: The following is intended to run in embedded environments that may not have a compliant STL if they have the STL at all. Some of this code could be replaced on systems with The STL to use std:: functionality.

Anyway, it's code I wrote, but it's not my code that really impresses me about this, but rather the C++ compiler.

C++
static_assert(pixel_type::template equals<gfx::rgb_pixel<16>>::value,
                          "this code needs to be ported for your display format");


That's a compile time check to determine if a pixel format is exactly the same as the specified pixel format.

Not impressed yet?

Those pixel formats are arbitrarily defined:

C++
// creates an RGB pixel by making each channel 
// one third of the whole. Any remainder bits
// are added to the green channel
template<size_t BitDepth>
using rgb_pixel = pixel<
    channel_traits<channel_name::R,(BitDepth/3)>,
    channel_traits<channel_name::G,((BitDepth/3)+(BitDepth%3))>,
    channel_traits<channel_name::B,(BitDepth/3)>
>;
// creates an RGBA pixel by making each channel 
// one quarter of the whole. Any remainder bits
// are added to the green channel
template<size_t BitDepth>
using rgba_pixel = pixel<
    channel_traits<channel_name::R,(BitDepth/4)>,
    channel_traits<channel_name::G,((BitDepth/4)+(BitDepth%4))>,
    channel_traits<channel_name::B,(BitDepth/4)>,
    channel_traits<channel_name::A,(BitDepth/4),0,(1<<(BitDepth/4))-1,(1<<(BitDepth/4))-1>
>;
// creates a grayscale or monochome pixel
template<size_t BitDepth>
using gsc_pixel = pixel<
    channel_traits<channel_name::L,BitDepth>
>;
// creates a Y'UV pixel by making each channel 
// one third of the whole. Any remainder bits
// are added to the Y' channel
template<size_t BitDepth>
using yuv_pixel = pixel<
    channel_traits<channel_name::Y,((BitDepth/3)+(BitDepth%3))>,
    channel_traits<channel_name::U,(BitDepth/3)>,
    channel_traits<channel_name::V,(BitDepth/3)>
>;


And furthermore

C++
// defines a channel for a pixel
template<
    // the channel name, like channel_name::R
    typename Name,
    // the bit depth
    size_t BitDepth, 
    // the minimum value
    bits::uintx<bits::get_word_size(BitDepth)> Min=0, 
    // the maximum value
#if HTCW_MAX_WORD >= 64
    bits::uintx<bits::get_word_size(BitDepth)> Max= ((BitDepth==64)?0xFFFFFFFFFFFFFFFF:((1<<BitDepth)-1)), 
#else
    bits::uintx<bits::get_word_size(BitDepth)> Max= ((BitDepth==32)?0xFFFFFFFF:((1<<BitDepth)-1)), 
#endif
    // the default value
    bits::uintx<bits::get_word_size(BitDepth)> Default = Min,
    // the scale denominator
    bits::uintx<bits::get_word_size(BitDepth)> Scale = Max
>
struct channel_traits {
    // this type
    using type = channel_traits<Name,BitDepth,Min,Max,Scale>;
    // the type that represents the name
    using name_type = Name;
    // the integer type of this channel
    using int_type = bits::uintx<bits::get_word_size(BitDepth)>;
    // the floating point type of this channel
    using real_type = bits::realx<16<=BitDepth?HTCW_MAX_WORD:32>;
    // the bit depth of this channel
    constexpr static const size_t bit_depth = BitDepth;
    // the minimum value
    constexpr static const int_type min = Min;
    // the maximum value
    constexpr static const int_type max = Max;
    // the default value
    constexpr static const int_type default_ = Default;
    // the scale denominator
    constexpr static const int_type scale = Scale;
    // the reciprocal of the scale denominator
    constexpr static const real_type scaler = 1/(real_type)Scale; 
    // a mask of the int value
    constexpr static const int_type int_mask = ~int_type(0);
    // a mask of the channel value
    constexpr static const int_type mask = bits::mask<BitDepth>::right;
    // constraints
    static_assert(BitDepth>0,"Bit depth must be greater than 0");
    static_assert(BitDepth<=64,"Bit depth must be less than or equal to 64");
    static_assert(Min<=Max,"Min must be less than or equal to Max");
    static_assert(Default>=Min,"Default must be greater than or equal to the minimum value");
    static_assert(Default<=Max,"Default must be less than or equal to the maximum value");
#if HTCW_MAX_WORD >= 64        
    static_assert(Max<=((BitDepth==64)?0xFFFFFFFFFFFFFFFF:((1<<BitDepth)-1)),"Max is greater than the maximum allowable value");
#else
    static_assert(Max<=((BitDepth==32)?0xFFFFFFFF:((1<<BitDepth)-1)),"Max is greater than the maximum allowable value");
#endif
    static_assert(Scale>0,"Scale must not be zero");
};


I don't know of any other language that allows this kind of compile time expressiveness. I wish they did. Sometimes it feels like I'm "in dialogue" with the compiler rather than with the generated/runtime code as with most languages.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix

QuestionPassing a class (pointer) to library - Linux Pin
Salvatore Terress31-May-24 9:00
Salvatore Terress31-May-24 9:00 
AnswerRe: Passing a class (pointer) to library - Linux Pin
k505431-May-24 10:25
mvek505431-May-24 10:25 
GeneralBUMP Re: Passing a class (pointer) to library - Linux Pin
Salvatore Terress31-May-24 14:12
Salvatore Terress31-May-24 14:12 
GeneralRe: Passing a class (pointer) to library - Linux Pin
Richard MacCutchan1-Jun-24 1:47
mveRichard MacCutchan1-Jun-24 1:47 
GeneralRe: BUMP Re: Passing a class (pointer) to library - Linux Pin
Richard MacCutchan4-Jun-24 2:02
mveRichard MacCutchan4-Jun-24 2:02 
QuestionHow C++ manage memory in event driven system ? Pin
Salvatore Terress30-May-24 4:44
Salvatore Terress30-May-24 4:44 
AnswerRe: How C++ manage memory in event driven system ? Pin
Richard MacCutchan30-May-24 5:47
mveRichard MacCutchan30-May-24 5:47 
AnswerRe: How C++ manage memory in event driven system ? Pin
k505430-May-24 5:58
mvek505430-May-24 5:58 
GeneralRe: How C++ manage memory in event driven system ? Pin
Salvatore Terress30-May-24 7:09
Salvatore Terress30-May-24 7:09 
GeneralRe: How C++ manage memory in event driven system ? Pin
Richard MacCutchan30-May-24 9:09
mveRichard MacCutchan30-May-24 9:09 
GeneralRe: How C++ manage memory in event driven system ? Pin
k505430-May-24 9:47
mvek505430-May-24 9:47 
GeneralRe: How C++ manage memory in event driven system ? Pin
Salvatore Terress30-May-24 12:54
Salvatore Terress30-May-24 12:54 
QuestionUPDATE CLOSED error: declaration of anonymous class must be a definition Pin
Salvatore Terress25-May-24 5:49
Salvatore Terress25-May-24 5:49 
AnswerRe: error: declaration of anonymous class must be a definition Pin
k505425-May-24 6:25
mvek505425-May-24 6:25 
GeneralRe: error: declaration of anonymous class must be a definition Pin
Salvatore Terress25-May-24 8:26
Salvatore Terress25-May-24 8:26 
GeneralRe: error: declaration of anonymous class must be a definition Pin
Richard MacCutchan25-May-24 21:21
mveRichard MacCutchan25-May-24 21:21 
GeneralRe: error: declaration of anonymous class must be a definition Pin
k505427-May-24 5:26
mvek505427-May-24 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.