Escualo
Escualo

Reputation: 42182

Using enum class for defining flags

What is the appropriate pattern for using enumerations as flags in modern C++?

The question stems from my reading of the technical specification A Proposal to Add 2D Graphics Rendering and Display to C++, where McLaughlin, Sutter, and Zink propose a C++ API for 2D graphics based on the Cairo API.

Throughout the API declaration, the authors make full use of C++11. In particular, their enumerations are all declared like:

enum class font_slant {
  normal,
  italic,
  oblique
};

Except for a single one:

namespace text_cluster_flags {
  enum text_cluster_flags : int {
    none     = 0x0,
    backward = 0x1
  };
}

The text_cluster_flags "type" is used in a class method:

void show_text_glyphs(const ::std::string& utf8,
    const ::std::vector<glyph>& glyphs,
    const ::std::vector<text_cluster>& clusters,
    text_cluster_flags cluster_flags);

I assume the extraneous declaration is so that the text_cluster_flags can be masked, as in:

auto flag = text_cluster_flags::none | text_cluster_flags::backward;

which you cannot do with enum class enumerations:

enum class flags {
  a = 0x0,
  b = 0x1
};

auto f = flags::a | flags::b; // error because operator `|` is
                              // not defined for enum class flags
                              // values

Should the authors have defined the masking operators? Or is their enum-within-namespace pattern effective practice?

Upvotes: 3

Views: 375

Answers (1)

user5326953
user5326953

Reputation: 21

It's modelled after the cairo API.

For font_slant we can see cairo's equivalent:

enum cairo_font_slant_t

typedef enum {
    CAIRO_FONT_SLANT_NORMAL,
    CAIRO_FONT_SLANT_ITALIC,
    CAIRO_FONT_SLANT_OBLIQUE
} cairo_font_slant_t;

Specifies variants of a font face based on their slant.

      CAIRO_FONT_SLANT_NORMAL Upright font style, since 1.0

      CAIRO_FONT_SLANT_ITALIC Italic font style, since 1.0

      CAIRO_FONT_SLANT_OBLIQUE Oblique font style, since 1.0

For text_cluster_flags we can see cairo's equivalent:

enum cairo_text_cluster_flags_t

typedef enum {
    CAIRO_TEXT_CLUSTER_FLAG_BACKWARD = 0x00000001
} cairo_text_cluster_flags_t;

Specifies properties of a text cluster mapping.

      CAIRO_TEXT_CLUSTER_FLAG_BACKWARD The clusters in the cluster array map to glyphs in the glyph array from end to start. (Since 1.8)

The function text_to_glyphs models cairo_show_text_glyphs, which takes a cairo_text_cluster_flags_t. Furthermore, the API has a function for getting the current slant. So my guess is:

  • enum class is meant for strong typing of a flag. It doesn't make any sense to have something that's both "normal" and "italic". These are attached to a "font face".

  • text_cluster_flags is a one-time deal. If you set it for the show glyphs function, it simply changes the behavior. It's not attached to a "text cluster" like slant is attached to a "font face". There's no reason for strong typing here.

Your interpretation was correct btw. Here's a snippet of the source code:

// ...

+       const cairo_glyph_t *cur_glyph;
+
+       if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
+       cur_glyph = glyphs + num_glyphs - 1;
+       else
+       cur_glyph = glyphs;
+
+       for (i = 0; i < num_clusters; i++) {
+       cairo_bool_t cluster_visible = FALSE;
+

// ...

Upvotes: 2

Related Questions