473,796 Members | 2,434 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 1325

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

Similar topics

3
2720
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
1777
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
7310
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
2326
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
3128
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
9680
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
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7547
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
5441
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...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
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
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
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.