Add C++ convenience methods

This commit is contained in:
Nigel Tao committed 2018-06-30 13:02:16 +10:00
1 parent 60a7b48745
commit 30cfaa5db7
20 files changed
+1444 -229

No files matched your search

+38 -13
View File
@@ -651,23 +651,13 @@ static inline uint32_t wuffs_base__rect_ie_u32__height(
// ---------------- I/O
// wuffs_base__io_buffer is a 1-dimensional buffer (a pointer and length), plus
// additional indexes into that buffer, plus an opened / closed flag.
//
// A value with all fields NULL or zero is a valid, empty buffer.
typedef struct {
uint8_t* ptr; // Pointer.
size_t len; // Length.
size_t wi; // Write index. Invariant: wi <= len.
size_t ri; // Read index. Invariant: ri <= wi.
bool closed; // No further writes are expected.
} wuffs_base__io_buffer;
struct wuffs_base__io_buffer__struct;
typedef struct {
// Do not access the private_impl's fields directly. There is no API/ABI
// compatibility or safety guarantee if you do so.
struct {
wuffs_base__io_buffer* buf;
struct wuffs_base__io_buffer__struct* buf;
// The bounds values are typically NULL, when created by the Wuffs public
// API. NULL means that the callee substitutes the implicit bounds derived
// from buf.
@@ -679,7 +669,7 @@ typedef struct {
// Do not access the private_impl's fields directly. There is no API/ABI
// compatibility or safety guarantee if you do so.
struct {
wuffs_base__io_buffer* buf;
struct wuffs_base__io_buffer__struct* buf;
// The bounds values are typically NULL, when created by the Wuffs public
// API. NULL means that the callee substitutes the implicit bounds derived
// from buf.
@@ -687,6 +677,25 @@ typedef struct {
} private_impl;
} wuffs_base__io_writer;
// wuffs_base__io_buffer is a 1-dimensional buffer (a pointer and length), plus
// additional indexes into that buffer, plus an opened / closed flag.
//
// A value with all fields NULL or zero is a valid, empty buffer.
typedef struct wuffs_base__io_buffer__struct {
uint8_t* ptr; // Pointer.
size_t len; // Length.
size_t wi; // Write index. Invariant: wi <= len.
size_t ri; // Read index. Invariant: ri <= wi.
bool closed; // No further writes are expected.
#ifdef __cplusplus
inline void compact();
inline wuffs_base__io_reader reader();
inline wuffs_base__io_writer writer();
#endif // __cplusplus
} wuffs_base__io_buffer;
// wuffs_base__io_buffer__compact moves any written but unread bytes to the
// start of the buffer.
static inline void wuffs_base__io_buffer__compact(wuffs_base__io_buffer* buf) {
@@ -715,6 +724,22 @@ static inline wuffs_base__io_writer wuffs_base__io_buffer__writer(
return ret;
}
#ifdef __cplusplus
inline void wuffs_base__io_buffer__struct::compact() {
wuffs_base__io_buffer__compact(this);
}
inline wuffs_base__io_reader wuffs_base__io_buffer__struct::reader() {
return wuffs_base__io_buffer__reader(this);
}
inline wuffs_base__io_writer wuffs_base__io_buffer__struct::writer() {
return wuffs_base__io_buffer__writer(this);
}
#endif // __cplusplus
// ---------------- Images
// wuffs_base__color_u32argb is an 8 bit per channel Alpha, Red, Green, Blue
+93 -1
View File
@@ -366,6 +366,11 @@ func (g *gen) genHeader(b *buffer) error {
return err
}
b.writes("// ---------------- C++ Convenience Methods \n\n")
if err := g.writeCppImpls(b); err != nil {
return err
}
b.writes("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n")
b.printf("#endif // %s\n\n", includeGuard)
return nil
@@ -676,8 +681,95 @@ func (g *gen) writeStruct(b *buffer, n *a.Struct) error {
}
}
}
b.writes("} private_impl;\n\n")
b.printf("} private_impl;\n } %s%s;\n\n", g.pkgPrefix, structName)
if n.Node().Raw().Flags()&a.FlagsPublic != 0 {
if err := g.writeCppPrototypes(b, n); err != nil {
return err
}
}
b.printf("} %s%s;\n\n", g.pkgPrefix, structName)
return nil
}
func (g *gen) writeCppPrototypes(b *buffer, n *a.Struct) error {
b.writes("#ifdef __cplusplus\n")
b.writes("inline void check_wuffs_version(size_t sizeof_star_self, uint64_t wuffs_version);\n")
structID := n.QID()[1]
for _, file := range g.files {
for _, tld := range file.TopLevelDecls() {
if (tld.Kind() != a.KFunc) || (tld.Raw().Flags()&a.FlagsPublic == 0) {
continue
}
f := tld.Func()
qqid := f.QQID()
if qqid[1] != structID {
continue
}
if err := g.writeFuncSignature(b, f, cppInsideStruct); err != nil {
return err
}
b.writes(";\n")
}
}
b.writes("#endif // __cplusplus\n\n")
return nil
}
func (g *gen) writeCppImpls(b *buffer) error {
b.writes("\n#ifdef __cplusplus\n\n")
publicStructs := map[t.ID]bool{}
for _, file := range g.files {
for _, tld := range file.TopLevelDecls() {
if (tld.Kind() != a.KStruct) || (tld.Raw().Flags()&a.FlagsPublic == 0) {
continue
}
n := tld.Struct()
structID := n.QID()[1]
structName := structID.Str(g.tm)
b.printf("inline void %s%s::check_wuffs_version(size_t sizeof_star_self, uint64_t wuffs_version) {\n",
g.pkgPrefix, structName)
b.printf("%s%s__check_wuffs_version(this, sizeof_star_self, wuffs_version);\n",
g.pkgPrefix, structName)
b.printf("}\n\n")
publicStructs[structID] = true
}
}
for _, file := range g.files {
for _, tld := range file.TopLevelDecls() {
if (tld.Kind() != a.KFunc) || (tld.Raw().Flags()&a.FlagsPublic == 0) {
continue
}
n := tld.Func()
if !publicStructs[n.QQID()[1]] {
continue
}
if err := g.writeFuncSignature(b, n, cppOutsideStruct); err != nil {
return err
}
b.writes("{ return ")
b.writes(g.funcCName(n))
b.writes("(this")
for _, o := range n.In().Fields() {
b.writeb(',')
b.writes(aPrefix)
b.writes(o.Field().Name().Str(g.tm))
}
b.writes(");}\n\n")
}
}
b.writes("#endif // __cplusplus\n\n")
return nil
}
+3 -3
View File
@@ -58,9 +58,9 @@ const basePublicH = "" +
"& r.min_incl_y == s.min_incl_y &&\n r.max_excl_x == s.max_excl_x && r.max_excl_y == s.max_excl_y) ||\n (wuffs_base__rect_ie_u32__is_empty(r) &&\n wuffs_base__rect_ie_u32__is_empty(s));\n}\n\nstatic inline bool wuffs_base__rect_ie_u32__contains(wuffs_base__rect_ie_u32 r,\n uint32_t x,\n uint32_t y) {\n return (r.min_incl_x <= x) && (x < r.max_excl_x) && (r.min_incl_y <= y) &&\n (y < r.max_excl_y);\n}\n\nstatic inline wuffs_base__rect_ie_u32 wuffs_base__rect_ie_u32__intersection(\n wuffs_base__rect_ie_u32 r,\n wuffs_base__rect_ie_u32 s) {\n r.min_incl_x = wuffs_base__u32__max(r.min_incl_x, s.min_incl_x);\n r.min_incl_y = wuffs_base__u32__max(r.min_incl_y, s.min_incl_y);\n r.max_excl_x = wuffs_base__u32__min(r.max_excl_x, s.max_excl_x);\n r.max_excl_y = wuffs_base__u32__min(r.max_excl_y, s.max_excl_y);\n return r;\n}\n\nstatic inline wuffs_base__rect_ie_u32 wuffs_base__rect_ie_u32__union" +
"(\n wuffs_base__rect_ie_u32 r,\n wuffs_base__rect_ie_u32 s) {\n if (wuffs_base__rect_ie_u32__is_empty(r)) {\n return s;\n }\n if (wuffs_base__rect_ie_u32__is_empty(s)) {\n return r;\n }\n r.min_incl_x = wuffs_base__u32__min(r.min_incl_x, s.min_incl_x);\n r.min_incl_y = wuffs_base__u32__min(r.min_incl_y, s.min_incl_y);\n r.max_excl_x = wuffs_base__u32__max(r.max_excl_x, s.max_excl_x);\n r.max_excl_y = wuffs_base__u32__max(r.max_excl_y, s.max_excl_y);\n return r;\n}\n\nstatic inline uint32_t wuffs_base__rect_ie_u32__width(\n wuffs_base__rect_ie_u32 r) {\n return wuffs_base__u32__sat_sub(r.max_excl_x, r.min_incl_x);\n}\n\nstatic inline uint32_t wuffs_base__rect_ie_u32__height(\n wuffs_base__rect_ie_u32 r) {\n return wuffs_base__u32__sat_sub(r.max_excl_y, r.min_incl_y);\n}\n\n" +
"" +
"// ---------------- I/O\n\n// wuffs_base__io_buffer is a 1-dimensional buffer (a pointer and length), plus\n// additional indexes into that buffer, plus an opened / closed flag.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n bool closed; // No further writes are expected.\n} wuffs_base__io_buffer;\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__io_buffer* buf;\n // The bounds values are typically NULL, when created by the Wuffs public\n // API. NULL means that the callee substitutes the implicit bounds derived\n // from buf.\n uint8_t* bounds[2];\n } private_impl;\n} wuffs_base__io_reader;\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/A" +
"BI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__io_buffer* buf;\n // The bounds values are typically NULL, when created by the Wuffs public\n // API. NULL means that the callee substitutes the implicit bounds derived\n // from buf.\n uint8_t* bounds[2];\n } private_impl;\n} wuffs_base__io_writer;\n\n// wuffs_base__io_buffer__compact moves any written but unread bytes to the\n// start of the buffer.\nstatic inline void wuffs_base__io_buffer__compact(wuffs_base__io_buffer* buf) {\n if (!buf || (buf->ri == 0)) {\n return;\n }\n size_t n = buf->wi - buf->ri;\n if (n != 0) {\n memmove(buf->ptr, buf->ptr + buf->ri, n);\n }\n buf->wi = n;\n buf->ri = 0;\n}\n\nstatic inline wuffs_base__io_reader wuffs_base__io_buffer__reader(\n wuffs_base__io_buffer* buf) {\n wuffs_base__io_reader ret = ((wuffs_base__io_reader){});\n ret.private_impl.buf = buf;\n return ret;\n}\n\nstatic inline wuffs_base__io_writer wuffs_base__io_buffer__writer(\n wuffs_base__io_buffer* buf) {\n wuffs_base__io" +
"_writer ret = ((wuffs_base__io_writer){});\n ret.private_impl.buf = buf;\n return ret;\n}\n\n" +
"// ---------------- I/O\n\nstruct wuffs_base__io_buffer__struct;\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n struct wuffs_base__io_buffer__struct* buf;\n // The bounds values are typically NULL, when created by the Wuffs public\n // API. NULL means that the callee substitutes the implicit bounds derived\n // from buf.\n uint8_t* bounds[2];\n } private_impl;\n} wuffs_base__io_reader;\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n struct wuffs_base__io_buffer__struct* buf;\n // The bounds values are typically NULL, when created by the Wuffs public\n // API. NULL means that the callee substitutes the implicit bounds derived\n // from buf.\n uint8_t* bounds[2];\n } private_impl;\n} wuffs_base__io_writer;\n\n// wuffs_base__io_buffer is a 1-dimensional buffer (a pointer and leng" +
"th), plus\n// additional indexes into that buffer, plus an opened / closed flag.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct wuffs_base__io_buffer__struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n bool closed; // No further writes are expected.\n\n#ifdef __cplusplus\n inline void compact();\n inline wuffs_base__io_reader reader();\n inline wuffs_base__io_writer writer();\n#endif // __cplusplus\n\n} wuffs_base__io_buffer;\n\n// wuffs_base__io_buffer__compact moves any written but unread bytes to the\n// start of the buffer.\nstatic inline void wuffs_base__io_buffer__compact(wuffs_base__io_buffer* buf) {\n if (!buf || (buf->ri == 0)) {\n return;\n }\n size_t n = buf->wi - buf->ri;\n if (n != 0) {\n memmove(buf->ptr, buf->ptr + buf->ri, n);\n }\n buf->wi = n;\n buf->ri = 0;\n}\n\nstatic inline wuffs_base__io_reader wuffs_base__io_buffer__reader(\n wuf" +
"fs_base__io_buffer* buf) {\n wuffs_base__io_reader ret = ((wuffs_base__io_reader){});\n ret.private_impl.buf = buf;\n return ret;\n}\n\nstatic inline wuffs_base__io_writer wuffs_base__io_buffer__writer(\n wuffs_base__io_buffer* buf) {\n wuffs_base__io_writer ret = ((wuffs_base__io_writer){});\n ret.private_impl.buf = buf;\n return ret;\n}\n\n#ifdef __cplusplus\n\ninline void wuffs_base__io_buffer__struct::compact() {\n wuffs_base__io_buffer__compact(this);\n}\n\ninline wuffs_base__io_reader wuffs_base__io_buffer__struct::reader() {\n return wuffs_base__io_buffer__reader(this);\n}\n\ninline wuffs_base__io_writer wuffs_base__io_buffer__struct::writer() {\n return wuffs_base__io_buffer__writer(this);\n}\n\n#endif // __cplusplus\n\n" +
"" +
"// ---------------- Images\n\n// wuffs_base__color_u32argb is an 8 bit per channel Alpha, Red, Green, Blue\n// color, as a uint32_t value. It is in word order, not byte order: its value\n// is always 0xAARRGGBB, regardless of endianness. It uses premultiplied alpha.\ntypedef uint32_t wuffs_base__color_u32argb;\n\n" +
"" +
+33 -11
View File
@@ -69,8 +69,17 @@ func (g *gen) funcCName(n *a.Func) string {
return g.pkgPrefix + n.FuncName().Str(g.tm)
}
func (g *gen) writeFuncSignature(b *buffer, n *a.Func) error {
if !n.Public() {
// C++ related function signature constants.
const (
cppNone = 0 // Not C++, just plain C.
cppInsideStruct = 1
cppOutsideStruct = 2
)
func (g *gen) writeFuncSignature(b *buffer, n *a.Func, cpp uint32) error {
if cpp != cppNone {
b.writes("inline ")
} else if !n.Public() {
b.writes("static ")
}
@@ -88,14 +97,27 @@ func (g *gen) writeFuncSignature(b *buffer, n *a.Func) error {
return fmt.Errorf("TODO: multiple return values")
}
b.writes(g.funcCName(n))
b.writeb('(')
comma := false
if r := n.Receiver(); !r.IsZero() {
b.printf("%s%s *self", g.pkgPrefix, r.Str(g.tm))
comma = true
switch cpp {
case cppNone:
b.writes(g.funcCName(n))
case cppInsideStruct:
b.writes(n.FuncName().Str(g.tm))
case cppOutsideStruct:
b.writes(g.pkgPrefix)
b.writes(n.Receiver().Str(g.tm))
b.writes("::")
b.writes(n.FuncName().Str(g.tm))
}
b.writeb('(')
comma := false
if cpp == cppNone {
if r := n.Receiver(); !r.IsZero() {
b.printf("%s%s *self", g.pkgPrefix, r.Str(g.tm))
comma = true
}
}
for _, o := range n.In().Fields() {
if comma {
b.writeb(',')
@@ -112,7 +134,7 @@ func (g *gen) writeFuncSignature(b *buffer, n *a.Func) error {
}
func (g *gen) writeFuncPrototype(b *buffer, n *a.Func) error {
if err := g.writeFuncSignature(b, n); err != nil {
if err := g.writeFuncSignature(b, n, cppNone); err != nil {
return err
}
b.writes(";\n\n")
@@ -123,7 +145,7 @@ func (g *gen) writeFuncImpl(b *buffer, n *a.Func) error {
k := g.funks[n.QQID()]
b.printf("// -------- func %s.%s\n\n", g.pkgName, n.QQID().Str(g.tm))
if err := g.writeFuncSignature(b, n); err != nil {
if err := g.writeFuncSignature(b, n, cppNone); err != nil {
return err
}
b.writes("{\n")