diff options
author | Noel Grandin <noelgrandin@gmail.com> | 2020-06-18 21:39:30 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2020-06-20 21:25:11 +0200 |
commit | cb95276e6e6bf12a1c06d5c252551e55c788fcb2 (patch) | |
tree | 9e54d9ddbd03f02f508af48ce8dce02d5c83a277 /include | |
parent | 292d9519bd368db69920cf0f8b94e0e51c3d14a1 (diff) |
use JsonWriter for the rest of ITiledRenderable
and fix bug in buffer reallacotion where mPos pointing
at the beginning of the new buffer instead of at the
correct index inside it.
Change-Id: Ie1ffaa176f6165e2cec85c93adc945312eff38e4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/96650
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'include')
-rw-r--r-- | include/tools/json_writer.hxx | 65 | ||||
-rw-r--r-- | include/vcl/ITiledRenderable.hxx | 18 |
2 files changed, 63 insertions, 20 deletions
diff --git a/include/tools/json_writer.hxx b/include/tools/json_writer.hxx index c0312f6c581d..c15c4b7401da 100644 --- a/include/tools/json_writer.hxx +++ b/include/tools/json_writer.hxx @@ -12,6 +12,11 @@ #include <rtl/ustring.hxx> #include <algorithm> +namespace rtl +{ +class OStringBuffer; +} + /** Simple JSON encoder designed specifically for LibreOfficeKit purposes. * * (1) Minimal allocations/re-allocations/copying @@ -21,10 +26,14 @@ namespace tools { class ScopedJsonWriterNode; +class ScopedJsonWriterArray; +class ScopedJsonWriterStruct; class TOOLS_DLLPUBLIC JsonWriter { friend class ScopedJsonWriterNode; + friend class ScopedJsonWriterArray; + friend class ScopedJsonWriterStruct; int mSpaceAllocated; char* mpBuffer; @@ -37,32 +46,36 @@ public: ~JsonWriter(); [[nodiscard]] ScopedJsonWriterNode startNode(const char*); + [[nodiscard]] ScopedJsonWriterArray startArray(const char*); + [[nodiscard]] ScopedJsonWriterStruct startStruct(); void put(const char* pPropName, const OUString& rPropValue); void put(const char* pPropName, const OString& rPropValue); void put(const char* pPropName, const char* pPropVal); void put(const char*, int); + /// This assumes that this data belongs at this point in the stream, and is valid, and properly encoded + void putRaw(const rtl::OStringBuffer&); + /** Hands ownership of the underlying storage buffer to the caller, * after this no more document modifications may be written. */ char* extractData(); + OString extractAsOString(); private: void endNode(); + void endArray(); + void endStruct(); void addCommaBeforeField(); + void reallocBuffer(int noMoreBytesRequired); + // this part inline to speed up the fast path inline void ensureSpace(int noMoreBytesRequired) { + assert(mpBuffer && "already extracted data"); int currentUsed = mPos - mpBuffer; if (currentUsed + noMoreBytesRequired >= mSpaceAllocated) - { - auto newSize = std::max(mSpaceAllocated * 2, (currentUsed + noMoreBytesRequired) * 2); - char* pNew = static_cast<char*>(malloc(newSize)); - memcpy(pNew, mpBuffer, currentUsed); - free(mpBuffer); - mpBuffer = pNew; - mPos = mpBuffer; - } + reallocBuffer(noMoreBytesRequired); } }; @@ -83,5 +96,41 @@ class ScopedJsonWriterNode public: ~ScopedJsonWriterNode() { mrWriter.endNode(); } }; + +/** + * Auto-closes the node. + */ +class ScopedJsonWriterArray +{ + friend class JsonWriter; + + JsonWriter& mrWriter; + + ScopedJsonWriterArray(JsonWriter& rWriter) + : mrWriter(rWriter) + { + } + +public: + ~ScopedJsonWriterArray() { mrWriter.endArray(); } +}; + +/** + * Auto-closes the node. + */ +class ScopedJsonWriterStruct +{ + friend class JsonWriter; + + JsonWriter& mrWriter; + + ScopedJsonWriterStruct(JsonWriter& rWriter) + : mrWriter(rWriter) + { + } + +public: + ~ScopedJsonWriterStruct() { mrWriter.endStruct(); } +}; }; /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/vcl/ITiledRenderable.hxx b/include/vcl/ITiledRenderable.hxx index e290c4960368..7b841844874d 100644 --- a/include/vcl/ITiledRenderable.hxx +++ b/include/vcl/ITiledRenderable.hxx @@ -173,9 +173,8 @@ public: * @param rRectangle - if not empty, then limit the output only to the area of this rectangle * @return a JSON describing position/content of rows/columns */ - virtual OUString getRowColumnHeaders(const tools::Rectangle& /*rRectangle*/) + virtual void getRowColumnHeaders(const tools::Rectangle& /*rRectangle*/, tools::JsonWriter& /*rJsonWriter*/) { - return OUString(); } /** @@ -183,9 +182,8 @@ public: * current' views' co-ordinate system. * (This could maybe also be used for tables in Writer/Impress in future?) */ - virtual OString getCellCursor() + virtual void getCellCursor(tools::JsonWriter& /*rJsonWriter*/) { - return OString(); } virtual PointerStyle getPointer() = 0; @@ -237,30 +235,26 @@ public: /// Implementation for /// lok::Document::getCommandValues(".uno:TrackedChangeAuthors"). - virtual OUString getTrackedChangeAuthors() + virtual void getTrackedChangeAuthors(tools::JsonWriter& /*rJsonWriter*/) { - return OUString(); } /// Implementation for /// lok::Document::getCommandValues(".uno:ViewAnnotations"); - virtual OUString getPostIts() + virtual void getPostIts(tools::JsonWriter& /*rJsonWriter*/) { - return OUString(); } /// Implementation for /// lok::Document::getCommandValues(".uno:ViewAnnotationsPosition"); - virtual OUString getPostItsPos() + virtual void getPostItsPos(tools::JsonWriter& /*rJsonWriter*/) { - return OUString(); } /// Implementation for /// lok::Document::getCommandValues(".uno:RulerState"); - virtual OUString getRulerState() + virtual void getRulerState(tools::JsonWriter& /*rJsonWriter*/) { - return OUString(); } /* |