本文主要阐述Navit的attr模型。attr是Navit的属性模型,它定义了Navit要用到的所有的属性。attr结构有一个enum attr_type type 枚举类型,一个联合变量u,采用联合变量主要是最大化的节省系统的开销。在联合变量u中,定义了navit用到的几乎所有的属性。具体的attr结构定义如下:
2 enum attr_type type;
3 union {
4 char * str;
5 void * data;
6 int num;
7 struct item * item;
8 enum item_type item_type;
9 enum projection projection;
10 double * numd;
11 struct color * color;
12 struct coord_geo * coord_geo;
13 struct navit * navit;
14 struct callback * callback;
15 struct callback_list * callback_list;
16 struct vehicle * vehicle;
17 struct layout * layout;
18 struct layer * layer;
19 struct map * map;
20 struct mapset * mapset;
21 struct log * log;
22 struct route * route;
23 struct navigation * navigation;
24 struct coord * coord;
25 struct pcoord * pcoord;
26 struct gui * gui;
27 struct graphics * graphics;
28 struct tracking * tracking;
29 struct itemgra * itemgra;
30 struct plugin * plugin;
31 struct plugins * plugins;
32 struct polygon * polygon;
33 struct polyline * polyline;
34 struct circle * circle;
35 struct text * text;
36 struct icon * icon;
37 struct image * image;
38 struct arrows * arrows;
39 struct element * element;
40 struct speech * speech;
41 struct cursor * cursor;
42 struct displaylist * displaylist;
43 struct transformation * transformation;
44 struct vehicleprofile * vehicleprofile;
45 struct roadprofile * roadprofile;
46 struct bookmarks * bookmarks;
47 struct range {
48 short min, max;
49 } range;
50 int * dash;
51 enum item_type * item_types;
52 enum attr_type * attr_types;
53 long long * num64;
54 struct attr * attrs;
55 } u;
56 };
attr模型主要由attr.h、attr.c和attr_def.h三文件组成。其中attr.h定义了attr模型的所有的宏、结构和函数,attr_def.h定义了attr_type要用到的一些量。attr.c是attr模型的实现。现在我们对attr进行详细的分析。在attr.h文件中,一开始就给出了item_type 和 attr_type的定义。attr_type定义是由定义宏解析attr_def.h文件来是先的。在宏的定义中涉及到了"##"的用法,"##"在宏定义中使用的比较普遍,主要是强制链接作用,详细的“##”使用说明并和“#”的对比请参考《C语言宏定义中#与##的用法》,这里不做重复说明。enum attr_type的定义如下:
2 #define ATTR2(x,y) attr_##y=x,
3 #define ATTR(x) attr_##x,
4 #include " attr_def.h "
5 #undef ATTR2
6 #undef ATTR
7 };
宏ATTR2 定义了attr_type各个数据类型在内存空间中的位置。而宏ATTR则定义了具体的各个数据枚举。在attr_def.h文件中,分别以item类型的起止位置“0x00010000~0x0001ffff”,int类型的起止位置“0x00020000~0x0002ffff”,在int类型中包含了绝对真值(rel_abs)类型的起始位置“0x00027500”和boolean类型的起止位置“0x00028000”,string类型的起止位置“0x00030000~0x0003ffff”,special类型的起止位置“0x00040000~0x0004ffff”,double类型的起止位置“0x00050000~0x0005ffff”,coor_geo(WSG84坐标系)类型起止位置“0x00060000~0x0006ffff”,color类型起止位置“0x00070000~0x0007ffff”,object类型的起止位置"0x00080000~0x0008ffff",coord类型的起止位置“0x00090000~0x0009ffff”,pcoord类型的起止位置“0x000a0000~0x000affff”,callback类型的起止位置“0x000b0000~0x000bffff”,int64类型的起止位置“0x000c0000~0x000cffff”,group类型的起止位置"0x000d0000~0x000dffff",item_type类型的起止位置“0x000e0000~0x000effff”。在各个类型的空间中,分别定了对应的一些数据,具体的请看attr_def.h文件代码。
2
3 /* common */
4 ATTR2( 0x00000000 ,none)
5 ATTR(any)
6 ATTR(any_xml)
7 /* *
8 *
9 * begin with type_item_begin
10 *
11 * */
12 ATTR2( 0x00010000 ,type_item_begin)
13 ATTR(town_streets_item)
14 ATTR(street_name_item)
15 ATTR(street_name_numbers_item)
16 ATTR(street_item)
17 ATTR(street_number_item)
18 ATTR(position_sat_item)
19 ATTR(current_item)
20 ATTR2( 0x0001ffff ,type_item_end)
21 /* *
22 *
23 * begin with type_int_begin
24 *
25 * */
26 ATTR2( 0x00020000 ,type_int_begin)
27 ATTR(h_remove)
28 ATTR(id)
29 ATTR(flags)
30 ATTR(w_remove)
31 ATTR(x_remove)
32 ATTR(y_remove)
33 ATTR(flush_size)
34 ATTR(flush_time)
35 ATTR(zipfile_ref)
36 ATTR(country_id)
37 ATTR(position_sats)
38 ATTR(position_sats_used)
39 ATTR(update)
40 ATTR(follow)
41 ATTR(length)
42 ATTR(time)
43 ATTR(destination_length)
44 ATTR(destination_time)
45 ATTR(speed)
46 ATTR(interval)
47 ATTR(position_qual)
48 ATTR(zoom)
49 ATTR(retry_interval)
50 ATTR(projection)
51 ATTR(offroad)
52 ATTR(vocabulary_name)
53 ATTR(vocabulary_name_systematic)
54 ATTR(vocabulary_distances)
55 ATTR(announce_name_systematic_first)
56 ATTR(antialias)
57 ATTR(order_delta)
58 ATTR(baudrate)
59 ATTR(font_size_remove)
60 ATTR(icon_xs)
61 ATTR(icon_l)
62 ATTR(icon_s)
63 ATTR(spacing)
64 ATTR(recent_dest)
65 ATTR(destination_distance)
66 ATTR(check_version)
67 ATTR(details)
68 ATTR(width)
69 ATTR(offset)
70 ATTR(directed)
71 ATTR(radius)
72 ATTR(text_size)
73 ATTR(level)
74 ATTR(icon_w)
75 ATTR(icon_h)
76 ATTR(rotation)
77 ATTR(checksum_ignore)
78 ATTR(position_fix_type)
79 ATTR(timeout)
80 ATTR(orientation)
81 ATTR(keyboard)
82 ATTR(position_sats_signal)
83 ATTR(cps)
84 ATTR(fast)
85 ATTR(osd_configuration)
86 ATTR(columns)
87 ATTR(align)
88 ATTR(sat_prn)
89 ATTR(sat_elevation)
90 ATTR(sat_azimuth)
91 ATTR(sat_snr)
92 ATTR(autozoom)
93 ATTR(version)
94 ATTR(autozoom_min)
95 ATTR(maxspeed)
96 ATTR(cdf_histsize)
97 ATTR(message_maxage)
98 ATTR(message_maxnum)
99 ATTR(pitch)
100 ATTR(roll)
101 ATTR(yaw)
102 ATTR(route_status)
103 ATTR(route_weight)
104 ATTR(distance_metric)
105 ATTR(route_mode)
106 ATTR(maxspeed_handling)
107 ATTR(flags_forward_mask)
108 ATTR(flags_reverse_mask)
109 ATTR(house_number_interpolation)
110 ATTR(house_number_left_interpolation)
111 ATTR(house_number_right_interpolation)
112 ATTR(delay)
113 ATTR(lag)
114 ATTR(bpp)
115 ATTR(fullscreen)
116 ATTR(windowid)
117 ATTR(hog)
118 ATTR(flags_town)
119 ATTR(flags_street)
120 ATTR(flags_house_number)
121 ATTR(use_camera)
122 ATTR(flags_graphics)
123 ATTR(zoom_min)
124 ATTR(zoom_max)
125 ATTR(gamma)
126 ATTR(brightness)
127 ATTR(contrast)
128 ATTR(height)
129 ATTR(minspeed)
130 ATTR(recspeed)
131 ATTR(speed_cond)
132 ATTR(shmkey)
133 ATTR(vehicle_width)
134 ATTR(vehicle_length)
135 ATTR(vehicle_height)
136 ATTR(vehicle_weight)
137 ATTR(vehicle_axle_weight)
138 ATTR(vehicle_dangerous_goods)
139 ATTR(shmsize)
140 ATTR(shmoffset)
141 ATTR(speed_category)
142 ATTR(static_speed)
143 ATTR(static_distance)
144 ATTR(through_traffic_penalty)
145 ATTR(through_traffic_flags)
146 ATTR2( 0x00027500 ,type_rel_abs_begin)
147 /* These attributes are int that can either hold relative *
148 * or absolute values. A relative value is indicated by *
149 * adding 0x60000000. *
150 * *
151 * The range of valid absolute values is -0x40000000 to *
152 * 0x40000000, the range of relative values is from *
153 * -0x20000000 to 0x20000000. */
154 ATTR(h)
155 ATTR(w)
156 ATTR(x)
157 ATTR(y)
158 ATTR(font_size)
159 /* *
160 *
161 * begin with type_boolean_begin
162 *
163 * */
164 ATTR2( 0x00028000 ,type_boolean_begin)
165 /* boolean */
166 ATTR(overwrite)
167 ATTR(active)
168 ATTR(follow_cursor)
169 ATTR(orientation_removeme)
170 ATTR(tracking)
171 ATTR(menubar)
172 ATTR(statusbar)
173 ATTR(toolbar)
174 ATTR(animate)
175 ATTR(lazy)
176 ATTR(mkdir)
177 ATTR(predraw)
178 ATTR(postdraw)
179 ATTR(button)
180 ATTR(ondemand)
181 ATTR(menu_on_map_click)
182 ATTR(direction)
183 ATTR(route_follow_straight_REMOVE) // This is to be removed with the next version
184 ATTR(gui_speech)
185 ATTR(town_id) /* fixme? */
186 ATTR(street_id) /* fixme? */
187 ATTR(district_id) /* fixme? */
188 ATTR(drag_bitmap)
189 ATTR(use_mousewheel)
190 ATTR(fullscreen_old)
191 ATTR(position_magnetic_direction)
192 ATTR(use_overlay)
193 ATTR(night_mode)
194 ATTR(autozoom_active)
195 ATTR(position_valid)
196 ATTR(frame)
197 ATTR(tell_street_name)
198 ATTR(bluetooth)
199 ATTR(signal_on_map_click)
200 ATTR(route_active)
201 ATTR(search_active)
202 ATTR(unsuspend)
203 ATTR2( 0x0002ffff ,type_int_end)
204 /* *
205 *
206 * begin with type_string_begin
207 *
208 * */
209 ATTR2( 0x00030000 ,type_string_begin)
210 ATTR(type)
211 ATTR(label)
212 ATTR(data)
213 ATTR(charset)
214 ATTR(country_all)
215 ATTR(country_iso3)
216 ATTR(country_iso2)
217 ATTR(country_car)
218 ATTR(country_name)
219 ATTR(town_name)
220 ATTR(town_postal)
221 ATTR(district_name)
222 ATTR(street_name)
223 ATTR(street_name_systematic)
224 ATTR(street_number)
225 ATTR(debug)
226 ATTR(address)
227 ATTR(phone)
228 ATTR(entry_fee)
229 ATTR(open_hours)
230 ATTR(skin)
231 ATTR(fullscreen_removeme)
232 ATTR(view_mode)
233 ATTR(tilt)
234 ATTR(media_window_title)
235 ATTR(media_cmd)
236 ATTR(image_codec)
237 /* poi */
238 ATTR(icono)
239 ATTR(info_html)
240 ATTR(price_html)
241 /* navigation */
242 ATTR(navigation_short)
243 ATTR(navigation_long)
244 ATTR(navigation_long_exact)
245 ATTR(navigation_speech)
246 ATTR(name)
247 ATTR(cursorname)
248 ATTR(source)
249 ATTR(description)
250 ATTR(gc_type)
251 ATTR(layout)
252 ATTR(position_nmea)
253 ATTR(gpsd_query)
254 ATTR(on_eof)
255 ATTR(command)
256 ATTR(src)
257 ATTR(path)
258 ATTR(font)
259 ATTR(url_local)
260 ATTR(gc_size)
261 ATTR(gc_difficulty)
262 ATTR(gc_terrain)
263 ATTR(icon_src)
264 ATTR(position_time_iso8601)
265 ATTR(house_number)
266 ATTR(osm_member)
267 ATTR(osm_tag)
268 ATTR(municipality_name)
269 ATTR(county_name)
270 ATTR(state_name)
271 ATTR(message)
272 ATTR(callbacks)
273 ATTR(enable_expression)
274 ATTR(fax)
275 ATTR(email)
276 ATTR(url)
277 ATTR(profilename)
278 ATTR(projectionname)
279 ATTR(town_or_district_name)
280 ATTR(postal)
281 ATTR(postal_mask)
282 ATTR(house_number_first)
283 ATTR(house_number_last)
284 ATTR(house_number_left_first)
285 ATTR(house_number_left_last)
286 ATTR(house_number_right_first)
287 ATTR(house_number_right_last)
288 ATTR(town_name_match)
289 ATTR(district_name_match)
290 ATTR(street_name_match)
291 ATTR(language)
292 ATTR(subtype)
293 ATTR(filter)
294 ATTR(daylayout)
295 ATTR(nightlayout)
296 ATTR(xml_text)
297 ATTR(layout_name)
298 ATTR(user_name)
299 ATTR(user_pass)
300 ATTR(status_text)
301 ATTR(log_gpx_desc)
302 ATTR(map_pass)
303 ATTR(validity_period)
304 ATTR(socket)
305 ATTR2( 0x0003ffff ,type_string_end)
306 /* *
307 *
308 * begin with type_special_begin
309 *
310 * */
311 ATTR2( 0x00040000 ,type_special_begin)
312 ATTR(order)
313 ATTR(item_type)
314 ATTR(item_types)
315 ATTR(dash)
316 ATTR(sequence_range)
317 ATTR(angle_range)
318 ATTR(speed_range)
319 ATTR(attr_types)
320 ATTR(ch_edge)
321 ATTR(zipfile_ref_block)
322 ATTR2( 0x0004ffff ,type_special_end)
323 /* *
324 *
325 * begin with type_double_begin
326 *
327 * */
328 ATTR2( 0x00050000 ,type_double_begin)
329 ATTR(position_height)
330 ATTR(position_speed)
331 ATTR(position_direction)
332 ATTR(position_hdop)
333 ATTR(position_radius)
334 ATTR2( 0x0005ffff ,type_double_end)
335 /* *
336 *
337 * begin with type_coord_geo_begin(coordinate system)
338 *
339 * */
340 ATTR2( 0x00060000 ,type_coord_geo_begin)
341 ATTR(position_coord_geo)
342 ATTR(center)
343 ATTR(click_coord_geo)
344 ATTR2( 0x0006ffff ,type_coord_geo_end)
345 /* *
346 *
347 * begin with type_color_begin
348 *
349 * */
350 ATTR2( 0x00070000 ,type_color_begin)
351 ATTR(color)
352 ATTR(color2)
353 ATTR(background_color)
354 ATTR(text_color)
355 ATTR(background_color2)
356 ATTR2( 0x0007ffff ,type_color_end)
357 /* *
358 *
359 * begin with type_object_begin
360 *
361 * */
362 ATTR2( 0x00080000 ,type_object_begin)
363 ATTR(navit)
364 ATTR(log)
365 ATTR(callback)
366 ATTR(route)
367 ATTR(navigation)
368 ATTR(vehicle)
369 ATTR(map)
370 ATTR(bookmark_map)
371 ATTR(bookmarks)
372 ATTR(former_destination_map)
373 ATTR(graphics)
374 ATTR(gui)
375 ATTR(trackingo) /* fixme */
376 ATTR(plugins)
377 ATTR(layer)
378 ATTR(itemgra)
379 ATTR(polygon)
380 ATTR(polyline)
381 ATTR(circle)
382 ATTR(text)
383 ATTR(icon)
384 ATTR(image)
385 ATTR(arrows)
386 ATTR(mapset)
387 ATTR(osd)
388 ATTR(plugin)
389 ATTR(speech)
390 ATTR(coord)
391 ATTR(private_data)
392 ATTR(callback_list)
393 ATTR(displaylist)
394 ATTR(transformation)
395 ATTR(vehicleprofile)
396 ATTR(roadprofile)
397 ATTR(announcement)
398 ATTR(cursor)
399 ATTR(config)
400 ATTR2( 0x0008ffff ,type_object_end)
401 /* *
402 *
403 * begin with type_coord_begin
404 *
405 * */
406 ATTR2( 0x00090000 ,type_coord_begin)
407 ATTR2( 0x0009ffff ,type_coord_end)
408 /* *
409 *
410 * begin with type_pcoord_begin
411 *
412 * */
413 ATTR2( 0x000a0000 ,type_pcoord_begin)
414 ATTR(destination)
415 ATTR(position)
416 ATTR2( 0x000affff ,type_pcoord_end)
417 /* *
418 *
419 * begin with type_callback_begin
420 *
421 * */
422 ATTR2( 0x000b0000 ,type_callback_begin)
423 ATTR(resize)
424 ATTR(motion)
425 ATTR(keypress)
426 ATTR(window_closed)
427 ATTR(log_gpx)
428 ATTR(log_textfile)
429 ATTR(graphics_ready)
430 ATTR2( 0x000bffff ,type_callback_end)
431 /* *
432 *
433 * begin with type_int64_begin
434 *
435 * */
436 ATTR2( 0x000c0000 ,type_int64_begin)
437 ATTR(osm_nodeid)
438 ATTR(osm_wayid)
439 ATTR(osm_relationid)
440 ATTR2( 0x000cffff ,type_int64_end)
441 /* *
442 *
443 * begin with type_group_begin
444 *
445 * */
446 ATTR2( 0x000d0000 ,type_group_begin)
447 ATTR(speed_dep)
448 ATTR2( 0x000dffff ,type_group_end)
449 /* *
450 *
451 * begin with type_item_type_begin
452 *
453 * */
454 ATTR2( 0x000e0000 ,type_item_type_begin)
455 ATTR2( 0x000effff ,type_item_type_end)
在定义好attr_type枚举类型后,attr给出了地图数据偏移量对应的宏定义,这些定义直接对应的地图的各个元素、特征和定义功能。例如定义了宏AF_ONEWAY (1<<0)表示单行道。这一块内容这里先不做详细说明和阐述,等在学习map地图模块是详细介绍。在宏的定义中,还给出了attr属性可能涉及到的相关值。
2 #define ATTR_REL_MAXABS 0x40000000
3 #define ATTR_REL_RELSHIFT 0x60000000
枚举类型attr_position_valid表示attr属性类型之间的位置关系
2 attr_position_valid_invalid,
3 attr_position_valid_static,
4 attr_position_valid_extrapolated_time,
5 attr_position_valid_extrapolated_spatial,
6 attr_position_valid_valid,
7 };
在枚举类型attr_type定了navit要用到的各种数据类型后,attr又给出了判断类型是否属于已定义类型的宏定义。
2 #define ATTR_IS_DOUBLE(x) ((x) >= attr_type_double_begin && (x) <= attr_type_double_end)
3 #define ATTR_IS_STRING(x) ((x) >= attr_type_string_begin && (x) <= attr_type_string_end)
4 #define ATTR_IS_OBJECT(x) ((x) >= attr_type_object_begin && (x) <= attr_type_object_end)
5 #define ATTR_IS_COORD_GEO(x) ((x) >= attr_type_coord_geo_begin && (x) <= attr_type_coord_geo_end)
6 #define ATTR_IS_NUMERIC(x) (ATTR_IS_INT(x) || ATTR_IS_DOUBLE(x))
7 #define ATTR_IS_COLOR(x) ((x) >= attr_type_color_begin && (x) <= attr_type_color_end)
8 #define ATTR_IS_PCOORD(x) ((x) >= attr_type_pcoord_begin && (x) <= attr_type_pcoord_end)
9 #define ATTR_IS_COORD(x) ((x) >= attr_type_coord_begin && (x) <= attr_type_coord_end)
10 #define ATTR_IS_GROUP(x) ((x) >= attr_type_group_begin && (x) <= attr_type_group_end)
当然也顺便用宏定义对attr的结构类型进行扩展,分别定义了一个STR、INT、ITEMS和LIST结构。
2 #define ATTR_DEF_INT(x,y) (&(struct attr){attr_##x,{(char *)(y)}})
3 #define ATTR_DEF_ITEMS(x,...) (&(struct attr){attr_##x,{(char *)((enum item_type[]){__VA_ARGS__ , type_none})}})
4 #define ATTR_LIST(...) (struct attr *[]) { __VA_ARGS__, NULL}
在经过上述的枚举、宏定义后,接下来attr给出了一些函数用于数据对象的创建、数据的转换和资源的释放等工作。