simple_parcelable.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "tests/simple_parcelable.h"
  17. #include <android-base/stringprintf.h>
  18. #include <binder/Parcel.h>
  19. #include <utils/String8.h>
  20. using android::base::StringPrintf;
  21. namespace android {
  22. namespace aidl {
  23. namespace tests {
  24. SimpleParcelable::SimpleParcelable(const std::string& name, int32_t number)
  25. : name_(name.c_str(), name.length()),
  26. number_(number) {}
  27. status_t SimpleParcelable::writeToParcel(Parcel* parcel) const {
  28. status_t status = parcel->writeString16(name_);
  29. if (status != OK) {
  30. return status;
  31. }
  32. status = parcel->writeInt32(number_);
  33. return status;
  34. }
  35. status_t SimpleParcelable::readFromParcel(const Parcel* parcel) {
  36. status_t status = parcel->readString16(&name_);
  37. if (status != OK) {
  38. return status;
  39. }
  40. return parcel->readInt32(&number_);
  41. }
  42. std::string SimpleParcelable::toString() const {
  43. return StringPrintf("%s(%d)", String8(name_).string(), number_);
  44. }
  45. } // namespace tests
  46. } // namespace aidl
  47. } // namespace android