1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
6 Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
7 Software License, Version 1.0. (See accompanying file
8 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
11 #if !defined(CPP_IFBLOCK_HPP_D4676B36_00C5_41F4_BC9F_9CBBAE3B8006_INCLUDED)
12 #define CPP_IFBLOCK_HPP_D4676B36_00C5_41F4_BC9F_9CBBAE3B8006_INCLUDED
15 #include <boost/wave/wave_config.hpp>
17 // this must occur after all of the includes and before any code appears
18 #ifdef BOOST_HAS_ABI_HEADERS
19 #include BOOST_ABI_PREFIX
22 ///////////////////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////////////////
28 // the class if_blocks handles recursive conditional compilation contexts
33 status(true), some_part_status(true),
34 enclosing_status(true), is_in_else(false)
37 if_block(bool status_, bool enclosing_status_) :
39 some_part_status(status_),
40 enclosing_status(enclosing_status_),
45 void set_status(bool status_)
49 some_part_status = true;
51 bool get_status() const { return status; }
52 bool get_some_part_status() const { return some_part_status; }
53 bool get_enclosing_status() const { return enclosing_status; }
54 bool get_in_else() const { return is_in_else; }
55 void set_in_else() { is_in_else = true; }
58 bool status; // Current block is true
59 bool some_part_status; // One of the preceding or current #if/#elif was true
60 bool enclosing_status; // Enclosing #if block is true
61 bool is_in_else; // Inside the #else part
64 ///////////////////////////////////////////////////////////////////////////////
65 // stack of conditional compilation contexts
67 : private std::stack<if_block>
70 typedef std::stack<if_block>::size_type size_type;
72 void enter_if_block(bool new_status)
74 // If enclosing block is false, then this block is also false
75 bool enclosing_status = get_status();
76 this->push (value_type (new_status && enclosing_status, enclosing_status));
78 bool enter_elif_block(bool new_status)
80 if (!is_inside_ifpart())
81 return false; // #elif without matching #if
83 if (get_enclosing_status()) {
85 // entered a (false) #elif block from a true block
86 this->top().set_status(false);
88 else if (new_status && !this->top().get_some_part_status()) {
89 // Entered true #elif block and no previous block was true
90 this->top().set_status(new_status);
95 bool enter_else_block()
97 if (!is_inside_ifpart())
98 return false; // #else without matching #if
100 if (get_enclosing_status()) {
101 if (!this->top().get_some_part_status()) {
102 // Entered (true) #else block and no previous block was true
103 this->top().set_status(true);
105 else if (get_status()) {
106 // Entered (false) #else block from true block
107 this->top().set_status(false);
111 this->top().set_in_else();
117 if (0 == this->size())
118 return false; // #endif without matching #if
124 // return, whether the top (innermost) condition is true or false
125 bool get_status() const
127 return 0 == this->size() || this->top().get_status();
129 bool get_some_part_status() const
131 return 0 == this->size() || this->top().get_some_part_status();
133 bool get_enclosing_status() const
135 return 0 == this->size() || this->top().get_enclosing_status();
138 size_type get_if_block_depth() const { return this->size(); }
141 bool is_inside_ifpart() const
143 return 0 != this->size() && !this->top().get_in_else();
145 bool is_inside_elsepart() const
147 return 0 != this->size() && this->top().get_in_else();
151 ///////////////////////////////////////////////////////////////////////////////
156 // the suffix header occurs after all of the code
157 #ifdef BOOST_HAS_ABI_HEADERS
158 #include BOOST_ABI_SUFFIX
161 #endif // !defined(CPP_IFBLOCK_HPP_D4676B36_00C5_41F4_BC9F_9CBBAE3B8006_INCLUDED)