473,804 Members | 2,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A technique for compile-time detection of erroneous bit-masks -opinionsrequest ed


Below is an example of how to do static type-checking of an erroneous
set of input masks.

The drawback is that it's a little too verbose and non-trivial to
understand and to make it truly

I'd like to hear if you think this is overboard or you'd like to see
more code like this ?

------------------------------------------------------------------------

#include "at_assert. h" // for static assert

// List of all the attributes and the bit positions
#define AT_WriteAttribu teList( A ) \
A( WriteAppend, 0 ) \
A( NoExistCreate, 1 ) \
A( CreateExclusive , 2 ) \
A( Truncate, 3 ) \
A( Sync, 4 ) \
A( DirectIO, 5 ) \

// for defining enum entries
#define AT_AttributeEnu mDef( Name, Num ) \
e_ ## Name = 1 << (Num),

// for declaring static member variables
// to use in expressions.
#define AT_StaticVariab leDecl( Name, Num ) \
static const AttrTempl< e_ ## Name > Name;

// for defining the member variables
#define AT_StaticVariab leDefn( Name, Num ) \
const Attr::AttrTempl < Attr::e_ ## Name > Attr::Name;

// Helper to define illegal combinations
#define AT_IllegalCombo ( X, A, B ) \
AT_StaticAssert ( \
( X & ( e_ ## A | e_ ## B ) ) != ( e_ ## A | e_ ## B ), \
Cannot_have_ ## A ## _and_ ## B ## _in_the_same_at tribute_set \
); \
// end macro //

// ======== Attr =============== =============== ==============
/**
* Attribute class - represents basic attributes
*
*/

class Attr
{
protected:

public: // Rather not have this private

// ======== Attribute =============== =============== ==============
/**
* The enums of various attributes.
*
*/

enum Attribute
{
// expands to define a list of enums
AT_WriteAttribu teList( AT_AttributeEnu mDef )
};

public:

// ======== AttrTempl =============== =============== =========
/**
* Template of a bit-mask - each time the operator | will return
* a new version of this.
*/

template <Attribute w_Attr>
class AttrTempl
{
public:

AT_IllegalCombo ( w_Attr, Truncate, WriteAppend )
AT_IllegalCombo ( w_Attr, Sync, DirectIO )

inline AttrTempl()
{}

inline operator Attr ()
{
return Attr( w_Attr );
}

};

// declare things like:
// static const AttrTempl< e_Name > Name
AT_WriteAttribu teList( AT_StaticVariab leDecl )

// make the constructor harder to create somthing
// unitended.
explicit Attr(
Attribute i_attr
)
: m_attribute( i_attr )
{
}
/**
* The resulting bitmask
*/
unsigned m_attribute;

};

// define the names (this goes in a .cpp file)
AT_WriteAttribu teList( AT_StaticVariab leDefn )
// bitwise overload of operator| that it's return type
// reflects the actual value.
template <Attr::Attribut e w_AttrLhs, Attr::Attribute w_AttrRhs>
inline Attr::AttrTempl <
static_cast<Att r::Attribute>(w _AttrLhs | w_AttrRhs)
operator | (
const Attr::AttrTempl < w_AttrLhs > & i_lhs,
const Attr::AttrTempl < w_AttrRhs > & i_rhs
) {
return Attr::AttrTempl <
static_cast<Att r::Attribute>(w _AttrLhs | w_AttrRhs)();

}

// simple attribute definition
Attr i_xattr =
Attr::WriteAppe nd | Attr::NoExistCr eate | Attr::CreateExc lusive;
// this one will fail at compile-time
//Attr i_xattr_bad =
// Attr::WriteAppe nd | Attr::Truncate;
Attr Foo()
{
return
Attr::WriteAppe nd
| Attr::NoExistCr eate
| Attr::CreateExc lusive;
}
Jul 22 '05 #1
0 1326

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2723
by: Jaakko Holster | last post by:
Lets run following code: ------- snip ------ class Perm { static $read = bindec('001'); static $write = bindec('010'); static $delete = bindec('100'); }
0
1781
by: Aaron Straup Cope | last post by:
Hi, Included below is a copy of a message I sent to the py-tutor list. It didn't garner much in the way of a solution and it was suggested that this list might be helpful. Thanks, -----Forwarded Message----- > From: Aaron Straup Cope <asc@vineyard.net>
60
7321
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm The detector requires javascript 1.0 to work. This translates to netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)
1
2327
by: Spur | last post by:
Hi all, I implemented a memory allocation/deallocation class that logs all new/delete calls (overloaded) and remembers for each allocated block where it was allocated from (using a macro that passes __FILE__ and __LINE__). On destruction, it reports all undeleted blocks (memory leaks). The class is implemented in a separate .h/.cpp pair, and should be linked to
10
3190
by: Agony.COM | last post by:
Hi there all, Is there a way, using Javascript, that you can detect if a popup window has opened successfully? I'm tring to work out if someone is running popup blocking software or not. Cheers, @ndyB
22
1776
by: Luke Matuszewski | last post by:
We all know that feature detection technique works from very beggining of user-agents supporting JavaScript. We can alway check if eg. document has a write property or smth else: if(document.write) { } We could do that for all of objects, so i have been surprised when i found 'in' operator, which for above example would be used like this:
1
2020
by: olaolu143 | last post by:
Hello! I am doing my BSc Hons final year project in the above topic (Detecting Masquerading using Intrusion Detection System). Can you please give me any suggestions or materials to help me? I will like to make the system Host based to start with, this is because I have only 3 months to complete the project. Can you please advice me on which technique to use for anomaly detection?
0
1929
by: origami.takarana | last post by:
Intrusion Detection Strategies ----------------------------------- Until now, we’ve primarily discussed monitoring in how it relates to intrusion detection, but there’s more to an overall intrusion detection installation than monitoring alone. Monitoring can help you spot problems in your network, as well as identify performance problems, but watching every second of traffic that passes through your network, manually searching for...
5
3131
MrPickle
by: MrPickle | last post by:
When using bit masks, how do you do "not bitmask", not sure how to explain it so: How could you do this in bitmasks: if(!something)
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.