Hello,

for the integer wraps used for flags I have a third proposition, a working 
example is attached:
* "enum class DisplayOption" for the individual flags
* "class DisplayOptions" with an implicit converter DisplayOption -> 
DisplayOptions 
So we get an enum for the single options and avoid the old enum-style.

Diether

#include <iostream>

enum class DisplayOption {
  SHOW_HEADING = 1 << 0,
  SHOW_DAY_NAMES = 1 << 1,
  NO_MONTH_CHANGE = 1 << 2,
  SHOW_WEEK_NUMBERS = 1 << 3,
  SHOW_DETAILS = 1 << 5
};
class DisplayOptions {
  friend DisplayOptions operator|(DisplayOption, DisplayOption);
  public:
    DisplayOptions() = default;
    DisplayOptions(DisplayOption v)
      : value(static_cast<int>(v))
    { }

    operator bool()
    { return value != 0; }

    DisplayOptions operator&(DisplayOption rhs) const
    {
      if (value & static_cast<int>(rhs))
        return rhs;
      else
        return {};
    }
    DisplayOptions& operator|=(DisplayOption rhs)
    {
      value |= static_cast<int>(rhs);
      return *this;
    }
  private:
    int value = 0;
};

DisplayOptions operator|(DisplayOption lhs, DisplayOption rhs)
{
  DisplayOptions res(lhs);
  res.value |= static_cast<int>(rhs);
  return res;
}

/*
 */
void write(DisplayOptions o)
{
  if (!o)
    std::cout << "none\n";
  if (o) {
    if (o & DisplayOption::SHOW_HEADING)
      std::cout << "show heading\n";
    if (o & DisplayOption::SHOW_DAY_NAMES)
      std::cout << "show day names\n";
    if (o & DisplayOption::NO_MONTH_CHANGE)
      std::cout << "no month names\n";
    if (o & DisplayOption::SHOW_WEEK_NUMBERS)
      std::cout << "show week numbers\n";
    if (o & DisplayOption::SHOW_DETAILS)
      std::cout << "show details\n";
  }
  std::cout << "\n";
}

/*
*/
int
main()
{
  auto x = DisplayOption::SHOW_HEADING;
  write(x);

  write(x | DisplayOption::SHOW_DETAILS);

  // not possible: x |= DisplayOption::SHOW_DETAILS

  auto y = DisplayOptions(DisplayOption::SHOW_DAY_NAMES);
  y |= DisplayOption::SHOW_DETAILS;
  write(y);

  return 0;
}

Attachment: pgp5TMaz1rg3t.pgp
Description: Digitale Signatur von OpenPGP

_______________________________________________
gtkmm-list mailing list
gtkmm-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtkmm-list

Reply via email to