Git commit 7ab2162e82e41b39063af430ad44d1f21ea437ac by Andreas Hartmetz. Committed on 17/11/2016 at 00:46. Pushed by ahartmetz into branch 'master'. Rework getting the current signature of a Reader. There are really two use cases, one of which needs a new method. Also reorder the arguments.h header for more consistent grouping, no functional change intended there. M +15 -4 serialization/arguments.cpp M +41 -40 serialization/arguments.h http://commits.kde.org/dferry/7ab2162e82e41b39063af430ad44d1f21ea437ac diff --git a/serialization/arguments.cpp b/serialization/arguments.cpp index 2919219..731f23b 100644 --- a/serialization/arguments.cpp +++ b/serialization/arguments.cpp @@ -949,10 +949,21 @@ bool Arguments::Reader::isInsideEmptyArray() const = cstring Arguments::Reader::currentSignature() const { - // ### not sure if determining length like that is the best way, shoul= d probably be as similar to - // libdbus-1 as possible (unless we can do much better). - return cstring(d->m_signature.ptr, - std::max(uint32(0), std::min(d->m_signature.length, d->= m_signaturePosition))); + return d->m_signature; +} + +cstring Arguments::Reader::currentSingleCompleteTypeSignature() const +{ + const uint32 startingLength =3D d->m_signature.length - d->m_signature= Position; + cstring sigCopy =3D { d->m_signature.ptr + d->m_signaturePosition, sta= rtingLength }; + Nesting nest; + if (!parseSingleCompleteType(&sigCopy, &nest)) { + // the signature should have been validated before, but e.g. in Fi= nished state this may happen + return cstring(); + } + sigCopy.ptr =3D d->m_signature.ptr + d->m_signaturePosition; + sigCopy.length =3D startingLength - sigCopy.length; + return sigCopy; } = void Arguments::Reader::replaceData(chunk data) diff --git a/serialization/arguments.h b/serialization/arguments.h index 4081e26..5d86866 100644 --- a/serialization/arguments.h +++ b/serialization/arguments.h @@ -37,53 +37,19 @@ class Message; class DFERRY_EXPORT Arguments { public: - Arguments(); // constructs an empty argument list - // constructs an argument list to deserialize data in @p data with sig= nature @p signature; - // if memOwnership is non-null, this means that signature and data's m= emory is contained in - // a malloc()ed block starting at memOwnership, and ~Arguments will fr= ee it. - // Otherwise, the instance assumes that @p signature and @p data live = in "borrowed" memory and - // you need to make sure that the memory lives as long as the Argument= s. - // (A notable user of this is Message - you can only get a const ref t= o its internal Arguments - // so you need to copy to take the Arguments away from the Message, w= hich copies out of the - // borrowed memory into heap memory so the copy is safe) - // The copy contructor and assignment operator will always copy the da= ta, so copying is safe - // regarding memory correctness but has a significant performance impa= ct. - Arguments(byte *memOwnership, cstring signature, chunk data, bool isBy= teSwapped =3D false); - - // use these wherever possible if you care at all about efficiency!! - Arguments(Arguments &&other); - Arguments &operator=3D(Arguments &&other); - - // copying needs special treatment due to the d-pointer - Arguments(const Arguments &other); - Arguments &operator=3D(const Arguments &other); - - ~Arguments(); - - // error (if any) propagates to Message, so it is still available later - Error error() const; - - std::string prettyPrint() const; - - cstring signature() const; - chunk data() const; - enum SignatureType { MethodSignature =3D 0, VariantSignature }; = - static bool isStringValid(cstring string); - static bool isObjectPathValid(cstring objectPath); - static bool isObjectPathElementValid(cstring pathElement); - static bool isSignatureValid(cstring signature, SignatureType type =3D= MethodSignature); - - enum { + enum + { MaxSignatureLength =3D 255 }; = - enum IoState { + enum IoState + { // "exceptional" states NotStarted =3D 0, Finished, @@ -124,8 +90,43 @@ public: UnixFd }; = -private: + Arguments(); // constructs an empty argument list + // constructs an argument list to deserialize data in @p data with sig= nature @p signature; + // if memOwnership is non-null, this means that signature and data's m= emory is contained in + // a malloc()ed block starting at memOwnership, and ~Arguments will fr= ee it. + // Otherwise, the instance assumes that @p signature and @p data live = in "borrowed" memory and + // you need to make sure that the memory lives as long as the Argument= s. + // (A notable user of this is Message - you can only get a const ref t= o its internal Arguments + // so you need to copy to take the Arguments away from the Message, w= hich copies out of the + // borrowed memory into heap memory so the copy is safe) + // The copy contructor and assignment operator will always copy the da= ta, so copying is safe + // regarding memory correctness but has a significant performance impa= ct. + Arguments(byte *memOwnership, cstring signature, chunk data, bool isBy= teSwapped =3D false); + + // use these wherever possible if you care at all about efficiency!! + Arguments(Arguments &&other); + Arguments &operator=3D(Arguments &&other); + + // copying needs special treatment due to the d-pointer + Arguments(const Arguments &other); + Arguments &operator=3D(const Arguments &other); + + ~Arguments(); + + // error (if any) propagates to Message, so it is still available later + Error error() const; = + std::string prettyPrint() const; + + cstring signature() const; + chunk data() const; + + static bool isStringValid(cstring string); + static bool isObjectPathValid(cstring objectPath); + static bool isObjectPathElementValid(cstring pathElement); + static bool isSignatureValid(cstring signature, SignatureType type =3D= MethodSignature); + +private: struct podCstring // Same as cstring but without ctor. // Can't put the cstring type into a union because i= t has a constructor :/ { @@ -148,7 +149,6 @@ private: } DataUnion; = public: - // error handling is done by asking state() or isError(), not by metho= d return values. // occasionally looking at isError() is less work than checking every = call. class DFERRY_EXPORT Reader @@ -171,6 +171,7 @@ public: cstring stateString() const; bool isInsideEmptyArray() const; cstring currentSignature() const; // current signature, either mai= n signature or current variant + cstring currentSingleCompleteTypeSignature() const; // HACK call this in NeedMoreData state when more data has been ad= ded; this replaces m_data // WARNING: calling replaceData() invalidates copies (if any) of t= his Reader void replaceData(chunk data);