Flattenable.h revision 2497a1524dd909d0eb933544c94d2c2e9e2c3394
1a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian/* 2a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * Copyright (C) 2010 The Android Open Source Project 3a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * 4a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 5a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * you may not use this file except in compliance with the License. 6a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * You may obtain a copy of the License at 7a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * 8a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 9a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * 10a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * Unless required by applicable law or agreed to in writing, software 11a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 12a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * See the License for the specific language governing permissions and 14a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * limitations under the License. 15a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian */ 16a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 17a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#ifndef ANDROID_UTILS_FLATTENABLE_H 18a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#define ANDROID_UTILS_FLATTENABLE_H 19a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 20a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 21a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#include <stdint.h> 22a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#include <sys/types.h> 23a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#include <utils/Errors.h> 24a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 25a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopiannamespace android { 26a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 272497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian/* 282497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * The Flattenable interface allows an object to serialize itself out 292497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * to a byte-buffer and an array of file descriptors. 302497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian */ 312497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 32a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopianclass Flattenable 33a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian{ 34a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopianpublic: 35a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // size in bytes of the flattened object 36a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian virtual size_t getFlattenedSize() const = 0; 37a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 38a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // number of file descriptors to flatten 39a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian virtual size_t getFdCount() const = 0; 40a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 41a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // flattens the object into buffer. 42a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // size should be at least of getFlattenedSize() 43a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // file descriptors are written in the fds[] array but ownership is 44a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // not transfered (ie: they must be dupped by the caller of 45a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // flatten() if needed). 46a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian virtual status_t flatten(void* buffer, size_t size, 47a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian int fds[], size_t count) const = 0; 48a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 49a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // unflattens the object from buffer. 50a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // size should be equal to the value of getFlattenedSize() when the 51a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // object was flattened. 52a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // unflattened file descriptors are found in the fds[] array and 53a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // don't need to be dupped(). ie: the caller of unflatten doesn't 54a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // keep ownership. If a fd is not retained by unflatten() it must be 55a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // explicitly closed. 56a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian virtual status_t unflatten(void const* buffer, size_t size, 57a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian int fds[], size_t count) = 0; 58a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 59a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopianprotected: 60a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian virtual ~Flattenable() = 0; 61a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 62a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian}; 63a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 642497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian/* 652497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenable is a protocol allowing object to serialize themselves out 662497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * to a byte-buffer. 672497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * 682497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenable objects must implement this protocol. 692497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * 702497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenable doesn't require the object to be virtual. 712497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian */ 722497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 732497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianclass LightFlattenable { 742497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianpublic: 752497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // returns whether this object always flatten into the same size. 762497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // for efficiency, this should always be inline. 772497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline bool isFixedSize() const; 782497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 792497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // returns size in bytes of the flattened object. must be a constant. 802497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline size_t getSize() const; 812497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 822497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // flattens the object into buffer. 832497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline status_t flatten(void* buffer) const; 842497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 852497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // unflattens the object from buffer of given size. 862497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline status_t unflatten(void const* buffer, size_t size); 872497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian}; 882497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 892497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 902497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianinline bool LightFlattenable<T>::isFixedSize() const { 912497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return static_cast<T const*>(this)->T::isFixedSize(); 922497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian} 932497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 942497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianinline size_t LightFlattenable<T>::getSize() const { 952497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return static_cast<T const*>(this)->T::getSize(); 962497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian} 972497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 982497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianinline status_t LightFlattenable<T>::flatten(void* buffer) const { 992497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return static_cast<T const*>(this)->T::flatten(buffer); 1002497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian} 1012497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1022497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianinline status_t LightFlattenable<T>::unflatten(void const* buffer, size_t size) { 1032497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return static_cast<T*>(this)->T::unflatten(buffer, size); 1042497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian} 1052497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1062497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian/* 1072497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenablePod is an implementation of the LightFlattenable protocol 1082497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * for POD (plain-old-data) objects. 1092497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian */ 1102497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1112497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianclass LightFlattenablePod : public LightFlattenable<T> { 1122497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianpublic: 1132497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline bool isFixedSize() const { 1142497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return true; 1152497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian } 1162497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1172497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline size_t getSize() const { 1182497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return sizeof(T); 1192497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian } 1202497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline status_t flatten(void* buffer) const { 1212497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian *reinterpret_cast<T*>(buffer) = *static_cast<T const*>(this); 1222497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return NO_ERROR; 1232497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian } 1242497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline status_t unflatten(void const* buffer, size_t) { 1252497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian *static_cast<T*>(this) = *reinterpret_cast<T const*>(buffer); 1262497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return NO_ERROR; 1272497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian } 1282497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian}; 1292497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1302497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 131a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian}; // namespace android 132a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 133a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 134a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#endif /* ANDROID_UTILS_FLATTENABLE_H */ 135