Updated copyright dates for 2009
[barry/progweb.git] / opensync-plugin / src / vbase.h
blob7660287339e2a2a25b55ac28788dd73056d59a1f
1 ///
2 /// \file vbase.h
3 /// Base class for vformat support
4 ///
6 /*
7 Copyright (C) 2006-2009, 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<b_VFormatAttribute, b_VFormatAttribute, &b_vformat_attribute_free> vAttrPtr;
115 typedef vSmartPtr<b_VFormatParam, b_VFormatParam, &b_vformat_attribute_param_free> vParamPtr;
116 typedef vSmartPtr<char, void, &g_free> gStringPtr;
120 // vAttr
122 /// Class for reading a b_VFormatAttribute. Reading does not require
123 /// memory management, so none is done.
125 class vAttr
127 b_VFormatAttribute *m_attr;
129 public:
130 vAttr()
131 : m_attr(0)
135 vAttr(b_VFormatAttribute *attr)
136 : m_attr(attr)
140 vAttr& operator=(b_VFormatAttribute *attr)
142 m_attr = attr;
143 return *this;
146 b_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);
154 std::string GetAllParams(const char *name);
159 // vBase
161 /// Base class containing vformat helper API.
163 class vBase
165 // internal data for managing the vformat
166 b_VFormat *m_format;
168 public:
169 // FIXME - if you put this class in the Barry library,
170 // you'll need to change the class hierarchy
171 class ConvertError : public std::runtime_error
173 public:
174 ConvertError(const std::string &msg) : std::runtime_error(msg) {}
177 protected:
178 vBase();
179 virtual ~vBase();
181 b_VFormat* Format() { return m_format; }
182 const b_VFormat* Format() const { return m_format; }
183 void SetFormat(b_VFormat *format);
185 void Clear();
187 vAttrPtr NewAttr(const char *name);
188 vAttrPtr NewAttr(const char *name, const char *value);
189 void AddAttr(vAttrPtr attr);
190 void AddValue(vAttrPtr &attr, const char *value);
191 void AddParam(vAttrPtr &attr, const char *name, const char *value);
193 std::string GetAttr(const char *attrname, const char *block = 0);
194 vAttr GetAttrObj(const char *attrname, int nth = 0, const char *block = 0);
197 #endif