//---------------------------------------------------------------------- // bool.h - declaration for a bool type // // This is about the most portable way to define a bool type. It // relies only on C syntax. It works better than a C++ enum because // the <, >, and == operators return type int, and you get warnings // about conversions if you try to assign to a bool type variable. // This fixes the warnings, at the cost of some type checking ability. // // WARNING: be careful when using expressions like: // // if (BooleanFunction() == true) ... // // since true is defined to be exactly 1, it is possible for // the BooleanFunction to return some other non-zero value, which // should be true, but which is not == 1. It is safer to just say // // if (BooleanFunction()) ... // // // HOW TO USE THIS FILE: // // This file should work on any compiler. You should be able to say // // #include "bool.h" // // and this file will figure out how to define the bool type for you, // even if "defining the bool type" means allowing the builtin type // to be used. If this file does not work for your compiler, please // email me (Andy, acollins@cs.washington.edu) and tell me about it. // // You don't need to know how this file works, but if you are curious, // you should be able to figure it out by looking at the comments // and probably a C preprocessor reference. // // Andy Collins -- CSE 143 Computer Programming II // Unversity of Washington Computer Science and EngineeringCSE 143 // Last update: 02 October 1997 //---------------------------------------------------------------------- #ifndef _BOOL_H_ // Standard multiple-inclusion boilerplate #define _BOOL_H_ // the following code identifies all forms of MS C, and disables // the warning that says you shouldn't be using bool, true and false // Unfortunately, it also knocks off all other warnings about using // features which are reserved. Oh well // -- Andy #ifdef _MSC_VER #if (_MSC_VER < 1100) // older than 5.0 #pragma warning(disable:4237) #endif #endif // The ifndefs here keep us from doing this under G++, since // that already has a bool, and under MSVC 5.0 (and hopefully // any nerwer versions, although the documentation wasn't // clear on the future trends of the numbering system. 1100 // means version 5.0) #define _NEED_TO_DEFINE_BOOL 1 // Assume we have to define it #ifdef __GNUC__ // find and handle G++ #undef _NEED_TO_DEFINE_BOOL #define _NEED_TO_DEFINE_BOOL 0 #endif #ifdef _MSC_VER // find and handle MSVC 5.0 #if (_MSC_VER >= 1100) // MSVC older than 5.0 #undef _NEED_TO_DEFINE_BOOL #define _NEED_TO_DEFINE_BOOL 0 #endif #endif #ifdef __MWERKS__ // find and handle Metrowerks CodeWarrior #if (__MWERKS__ != 1) // if we're running CW7 or later... #if __option (bool) // and bool support is active... #undef _NEED_TO_DEFINE_BOOL // then we don't need the hack #define _NEED_TO_DEFINE_BOOL 0 #endif #endif #endif #if (_NEED_TO_DEFINE_BOOL == 1) enum {false = 0, true = 1}; // this gives us true and false typedef int bool; // this gives us bool #endif #endif // _BOOL_H_