Skip to content

Commit

Permalink
Replace makeshift C++17 with standard
Browse files Browse the repository at this point in the history
  • Loading branch information
JarrettSJohnson committed Apr 10, 2024
1 parent 0719986 commit 1180fec
Show file tree
Hide file tree
Showing 34 changed files with 50 additions and 300 deletions.
30 changes: 3 additions & 27 deletions include/pymol/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,6 @@ template <class C, class Pred> void erase_if(C& c, Pred pred)
#endif
}

/**
* @brief C++17's version of std::clamp
*/
template<typename T>
const T& clamp(const T& value, const T& low, const T& high){
assert(low <= high);
return std::max(low, std::min(value, high));
}

/**
* @brief C++14's std::equal
*/

template <typename InIter1, typename InIter2>
bool equal(InIter1 first1, InIter1 last1, InIter2 first2)
{
for (; first1 != last1; ++first1, ++first2) {
if (*first1 != *first2) {
return false;
}
}
return true;
}

/**
* @brief Checks whether two floating point values are nearly equal
* @param first floating point number
Expand All @@ -60,7 +36,7 @@ bool equal(InIter1 first1, InIter1 last1, InIter2 first2)
* @tparam U second floating point type
*/

template <typename T, typename U, typename CommonT = pymol::common_type_t<T, U>>
template <typename T, typename U, typename CommonT = std::common_type_t<T, U>>
bool almost_equal(T a, U b, CommonT epsilon = 1e-6)
{
return std::abs(a - b) <= epsilon;
Expand Down Expand Up @@ -136,7 +112,7 @@ bool equal(const RangeT1& first, const RangeT2& second)
if (range1Size != range2Size) {
return false;
}
return pymol::equal(std::begin(first), std::end(first), std::begin(second));
return std::equal(std::begin(first), std::end(first), std::begin(second));
}

/**
Expand Down Expand Up @@ -164,7 +140,7 @@ bool equal(const RangeT1& first, const RangeT2& second, Pred p)
}
}
return true;
//return pymol::equal(std::begin(first), std::end(first), std::begin(second));
//return std::equal(std::begin(first), std::end(first), std::begin(second));
}

/**
Expand Down
20 changes: 0 additions & 20 deletions include/pymol/functional.h

This file was deleted.

42 changes: 0 additions & 42 deletions include/pymol/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,6 @@

namespace pymol
{

#if __cplusplus >= 201402L
using std::make_unique;
#else
/**
* @brief C++14's std::make_unique<T>
* @param args arguments for constructor of Type T
* @return A std::unique_ptr for type T
*/

template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args &&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

template <typename T, typename = pymol::enable_if_t<std::is_array<T>::value>>
std::unique_ptr<T> make_unique(std::size_t size){
return std::unique_ptr<T>(new pymol::remove_extent_t<T>[size]());
}
#endif

#if __cplusplus >= 201703L
using std::destroy_at;
using std::destroy;
#else
/*
* @brief C++17's std::destroy_at
*/
template <typename T> void destroy_at(T* p) { p->~T(); }

/*
* @brief C++17's std::destroy
*/

template <typename T> void destroy(T* iter, T* end)
{
for(; iter != end; ++iter) {
destroy_at(std::addressof(*iter));
}
}
#endif

/**
* A copyable unique pointer. Will make a copy of the managed object with "new".
*/
Expand Down
41 changes: 0 additions & 41 deletions include/pymol/tuple.h

This file was deleted.

33 changes: 3 additions & 30 deletions include/pymol/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,16 @@
namespace pymol
{

template<typename T>
using decay_t = typename std::decay<T>::type;

template <bool B, typename T = void>
using enable_if_t = typename std::enable_if<B, T>::type;

template <typename T>
using remove_reference_t = typename std::remove_reference<T>::type;

template <typename T>
using remove_cv_t = typename std::remove_cv<T>::type;

template <typename T>
using remove_cvref_t = remove_cv_t<remove_reference_t<T>>;

template <typename T>
using remove_extent_t = typename std::remove_extent<T>::type;

template <typename T, typename U>
using common_type_t = typename std::common_type<T, U>::type;

template <typename T>
using result_of_t = typename std::result_of<T>::type;

// Non-STL
template <typename T, typename U>
using forward_check_t = pymol::enable_if_t<std::is_same<pymol::remove_reference_t<T>, U>::value>;

//! Casts a pointer of type T to a pointer of type T[N]
template <size_t N, typename T> remove_reference_t<T (*)[N]> reshape(T* flat)
template <size_t N, typename T> std::remove_reference_t<T (*)[N]> reshape(T* flat)
{
return reinterpret_cast<T(*)[N]>(flat);
}

//! Casts a pointer of type T[N] to a pointer of type T
template <typename T> remove_extent_t<T>* flatten(T* shaped)
template <typename T> std::remove_extent_t<T>* flatten(T* shaped)
{
return reinterpret_cast<remove_extent_t<T>*>(shaped);
return reinterpret_cast<std::remove_extent_t<T>*>(shaped);
}
}
6 changes: 3 additions & 3 deletions layer0/Bezier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void BezierSpline::addBezierPoint()

std::pair<int, float> BezierSpline::getIndexAndLocalT(float globalT) const
{
auto t = clamp(globalT, 0.0f, 1.0f);
auto t = std::clamp(globalT, 0.0f, 1.0f);
if (t == 1.0f) {
assert(bezierPoints.size() >= 2);
return std::make_pair(static_cast<int>(bezierPoints.size() - 2), t);
Expand All @@ -108,7 +108,7 @@ glm::vec3 BezierSpline::GetBezierPoint(
glm::vec3 BezierSpline::GetBezierPoint(const glm::vec3& p0, const glm::vec3& p1,
const glm::vec3& p2, const glm::vec3& p3, float t)
{
t = clamp(t, 0.0f, 1.0f);
t = std::clamp(t, 0.0f, 1.0f);
float oneMinusT = 1.0f - t;
return oneMinusT * oneMinusT * oneMinusT * p0 +
3.0f * oneMinusT * oneMinusT * t * p1 + 3.0f * oneMinusT * t * t * p2 +
Expand All @@ -118,7 +118,7 @@ glm::vec3 BezierSpline::GetBezierPoint(const glm::vec3& p0, const glm::vec3& p1,
glm::vec3 BezierSpline::GetBezierFirstDerivative(const glm::vec3& p0,
const glm::vec3& p1, const glm::vec3& p2, const glm::vec3& p3, float t)
{
t = clamp(t, 0.0f, 1.0f);
t = std::clamp(t, 0.0f, 1.0f);
float oneMinusT = 1.0f - t;
return 3.0f * oneMinusT * oneMinusT * (p1 - p0) +
6.0f * oneMinusT * t * (p2 - p1) + 3.0f * t * t * (p3 - p2);
Expand Down
3 changes: 1 addition & 2 deletions layer0/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <functional>
#include <vector>
#include "pymol/functional.h"

namespace pymol
{
Expand All @@ -19,7 +18,7 @@ template <typename... ParamTs> class Event
return;
}
for (const auto& callback : m_callbacks) {
pymol::invoke(callback, params...);
std::invoke(callback, params...);
}
}

Expand Down
6 changes: 3 additions & 3 deletions layer0/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,9 @@ void MapLocus(const MapType * I, const float *v, int *a, int *b, int *c)
bt = (int) ((v[1] - I->Min[1]) * invDiv) + MapBorder;
ct = (int) ((v[2] - I->Min[2]) * invDiv) + MapBorder;

*a = pymol::clamp(at, I->iMin[0], I->iMax[0]);
*b = pymol::clamp(bt, I->iMin[1], I->iMax[1]);
*c = pymol::clamp(ct, I->iMin[2], I->iMax[2]);
*a = std::clamp(at, I->iMin[0], I->iMax[0]);
*b = std::clamp(bt, I->iMin[1], I->iMax[1]);
*c = std::clamp(ct, I->iMin[2], I->iMax[2]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion layer0/MyPNG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ std::unique_ptr<pymol::Image> MyPNGRead(const char *file_name)
png_read_end(png_ptr, info_ptr);
}

img = pymol::make_unique<pymol::Image>(width, height);
img = std::make_unique<pymol::Image>(width, height);
if(ok) {
auto p = img->bits();
for(row = 0; row < (signed) height; row++) {
Expand Down
6 changes: 3 additions & 3 deletions layer0/PostProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ const renderTarget_t::shape_type PostProcess::size(std::size_t idx) const
OIT_PostProcess::OIT_PostProcess(int width, int height, renderBuffer_t* rbo)
{
if (TM3_IS_ONEBUF) {
auto rt0 = pymol::make_unique<renderTarget_t>(width, height);
auto rt0 = std::make_unique<renderTarget_t>(width, height);
rt0->layout({{4, rt_layout_t::FLOAT}}, rbo);
m_renderTargets.push_back(std::move(rt0));

auto rt1 = pymol::make_unique<renderTarget_t>(width, height);
auto rt1 = std::make_unique<renderTarget_t>(width, height);
rt1->layout({{1, rt_layout_t::FLOAT}}, rt0->rbo());
m_renderTargets.push_back(std::move(rt1));
} else {
Expand All @@ -74,7 +74,7 @@ OIT_PostProcess::OIT_PostProcess(int width, int height, renderBuffer_t* rbo)
layouts.emplace_back(2, rt_layout_t::FLOAT);
}

auto rt0 = pymol::make_unique<renderTarget_t>(width, height);
auto rt0 = std::make_unique<renderTarget_t>(width, height);
rt0->layout(std::move(layouts), rbo);
m_renderTargets.push_back(std::move(rt0));
}
Expand Down
2 changes: 1 addition & 1 deletion layer0/ShaderMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ void CShaderMgr::bindOffscreenOIT(int width, int height, int drawbuf) {
renderTarget_t::shape_type req_size(width, height);

if(!oit_pp || oit_pp->size() != req_size) {
oit_pp = pymol::make_unique<OIT_PostProcess>(
oit_pp = std::make_unique<OIT_PostProcess>(
width, height, getGPUBuffer<renderTarget_t>(offscreen_rt)->_rbo);
} else {
if (!TM3_IS_ONEBUF) {
Expand Down
2 changes: 1 addition & 1 deletion layer1/CGO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4103,7 +4103,7 @@ CGO *CGOOptimizeSpheresToVBONonIndexed(const CGO * I, int est, bool addshaders,

CGO* CGOOptimizeBezier(const CGO* I)
{
auto cgo = pymol::make_unique<CGO>(I->G);
auto cgo = std::make_unique<CGO>(I->G);
int num_splines = CGOCountNumberOfOperationsOfType(I, CGO_BEZIER);
auto vbo = I->G->ShaderMgr->newGPUBuffer<VertexBuffer>();
std::vector<float> vertData;
Expand Down
2 changes: 1 addition & 1 deletion layer1/CGOGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2537,7 +2537,7 @@ void CGORenderGLAlpha(CGO* I, RenderInfo* info, bool calcDepth)
float* const pc = it.data();
assert(base < pc && pc < I->op + I->c);
auto i =
pymol::clamp<int>((pc[4] - I->z_min) * range_factor, 0, i_size);
std::clamp<int>((pc[4] - I->z_min) * range_factor, 0, i_size);
CGO_put_int(pc, start[i]);
start[i] = (pc - base); /* NOTE: will always be > 0 since we have
CGO_read_int'd */
Expand Down
2 changes: 1 addition & 1 deletion layer1/Extrude.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ void ExtrudeShiftToAxis(CExtrude* I, float radius, int sampling)
float* avg = smoothed.data() + (a - 1) * 3;

for (int j = -w2; j <= w2; ++j) {
int const k = pymol::clamp(a + j, 0, I->N - 1);
int const k = std::clamp(a + j, 0, I->N - 1);
add3f(I->p + k * 3, avg, avg);
}

Expand Down
4 changes: 2 additions & 2 deletions layer1/Ortho.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ void OrthoBackgroundTextureNeedsUpdate(PyMOLGlobals * G){
static std::unique_ptr<pymol::Image> makeBgGridImage(PyMOLGlobals* G)
{
auto const& grid = G->Scene->grid;
auto tmpImg = pymol::make_unique<pymol::Image>( //
auto tmpImg = std::make_unique<pymol::Image>( //
grid.n_col > 1 ? grid.n_col : 1, //
grid.n_row > 1 ? grid.n_row : 1);

Expand All @@ -1343,7 +1343,7 @@ static std::unique_ptr<pymol::Image> makeBgGradientImage(PyMOLGlobals* G)
{
constexpr unsigned height = BACKGROUND_TEXTURE_SIZE;

auto tmpImg = pymol::make_unique<pymol::Image>(1, height);
auto tmpImg = std::make_unique<pymol::Image>(1, height);

float top[3], bottom[3], mixed[4]{0, 0, 0, 1};
copy3f(ColorGet(G, SettingGet_color(G, cSetting_bg_rgb_top)), top);
Expand Down
4 changes: 2 additions & 2 deletions layer1/SceneRay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ bool SceneRay(PyMOLGlobals * G,
switch (mode) {
case 0: /* mode 0 is built-in */
{
auto image = pymol::make_unique<pymol::Image>(ray_width, ray_height);
auto image = std::make_unique<pymol::Image>(ray_width, ray_height);
std::uint32_t background;

RayRender(ray, image->pixels(), timing, angle, antialias, &background);
Expand All @@ -346,7 +346,7 @@ bool SceneRay(PyMOLGlobals * G,
I->Image = std::move(image);
} else {
if(!I->Image) { /* alloc on first pass */
I->Image = pymol::make_unique<pymol::Image>(tot_width, tot_height);
I->Image = std::make_unique<pymol::Image>(tot_width, tot_height);
if(I->Image) {
unsigned int tot_size = tot_width * tot_height;
{ /* fill with background color */
Expand Down
2 changes: 1 addition & 1 deletion layer1/ScrollBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void ScrollBar::update()
m_ValueMax = static_cast<float>(m_ListSize - m_DisplaySize);
if(m_ValueMax < 1)
m_ValueMax = 1;
m_Value = pymol::clamp(m_Value, 0.0f, m_ValueMax);
m_Value = std::clamp(m_Value, 0.0f, m_ValueMax);
}

void ScrollBar::fill(CGO* orthoCGO)
Expand Down
2 changes: 1 addition & 1 deletion layer1/ScrollBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ScrollBar : public Block {
bool isMaxed() const;
void fill(CGO *orthoCGO);
void setValueNoCheck(float _value) { m_Value = _value; }
void setValue(float _value){ m_Value = pymol::clamp(_value, 0.0f, m_ValueMax); }
void setValue(float _value){ m_Value = std::clamp(_value, 0.0f, m_ValueMax); }
void drawNoFill(CGO *orthoCGO) { drawImpl(false, orthoCGO); }
int grabbed() { return OrthoGrabbedBy(m_G, this); }
float getValue() const { return m_Value; }
Expand Down
Loading

0 comments on commit 1180fec

Please sign in to comment.