# typehandlers.codesink: classes that receive generated source code

Objects that receive generated C/C++ code lines, reindents them, and writes them to a file, memory, or another code sink object.

class pybindgen.typehandlers.codesink.CodeSink

  Bases: `object`

  Abstract base class for code sinks

  Constructor

      >>> sink = MemoryCodeSink()
      >>> sink.writeln("foo();")
      >>> sink.writeln("if (true) {")
      >>> sink.indent()
      >>> sink.writeln("bar();")
      >>> sink.unindent()
      >>> sink.writeln("zbr();")
      >>> print sink.flush().rstrip()
      foo();
      if (true) {
          bar();
      zbr();

      >>> sink = MemoryCodeSink()
      >>> sink.writeln("foo();")
      >>> sink.writeln()
      >>> sink.writeln("bar();")
      >>> print len(sink.flush().split("\n"))
      4

  indent(level=4)

    Add a certain ammount of indentation to all lines written from now on and until unindent() is called

  unindent()

    Revert indentation level to the value before last indent() call

  writeln(line='')

    Write one or more lines of code

class pybindgen.typehandlers.codesink.FileCodeSink(file_)

  Bases: `pybindgen.typehandlers.codesink.CodeSink`

  A code sink that writes to a file-like object

  Parameters

    file – a file like object

  writeln(line='')

    Write one or more lines of code

class pybindgen.typehandlers.codesink.MemoryCodeSink

  Bases: `pybindgen.typehandlers.codesink.CodeSink`

  A code sink that keeps the code in memory, and can later flush the code to another code sink

  Constructor

  flush()

    Flushes the code and returns the formatted output as a return value string

  flush_to(sink)

    Flushes code to another code sink :param sink: another CodeSink instance

  writeln(line='')

    Write one or more lines of code

class pybindgen.typehandlers.codesink.NullCodeSink

  Bases: `pybindgen.typehandlers.codesink.CodeSink`

  A code sink that discards all content. Useful to ‘test’ if code generation would work without actually generating anything.

  Constructor

  flush()

    Flushes the code and returns the formatted output as a return value string

  flush_to(sink)

    Flushes code to another code sink :param sink: another CodeSink instance

  writeln(line='')

    Write one or more lines of code