Generates Fleece-encoded data. */
| 27 | |
| 28 | /** Generates Fleece-encoded data. */ |
| 29 | class Encoder { |
| 30 | public: |
| 31 | /** Constructs an encoder. */ |
| 32 | Encoder(size_t reserveOutputSize =256); |
| 33 | Encoder(FILE* NONNULL); |
| 34 | ~Encoder(); |
| 35 | |
| 36 | /** Sets the uniqueStrings property. If true (the default), the encoder tries to write |
| 37 | each unique string only once. This saves space but makes the encoder slightly slower. */ |
| 38 | void uniqueStrings(bool b) {_uniqueStrings = b;} |
| 39 | |
| 40 | /** Sets the base Fleece data that the encoded data will be (logically) appended to. |
| 41 | Any writeValue() calls whose Value points into the base data will be written as |
| 42 | pointers. |
| 43 | @param base The base Fleece document that's being appended to. |
| 44 | @param markExternPointers If true, pointers into the base document (i.e. out of the |
| 45 | encoded data) will be marked with the `extern` flag. The resulting Fleece |
| 46 | document must then be opened as a Doc using the `externData` property |
| 47 | pointing to wherever a copy of the base document is. |
| 48 | @param cutoff If nonzero, this specifies the maximum number of bytes of the base |
| 49 | (starting from the end) that should be used. Any base data before |
| 50 | the cutoff will not be referenced in the encoder output. */ |
| 51 | void setBase(slice base, bool markExternPointers =false, size_t cutoff =0); |
| 52 | |
| 53 | /** Scans the base document for strings and adds them to the encoder's string table. |
| 54 | If equivalent strings are written to the encoder they will then be encoded as pointers |
| 55 | to the existing strings. */ |
| 56 | void reuseBaseStrings(); |
| 57 | |
| 58 | bool valueIsInBase(const Value *value) const; |
| 59 | |
| 60 | bool isEmpty() const {return _out.length() == 0 && _stackDepth == 1 && _items->empty();} |
| 61 | size_t bytesWritten() const {return _out.length();} // may be an underestimate |
| 62 | |
| 63 | /** Ends encoding, writing the last of the data to the Writer. */ |
| 64 | void end(); |
| 65 | |
| 66 | /** Returns the encoded data. This implicitly calls end(). */ |
| 67 | alloc_slice finish(); |
| 68 | |
| 69 | /** Returns the encoded data as a Doc. This implicitly calls end(). */ |
| 70 | Retained<Doc> finishDoc(); |
| 71 | |
| 72 | /** Resets the encoder so it can be used again. */ |
| 73 | void reset(); |
| 74 | |
| 75 | /////// Writing data: |
| 76 | |
| 77 | void writeNull(); |
| 78 | void writeUndefined(); |
| 79 | void writeBool(bool); |
| 80 | |
| 81 | void writeInt(int64_t i); |
| 82 | void writeUInt(uint64_t i); |
| 83 | void writeFloat(float); |
| 84 | void writeDouble(double); |
| 85 | |
| 86 | void writeString(slice s) {(void)_writeString(s);} |
nothing calls this directly
no outgoing calls
no test coverage detected