473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

A technique for compile-time detection of erroneous bit-masks -opinionsrequested


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_WriteAttributeList( 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_AttributeEnumDef( Name, Num ) \
e_ ## Name = 1 << (Num),

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

// for defining the member variables
#define AT_StaticVariableDefn( 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_attribute_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_WriteAttributeList( AT_AttributeEnumDef )
};

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_WriteAttributeList( AT_StaticVariableDecl )

// 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_WriteAttributeList( AT_StaticVariableDefn )
// bitwise overload of operator| that it's return type
// reflects the actual value.
template <Attr::Attribute w_AttrLhs, Attr::Attribute w_AttrRhs>
inline Attr::AttrTempl<
static_cast<Attr::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<Attr::Attribute>(w_AttrLhs | w_AttrRhs)();

}

// simple attribute definition
Attr i_xattr =
Attr::WriteAppend | Attr::NoExistCreate | Attr::CreateExclusive;
// this one will fail at compile-time
//Attr i_xattr_bad =
// Attr::WriteAppend | Attr::Truncate;
Attr Foo()
{
return
Attr::WriteAppend
| Attr::NoExistCreate
| Attr::CreateExclusive;
}
Jul 22 '05 #1
0 1294

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

Similar topics

3
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
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, ...
60
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 ...
1
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...
10
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. ...
22
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: ...
1
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? ...
0
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...
5
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.