Skip to content

Add a generated stringification method #580

@staktrace

Description

@staktrace

I'd like to add the ability for cbindgen to generate a stringifier for enums and structs. It could be controlled by a derive-ostream option similar to derive-eq and friends. For enums it would generate something like:

std::ostream& operator<<(std::ostream& aStream, const MyEnum& aEnum) {
  switch (aEnum) {
    case One:
      aStream << "One";
      break;
    ...
  }
  return aStream;
}

and for structs it would do something like this:

struct MyStruct {
  int x;
  int y;
  ...
  friend std::ostream& operator<<(std::ostream& aStream, const MyStruct& aStruct) {
    return aStream << "{ x=" << aStruct.x << ", y=" << aStruct.y << " }";
  }
  ...
};

Any objections/suggestions? The goal here is to remove the hand-written stuff that we have in m-c as part of bug the cleanup I'm doing in bug 1666802 and friends.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions