src/main.h

Mon, 24 Aug 2020 23:02:30 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 24 Aug 2020 23:02:30 +0300
changeset 93
6fe24fd945c0
parent 81
62373840e33a
child 96
165777a20dc7
permissions
-rw-r--r--

fix warnings

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2020 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once
#include <QString>
#include <QVector>
#include <QSet>
#include <memory>
#include "basics.h"
#include "utility.h"
#include "maths.h"
#include "geometry.h"
#include "functional.h"

namespace settingGroups
{
	// List of setting groups
	constexpr char mainwindow[] = "mainwindow";
}

namespace ldraw
{
	class Object;

	// Uniquely identifies a model body object
	template<typename T>
	struct Id
	{
		std::int32_t value;
		template<typename A, typename B>
		static constexpr bool is_base_or_base_of = std::disjunction_v<std::is_base_of<A, B>, std::is_base_of<B, A>>;
		template<typename R, typename = std::enable_if_t<is_base_or_base_of<T, R>>>
		constexpr bool operator<(ldraw::Id<R> other) const
		{
			return this->value < other.value;
		}
		friend constexpr unsigned int qHash(ldraw::Id<T> id)
		{
			return qHash(id.value);
		}
		// Allow comparing ids as long as they are related
		template<typename R, typename = std::enable_if_t<is_base_or_base_of<T, R>>>
		friend bool operator==(ldraw::Id<T> one, ldraw::Id<R> other)
		{
			return one.value == other.value;
		}
		// Allow upcasting
		template<typename R, typename = std::enable_if_t<std::is_base_of_v<R, T>>>
		constexpr operator Id<R>() const
		{
			return Id<R>{this->value};
		}
	};

	using id_t = Id<Object>;
	using triangleid_t = Id<class Triangle>;
	using quadrilateralid_t = Id<class Quadrilateral>;
	using edgeid_t = Id<class EdgeLine>;
	using conditionaledgeid_t = Id<class ConditionalEdge>;
	using subfileid_t = Id<class SubfileReference>;

	constexpr struct NullId
	{
		template<typename T>
		constexpr operator Id<T>() const
		{
			return Id<T>{0};
		}
		static constexpr decltype(ldraw::id_t::value) value = 0;
	} NULL_ID = {};

	template<typename T>
	inline bool operator==(Id<T> one, decltype(NULL_ID))
	{
		return one.value == 0;
	}

	template<typename T>
	inline bool operator<(Id<T> one, decltype(NULL_ID))
	{
		return one.value < 0;
	}
}

constexpr std::size_t operator""_z(const unsigned long long int x)
{
	return static_cast<std::size_t>(x);
}

inline QString operator""_q(const char* string, const unsigned long int length)
{
	Q_UNUSED(length)
	return QString{string};
}

inline QPointF pointToPointF(const QPoint& point)
{
	return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())};
}

inline QPoint pointFToPoint(const QPointF& point)
{
	return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))};
}

/**
 * \brief Hints to the specified vector that a certain amount of new elements are going to be added.
 * \param vector vector to consider
 * \param amount amount of new elements to expect
 */
template<typename T>
void reserveMore(std::vector<T>& vector, std::size_t amount)
{
	vector.reserve(vector.size() + amount);
}

inline QString vectorToString(const glm::vec2& vec)
{
	return "(%1, %2)"_q
		.arg(toDouble(vec.x))
		.arg(toDouble(vec.y));
}

inline QString vectorToString(const glm::vec3& vec)
{
	return "(%1, %2, %3)"_q
		.arg(toDouble(vec.x))
		.arg(toDouble(vec.y))
		.arg(toDouble(vec.z));
}

inline QString vectorToString(const glm::vec4& vec)
{
	return "(%1, %2, %3, %4)"_q
		.arg(toDouble(vec.x))
		.arg(toDouble(vec.y))
		.arg(toDouble(vec.z))
		.arg(toDouble(vec.w));
}

mercurial