Browse Source

[wip]

TODO: how to make function calls this pretty?

    auto f = sin(x) ^ 2
    f(1.0f);
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
dc939f128b
  1. 1
      src/Math/CMakeLists.txt
  2. 4
      src/Math/VectorFields/CMakeLists.txt
  3. 89
      src/Math/VectorFields/Function.h
  4. 1
      src/Math/VectorFields/Test/CMakeLists.txt
  5. 36
      src/Math/VectorFields/Test/FunctionTest.cpp
  6. 31
      src/Math/VectorFields/Test/FunctionTest.h

1
src/Math/CMakeLists.txt

@ -18,6 +18,7 @@ install(FILES ${MagnumMath_HEADERS} DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/Ma
add_subdirectory(Algorithms)
add_subdirectory(Geometry)
add_subdirectory(VectorFields)
if(BUILD_TESTS)
enable_testing()

4
src/Math/VectorFields/CMakeLists.txt

@ -0,0 +1,4 @@
if(BUILD_TESTS)
enable_testing()
add_subdirectory(Test)
endif()

89
src/Math/VectorFields/Function.h

@ -0,0 +1,89 @@
#ifndef Magnum_Math_VectorFields_Function_h
#define Magnum_Math_VectorFields_Function_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include <functional>
namespace Magnum { namespace Math { namespace VectorFields {
template<char character, class T> class VariableValue {
VariableValue(const VariableValue<character, T>&) = delete;
VariableValue(VariableValue<character, T>&&) = delete;
VariableValue<character, T>& operator=(const VariableValue<character, T>&) = delete;
VariableValue<character, T>& operator=(VariableValue<character, T>&&) = delete;
public:
static const char Character = character;
inline constexpr VariableValue(T value): _value(value) {}
inline constexpr operator T() const { return _value; }
private:
T _value;
};
#ifndef DOXYGEN_GENERATING_OUTPUT
namespace Implementation {
template<char character, class T> inline constexpr T extract() {
throw;
}
template<char character, class T, class ...U> inline constexpr T extract(const VariableValue<character, T>& first, const U&...) {
return first;
}
template<char character, class T, class U, class ...V> inline constexpr typename std::enable_if<!std::is_same<U, VariableValue<character, T>>::value, T>::type extract(const U&, const V&... next) {
return extract<character, T>(next...);
}
}
#endif
template<char character, class T> class Variable {
public:
static const char Character = character;
template<class ...U> T operator()(U... values) {
return Implementation::extract<character>(values...);
}
};
template<class T, class Operand, class Functor> class UnaryOperator {
public:
UnaryOperator(const Operand& operand): operand(operand) {}
template<class ...U> T operator()(U... values) {
return Functor()(operand(values...));
}
private:
Operand operand;
};
template<class T, class LeftOperand, class RightOperand, class Functor> class BinaryOperator {
public:
BinaryOperator(const Operand& left, const Operand& right) {}
template<class ...U> T operator()(U... values) {
return Functor()(leftOperand(values...), rightOperand(values...));
}
protected:
LeftOperand leftOperand;
RightOperand rightOperand;
};
}}}
#endif

1
src/Math/VectorFields/Test/CMakeLists.txt

@ -0,0 +1 @@
corrade_add_test2(MathVectorFieldsFunctionTest FunctionTest.cpp)

36
src/Math/VectorFields/Test/FunctionTest.cpp

@ -0,0 +1,36 @@
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include "FunctionTest.h"
#include "Math/VectorFields/Function.h"
CORRADE_TEST_MAIN(Magnum::Math::VectorFields::Test::FunctionTest)
namespace Magnum { namespace Math { namespace VectorFields { namespace Test {
FunctionTest::FunctionTest() {
addTests(&FunctionTest::extract);
}
void FunctionTest::extract() {
VariableValue<'x', float> x(1.0f);
VariableValue<'y', float> y(-1.0f);
VariableValue<'z', float> z(10.0f);
CORRADE_COMPARE((Implementation::extract<'y', float>(x, y, z)), -1.0f);
}
}}}}

31
src/Math/VectorFields/Test/FunctionTest.h

@ -0,0 +1,31 @@
#ifndef Magnum_Math_VectorFields_Test_FunctionTest_h
#define Magnum_Math_VectorFields_Test_FunctionTest_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include <TestSuite/Tester.h>
namespace Magnum { namespace Math { namespace VectorFields { namespace Test {
class FunctionTest: public Corrade::TestSuite::Tester<FunctionTest> {
public:
FunctionTest();
void extract();
};
}}}}
#endif
Loading…
Cancel
Save