OpenWalnut  1.5.0dev
WGEShaderDefineOptions.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <stdarg.h>
26 
27 #include <algorithm>
28 #include <string>
29 #include <vector>
30 
31 #include "../../common/exceptions/WPreconditionNotMet.h"
32 #include "WGEShaderDefineOptions.h"
33 
35  std::string option2, std::string option3, std::string option4, std::string option5,
36  std::string option6, std::string option7, std::string option8, std::string option9,
37  std::string option10 ):
39  m_options( 1, first ),
40  m_idx( 1, 0 )
41 {
42  // init
43  if( !option2.empty() )
44  {
45  m_options.push_back( option2 );
46  }
47  if( !option3.empty() )
48  {
49  m_options.push_back( option3 );
50  }
51  if( !option4.empty() )
52  {
53  m_options.push_back( option4 );
54  }
55  if( !option5.empty() )
56  {
57  m_options.push_back( option5 );
58  }
59  if( !option6.empty() )
60  {
61  m_options.push_back( option6 );
62  }
63  if( !option7.empty() )
64  {
65  m_options.push_back( option7 );
66  }
67  if( !option8.empty() )
68  {
69  m_options.push_back( option8 );
70  }
71  if( !option9.empty() )
72  {
73  m_options.push_back( option9 );
74  }
75  if( !option10.empty() )
76  {
77  m_options.push_back( option10 );
78  }
79 }
80 
81 WGEShaderDefineOptions::WGEShaderDefineOptions( std::vector< std::string > options ):
83  m_options( options ),
84  m_idx( 1, 0 )
85 {
86  WPrecond( options.size() >= 1, "You need to specify at least one option." );
87 }
88 
90 {
91  // cleanup
92 }
93 
94 std::string WGEShaderDefineOptions::process( const std::string& /*file*/, const std::string& code ) const
95 {
96  if( !getActive() )
97  {
98  return code;
99  }
100 
101  // add a define for every active option
102  std::stringstream ss;
103  for( IdxList::const_iterator iter = m_idx.begin(); iter != m_idx.end(); ++iter )
104  {
105  ss << "#define " + getOptionName( *iter ) << std::endl;
106  }
107 
108  // add the original code again
109  ss << code;
110  return ss.str();
111 }
112 
114 {
115  return m_idx;
116 }
117 
118 std::string WGEShaderDefineOptions::getOptionName( size_t idx ) const
119 {
120  WPrecond( idx < m_options.size(), "Index invalid." );
121  return m_options[ idx ];
122 }
123 
124 void WGEShaderDefineOptions::activateOption( size_t idx, bool exclusive )
125 {
126  WPrecond( idx < m_options.size(), "Index invalid." );
127 
128  // if this option is already active, avoid an update even if exclusive is true
129  if( ( m_idx.size() == 1 ) && ( m_idx[ 0 ] == idx ) )
130  {
131  return;
132  }
133 
134  if( exclusive )
135  {
136  m_idx.clear();
137  }
138 
139  // is the option already active?
140  if( std::find( m_idx.begin(), m_idx.end(), idx ) == m_idx.end() )
141  {
142  m_idx.push_back( idx );
143  updated();
144  }
145 }
146 
148 {
149  IdxList::iterator iter = std::find( m_idx.begin(), m_idx.end(), idx );
150  if( iter != m_idx.end() )
151  {
152  m_idx.erase( iter );
153  updated();
154  }
155 }
156 
158 {
159  // simply add all
160  for( size_t i = 0; i < m_options.size(); ++i )
161  {
162  m_idx.push_back( i );
163  }
164 
165  updated();
166 }
167 
169 {
170  // clear active list
171  m_idx.clear();
172  updated();
173 }
174 
175 void WGEShaderDefineOptions::addOption( std::string opt )
176 {
177  WPrecond( !opt.empty(), "Options need to have a non-empty name." );
178  if( std::find( m_options.begin(), m_options.end(), opt ) == m_options.end() )
179  {
180  m_options.push_back( opt );
181 
182  // signal update
183  updated();
184  }
185 }
186 
188 {
189  if( m_idx != newList )
190  {
191  m_idx = newList;
192  updated();
193  }
194 }
195 
void activateAllOptions()
Activates all the options.
virtual ~WGEShaderDefineOptions()
Destructor.
void deactivateOption(size_t idx)
De-activates the specified option.
WGEShaderDefineOptions(std::string first, std::string option2="", std::string option3="", std::string option4="", std::string option5="", std::string option6="", std::string option7="", std::string option8="", std::string option9="", std::string option10="")
Create a new instance of this class.
std::vector< std::string > m_options
The list of options.
IdxList m_idx
The currently selected options.
const IdxList & getActiveOptions() const
Returns the currently active option as index.
void deactivateAllOptions()
De-activates all the options.
std::vector< size_t > IdxList
The type of the index list.
std::string getOptionName(size_t idx) const
Returns the name of the specified option.
void setActivationList(const IdxList &newList)
Sets the specified index list as the new activation list.
virtual std::string process(const std::string &file, const std::string &code) const
Process the whole code.
void addOption(std::string opt)
Adds the specified string as option which is inserted to the code as "#define NAME" if active.
void activateOption(size_t idx, bool exclusive=true)
Activates the option specified.
Base class for each preprocessing possible to shader code.
virtual void updated()
Fires m_updateCondition which should denote an update in the preprocessor filter.
bool getActive() const
If the preprocessor is active, this returns true.