From kde-commits Mon Nov 28 17:45:44 2016 From: Andreas Hartmetz Date: Mon, 28 Nov 2016 17:45:44 +0000 To: kde-commits Subject: [dferry] serialization: Rework handling of "state" in Arguments::Writer. Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=148035515507334 Git commit 95158b03b21bb0212a5fe83ff0f04b1777117987 by Andreas Hartmetz. Committed on 28/11/2016 at 17:42. Pushed by ahartmetz into branch 'master'. Rework handling of "state" in Arguments::Writer. It was very similar to Reader and that didn't make sense. For Writer, the user-visible state either says "you must write xyz next" or "you can write whatever you want next" or "something went wrong, this instance is hosed". It makes no sense to have state equal the last thing written, which happened most of the time. This part of the API is almost never needed, but at least it makes sense now. M +17 -20 serialization/arguments.cpp M +2 -2 serialization/arguments.h https://commits.kde.org/dferry/95158b03b21bb0212a5fe83ff0f04b1777117987 diff --git a/serialization/arguments.cpp b/serialization/arguments.cpp index 44d5033..2c9180e 100644 --- a/serialization/arguments.cpp +++ b/serialization/arguments.cpp @@ -2031,13 +2031,13 @@ cstring Arguments::Writer::currentSignature() const std::max(uint32(0), std::min(d->m_signature.length, d->= m_signaturePosition))); } = -void Arguments::Writer::doWritePrimitiveType(uint32 alignAndSize) +void Arguments::Writer::doWritePrimitiveType(IoState type, uint32 alignAnd= Size) { d->m_dataPosition =3D align(d->m_dataPosition, alignAndSize); const uint32 newDataPosition =3D d->m_dataPosition + alignAndSize; d->reserveData(newDataPosition); = - switch(m_state) { + switch(type) { case Boolean: { uint32 num =3D m_u.Boolean ? 1 : 0; basic::writeUint32(d->m_data + d->m_dataPosition, num); @@ -2079,15 +2079,15 @@ void Arguments::Writer::doWritePrimitiveType(uint32= alignAndSize) d->m_elements.push_back(Private::ElementInfo(alignAndSize, alignAndSiz= e)); } = -void Arguments::Writer::doWriteString(uint32 lengthPrefixSize) +void Arguments::Writer::doWriteString(IoState type, uint32 lengthPrefixSiz= e) { - if (m_state =3D=3D String) { + if (type =3D=3D String) { VALID_IF(Arguments::isStringValid(cstring(m_u.String.ptr, m_u.Stri= ng.length)), Error::InvalidString); - } else if (m_state =3D=3D ObjectPath) { + } else if (type =3D=3D ObjectPath) { VALID_IF(Arguments::isObjectPathValid(cstring(m_u.String.ptr, m_u.= String.length)), Error::InvalidObjectPath); - } else if (m_state =3D=3D Signature) { + } else if (type =3D=3D Signature) { VALID_IF(Arguments::isSignatureValid(cstring(m_u.String.ptr, m_u.S= tring.length)), Error::InvalidSignature); } @@ -2137,7 +2137,7 @@ void Arguments::Writer::advanceState(cstring signatur= eFragment, IoState newState // assert(d->m_nesting.total() =3D=3D d->m_aggregateStack.size()); assert((d->m_nesting.total() =3D=3D 0) =3D=3D d->m_aggregateStack.empt= y()); = - m_state =3D newState; + m_state =3D AnyData; uint32 alignment =3D 1; bool isPrimitiveType =3D false; bool isStringType =3D false; @@ -2162,12 +2162,12 @@ void Arguments::Writer::advanceState(cstring signat= ureFragment, IoState newState // arrays and variants may contain just one single complet= e type; note that this will // trigger only when not inside an aggregate inside the va= riant or (see below) array if (d->m_signaturePosition >=3D 1) { - VALID_IF(m_state =3D=3D EndVariant, Error::NotSingleCo= mpleteTypeInVariant); + VALID_IF(newState =3D=3D EndVariant, Error::NotSingleC= ompleteTypeInVariant); } break; case BeginArray: if (d->m_signaturePosition >=3D aggregateInfo.arr.containe= dTypeBegin + 1 - && m_state !=3D EndArray) { + && newState !=3D EndArray) { // we are not at start of contained type's signature, = the array is at top of stack // -> we are at the end of the single complete type in= side the array, start the next // entry. TODO: check compatibility (essentially what'= s in the else branch below) @@ -2182,7 +2182,7 @@ void Arguments::Writer::advanceState(cstring signatur= eFragment, IoState newState // first type has been checked already, second must be pre= sent (checked in EndDict // state handler). no third type allowed. if (d->m_signaturePosition >=3D aggregateInfo.arr.containe= dTypeBegin + 2 - && m_state !=3D EndDict) { + && newState !=3D EndDict) { // Start the next dict entry d->m_nesting.parenCount +=3D 1; // align to dict entry @@ -2227,21 +2227,21 @@ void Arguments::Writer::advanceState(cstring signat= ureFragment, IoState newState } = if (isPrimitiveType) { - doWritePrimitiveType(alignment); + doWritePrimitiveType(newState, alignment); return; } if (isStringType) { // In case of nil array, skip writing to make sure that the input = string (which is explicitly // allowed to be garbage) is not validated and no wild pointer is = dereferenced. if (likely(!d->m_nilArrayNesting)) { - doWriteString(alignment); + doWriteString(newState, alignment); } return; } = Private::AggregateInfo aggregateInfo; = - switch (m_state) { + switch (newState) { case BeginStruct: VALID_IF(d->m_nesting.beginParen(), Error::ExcessiveNesting); aggregateInfo.aggregateType =3D BeginStruct; @@ -2310,26 +2310,25 @@ void Arguments::Writer::advanceState(cstring signat= ureFragment, IoState newState case BeginDict: case BeginArray: { VALID_IF(d->m_nesting.beginArray(), Error::ExcessiveNesting); - if (m_state =3D=3D BeginDict) { + if (newState =3D=3D BeginDict) { // not re-opened before each element: there is no observable d= ifference for clients VALID_IF(d->m_nesting.beginParen(), Error::ExcessiveNesting); } - aggregateInfo.aggregateType =3D m_state; + aggregateInfo.aggregateType =3D newState; aggregateInfo.arr.containedTypeBegin =3D d->m_signaturePosition; d->m_aggregateStack.reserve(8); d->m_aggregateStack.push_back(aggregateInfo); = d->m_elements.push_back(Private::ElementInfo(4, Private::ElementIn= fo::ArrayLengthField)); - if (m_state =3D=3D BeginDict) { + if (newState =3D=3D BeginDict) { d->m_elements.push_back(Private::ElementInfo(structAlignment, = 0)); // align to dict entry m_state =3D DictKey; - return; } break; } = case EndDict: case EndArray: { - const bool isDict =3D m_state =3D=3D EndDict; + const bool isDict =3D newState =3D=3D EndDict; if (isDict) { d->m_nesting.endParen(); } @@ -2357,8 +2356,6 @@ void Arguments::Writer::advanceState(cstring signatur= eFragment, IoState newState VALID_IF(false, Error::InvalidType); break; } - - m_state =3D AnyData; } = void Arguments::Writer::beginArrayOrDict(IoState beginWhat, ArrayOption op= tion) diff --git a/serialization/arguments.h b/serialization/arguments.h index a4b28c0..85d3f09 100644 --- a/serialization/arguments.h +++ b/serialization/arguments.h @@ -346,8 +346,8 @@ public: friend class Private; = private: - void doWritePrimitiveType(uint32 alignAndSize); - void doWriteString(uint32 lengthPrefixSize); + void doWritePrimitiveType(IoState type, uint32 alignAndSize); + void doWriteString(IoState type, uint32 lengthPrefixSize); void advanceState(cstring signatureFragment, IoState newState); void beginArrayOrDict(IoState beginWhat, ArrayOption option); void finishInternal();