Matroska-Org / libmatroska

a C++ libary to parse Matroska files (.mkv and .mka)
GNU Lesser General Public License v2.1
318 stars 57 forks source link

querying valid values for enums #149

Open mbunkus opened 9 months ago

mbunkus commented 9 months ago

Looking at #148 which embeds the information about the minimum & maximum version number an element is valid for in libMatroska, I thought about also having the information available which values are valid for the various enums we have in the specs. At the moment we only have C-style enum for them, which means we can use them in the code via their names, but there's no way to have the program query the library if a given value is valid or not.

For example, it would be nice to be able to query libMatroska & ask if the value 1 is valid for the "color primaries" element (it is) or if 14 is (it isn't).

On top of that it would be nice if the library simply provided a list of valid value/description pairs that an application can use for selection or to translate what a given value refers to (think mkvinfo outputting the name of the "color primaries" along with the numeric value).

I'm thinking about two or three functions, the example being for an unsigned integer-based enum:

static bool IsValueInSpecs(uint64_t value);
static std::string GetDescriptionForValueFromSpecs(uint64_t value);
static std::vector<std::pair<uint64_t, std::string>> GetValuesDescriptionsFromSpecs();

I'm trying to avoid the word "valid" here as these functions are all static; they only provide information straight from the specs. Not really happy with the names, though…

robUx4 commented 9 months ago

Sounds good although I don't understand the static part. It seems the values are tied to one element (it is in the XML spec) and so each enum belongs to a particular element (and can be moved there). The enums are generated from the XML so we can do all kind of things here.

mbunkus commented 9 months ago

I meant "static member functions", not "static global functions", e.g.

class KaxTracktype {
public:
  static bool IsValueInSpecs(uint64_t value);
};

As in… a static function on the respective classes instead of a member function on the instances of the classes.

mbunkus commented 9 months ago

We might also add

class KaxTrackType {
  bool IsCurrentValueInSpecs() const {
    if (!ValueIsSet())
      return false;
    return IsValueInSpecs(GetValue());
  }
};

as an instance method. The other three functions I mentioned are independent of the instance, though.

robUx4 commented 9 months ago

Ah yes, that could work, although I would put that in the EbmlCallbacks (which name is not very good) as it holds all the specs data and there's one static instance per class.

mbunkus commented 9 months ago

Ah, you're right, that would be the logical place to put the static functions.