Browse Source

NoInit constructor for AbstractShaderProgram.

Also made it possible to call AbstractShaderProgram destructor without
an GL context being active.
pull/196/head
Vladimír Vondruš 9 years ago
parent
commit
13d9a8f6f5
  1. 6
      src/Magnum/AbstractShaderProgram.cpp
  2. 14
      src/Magnum/AbstractShaderProgram.h
  3. 56
      src/Magnum/Test/AbstractShaderProgramTest.cpp
  4. 1
      src/Magnum/Test/CMakeLists.txt

6
src/Magnum/AbstractShaderProgram.cpp

@ -277,16 +277,20 @@ AbstractShaderProgram::AbstractShaderProgram(): _id(glCreateProgram()) {
CORRADE_INTERNAL_ASSERT(_id != Implementation::State::DisengagedBinding);
}
AbstractShaderProgram::AbstractShaderProgram(NoCreateT) noexcept: _id{0} {}
AbstractShaderProgram::AbstractShaderProgram(AbstractShaderProgram&& other) noexcept: _id(other._id) {
other._id = 0;
}
AbstractShaderProgram::~AbstractShaderProgram() {
if(!_id) return;
/* Remove current usage from the state */
GLuint& current = Context::current().state().shaderProgram->current;
if(current == _id) current = 0;
if(_id) glDeleteProgram(_id);
glDeleteProgram(_id);
}
AbstractShaderProgram& AbstractShaderProgram::operator=(AbstractShaderProgram&& other) noexcept {

14
src/Magnum/AbstractShaderProgram.h

@ -35,6 +35,7 @@
#include "Magnum/AbstractObject.h"
#include "Magnum/Attribute.h"
#include "Magnum/Tags.h"
#if defined(CORRADE_TARGET_WINDOWS) && !defined(MAGNUM_TARGET_GLES2)
#include <vector>
@ -695,6 +696,19 @@ class MAGNUM_EXPORT AbstractShaderProgram: public AbstractObject {
*/
explicit AbstractShaderProgram();
/**
* @brief Construct without creating the underlying OpenGL object
*
* The constructed instance is equivalent to moved-from state. Useful
* in cases where you will overwrite the instance later anyway. Move
* another object over it to make it useful.
*
* This function can be safely used for constructing (and later
* destructing) objects even without any OpenGL context being active.
* @see @ref AbstractShaderProgram()
*/
explicit AbstractShaderProgram(NoCreateT) noexcept;
/** @brief Copying is not allowed */
AbstractShaderProgram(const AbstractShaderProgram&) = delete;

56
src/Magnum/Test/AbstractShaderProgramTest.cpp

@ -0,0 +1,56 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <Corrade/TestSuite/Tester.h>
#include "Magnum/AbstractShaderProgram.h"
namespace Magnum { namespace Test {
struct AbstractShaderProgramTest: TestSuite::Tester {
explicit AbstractShaderProgramTest();
void constructNoCreate();
};
AbstractShaderProgramTest::AbstractShaderProgramTest() {
addTests({&AbstractShaderProgramTest::constructNoCreate});
}
void AbstractShaderProgramTest::constructNoCreate() {
{
struct: AbstractShaderProgram {
using AbstractShaderProgram::AbstractShaderProgram;
} shader{NoCreate};
CORRADE_COMPARE(shader.id(), 0);
}
CORRADE_VERIFY(true);
}
}}
CORRADE_TEST_MAIN(Magnum::Test::AbstractShaderProgramTest)

1
src/Magnum/Test/CMakeLists.txt

@ -25,6 +25,7 @@
corrade_add_test(ArrayTest ArrayTest.cpp LIBRARIES Magnum)
corrade_add_test(AttributeTest AttributeTest.cpp LIBRARIES Magnum)
corrade_add_test(AbstractShaderProgramTest AbstractShaderProgramTest.cpp LIBRARIES Magnum)
corrade_add_test(BufferTest BufferTest.cpp LIBRARIES Magnum)
corrade_add_test(FormatTest FormatTest.cpp LIBRARIES Magnum)
corrade_add_test(ContextTest ContextTest.cpp LIBRARIES Magnum)

Loading…
Cancel
Save