(C++) Shape
January 25, 2018 · View on GitHub
(C++) Shape



Technical facts
./CppShape/CppShape.pri
INCLUDEPATH += \ ../../Classes/CppShape SOURCES += \ ../../Classes/CppShape/shape.cpp HEADERS += \ ../../Classes/CppShape/shape.h OTHER_FILES += \ ../../Classes/CppShape/Licence.txt
./CppShape/shape.h
//--------------------------------------------------------------------------- /* Shape, shape class Copyright 2011-2015 Richel Bilderbeek 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/>. */ //--------------------------------------------------------------------------- //From http://www.richelbilderbeek.nl/CppShape.htm //--------------------------------------------------------------------------- #ifndef SHAPE_H #define SHAPE_H #include <string> #include <vector> #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #pragma GCC diagnostic ignored "-Wunused-but-set-parameter" #include <boost/checked_delete.hpp> #include <boost/signals2.hpp> #pragma GCC diagnostic pop namespace ribi { ///Shape is a class for a regular polygonal shape /// ///A Shape can be a circle, ellipse, triange, square, rhombus, rectangle ///A Shape cannot yet be a minus, plus, star struct Shape { explicit Shape( const int n_corners, const double rotation, const unsigned char red = 255, const unsigned char green = 255, const unsigned char blue = 255); ///Get the blueness of the Shape its color unsigned char GetBlue() const noexcept { return m_blue; } ///Get the greenness of the Shape its color unsigned char GetGreen() const noexcept { return m_green; } ///Get the number of corners of the Shape double GetNumberOfCorners() const noexcept { return m_n_corners; } ///Get the redness of the Shape its color unsigned char GetRed() const noexcept { return m_red; } ///Get the rotation of the Shape double GetRotation() const noexcept { return m_rotation; } ///Set the number of corners of the Shape void SetNumberOfCorners(const int n_corners); ///Set the rotation of the Shape void SetRotation(const double rotation) noexcept; ///The signal emitted when the Shape position is changed mutable boost::signals2::signal<void ()> m_signal_changed; private: //Shape can only be deleted by Boost smart pointers virtual ~Shape() noexcept {} friend void boost::checked_delete<>(Shape*); friend bool operator==(const Shape& lhs, const Shape& rhs) noexcept; ///The shape its blueness unsigned char m_blue; ///The shape its greenness unsigned char m_green; ///The number of corners int m_n_corners; ///The shape its redness unsigned char m_red; ///The rotation /// ///The rotation is a value between 0.0 and 2.0 * pi, ///in which 0.0 * pi is the equivalent to 12:00 o'clock ///and which 0.5 * pi is the equivalent to 3:00 o'clock ///Values not in this range are accepted nonetheless double m_rotation; #ifndef NDEBUG ///Test this class static void Test() noexcept; #endif public: static std::string GetVersion() noexcept; static std::vector<std::string> GetVersionHistory() noexcept; }; bool operator==(const Shape& lhs, const Shape& rhs) noexcept; } //~namespace ribi #endif // SHAPE_H
./CppShape/shape.cpp
//--------------------------------------------------------------------------- /* Shape, shape class Copyright 2011-2015 Richel Bilderbeek 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/>. */ //--------------------------------------------------------------------------- //From http://www.richelbilderbeek.nl/CppShape.htm //--------------------------------------------------------------------------- #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #pragma GCC diagnostic ignored "-Wunused-but-set-parameter" #include "shape.h" #include <cassert> #include <boost/math/constants/constants.hpp> //#include "geometry.h" #include "trace.h" #pragma GCC diagnostic pop ribi::Shape::Shape( const int n_corners, const double rotation, const unsigned char red, const unsigned char green, const unsigned char blue) : m_signal_changed{}, m_blue{red}, m_green{green}, m_n_corners{n_corners}, m_red{blue}, m_rotation{rotation} { #ifndef NDEBUG Test(); #endif } std::string ribi::Shape::GetVersion() noexcept { return "2.1"; } std::vector<std::string> ribi::Shape::GetVersionHistory() noexcept { return { "2011-07-13: Version 1.0: initial version", "2011-08-08: Version 2.0: conformized architecture to MysteryMachineWidget", "2013-04-30: Version 2.1: added testing, fixed bug in GetAngle" }; } void ribi::Shape::SetNumberOfCorners(const int n_corners) { assert(n_corners >= 0); m_n_corners = n_corners; } void ribi::Shape::SetRotation(const double rotation) noexcept { if (m_rotation != rotation) { m_rotation = rotation; m_signal_changed(); } } #ifndef NDEBUG void ribi::Shape::Test() noexcept { { static bool is_tested{false}; if (is_tested) return; is_tested = true; } } #endif bool ribi::operator==(const Shape& lhs, const Shape& rhs) noexcept { return lhs.m_blue == rhs.m_blue && lhs.m_green == rhs.m_green && lhs.m_n_corners == rhs.m_n_corners && lhs.m_red == rhs.m_red && lhs.m_rotation == rhs.m_rotation; }