Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9f106bec authored by David Pursell's avatar David Pursell Committed by android-build-merger
Browse files

Merge "Flattenable: switch from assignment to memcpy()." am: b2356a69

am: bb276d85

Change-Id: I2d7317f601dcfc85705f2e71408c314de55a0f69
parents ea2b61c7 bb276d85
Loading
Loading
Loading
Loading
+9 −2
Original line number Original line Diff line number Diff line
@@ -19,10 +19,13 @@




#include <stdint.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/Errors.h>
#include <utils/Debug.h>
#include <utils/Debug.h>


#include <type_traits>

namespace android {
namespace android {




@@ -60,14 +63,18 @@ public:
    // write a POD structure
    // write a POD structure
    template<typename T>
    template<typename T>
    static void write(void*& buffer, size_t& size, const T& value) {
    static void write(void*& buffer, size_t& size, const T& value) {
        *static_cast<T*>(buffer) = value;
        static_assert(std::is_trivially_copyable<T>::value,
                      "Cannot flatten a non-trivially-copyable type");
        memcpy(buffer, &value, sizeof(T));
        advance(buffer, size, sizeof(T));
        advance(buffer, size, sizeof(T));
    }
    }


    // read a POD structure
    // read a POD structure
    template<typename T>
    template<typename T>
    static void read(void const*& buffer, size_t& size, T& value) {
    static void read(void const*& buffer, size_t& size, T& value) {
        value = *static_cast<T const*>(buffer);
        static_assert(std::is_trivially_copyable<T>::value,
                      "Cannot unflatten a non-trivially-copyable type");
        memcpy(&value, buffer, sizeof(T));
        advance(buffer, size, sizeof(T));
        advance(buffer, size, sizeof(T));
    }
    }
};
};