1/*
2 * Copyright (C) 2018 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
17#define LOG_TAG "StatsDimensionsValue"
18
19#include "android/os/StatsDimensionsValue.h"
20
21#include <cutils/log.h>
22
23using android::Parcel;
24using android::Parcelable;
25using android::status_t;
26using std::vector;
27
28namespace android {
29namespace os {
30
31StatsDimensionsValue::StatsDimensionsValue() {};
32
33StatsDimensionsValue::StatsDimensionsValue(int32_t field, String16 value) :
34    mField(field),
35    mValueType(kStrValueType),
36    mStrValue(value) {
37}
38StatsDimensionsValue::StatsDimensionsValue(int32_t field, int32_t value) :
39    mField(field),
40    mValueType(kIntValueType),
41    mIntValue(value) {
42}
43StatsDimensionsValue::StatsDimensionsValue(int32_t field, int64_t value) :
44    mField(field),
45    mValueType(kLongValueType),
46    mLongValue(value) {
47}
48StatsDimensionsValue::StatsDimensionsValue(int32_t field, bool value) :
49    mField(field),
50    mValueType(kBoolValueType),
51    mBoolValue(value) {
52}
53StatsDimensionsValue::StatsDimensionsValue(int32_t field, float value) :
54    mField(field),
55    mValueType(kFloatValueType),
56    mFloatValue(value) {
57}
58StatsDimensionsValue::StatsDimensionsValue(int32_t field, vector<StatsDimensionsValue> value) :
59    mField(field),
60    mValueType(kTupleValueType),
61    mTupleValue(value) {
62}
63
64StatsDimensionsValue::~StatsDimensionsValue() {}
65
66status_t
67StatsDimensionsValue::writeToParcel(Parcel* out) const {
68    status_t err ;
69
70    err = out->writeInt32(mField);
71    if (err != NO_ERROR) {
72        return err;
73    }
74    err = out->writeInt32(mValueType);
75    if (err != NO_ERROR) {
76        return err;
77    }
78    switch (mValueType) {
79        case kStrValueType:
80            err = out->writeString16(mStrValue);
81            break;
82        case kIntValueType:
83            err = out->writeInt32(mIntValue);
84            break;
85        case kLongValueType:
86            err = out->writeInt64(mLongValue);
87            break;
88        case kBoolValueType:
89            err = out->writeBool(mBoolValue);
90            break;
91        case kFloatValueType:
92            err = out->writeFloat(mFloatValue);
93            break;
94        case kTupleValueType:
95            {
96                int sz = mTupleValue.size();
97                err = out->writeInt32(sz);
98                if (err != NO_ERROR) {
99                    return err;
100                }
101                for (int i = 0; i < sz; ++i) {
102                    err = mTupleValue[i].writeToParcel(out);
103                    if (err != NO_ERROR) {
104                        return err;
105                    }
106                }
107            }
108            break;
109        default:
110            err = UNKNOWN_ERROR;
111            break;
112    }
113    return err;
114}
115
116status_t
117StatsDimensionsValue::readFromParcel(const Parcel* in)
118{
119    // Implement me if desired. We don't currently use this.
120    ALOGE("Cannot do c++ StatsDimensionsValue.readFromParcel(); it is not implemented.");
121    (void)in; // To prevent compile error of unused parameter 'in'
122    return UNKNOWN_ERROR;
123}
124
125}  // namespace os
126}  // namespace android
127