- opensync plugin fixes:
[barry.git] / opensync-plugin / src / vbase.h
blob8eaa434ac4d109c05f5cce7247c592bedd5d9ad3
1 ///
2 /// \file vbase.h
3 /// Base class for vformat support
4 ///
6 /*
7 Copyright (C) 2006-2007, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_SYNC_VBASE_H__
23 #define __BARRY_SYNC_VBASE_H__
25 #include <string>
26 #include <stdexcept>
27 #include "vformat.h"
29 // forward declarations
30 class BarryEnvironment;
33 // FIXME - possibly scrap this in favour of opensync's xml time routines,
34 // but this will require an overhaul of the plugin itself.
35 class vTimeZone
37 public:
38 struct ZoneBlock
40 bool m_daylightSaving;
41 std::string m_name;
42 std::string m_dtstart;
43 std::string m_tzoffsetfrom;
44 std::string m_tzoffsetto;
47 private:
48 bool m_valid;
50 public:
51 std::string m_id;
53 public:
54 vTimeZone();
55 ~vTimeZone();
57 void Clear();
59 bool IsValid();
61 /// used for comparing by TZID
62 bool operator==(const std::string &tzid) const;
66 // A special smart pointer for vformat pointer handling.
67 // Behaves like std::auto_ptr<> in that only one object
68 // at a time owns the pointer, and destruction frees it.
69 template <class T, class FT, void (*FreeFunc)(FT *pt)>
70 class vSmartPtr
72 mutable T *m_pt;
74 public:
75 vSmartPtr() : m_pt(0) {}
76 vSmartPtr(T *pt) : m_pt(pt) {}
77 vSmartPtr(const vSmartPtr &sp) : m_pt(sp.m_pt)
79 sp.m_pt = 0;
81 ~vSmartPtr()
83 if( m_pt )
84 FreeFunc(m_pt);
87 vSmartPtr& operator=(T *pt)
89 Extract();
90 m_pt = pt;
91 return *this;
94 vSmartPtr& operator=(const vSmartPtr &sp)
96 Extract();
97 m_pt = sp.Extract();
98 return *this;
101 T* Extract()
103 T *rp = m_pt;
104 m_pt = 0;
105 return rp;
108 T* Get()
110 return m_pt;
114 typedef vSmartPtr<VFormatAttribute, VFormatAttribute, &vformat_attribute_free> vAttrPtr;
115 typedef vSmartPtr<VFormatParam, VFormatParam, &vformat_attribute_param_free> vParamPtr;
116 typedef vSmartPtr<char, void, &g_free> gStringPtr;
120 // vAttr
122 /// Class for reading a VFormatAttribute. Reading does not require
123 /// memory management, so none is done.
125 class vAttr
127 VFormatAttribute *m_attr;
129 public:
130 vAttr()
131 : m_attr(0)
135 vAttr(VFormatAttribute *attr)
136 : m_attr(attr)
140 vAttr& operator=(VFormatAttribute *attr)
142 m_attr = attr;
143 return *this;
146 VFormatAttribute* Get() { return m_attr; }
148 // These functions do not throw an error if the value
149 // is NULL or does not exist (for example, if you ask for
150 // value #5 and there are only 4).
151 std::string GetName();
152 std::string GetValue(int nth = 0);
153 std::string GetParam(const char *name, int nth = 0);
158 // vBase
160 /// Base class containing vformat helper API.
162 class vBase
164 // internal data for managing the vformat
165 VFormat *m_format;
167 public:
168 // FIXME - if you put this class in the Barry library,
169 // you'll need to change the class hierarchy
170 class ConvertError : public std::runtime_error
172 public:
173 ConvertError(const std::string &msg) : std::runtime_error(msg) {}
176 protected:
177 vBase();
178 virtual ~vBase();
180 VFormat* Format() { return m_format; }
181 const VFormat* Format() const { return m_format; }
182 void SetFormat(VFormat *format);
184 void Clear();
186 vAttrPtr NewAttr(const char *name);
187 vAttrPtr NewAttr(const char *name, const char *value);
188 void AddAttr(vAttrPtr attr);
189 void AddValue(vAttrPtr &attr, const char *value);
190 void AddParam(vAttrPtr &attr, const char *name, const char *value);
192 std::string GetAttr(const char *attrname, const char *block = 0);
193 vAttr GetAttrObj(const char *attrname, int nth = 0, const char *block = 0);
196 #endif