Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » Make THISFN simpler and more powerful (by taking advantage of some new c++ feature)
Re: Make THISFN simpler and more powerful [message #60932 is a reply to message #60928] Thu, 10 October 2024 14:06 Go to previous messageGo to previous message
Lance is currently offline  Lance
Messages: 656
Registered: March 2007
Contributor
Lance wrote on Wed, 09 October 2024 10:17
C++23 provides a remedy to write portable bitfield code, without having to resort to unnecessary bit by bit initialization/update.

Here is an example.
union Flags{
	int32 dummy;
	struct{
		byte	borderLeft:3;
		byte	borderRight:3;
		byte	borderTop:3;
		byte	borderBottom:3;
		byte	halign:2;
		byte	valign:2; //16th bit
	
		bool		faceNotNull:1;
		bool		boldNotNull:1;
		bool		heightNotNull:1;
		bool		widthNotNull:1;
		bool		underlineNotNull:1;
		bool		italicNotNull:1;
		bool		strikeoutNotNull:1; //23rd bit
	};
	
	Flags() : dummy(0){ static_assert(sizeof(*this)==sizeof(dummy)); }
	
	static constexpr int32 FontMask()
	{
		Flags f;
		f.faceNotNull = true;
		f.boldNotNull = true;
		f.heightNotNull = true;
		f.widthNotNull = true;
		f.underlineNotNull = true;
		f.italicNotNull = true;
		f.strikeoutNotNull = true;
		return f.dummy;
	}
	
	void Border(int border)
	{
		borderLeft = borderRight = borderTop = borderBottom = border;
	}
	
	void SetAllFontProperties()
	{
		dummy |= FontMask();
	}

	void ClearAllFontProperties()
	{
		dummy &= ~FontMask();
	}
	
	bool AllFontPropertiesSet()const
	{
		return ( dummy & FontMask() ) == FontMask();
	}
};


It's a lot more work. But there is one added benefit: if you change the bitfields definition and add a few bits in front of the font properties group, you have a better chance not to break existing code unknowingly.


with std=c++23, gcc and msvc accept the above code. But clang rejects it, for good reason. The union default constructor used in constexpr ...FontMask()const should be constexpr modified too.
	constexpr Flags() : dummy(0){
		static_assert(sizeof(*this)==sizeof(dummy)); 
       }

 
Read Message icon10.gif
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Math - GaussJordan function
Next Topic: Clang cannot find DLLs
Goto Forum:
  


Current Time: Fri May 09 22:08:54 CEST 2025

Total time taken to generate the page: 0.02746 seconds