annotate base/Point.h @ 1611:b2f32c554199 single-point

Pull out the Point class, plus start testing NoteModel, plus actually add the tests...
author Chris Cannam
date Tue, 05 Mar 2019 15:15:11 +0000
parents
children 23a29e5dc0e9
rev   line source
Chris@1611 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1611 2
Chris@1611 3 /*
Chris@1611 4 Sonic Visualiser
Chris@1611 5 An audio file viewer and annotation editor.
Chris@1611 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1611 7 This file copyright 2006 Chris Cannam.
Chris@1611 8
Chris@1611 9 This program is free software; you can redistribute it and/or
Chris@1611 10 modify it under the terms of the GNU General Public License as
Chris@1611 11 published by the Free Software Foundation; either version 2 of the
Chris@1611 12 License, or (at your option) any later version. See the file
Chris@1611 13 COPYING included with this distribution for more information.
Chris@1611 14 */
Chris@1611 15
Chris@1611 16 #ifndef SV_POINT_H
Chris@1611 17 #define SV_POINT_H
Chris@1611 18
Chris@1611 19 #include <QString>
Chris@1611 20
Chris@1611 21 #include "BaseTypes.h"
Chris@1611 22
Chris@1611 23 class Point
Chris@1611 24 {
Chris@1611 25 public:
Chris@1611 26 Point(sv_frame_t frame, QString label) :
Chris@1611 27 m_haveValue(false), m_haveLevel(false), m_haveFrame(true),
Chris@1611 28 m_haveDuration(false), m_haveReferenceFrame(false), m_haveLabel(true),
Chris@1611 29 m_value(0.f), m_level(0.f), m_frame(frame),
Chris@1611 30 m_duration(0), m_referenceFrame(0), m_label(label) { }
Chris@1611 31
Chris@1611 32 Point(sv_frame_t frame, float value, QString label) :
Chris@1611 33 m_haveValue(true), m_haveLevel(false), m_haveFrame(true),
Chris@1611 34 m_haveDuration(false), m_haveReferenceFrame(false), m_haveLabel(true),
Chris@1611 35 m_value(value), m_level(0.f), m_frame(frame),
Chris@1611 36 m_duration(0), m_referenceFrame(0), m_label(label) { }
Chris@1611 37
Chris@1611 38 Point(sv_frame_t frame, float value, sv_frame_t duration, QString label) :
Chris@1611 39 m_haveValue(true), m_haveLevel(false), m_haveFrame(true),
Chris@1611 40 m_haveDuration(true), m_haveReferenceFrame(false), m_haveLabel(true),
Chris@1611 41 m_value(value), m_level(0.f), m_frame(frame),
Chris@1611 42 m_duration(duration), m_referenceFrame(0), m_label(label) { }
Chris@1611 43
Chris@1611 44 Point(sv_frame_t frame, float value, sv_frame_t duration, float level, QString label) :
Chris@1611 45 m_haveValue(true), m_haveLevel(true), m_haveFrame(true),
Chris@1611 46 m_haveDuration(true), m_haveReferenceFrame(false), m_haveLabel(true),
Chris@1611 47 m_value(value), m_level(level), m_frame(frame),
Chris@1611 48 m_duration(duration), m_referenceFrame(0), m_label(label) { }
Chris@1611 49
Chris@1611 50 Point(const Point &point) =default;
Chris@1611 51 Point &operator=(const Point &point) =default;
Chris@1611 52 Point &operator=(Point &&point) =default;
Chris@1611 53
Chris@1611 54 bool haveFrame() const { return m_haveFrame; }
Chris@1611 55 sv_frame_t getFrame() const { return m_frame; }
Chris@1611 56
Chris@1611 57 Point withFrame(sv_frame_t frame) const {
Chris@1611 58 Point p(*this);
Chris@1611 59 p.m_haveFrame = true;
Chris@1611 60 p.m_frame = frame;
Chris@1611 61 return p;
Chris@1611 62 }
Chris@1611 63
Chris@1611 64 bool haveValue() const { return m_haveValue; }
Chris@1611 65 float getValue() const { return m_value; }
Chris@1611 66
Chris@1611 67 Point withValue(float value) const {
Chris@1611 68 Point p(*this);
Chris@1611 69 p.m_haveValue = true;
Chris@1611 70 p.m_value = value;
Chris@1611 71 return p;
Chris@1611 72 }
Chris@1611 73
Chris@1611 74 bool haveDuration() const { return m_haveDuration; }
Chris@1611 75 sv_frame_t getDuration() const { return m_duration; }
Chris@1611 76
Chris@1611 77 Point withDuration(sv_frame_t duration) const {
Chris@1611 78 Point p(*this);
Chris@1611 79 p.m_haveDuration = true;
Chris@1611 80 p.m_duration = duration;
Chris@1611 81 return p;
Chris@1611 82 }
Chris@1611 83
Chris@1611 84 bool haveLabel() const { return m_haveLabel; }
Chris@1611 85 QString getLabel() const { return m_label; }
Chris@1611 86
Chris@1611 87 Point withLabel(QString label) const {
Chris@1611 88 Point p(*this);
Chris@1611 89 p.m_haveLabel = true;
Chris@1611 90 p.m_label = label;
Chris@1611 91 return p;
Chris@1611 92 }
Chris@1611 93
Chris@1611 94 bool haveLevel() const { return m_haveLevel; }
Chris@1611 95 float getLevel() const { return m_level; }
Chris@1611 96 Point withLevel(float level) const {
Chris@1611 97 Point p(*this);
Chris@1611 98 p.m_haveLevel = true;
Chris@1611 99 p.m_level = level;
Chris@1611 100 return p;
Chris@1611 101 }
Chris@1611 102
Chris@1611 103 bool haveReferenceFrame() const { return m_haveReferenceFrame; }
Chris@1611 104 sv_frame_t getReferenceFrame() const { return m_referenceFrame; }
Chris@1611 105
Chris@1611 106 bool referenceFrameDiffers() const { // from point frame
Chris@1611 107 return m_haveReferenceFrame && (m_referenceFrame != m_frame);
Chris@1611 108 }
Chris@1611 109
Chris@1611 110 Point withReferenceFrame(sv_frame_t frame) const {
Chris@1611 111 Point p(*this);
Chris@1611 112 p.m_haveReferenceFrame = true;
Chris@1611 113 p.m_referenceFrame = frame;
Chris@1611 114 return p;
Chris@1611 115 }
Chris@1611 116
Chris@1611 117 private:
Chris@1611 118 // The order of fields here is chosen to minimise overall size of struct.
Chris@1611 119 // If you change something, check what difference it makes to packing.
Chris@1611 120 bool m_haveValue : 1;
Chris@1611 121 bool m_haveLevel : 1;
Chris@1611 122 bool m_haveFrame : 1;
Chris@1611 123 bool m_haveDuration : 1;
Chris@1611 124 bool m_haveReferenceFrame : 1;
Chris@1611 125 bool m_haveLabel : 1;
Chris@1611 126 float m_value;
Chris@1611 127 float m_level;
Chris@1611 128 sv_frame_t m_frame;
Chris@1611 129 sv_frame_t m_duration;
Chris@1611 130 sv_frame_t m_referenceFrame;
Chris@1611 131 QString m_label;
Chris@1611 132 };
Chris@1611 133
Chris@1611 134 #endif