Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #713: SimplePropertyMap.d

File SimplePropertyMap.d, 1.8 kB (added by schveiguy, 1 year ago)

Simple implementation of PropertyMap? interface

Line 
1 /*******************************************************************************
2
3         copyright:      Copyright (c) 2007 Steven Schveighoffer. All rights reserved
4
5         license:        BSD style: $(LICENSE)
6
7         version:        Initial release: Oct 2007
8
9         author:         Steven Schveighoffer
10
11 *******************************************************************************/
12
13 module tango.text.SimplePropertyMap;
14
15 private import tango.text.Properties;
16
17 /*******************************************************************************
18
19         This property map wraps a T[T[]] element which is used as a the
20         property container.
21
22 *******************************************************************************/
23
24 class SimplePropertyMap(T) : PropertyMap!(T)
25 {
26         private T[][T[]] map;
27         void add(T[] name, T[] value)
28         {
29                 map[name] = value;
30         }
31
32         void remove(T[] name)
33         {
34                 map.remove(name);
35         }
36
37         T[] opIndex(T[] name)
38         {
39                 T[] *value = name in map;
40                 if(value is null)
41                         return null;
42                 return *value;
43         }
44
45         T[] opIndexAssign(T[] value, T[] name)
46         {
47                 map[name] = value;
48                 return value;
49         }
50
51         int opApply(int delegate(ref T[] name, ref T[] value) dg)
52         {
53                 int result = 0;
54                 foreach(k, v; map)
55                 {
56                         if((result = dg(k, v)) != 0)
57                                 break;
58                 }
59                 return result;
60         }
61
62         void clear()
63         {
64                 map = map.init;
65         }
66
67         bool contains(T[] name)
68         {
69                 return (name in map) !is null;
70         }
71 }