Browse Source

Make Shader::addSource() move-aware.

Also update addFile() to prevent unnecessary copying.
pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
0804106f62
  1. 19
      src/Shader.cpp
  2. 2
      src/Shader.h

19
src/Shader.cpp

@ -79,13 +79,15 @@ Shader& Shader::operator=(Shader&& other) {
return *this; return *this;
} }
Shader& Shader::addSource(const std::string& source) { Shader& Shader::addSource(std::string source) {
if(source.empty()) return *this; if(source.empty()) return *this;
if(_state == State::Initialized) if(_state == State::Initialized) {
/* Fix line numbers, so line 41 of third added file is marked as 3(41). /* Fix line numbers, so line 41 of third added file is marked as 3(41).
Source 0 is the #version string added in constructor. */ Source 0 is the #version string added in constructor. */
sources.push_back("#line 1 " + std::to_string(sources.size()) + '\n' + source); sources.push_back("#line 1 " + std::to_string(sources.size()) + '\n');
sources.push_back(std::move(source));
}
return *this; return *this;
} }
@ -96,18 +98,15 @@ Shader& Shader::addFile(const std::string& filename) {
/* Get size of shader and initialize buffer */ /* Get size of shader and initialize buffer */
file.seekg(0, std::ios::end); file.seekg(0, std::ios::end);
std::size_t size = file.tellg(); std::string source(file.tellg(), '\0');
char* source = new char[size+1];
source[size] = '\0';
/* Read data, close */ /* Read data, close */
file.seekg(0, std::ios::beg); file.seekg(0, std::ios::beg);
file.read(source, size); file.read(&source[0], source.size());
file.close(); file.close();
/* Add to sources and free the buffer */ /* Move to sources */
addSource(source); addSource(std::move(source));
delete[] source;
return *this; return *this;
} }

2
src/Shader.h

@ -142,7 +142,7 @@ class MAGNUM_EXPORT Shader {
* one source. If passed string is empty, the function does nothing. * one source. If passed string is empty, the function does nothing.
* @see addFile() * @see addFile()
*/ */
Shader& addSource(const std::string& source); Shader& addSource(std::string source);
/** /**
* @brief Add source file * @brief Add source file

Loading…
Cancel
Save