Various header cleanups. NFC.

* Don't use a reserved identifier in include guards.
  * Use fabs() from <cmath> instead of our own ffabs().
    This shouldn't make any difference with modern toolchains.
  * Convert a few preprocessor macros to constexprs.
pull/598/head^2
whitequark 2020-05-10 09:24:12 +00:00
parent e00d4867e1
commit d857e3e9cd
5 changed files with 28 additions and 35 deletions

View File

@ -7,6 +7,8 @@
#ifndef SOLVESPACE_GUI_H #ifndef SOLVESPACE_GUI_H
#define SOLVESPACE_GUI_H #define SOLVESPACE_GUI_H
class RgbaColor;
namespace Platform { namespace Platform {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -4,13 +4,14 @@
// Copyright 2016 whitequark // Copyright 2016 whitequark
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef __RESOURCE_H #ifndef SOLVESPACE_RESOURCE_H
#define __RESOURCE_H #define SOLVESPACE_RESOURCE_H
class Camera; class Camera;
class Point2d; class Point2d;
class Pixmap; class Pixmap;
class Vector; class Vector;
class RgbaColor;
std::string LoadString(const std::string &name); std::string LoadString(const std::string &name);
std::string LoadStringFromGzip(const std::string &name); std::string LoadStringFromGzip(const std::string &name);

View File

@ -151,7 +151,7 @@ void SolveSpaceUI::Init() {
} }
bool SolveSpaceUI::LoadAutosaveFor(const Platform::Path &filename) { bool SolveSpaceUI::LoadAutosaveFor(const Platform::Path &filename) {
Platform::Path autosaveFile = filename.WithExtension(AUTOSAVE_EXT); Platform::Path autosaveFile = filename.WithExtension(BACKUP_EXT);
FILE *f = OpenFile(autosaveFile, "rb"); FILE *f = OpenFile(autosaveFile, "rb");
if(!f) if(!f)
@ -474,7 +474,7 @@ bool SolveSpaceUI::GetFilenameAndSave(bool saveAs) {
if(saveAs || saveFile.IsEmpty()) { if(saveAs || saveFile.IsEmpty()) {
Platform::FileDialogRef dialog = Platform::CreateSaveFileDialog(GW.window); Platform::FileDialogRef dialog = Platform::CreateSaveFileDialog(GW.window);
dialog->AddFilter(C_("file-type", "SolveSpace models"), { "slvs" }); dialog->AddFilter(C_("file-type", "SolveSpace models"), { SKETCH_EXT });
dialog->ThawChoices(settings, "Sketch"); dialog->ThawChoices(settings, "Sketch");
if(!newSaveFile.IsEmpty()) { if(!newSaveFile.IsEmpty()) {
dialog->SetFilename(newSaveFile); dialog->SetFilename(newSaveFile);
@ -503,13 +503,13 @@ void SolveSpaceUI::Autosave()
ScheduleAutosave(); ScheduleAutosave();
if(!saveFile.IsEmpty() && unsaved) { if(!saveFile.IsEmpty() && unsaved) {
SaveToFile(saveFile.WithExtension(AUTOSAVE_EXT)); SaveToFile(saveFile.WithExtension(BACKUP_EXT));
} }
} }
void SolveSpaceUI::RemoveAutosave() void SolveSpaceUI::RemoveAutosave()
{ {
Platform::Path autosaveFile = saveFile.WithExtension(AUTOSAVE_EXT); Platform::Path autosaveFile = saveFile.WithExtension(BACKUP_EXT);
RemoveFile(autosaveFile); RemoveFile(autosaveFile);
} }

View File

@ -85,6 +85,7 @@ namespace SolveSpace {
using std::min; using std::min;
using std::max; using std::max;
using std::swap; using std::swap;
using std::fabs;
#if defined(__GNUC__) #if defined(__GNUC__)
__attribute__((noreturn)) __attribute__((noreturn))
@ -116,44 +117,31 @@ inline double WRAP_SYMMETRIC(double v, double n) {
return v; return v;
} }
// Why is this faster than the library function?
inline double ffabs(double v) { return (v > 0) ? v : (-v); }
#define CO(v) (v).x, (v).y, (v).z #define CO(v) (v).x, (v).y, (v).z
#define ANGLE_COS_EPS (1e-6) static constexpr double ANGLE_COS_EPS = 1e-6;
#define LENGTH_EPS (1e-6) static constexpr double LENGTH_EPS = 1e-6;
#define VERY_POSITIVE (1e10) static constexpr double VERY_POSITIVE = 1e10;
#define VERY_NEGATIVE (-1e10) static constexpr double VERY_NEGATIVE = -1e10;
inline double Random(double vmax) { inline double Random(double vmax) {
return (vmax*rand()) / RAND_MAX; return (vmax*rand()) / RAND_MAX;
} }
#include "platform/platform.h"
#include "platform/gui.h"
#include "resource.h"
class Expr; class Expr;
class ExprVector; class ExprVector;
class ExprQuaternion; class ExprQuaternion;
class RgbaColor; class RgbaColor;
enum class Command : uint32_t; enum class Command : uint32_t;
//================ // Temporary heap, defined in the platform-specific code.
// From the platform-specific code.
#include "platform/platform.h"
#include "platform/gui.h"
const size_t MAX_RECENT = 8;
#define AUTOSAVE_EXT "slvs~"
void *AllocTemporary(size_t n); void *AllocTemporary(size_t n);
void FreeAllTemporary(); void FreeAllTemporary();
// End of platform-specific functions
//================
#include "resource.h"
enum class Unit : uint32_t { enum class Unit : uint32_t {
MM = 0, MM = 0,
INCHES, INCHES,
@ -670,7 +658,9 @@ public:
static void MenuFile(Command id); static void MenuFile(Command id);
void Autosave(); void Autosave();
void RemoveAutosave(); void RemoveAutosave();
static const size_t MAX_RECENT = 8; static constexpr size_t MAX_RECENT = 8;
static constexpr const char *SKETCH_EXT = "slvs";
static constexpr const char *BACKUP_EXT = "slvs~";
std::vector<Platform::Path> recentFiles; std::vector<Platform::Path> recentFiles;
bool Load(const Platform::Path &filename); bool Load(const Platform::Path &filename);
bool GetFilenameAndSave(bool saveAs); bool GetFilenameAndSave(bool saveAs);

View File

@ -187,15 +187,15 @@ bool System::SolveLinearSystem(double X[], double A[][MAX_UNKNOWNS],
// greater. First, find a pivot (between rows i and N-1). // greater. First, find a pivot (between rows i and N-1).
max = 0; max = 0;
for(ip = i; ip < n; ip++) { for(ip = i; ip < n; ip++) {
if(ffabs(A[ip][i]) > max) { if(fabs(A[ip][i]) > max) {
imax = ip; imax = ip;
max = ffabs(A[ip][i]); max = fabs(A[ip][i]);
} }
} }
// Don't give up on a singular matrix unless it's really bad; the // Don't give up on a singular matrix unless it's really bad; the
// assumption code is responsible for identifying that condition, // assumption code is responsible for identifying that condition,
// so we're not responsible for reporting that error. // so we're not responsible for reporting that error.
if(ffabs(max) < 1e-20) continue; if(fabs(max) < 1e-20) continue;
// Swap row imax with row i // Swap row imax with row i
for(jp = 0; jp < n; jp++) { for(jp = 0; jp < n; jp++) {
@ -217,7 +217,7 @@ bool System::SolveLinearSystem(double X[], double A[][MAX_UNKNOWNS],
// We've put the matrix in upper triangular form, so at this point we // We've put the matrix in upper triangular form, so at this point we
// can solve by back-substitution. // can solve by back-substitution.
for(i = n - 1; i >= 0; i--) { for(i = n - 1; i >= 0; i--) {
if(ffabs(A[i][i]) < 1e-20) continue; if(fabs(A[i][i]) < 1e-20) continue;
temp = B[i]; temp = B[i];
for(j = n - 1; j > i; j--) { for(j = n - 1; j > i; j--) {
@ -309,7 +309,7 @@ bool System::NewtonSolve(int tag) {
if(isnan(mat.B.num[i])) { if(isnan(mat.B.num[i])) {
return false; return false;
} }
if(ffabs(mat.B.num[i]) > CONVERGE_TOLERANCE) { if(fabs(mat.B.num[i]) > CONVERGE_TOLERANCE) {
converged = false; converged = false;
break; break;
} }
@ -493,7 +493,7 @@ didnt_converge:
SK.constraint.ClearTags(); SK.constraint.ClearTags();
// Not using range-for here because index is used in additional ways // Not using range-for here because index is used in additional ways
for(i = 0; i < eq.n; i++) { for(i = 0; i < eq.n; i++) {
if(ffabs(mat.B.num[i]) > CONVERGE_TOLERANCE || isnan(mat.B.num[i])) { if(fabs(mat.B.num[i]) > CONVERGE_TOLERANCE || isnan(mat.B.num[i])) {
// This constraint is unsatisfied. // This constraint is unsatisfied.
if(!mat.eq[i].isFromConstraint()) continue; if(!mat.eq[i].isFromConstraint()) continue;