1 /*
2  *  Created by Phil on 29/10/2010.
3  *  Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
4  *
5  *  Distributed under the Boost Software License, Version 1.0. (See accompanying
6  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  */
8 #ifndef TWOBLUECUBES_CATCH_COMMON_H_INCLUDED
9 #define TWOBLUECUBES_CATCH_COMMON_H_INCLUDED
10 
11 #include "catch_compiler_capabilities.h"
12 
13 #define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line
14 #define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line )
15 #ifdef CATCH_CONFIG_COUNTER
16 #  define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __COUNTER__ )
17 #else
18 #  define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ )
19 #endif
20 
21 #define INTERNAL_CATCH_STRINGIFY2( expr ) #expr
22 #define INTERNAL_CATCH_STRINGIFY( expr ) INTERNAL_CATCH_STRINGIFY2( expr )
23 
24 #include <sstream>
25 #include <algorithm>
26 
27 namespace Catch {
28 
29     struct IConfig;
30 
31     struct CaseSensitive { enum Choice {
32         Yes,
33         No
34     }; };
35 
36     class NonCopyable {
37 #ifdef CATCH_CONFIG_CPP11_GENERATED_METHODS
38         NonCopyable( NonCopyable const& )              = delete;
39         NonCopyable( NonCopyable && )                  = delete;
40         NonCopyable& operator = ( NonCopyable const& ) = delete;
41         NonCopyable& operator = ( NonCopyable && )     = delete;
42 #else
43         NonCopyable( NonCopyable const& info );
44         NonCopyable& operator = ( NonCopyable const& );
45 #endif
46 
47     protected:
NonCopyable()48         NonCopyable() {}
49         virtual ~NonCopyable();
50     };
51 
52     class SafeBool {
53     public:
54         typedef void (SafeBool::*type)() const;
55 
makeSafe(bool value)56         static type makeSafe( bool value ) {
57             return value ? &SafeBool::trueValue : 0;
58         }
59     private:
trueValue()60         void trueValue() const {}
61     };
62 
63     template<typename ContainerT>
deleteAll(ContainerT & container)64     void deleteAll( ContainerT& container ) {
65         typename ContainerT::const_iterator it = container.begin();
66         typename ContainerT::const_iterator itEnd = container.end();
67         for(; it != itEnd; ++it )
68             delete *it;
69     }
70     template<typename AssociativeContainerT>
deleteAllValues(AssociativeContainerT & container)71     void deleteAllValues( AssociativeContainerT& container ) {
72         typename AssociativeContainerT::const_iterator it = container.begin();
73         typename AssociativeContainerT::const_iterator itEnd = container.end();
74         for(; it != itEnd; ++it )
75             delete it->second;
76     }
77 
78     bool startsWith( std::string const& s, std::string const& prefix );
79     bool startsWith( std::string const& s, char prefix );
80     bool endsWith( std::string const& s, std::string const& suffix );
81     bool endsWith( std::string const& s, char suffix );
82     bool contains( std::string const& s, std::string const& infix );
83     void toLowerInPlace( std::string& s );
84     std::string toLower( std::string const& s );
85     std::string trim( std::string const& str );
86     bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis );
87 
88     struct pluralise {
89         pluralise( std::size_t count, std::string const& label );
90 
91         friend std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser );
92 
93         std::size_t m_count;
94         std::string m_label;
95     };
96 
97     struct SourceLineInfo {
98 
99         SourceLineInfo();
100         SourceLineInfo( char const* _file, std::size_t _line );
101 #  ifdef CATCH_CONFIG_CPP11_GENERATED_METHODS
102         SourceLineInfo(SourceLineInfo const& other)          = default;
103         SourceLineInfo( SourceLineInfo && )                  = default;
104         SourceLineInfo& operator = ( SourceLineInfo const& ) = default;
105         SourceLineInfo& operator = ( SourceLineInfo && )     = default;
106 #  endif
107         bool empty() const;
108         bool operator == ( SourceLineInfo const& other ) const;
109         bool operator < ( SourceLineInfo const& other ) const;
110 
111         char const* file;
112         std::size_t line;
113     };
114 
115     std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info );
116 
117     // This is just here to avoid compiler warnings with macro constants and boolean literals
isTrue(bool value)118     inline bool isTrue( bool value ){ return value; }
alwaysTrue()119     inline bool alwaysTrue() { return true; }
alwaysFalse()120     inline bool alwaysFalse() { return false; }
121 
122     void throwLogicError( std::string const& message, SourceLineInfo const& locationInfo );
123 
124     void seedRng( IConfig const& config );
125     unsigned int rngSeed();
126 
127     // Use this in variadic streaming macros to allow
128     //    >> +StreamEndStop
129     // as well as
130     //    >> stuff +StreamEndStop
131     struct StreamEndStop {
132         std::string operator+() {
133             return std::string();
134         }
135     };
136     template<typename T>
137     T const& operator + ( T const& value, StreamEndStop ) {
138         return value;
139     }
140 }
141 
142 #define CATCH_INTERNAL_LINEINFO ::Catch::SourceLineInfo( __FILE__, static_cast<std::size_t>( __LINE__ ) )
143 #define CATCH_INTERNAL_ERROR( msg ) ::Catch::throwLogicError( msg, CATCH_INTERNAL_LINEINFO );
144 
145 #endif // TWOBLUECUBES_CATCH_COMMON_H_INCLUDED
146 
147