doc: added man page for btarcmp
[barry.git] / test / fhbuild.cc
blob10a155c4c4c62d716cb19a8e89365f96d5094b64
1 ///
2 /// \file fhbuild.cc
3 /// Compile-time test to make sure all FieldHandle<> templates
4 /// and classes are complete.
5 ///
7 /*
8 Copyright (C) 2012, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include <barry/barry.h>
24 #include <iostream>
26 using namespace std;
27 using namespace Barry;
29 void DumpId(const FieldIdentity &id)
31 cout << " sub = " << (id.HasSubfields ? "true" : "false")
32 << ", parent = "
33 << (id.ParentName ? id.ParentName : "NULL") << endl;
34 cout << " FieldTypeCode = " << id.FieldTypeCode << endl;
35 cout << " Ldif = " << (id.Ldif ? id.Ldif : "NULL")
36 << ", ObjectClass = "
37 << (id.ObjectClass ? id.ObjectClass : "NULL")
38 << endl;
39 cout << " Iconv = " << (id.IconvNeeded ? "true" : "false")
40 << endl << endl;
43 void DumpEnumConstants(const EnumConstants *ec)
45 cout << " Available constants:" << endl;
47 EnumConstants::EnumConstantList::const_iterator
48 b = ec->GetConstantList().begin(),
49 e = ec->GetConstantList().end();
50 for( ; b != e; ++b ) {
51 cout << " " << b->DisplayName
52 << " (" << b->Name << ")"
53 << " = " << b->Value << endl;
55 cout << endl;
58 std::ostream& operator<<(std::ostream &os, const EmailList &el)
60 bool first = true;
61 for( EmailList::const_iterator b = el.begin(); b != el.end(); ++b ) {
62 if( !first )
63 os << ", ";
64 else
65 first = false;
67 os << *b;
69 return os;
72 template <class RecordT>
73 struct FieldHandler
75 const RecordT &m_rec;
77 FieldHandler(const RecordT &obj)
78 : m_rec(obj)
82 void operator()(EnumFieldBase<RecordT> *ep,
83 const FieldIdentity &id) const
85 cout << id.DisplayName << " (" << id.Name << "): "
86 << ep->GetName(ep->GetValue(m_rec))
87 << " (" << ep->GetValue(m_rec) << ")"
88 << endl;
89 DumpId(id);
90 DumpEnumConstants(ep);
93 void operator()(typename FieldHandle<RecordT>::PostalPointer pp,
94 const FieldIdentity &id) const
96 cout << id.DisplayName << " (" << id.Name << "): "
97 << m_rec.*(pp.m_PostalAddress).*(pp.m_PostalField)
98 << endl;
99 DumpId(id);
102 template <class TypeT>
103 void operator()(TypeT RecordT::* mp, const FieldIdentity &id) const
105 cout << id.DisplayName << " (" << id.Name << "): "
106 << m_rec.*mp << endl;
107 DumpId(id);
111 class VirtualFieldHandler : public FieldValueHandlerBase
113 public:
114 /// For type std::string
115 virtual void operator()(const std::string &v,
116 const FieldIdentity &id) const
118 cout << id.DisplayName << " (" << id.Name << "): "
119 << v << endl;
120 DumpId(id);
122 /// For type EmailAddressList
123 virtual void operator()(const EmailAddressList &v,
124 const FieldIdentity &id) const
126 cout << id.DisplayName << " (" << id.Name << "): "
127 << v << endl;
128 DumpId(id);
130 /// For type time_t
131 virtual void operator()(const time_t &v,
132 const FieldIdentity &id) const
134 cout << id.DisplayName << " (" << id.Name << "): "
135 << v << endl;
136 DumpId(id);
138 /// For type uint8_t
139 virtual void operator()(const uint8_t &v,
140 const FieldIdentity &id) const
142 cout << id.DisplayName << " (" << id.Name << "): "
143 << (unsigned int) v << endl;
144 DumpId(id);
146 /// For type uint16_t
147 virtual void operator()(const uint16_t &v,
148 const FieldIdentity &id) const
150 cout << id.DisplayName << " (" << id.Name << "): "
151 << v << endl;
152 DumpId(id);
154 /// For type uint32_t
155 virtual void operator()(const uint32_t &v,
156 const FieldIdentity &id) const
158 cout << id.DisplayName << " (" << id.Name << "): "
159 << v << endl;
160 DumpId(id);
162 /// For type uint64_t
163 virtual void operator()(const uint64_t &v,
164 const FieldIdentity &id) const
166 cout << id.DisplayName << " (" << id.Name << "): "
167 << v << endl;
168 DumpId(id);
170 /// For type bool
171 virtual void operator()(const bool &v,
172 const FieldIdentity &id) const
174 cout << id.DisplayName << " (" << id.Name << "): "
175 << v << endl;
176 DumpId(id);
178 /// For type int32_t
179 virtual void operator()(const int32_t &v,
180 const FieldIdentity &id) const
182 cout << id.DisplayName << " (" << id.Name << "): "
183 << v << endl;
184 DumpId(id);
186 /// For type EmailList
187 virtual void operator()(const EmailList &v,
188 const FieldIdentity &id) const
190 cout << id.DisplayName << " (" << id.Name << "): "
191 << v << endl;
192 DumpId(id);
194 /// For type Date
195 virtual void operator()(const Date &v,
196 const FieldIdentity &id) const
198 cout << id.DisplayName << " (" << id.Name << "): "
199 << v << endl;
200 DumpId(id);
202 /// For type CategoryList
203 virtual void operator()(const CategoryList &v,
204 const FieldIdentity &id) const
206 cout << id.DisplayName << " (" << id.Name << "): "
207 << v << endl;
208 DumpId(id);
210 /// For type PostalAddress
211 virtual void operator()(const PostalAddress &v,
212 const FieldIdentity &id) const
214 cout << id.DisplayName << " (" << id.Name << "): "
215 << v << endl;
216 DumpId(id);
218 /// For type UnknownsType
219 virtual void operator()(const UnknownsType &v,
220 const FieldIdentity &id) const
222 cout << id.DisplayName << " (" << id.Name << "): "
223 << v << endl;
224 DumpId(id);
228 void TestContact()
230 Contact contact;
231 contact.RecType = 8;
232 contact.RecordId = 100000;
233 contact.EmailAddresses.push_back("cdfrey@foursquare.net");
234 contact.EmailAddresses.push_back("cdfrey@netdirect.ca");
235 contact.Phone = "519-555-1212";
236 contact.Fax = "no fax";
237 contact.HomeFax = "home fax";
238 contact.WorkPhone = "work 51235";
239 contact.WorkPhone2 = "work2 12351234";
240 contact.HomePhone = "home 12341234";
241 contact.HomePhone2 = "home2 4234234";
242 contact.MobilePhone = "mobile 88888";
243 contact.MobilePhone2 = "mobile2 23424";
244 contact.Pager = "pager 123412342";
245 contact.PIN = "PIN 1234";
246 contact.Radio = "CBC";
247 contact.OtherPhone = "other 12341234";
248 contact.FirstName = "Chris";
249 contact.LastName = "Frey";
250 contact.Company = "Company naaaaaaaaaaame";
251 contact.DefaultCommunicationsMethod = "E";
252 contact.JobTitle = "Programmer";
253 contact.PublicKey = "seeeecrit";
254 contact.URL = "http://netdirect.ca/~cdfrey/";
255 contact.Prefix = "nothing";
256 contact.Notes = "This is a notes field that can hold mega data";
257 contact.UserDefined1 = "user 1";
258 contact.UserDefined2 = "user 2";
259 contact.UserDefined3 = "user 3";
260 contact.UserDefined4 = "user 4";
261 contact.Image = "no image available";
262 contact.Nickname = "LRU";
263 Date d;
264 d.FromYYYYMMDD("20120101");
265 contact.Birthday = d;
266 contact.WorkAddress.Address1 = "work address 1";
267 contact.WorkAddress.Address2 = "work address 2";
268 contact.WorkAddress.Address3 = "work address 3";
269 contact.WorkAddress.City = "work city";
270 contact.WorkAddress.Province = "work Province";
271 contact.WorkAddress.PostalCode = "work PostalCode";
272 contact.WorkAddress.Country = "work Country";
273 contact.HomeAddress.Address1 = "home address 1";
274 contact.HomeAddress.Address2 = "home address 2";
275 contact.HomeAddress.Address3 = "home address 3";
276 contact.HomeAddress.City = "home city";
277 contact.HomeAddress.Province = "home Province";
278 contact.HomeAddress.PostalCode = "home PostalCode";
279 contact.HomeAddress.Country = "home Country";
280 contact.Categories.push_back("office");
281 contact.Categories.push_back("entertainment");
283 FieldHandler<Contact> handler(contact);
284 ForEachField(Contact::GetFieldHandles(), handler);
286 cout << "================================================" << endl;
288 VirtualFieldHandler vhandler;
289 ForEachFieldValue(contact, vhandler);
292 void BlankTestAll()
294 VirtualFieldHandler vhandler;
296 cout << "All parsers......" << endl;
298 #undef HANDLE_PARSER
299 #define HANDLE_PARSER(tname) \
300 cout << "Record: " << #tname << endl; \
301 cout << "===================================================" << endl;\
302 tname obj##tname; \
303 FieldHandler<tname> fh##tname(obj##tname); \
304 cout << "By member pointer..." << endl; \
305 ForEachField(tname::GetFieldHandles(), fh##tname); \
306 cout << "\nBy value..." << endl; \
307 ForEachFieldValue(obj##tname, vhandler); \
308 cout << endl;
310 ALL_KNOWN_PARSER_TYPES
313 cout << "All builders........" << endl;
315 #undef HANDLE_BUILDER
316 #define HANDLE_BUILDER(tname) \
317 cout << "Record: " << #tname << endl; \
318 cout << "===================================================" << endl;\
319 tname bobj##tname; \
320 FieldHandler<tname> bfh##tname(bobj##tname); \
321 cout << "By member pointer..." << endl; \
322 ForEachField(tname::GetFieldHandles(), bfh##tname); \
323 cout << "\nBy value..." << endl; \
324 ForEachFieldValue(bobj##tname, vhandler); \
325 cout << endl;
327 ALL_KNOWN_BUILDER_TYPES
330 int main()
332 TestContact();
333 BlankTestAll();