pianod2
multisource multiuser scriptable networked music player
ratings.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <config.h>
13 
14 #include "lookup.h"
15 
17 enum class RatingScheme {
18  NOBODY,
19  INDIVIDUAL,
20  OWNER
21 };
22 
24 enum class Rating {
25  UNRATED,
26  REPUGNANT,
27  AWFUL,
28  BAD,
29  POOR,
30  LACKLUSTER,
31  NEUTRAL,
32  OKAY,
33  GOOD,
34  EXCELLENT,
35  SUPERB
36 };
37 
39 inline constexpr float ratingAsFloat (Rating rating) {
40  return (((float)(rating)) / 2);
41 }
42 
45  unsigned count = 0;
46  unsigned total = 0;
47 public:
50  inline void add (Rating rating) {
51  if (rating != Rating::UNRATED) {
52  count++;
53  total += static_cast <int> (rating);
54  }
55  }
59  float operator ()(Rating default_value = Rating::UNRATED) {
60  return (count ? (static_cast <float> (total) / static_cast <float> (count * 2))
61  : ratingAsFloat (default_value));
62  }
66  double rating (Rating default_value = Rating::UNRATED) {
67  return (count ? (static_cast <double> (total) / static_cast <double> (count))
68  : static_cast <double> (default_value));
69  }
71  inline unsigned sum () const { return total; };
73  inline unsigned items () const { return count; };
74 
75 };
76 
77 
78 Rating floatToRating (float rating);
79 
80 typedef struct rating_lookup_t {
82  const char *name;
84 
89 class RatingLookup : public LookupTable<Rating, RATING_LOOKUP> {
91 public:
93  bool tryGetPrecise (const char *s, float *rating) const noexcept;
94  inline bool tryGetPrecise (const std::string &s, float *rating) const noexcept {
95  return tryGetPrecise (s.c_str(), rating);
96  }
97  float getPrecise (const char *s) const;
98  inline float getPrecise (const std::string &s) const {
99  return getPrecise (s.c_str());
100  }
101  virtual const RATING_LOOKUP *get (const char *s) const noexcept override;
102 };
103 
104 extern const RatingLookup RATINGS;
105 
Class to map between enumerations and their text values.
Definition: lookup.h:30
A class to average ratings.
Definition: ratings.h:44
float operator()(Rating default_value=Rating::UNRATED)
Return the average as a star-rating float.
Definition: ratings.h:59
unsigned count
Number of ratings being averaged.
Definition: ratings.h:45
unsigned total
Total of enumeration values being averaged.
Definition: ratings.h:46
unsigned sum() const
Retrieve the sum of the ratings in the average.
Definition: ratings.h:71
unsigned items() const
Retrieve the number of items included in the average.
Definition: ratings.h:73
void add(Rating rating)
Add mote data to the average.
Definition: ratings.h:50
double rating(Rating default_value=Rating::UNRATED)
Return the average as a rating-range double.
Definition: ratings.h:66
String to rating conversion.
Definition: ratings.h:89
LookupTable< Rating, RATING_LOOKUP > base_class
Definition: ratings.h:90
float getPrecise(const char *s) const
Get a precise rating, accepting either a rating string or number.
Definition: ratings.cpp:81
float getPrecise(const std::string &s) const
Definition: ratings.h:98
bool tryGetPrecise(const char *s, float *rating) const noexcept
Retrieve the enumerator corresponding to text name.
Definition: ratings.cpp:63
bool tryGetPrecise(const std::string &s, float *rating) const noexcept
Definition: ratings.h:94
virtual const RATING_LOOKUP * get(const char *s) const noexcept override
Retrieve the enumerator corresponding to text name.
Definition: ratings.cpp:45
Lookup tables.
constexpr float ratingAsFloat(Rating rating)
Convert from a ratings enumeration to a floating-point star-rating.
Definition: ratings.h:39
Rating
Discrete ratings values.
Definition: ratings.h:24
@ POOR
2 star, 4 enum
@ AWFUL
1 star, 2 enum
@ NEUTRAL
3 star, 6 enum
@ GOOD
4 star, 8 enum
@ LACKLUSTER
2.5 star, 5 enum
@ OKAY
3.5 star, 7 enum
@ REPUGNANT
0.5 star, 1 enum
@ EXCELLENT
4.5 star, 9 enum
@ UNRATED
Unrated/no rating found, 0 enum.
@ SUPERB
5 star, 10 enum
@ BAD
1.5 star, 3 enum
Rating floatToRating(float rating)
Convert a star-rating to a ratings enumeration.
Definition: ratings.cpp:92
const RatingLookup RATINGS
RatingScheme
Rules of who may rate an object.
Definition: ratings.h:17
@ INDIVIDUAL
Each user gets a private rating for this song.
@ NOBODY
Ratings are unsupported.
@ OWNER
One rating/song, so only ratable by owner.
struct rating_lookup_t RATING_LOOKUP
Definition: ratings.h:80
const char * name
Definition: ratings.h:82
Rating value
Definition: ratings.h:81