1 using System;
2 using System.Collections;
3 using CodeFormatter = System.Text.CodeFormatter;
4
5 namespace cmsx.intermediate
6 {
7 public abstract class MDItem
8 {
9 public virtual default ~MDItem();
10 }
11
12 public class MDBool : MDItem
13 {
14 public nothrow MDBool(bool value_) : value(value_)
15 {
16 }
17 public bool value;
18 }
19
20 public class MDLong : MDItem
21 {
22 public nothrow MDLong(long value_) : value(value_)
23 {
24 }
25 public long value;
26 }
27
28 public class MDString : MDItem
29 {
30 public nothrow MDString(const string& value_) : value(value_)
31 {
32 }
33 public string value;
34 }
35
36 public class MDStructRef : MDItem
37 {
38 public nothrow MDStructRef(int id_) : id(id_)
39 {
40 }
41 public int id;
42 }
43
44 public class MDStruct : MDItem
45 {
46 public nothrow MDStruct(int id_) : id(id_)
47 {
48 }
49 public void AddItem(const string& fieldName, MDItem* item)
50 {
51 itemMap[fieldName] = item;
52 }
53 public MDItem* GetItem(const string& fieldName) const
54 {
55 HashMap<string, MDItem*>.ConstIterator it = itemMap.CFind(fieldName);
56 if (it != itemMap.CEnd())
57 {
58 return it->second;
59 }
60 else
61 {
62 throw Exception("metadata item '" + fieldName + "' not found");
63 }
64 }
65 public bool HasItem(const string& fieldName) const
66 {
67 HashMap<string, MDItem*>.ConstIterator it = itemMap.CFind(fieldName);
68 return it != itemMap.CEnd();
69 }
70 public int id;
71 public HashMap<string, MDItem*> itemMap;
72 }
73
74 public class Metadata
75 {
76 public MDBool* CreateMDBool(bool value)
77 {
78 MDBool* item = new MDBool(value);
79 AddItem(item);
80 return item;
81 }
82 public MDLong* CreateMDLong(long value)
83 {
84 MDLong* item = new MDLong(value);
85 AddItem(item);
86 return item;
87 }
88 public MDString* CreateMDString(const string& value)
89 {
90 MDString* item = new MDString(value);
91 AddItem(item);
92 return item;
93 }
94 public MDStructRef* CreateMDStructRef(int id)
95 {
96 MDStructRef* item = new MDStructRef(id);
97 AddItem(item);
98 return item;
99 }
100 public MDStruct* CreateMDStruct(int id)
101 {
102 MDStruct* s = new MDStruct(id);
103 structs.Add(UniquePtr<MDStruct>(s));
104 structMap[id] = s;
105 return s;
106 }
107 public MDStruct* GetMDStruct(int id)
108 {
109 HashMap<int, MDStruct*>.ConstIterator it = structMap.CFind(id);
110 if (it != structMap.CEnd())
111 {
112 return it->second;
113 }
114 else
115 {
116 throw Exception("metadata structure " + ToString(id) + " not found");
117 }
118 }
119 private void AddItem(MDItem* item)
120 {
121 items.Add(UniquePtr<MDItem>(item));
122 }
123 private List<UniquePtr<MDItem>> items;
124 private List<UniquePtr<MDStruct>> structs;
125 private HashMap<int, MDStruct*> structMap;
126 }
127 }