@ -4,7 +4,6 @@
# ifndef I18N_PHONENUMBERS_BASE_BASICTYPES_H_
# define I18N_PHONENUMBERS_BASE_BASICTYPES_H_
# pragma once
# include <limits.h> // So we can set the bounds of our types
# include <stddef.h> // For size_t
@ -15,6 +14,9 @@
# include <stdint.h> // For intptr_t.
# endif
namespace i18n {
namespace phonenumbers {
# ifdef INT64_MAX
/ / INT64_MAX is defined if C99 stdint . h is included ; use the
@ -94,26 +96,11 @@ typedef signed int char32;
/ / A macro to disallow the copy constructor and operator = functions
/ / This should be used in the private : declarations for a class
# if !defined(DISALLOW_COPY_AND_ASSIGN)
# define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName ( const TypeName & ) ; \
void operator = ( const TypeName & )
/ / An older , deprecated , politically incorrect name for the above .
/ / NOTE : The usage of this macro was baned from our code base , but some
/ / third_party libraries are yet using it .
/ / TODO ( tfarina ) : Figure out how to fix the usage of this macro in the
/ / third_party libraries and get rid of it .
# define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName)
/ / A macro to disallow all the implicit constructors , namely the
/ / default constructor , copy constructor and operator = functions .
/ /
/ / This should be used in the private : declarations for a class
/ / that wants to prevent anyone from instantiating it . This is
/ / especially useful for classes containing only static methods .
# define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
TypeName ( ) ; \
DISALLOW_COPY_AND_ASSIGN ( TypeName )
# endif
/ / The arraysize ( arr ) macro returns the # of elements in an array arr .
/ / The expression is a compile - time constant , and therefore can be
@ -140,7 +127,9 @@ template <typename T, size_t N>
char ( & ArraySizeHelper ( const T ( & array ) [ N ] ) ) [ N ] ;
# endif
# if !defined(arraysize)
# define arraysize(array) (sizeof(ArraySizeHelper(array)))
# endif
/ / ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize ,
/ / but can be used on anonymous types or types defined inside
@ -179,32 +168,11 @@ char (&ArraySizeHelper(const T (&array)[N]))[N];
/ / where a pointer is 4 bytes , this means all pointers to a type whose
/ / size is 3 or greater than 4 will be ( righteously ) rejected .
# if !defined(ARRAYSIZE_UNSAFE)
# define ARRAYSIZE_UNSAFE(a) \
( ( sizeof ( a ) / sizeof ( * ( a ) ) ) / \
static_cast < size_t > ( ! ( sizeof ( a ) % sizeof ( * ( a ) ) ) ) )
/ / Use implicit_cast as a safe version of static_cast or const_cast
/ / for upcasting in the type hierarchy ( i . e . casting a pointer to Foo
/ / to a pointer to SuperclassOfFoo or casting a pointer to Foo to
/ / a const pointer to Foo ) .
/ / When you use implicit_cast , the compiler checks that the cast is safe .
/ / Such explicit implicit_casts are necessary in surprisingly many
/ / situations where C + + demands an exact type match instead of an
/ / argument type convertable to a target type .
/ /
/ / The From type can be inferred , so the preferred syntax for using
/ / implicit_cast is the same as for static_cast etc . :
/ /
/ / implicit_cast < ToType > ( expr )
/ /
/ / implicit_cast would have been part of the C + + standard library ,
/ / but the proposal was submitted too late . It will probably make
/ / its way into the language in the future .
template < typename To , typename From >
inline To implicit_cast ( From const & f ) {
return f ;
}
# endif
/ / The COMPILE_ASSERT macro can be used to verify that a compile time
/ / expression is true . For example , you could use it to verify the
@ -225,61 +193,12 @@ template <bool>
struct CompileAssert {
} ;
# undef COMPILE_ASSERT
# if !defined(COMPILE_ASSERT)
# define COMPILE_ASSERT(expr, msg) \
typedef CompileAssert < ( bool ( expr ) ) > msg [ bool ( expr ) ? 1 : - 1 ]
# endif
/ / Implementation details of COMPILE_ASSERT :
/ /
/ / - COMPILE_ASSERT works by defining an array type that has - 1
/ / elements ( and thus is invalid ) when the expression is false .
/ /
/ / - The simpler definition
/ /
/ / # define COMPILE_ASSERT ( expr , msg ) typedef char msg [ ( expr ) ? 1 : - 1 ]
/ /
/ / does not work , as gcc supports variable - length arrays whose sizes
/ / are determined at run - time ( this is gcc ' s extension and not part
/ / of the C + + standard ) . As a result , gcc fails to reject the
/ / following code with the simple definition :
/ /
/ / int foo ;
/ / COMPILE_ASSERT ( foo , msg ) ; / / not supposed to compile as foo is
/ / / / not a compile - time constant .
/ /
/ / - By using the type CompileAssert < ( bool ( expr ) ) > , we ensures that
/ / expr is a compile - time constant . ( Template arguments must be
/ / determined at compile - time . )
/ /
/ / - The outter parentheses in CompileAssert < ( bool ( expr ) ) > are necessary
/ / to work around a bug in gcc 3.4 .4 and 4.0 .1 . If we had written
/ /
/ / CompileAssert < bool ( expr ) >
/ /
/ / instead , these compilers will refuse to compile
/ /
/ / COMPILE_ASSERT ( 5 > 0 , some_message ) ;
/ /
/ / ( They seem to think the " > " in " 5 > 0 " marks the end of the
/ / template argument list . )
/ /
/ / - The array size is ( bool ( expr ) ? 1 : - 1 ) , instead of simply
/ /
/ / ( ( expr ) ? 1 : - 1 ) .
/ /
/ / This is to avoid running into a bug in MS VC 7.1 , which
/ / causes ( ( 0.0 ) ? 1 : - 1 ) to incorrectly evaluate to 1.
/ / Used to explicitly mark the return value of a function as unused . If you are
/ / really sure you don ' t want to do anything with the return value of a function
/ / that has been marked WARN_UNUSED_RESULT , wrap it with this . Example :
/ /
/ / scoped_ptr < MyType > my_var = . . . ;
/ / if ( TakeOwnership ( my_var . get ( ) ) = = SUCCESS )
/ / ignore_result ( my_var . release ( ) ) ;
/ /
template < typename T >
inline void ignore_result ( const T & ) {
}
} / / namespace phonenumbers
} / / namespace i18n
# endif / / I18N_PHONENUMBERS_BASE_BASICTYPES_H_