Motr  M0
cursor.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2011-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "lib/assert.h"
24 
25 #include "xcode/xcode.h"
26 
33 M0_INTERNAL void m0_xcode_cursor_init(struct m0_xcode_cursor *it,
34  const struct m0_xcode_obj *obj)
35 {
36  M0_SET0(it);
37  m0_xcode_cursor_top(it)->s_obj = *obj;
38 }
39 
40 M0_INTERNAL struct m0_xcode_cursor_frame *
42 {
43  M0_PRE(IS_IN_ARRAY(it->xcu_depth, it->xcu_stack));
44  return &it->xcu_stack[it->xcu_depth];
45 }
46 
47 M0_INTERNAL const struct m0_xcode_field *
49 {
50  if (it->xcu_depth > 0) {
51  const struct m0_xcode_cursor_frame *pre =
52  m0_xcode_cursor_top((void *)it) - 1; /* Drop const. */
53  return &pre->s_obj.xo_type->xct_child[pre->s_fieldno];
54  } else
55  return NULL;
56 }
57 
58 
59 M0_INTERNAL int m0_xcode_next(struct m0_xcode_cursor *it)
60 {
61  struct m0_xcode_cursor_frame *top;
62  struct m0_xcode_cursor_frame *next;
63  const struct m0_xcode_type *xt;
64  int nr;
65 
66  M0_PRE(it->xcu_depth >= 0);
67 
69  xt = top->s_obj.xo_type;
70  nr = xt->xct_nr;
71 
73  "xct_name: %s", xt->xct_name);
74  switch (top->s_flag) {
76  top->s_flag = M0_XCODE_CURSOR_PRE;
77  break;
78  case M0_XCODE_CURSOR_IN:
79  switch (xt->xct_aggr) {
80  case M0_XA_RECORD:
81  case M0_XA_TYPEDEF:
82  ++top->s_fieldno;
83  break;
84  case M0_XA_SEQUENCE:
85  if (top->s_fieldno == 0) {
86  top->s_elno = 0;
87  top->s_fieldno = 1;
88  } else {
89  case M0_XA_ARRAY: /* sic. All hail C. */
90  ++top->s_elno;
91  }
92  if (top->s_elno >= m0_xcode_tag(&top->s_obj)) {
93  top->s_elno = 0;
94  top->s_fieldno = 2;
95  }
96  break;
97  case M0_XA_UNION:
98  if (top->s_fieldno != 0) {
99  top->s_fieldno = nr;
100  break;
101  }
102  while (++top->s_fieldno < nr &&
103  m0_xcode_tag(&top->s_obj) !=
104  xt->xct_child[top->s_fieldno].xf_tag) {
105  ;
106  }
107  break;
108  case M0_XA_OPAQUE:
109  default:
110  M0_IMPOSSIBLE("wrong aggregation type");
111  }
112  /* fall through */
113  case M0_XCODE_CURSOR_PRE:
114  if (top->s_fieldno < nr) {
115  top->s_flag = M0_XCODE_CURSOR_IN;
116  if (xt->xct_aggr != M0_XA_ATOM) {
117  int result;
118 
119  ++it->xcu_depth;
121  result = m0_xcode_subobj(&next->s_obj,
122  &top->s_obj,
123  top->s_fieldno,
124  top->s_elno);
125  if (result != 0)
126  return result;
127  next->s_fieldno = 0;
128  next->s_elno = 0;
129  next->s_flag = M0_XCODE_CURSOR_PRE;
130  next->s_datum = 0;
131  }
132  } else
133  top->s_flag = M0_XCODE_CURSOR_POST;
134  break;
136  if (--it->xcu_depth < 0)
137  return 0;
140  top->s_flag = M0_XCODE_CURSOR_IN;
141  break;
142  default:
143  M0_IMPOSSIBLE("wrong order");
144  }
145  return +1;
146 }
147 
148 M0_INTERNAL void m0_xcode_skip(struct m0_xcode_cursor *it)
149 {
151 }
152 
155 /*
156  * Local variables:
157  * c-indentation-style: "K&R"
158  * c-basic-offset: 8
159  * tab-width: 8
160  * fill-column: 80
161  * scroll-step: 1
162  * End:
163  */
M0_INTERNAL struct m0_xcode_cursor_frame * m0_xcode_cursor_top(struct m0_xcode_cursor *it)
Definition: cursor.c:41
static size_t nr
Definition: dump.c:1505
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
size_t xct_nr
Definition: xcode.h:343
M0_INTERNAL int m0_xcode_subobj(struct m0_xcode_obj *subobj, const struct m0_xcode_obj *obj, int fieldno, uint64_t elno)
Definition: xcode.c:631
static struct m0_be_emap_cursor it
Definition: extmap.c:46
struct m0_xcode_field xct_child[0]
Definition: xcode.h:345
Definition: xcode.c:59
#define M0_SET0(obj)
Definition: misc.h:64
static struct m0_xcode_type ** xt[]
Definition: protocol.c:64
static struct foo * obj
Definition: tlist.c:302
static int top
Definition: formation2.c:86
M0_INTERNAL uint64_t m0_xcode_tag(const struct m0_xcode_obj *obj)
Definition: xcode.c:679
#define M0_ASSERT(cond)
static int next[]
Definition: cp.c:248
uint64_t xf_tag
Definition: xcode.h:271
M0_INTERNAL int m0_xcode_next(struct m0_xcode_cursor *it)
Definition: cursor.c:59
bool m0_xcode_type_invariant(const struct m0_xcode_type *xt)
Definition: xcode.c:74
#define M0_ASSERT_INFO_EX(cond, fmt,...)
M0_INTERNAL void m0_xcode_cursor_init(struct m0_xcode_cursor *it, const struct m0_xcode_obj *obj)
Definition: cursor.c:33
enum m0_xcode_aggr xct_aggr
Definition: xcode.h:316
#define IS_IN_ARRAY(idx, array)
Definition: misc.h:311
M0_INTERNAL void m0_xcode_skip(struct m0_xcode_cursor *it)
Definition: cursor.c:148
M0_INTERNAL const struct m0_xcode_field * m0_xcode_cursor_field(const struct m0_xcode_cursor *it)
Definition: cursor.c:48
const char * xct_name
Definition: xcode.h:318
#define M0_IMPOSSIBLE(fmt,...)