1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Keep this file in sync with entry_kernel.h.
6
7#include "sync/syncable/syncable_enum_conversions.h"
8
9#include "base/basictypes.h"
10#include "base/logging.h"
11
12namespace syncer {
13namespace syncable {
14
15// We can't tokenize expected_min/expected_max since it can be a
16// general expression.
17#define ASSERT_ENUM_BOUNDS(enum_min, enum_max, expected_min, expected_max) \
18  COMPILE_ASSERT(static_cast<int>(enum_min) ==                          \
19                 static_cast<int>(expected_min),                        \
20                 enum_min##_not_expected_min);                          \
21  COMPILE_ASSERT(static_cast<int>(enum_max) ==                          \
22                 static_cast<int>(expected_max),                        \
23                 enum_max##_not_expected_max);
24
25#define ENUM_CASE(enum_value) case enum_value: return #enum_value
26
27const char* GetMetahandleFieldString(MetahandleField metahandle_field) {
28  ASSERT_ENUM_BOUNDS(META_HANDLE, META_HANDLE,
29                     INT64_FIELDS_BEGIN, BASE_VERSION - 1);
30  switch (metahandle_field) {
31    ENUM_CASE(META_HANDLE);
32  }
33  NOTREACHED();
34  return "";
35}
36
37const char* GetBaseVersionString(BaseVersion base_version) {
38  ASSERT_ENUM_BOUNDS(BASE_VERSION, BASE_VERSION,
39                     META_HANDLE + 1, SERVER_VERSION - 1);
40  switch (base_version) {
41    ENUM_CASE(BASE_VERSION);
42  }
43  NOTREACHED();
44  return "";
45}
46
47const char* GetInt64FieldString(Int64Field int64_field) {
48  ASSERT_ENUM_BOUNDS(SERVER_VERSION, TRANSACTION_VERSION,
49                     BASE_VERSION + 1, INT64_FIELDS_END - 1);
50  switch (int64_field) {
51    ENUM_CASE(SERVER_VERSION);
52    ENUM_CASE(LOCAL_EXTERNAL_ID);
53    ENUM_CASE(TRANSACTION_VERSION);
54    case INT64_FIELDS_END: break;
55  }
56  NOTREACHED();
57  return "";
58}
59
60const char* GetTimeFieldString(TimeField time_field) {
61  ASSERT_ENUM_BOUNDS(MTIME, SERVER_CTIME,
62                     TIME_FIELDS_BEGIN, TIME_FIELDS_END - 1);
63  switch (time_field) {
64    ENUM_CASE(MTIME);
65    ENUM_CASE(SERVER_MTIME);
66    ENUM_CASE(CTIME);
67    ENUM_CASE(SERVER_CTIME);
68    case TIME_FIELDS_END: break;
69  }
70  NOTREACHED();
71  return "";
72}
73
74const char* GetIdFieldString(IdField id_field) {
75  ASSERT_ENUM_BOUNDS(ID, SERVER_PARENT_ID,
76                     ID_FIELDS_BEGIN, ID_FIELDS_END - 1);
77  switch (id_field) {
78    ENUM_CASE(ID);
79    ENUM_CASE(PARENT_ID);
80    ENUM_CASE(SERVER_PARENT_ID);
81    case ID_FIELDS_END: break;
82  }
83  NOTREACHED();
84  return "";
85}
86
87const char* GetIndexedBitFieldString(IndexedBitField indexed_bit_field) {
88  ASSERT_ENUM_BOUNDS(IS_UNSYNCED, IS_UNAPPLIED_UPDATE,
89                     BIT_FIELDS_BEGIN, INDEXED_BIT_FIELDS_END - 1);
90  switch (indexed_bit_field) {
91    ENUM_CASE(IS_UNSYNCED);
92    ENUM_CASE(IS_UNAPPLIED_UPDATE);
93    case INDEXED_BIT_FIELDS_END: break;
94  }
95  NOTREACHED();
96  return "";
97}
98
99const char* GetIsDelFieldString(IsDelField is_del_field) {
100  ASSERT_ENUM_BOUNDS(IS_DEL, IS_DEL,
101                     INDEXED_BIT_FIELDS_END, IS_DIR - 1);
102  switch (is_del_field) {
103    ENUM_CASE(IS_DEL);
104  }
105  NOTREACHED();
106  return "";
107}
108
109const char* GetBitFieldString(BitField bit_field) {
110  ASSERT_ENUM_BOUNDS(IS_DIR, SERVER_IS_DEL,
111                     IS_DEL + 1, BIT_FIELDS_END - 1);
112  switch (bit_field) {
113    ENUM_CASE(IS_DIR);
114    ENUM_CASE(SERVER_IS_DIR);
115    ENUM_CASE(SERVER_IS_DEL);
116    case BIT_FIELDS_END: break;
117  }
118  NOTREACHED();
119  return "";
120}
121
122const char* GetStringFieldString(StringField string_field) {
123  ASSERT_ENUM_BOUNDS(NON_UNIQUE_NAME, UNIQUE_BOOKMARK_TAG,
124                     STRING_FIELDS_BEGIN, STRING_FIELDS_END - 1);
125  switch (string_field) {
126    ENUM_CASE(NON_UNIQUE_NAME);
127    ENUM_CASE(SERVER_NON_UNIQUE_NAME);
128    ENUM_CASE(UNIQUE_SERVER_TAG);
129    ENUM_CASE(UNIQUE_CLIENT_TAG);
130    ENUM_CASE(UNIQUE_BOOKMARK_TAG);
131    case STRING_FIELDS_END: break;
132  }
133  NOTREACHED();
134  return "";
135}
136
137const char* GetProtoFieldString(ProtoField proto_field) {
138  ASSERT_ENUM_BOUNDS(SPECIFICS, BASE_SERVER_SPECIFICS,
139                     PROTO_FIELDS_BEGIN, PROTO_FIELDS_END - 1);
140  switch (proto_field) {
141    ENUM_CASE(SPECIFICS);
142    ENUM_CASE(SERVER_SPECIFICS);
143    ENUM_CASE(BASE_SERVER_SPECIFICS);
144    case PROTO_FIELDS_END: break;
145  }
146  NOTREACHED();
147  return "";
148}
149
150const char* GetUniquePositionFieldString(UniquePositionField position_field) {
151  ASSERT_ENUM_BOUNDS(SERVER_UNIQUE_POSITION, UNIQUE_POSITION,
152                     UNIQUE_POSITION_FIELDS_BEGIN,
153                     UNIQUE_POSITION_FIELDS_END - 1);
154  switch(position_field) {
155    ENUM_CASE(SERVER_UNIQUE_POSITION);
156    ENUM_CASE(UNIQUE_POSITION);
157    case UNIQUE_POSITION_FIELDS_END: break;
158  }
159  NOTREACHED();
160  return "";
161}
162
163const char* GetAttachmentMetadataFieldString(
164    AttachmentMetadataField attachment_metadata_field) {
165  ASSERT_ENUM_BOUNDS(ATTACHMENT_METADATA,
166                     ATTACHMENT_METADATA,
167                     ATTACHMENT_METADATA_FIELDS_BEGIN,
168                     ATTACHMENT_METADATA_FIELDS_END - 1);
169  switch(attachment_metadata_field) {
170    ENUM_CASE(ATTACHMENT_METADATA);
171    case ATTACHMENT_METADATA_FIELDS_END: break;
172  }
173  NOTREACHED();
174  return "";
175}
176
177const char* GetBitTempString(BitTemp bit_temp) {
178  ASSERT_ENUM_BOUNDS(SYNCING, SYNCING,
179                     BIT_TEMPS_BEGIN, BIT_TEMPS_END - 1);
180  switch (bit_temp) {
181    ENUM_CASE(SYNCING);
182    case BIT_TEMPS_END: break;
183  }
184  NOTREACHED();
185  return "";
186}
187
188#undef ENUM_CASE
189#undef ASSERT_ENUM_BOUNDS
190
191}  // namespace syncable
192}  // namespace syncer
193