summaryrefslogtreecommitdiffabout
path: root/include/konforka
authorMichael Krelin <hacker@klever.net>2005-01-29 19:41:34 (UTC)
committer Michael Krelin <hacker@klever.net>2005-01-29 19:41:34 (UTC)
commitee7ac58694c16cdd922a6eeddbe1a2eba0da7b4e (patch) (unidiff)
treebe8029ce4a68870d991651927a27df8ee9d89617 /include/konforka
parentd851b00e16839d6ec14da3a5c442be62ca868bba (diff)
downloadkonforka-ee7ac58694c16cdd922a6eeddbe1a2eba0da7b4e.zip
konforka-ee7ac58694c16cdd922a6eeddbe1a2eba0da7b4e.tar.gz
konforka-ee7ac58694c16cdd922a6eeddbe1a2eba0da7b4e.tar.bz2
initial commit.0.0
Diffstat (limited to 'include/konforka') (more/less context) (ignore whitespace changes)
-rw-r--r--include/konforka/basic_wrapper.h118
-rw-r--r--include/konforka/exception.h203
-rw-r--r--include/konforka/pointer_map.h105
-rw-r--r--include/konforka/pqxx_pile.h59
-rw-r--r--include/konforka/resource_pile.h118
-rw-r--r--include/konforka/responsible_wrapper.h99
6 files changed, 702 insertions, 0 deletions
diff --git a/include/konforka/basic_wrapper.h b/include/konforka/basic_wrapper.h
new file mode 100644
index 0000000..909f8b6
--- a/dev/null
+++ b/include/konforka/basic_wrapper.h
@@ -0,0 +1,118 @@
1#ifndef __KONFORKA_BASIC_WRAPPER_H
2#define __KONFORKA_BASIC_WRAPPER_H
3
4/**
5 * @file
6 * @brief The konforka::basic_wrapper class declaration.
7 */
8
9#include <konforka/exception.h>
10
11namespace konforka {
12
13 /**
14 * @brief The basic wrapper class.
15 *
16 */
17 template<typename T>
18 class basic_wrapper {
19 public:
20 /**
21 * The type of wrapped content.
22 */
23 typedef T content_type;
24
25 /**
26 * The content wrapped into the instance.
27 */
28 content_type opkele;
29 /**
30 * This boolean indicates whether the object has a content
31 * attached to it.
32 */
33 bool bopkele;
34
35 /**
36 * Default constructors creates the object with no content
37 * associated with it.
38 */
39 basic_wrapper()
40 : bopkele(false) { }
41 /**
42 * The constructor associates the content give with the object.
43 * @param o the content being attached to the object.
44 */
45 basic_wrapper(content_type o)
46 : opkele(o), bopkele(true) { }
47 /**
48 * Virtual destructor doing nothing in this class.
49 */
50 virtual ~basic_wrapper() { }
51
52 /**
53 * Attaches the given content to the object.
54 * @param o the content being attached to the object.
55 */
56 void attach(content_type o) {
57 opkele = o;
58 bopkele = true;
59 }
60
61 /**
62 * Test whether the object has content attached.
63 * @return naturally, returns true if yes.
64 */
65 bool is() const {
66 return bopkele;
67 }
68
69 /**
70 * Throws an exception in case the object lacks content
71 * attached.
72 */
73 void ensure() const {
74 if(!is())
75 throw konforka::exception(CODEPOINT,"no content attached");
76 }
77
78 /**
79 * Get the content attached to the object. It would throw an
80 * exception if there is no content attached.
81 * @return reference to the content.
82 */
83 virtual content_type& get_content() {
84 ensure();
85 return opkele;
86 }
87 /**
88 * Get the content attached to the object. It would throw an
89 * exception if there is no content attached.
90 * @return const reference to the content.
91 */
92 virtual const content_type& get_content() const {
93 ensure();
94 return opkele;
95 }
96
97 /**
98 * Casts the object to the reference to the content type,
99 * throwing an exception if there's no content associated with
100 * an object.
101 * @return reference to the content attached.
102 * @see get_content()
103 */
104 operator const content_type&(void) const { return get_content(); }
105 /**
106 * Casts the object to the const reference to the content type,
107 * throwing an exception if there's no content associated with
108 * an object.
109 * @return reference to the content attached.
110 * @see get_content()
111 */
112 operator content_type&(void) { return get_content(); }
113 };
114
115}
116
117#endif /* __KONFORKA_BASIC_WRAPPER_H */
118/* vim:set ft=cpp: */
diff --git a/include/konforka/exception.h b/include/konforka/exception.h
new file mode 100644
index 0000000..5e0bf96
--- a/dev/null
+++ b/include/konforka/exception.h
@@ -0,0 +1,203 @@
1#ifndef __KONFORKA_EXCEPTION_H
2#define __KONFORKA_EXCEPTION_H
3
4#include <exception>
5#include <string>
6#include <list>
7
8/**
9 * @file
10 * @brief exception-related stuff.
11 *
12 * Basic exception-related declarations and definitions.
13 */
14
15/**
16 * @def CODEPOINT
17 * the convenience definition of the parameters passed to the
18 * konforka::code_point constructor.
19 */
20 #defineCODEPOINT __FILE__,__PRETTY_FUNCTION__,__LINE__
21/**
22 * @def NOCODEPOINT
23 * the convenience definition for the codepoint denoting no particular point in
24 * code.
25 */
26#define NOCODEPOINT "no information"
27
28/**
29 * @brief The main konforka namespace.
30 */
31namespace konforka {
32 using std::string;
33 using std::list;
34
35 /**
36 * @brief Pinpoint the code context.
37 *
38 * Class, holding the point in code, for instance, where the exception
39 * occured.
40 */
41 class code_point {
42 public:
43 /**
44 * The string describing the point in code.
45 */
46 string where;
47 /**
48 * The file (as provided by __FILE__) if available.
49 */
50 string file;
51 /**
52 * The function name (as provided by __PRETTY_FUNCTION__) if
53 * available.
54 */
55 string function;
56 /**
57 * The line number (as provided by __LINE__) if available.
58 */
59 int line;
60
61 /**
62 * Constructs the object, using only textual description of the
63 * point in code (no file, function, line information).
64 * @param w the description of the point in code.
65 */
66 code_point(const string& w);
67 /**
68 * Constructs the object, specifying the exact position in code.
69 * @param fi source file name.
70 * @param fu function name.
71 * @param l the line number.
72 */
73 code_point(const string& fi,const string& fu,int l);
74
75 /**
76 * Extract the information on the point in code.
77 * @return the reference to the character string.
78 * @see where
79 */
80 const char *c_str() const throw();
81
82 /**
83 * Build the textual description from the broken down information on
84 * the point in code.
85 * @see where
86 * @see file
87 * @see function
88 * @see line
89 */
90 void make_where();
91 };
92
93 /**
94 * @brief The basic exception class.
95 */
96 class exception : public std::exception {
97 public:
98 /**
99 * The string, containing the description of exception.
100 */
101 string _what;
102 /**
103 * Reference to the point in code where the exception has occured.
104 */
105 code_point _where;
106 /**
107 * In case the exception has been rethrown a number of times, here
108 * we can trace where was it seen (a kind of backtrace).
109 */
110 list<code_point> _seen;
111
112 /**
113 * The simple constructor, only providing textual information on the
114 * exception nature and the point in code where the exception has
115 * occured.
116 * @param whe the point in code.
117 * @param wha the description of exception.
118 */
119 exception(const string& whe, const string& wha);
120 /**
121 * The constructor, storing elaborate information on where the
122 * exception has occured.
123 * @param fi source file name.
124 * @param fu function name.
125 * @param l line number.
126 * @param w the error message.
127 * @see #CODEPOINT
128 */
129 exception(const string& fi,const string& fu,int l,const string& w);
130 virtual ~exception() throw();
131
132 /**
133 * Extract the textual information on the point in code where
134 * exception has occured.
135 * @return the string describing point in code where exception has
136 * occured.
137 */
138 virtual const char* where() const throw();
139 /**
140 * Extract the textual information on the nature of the exception.
141 * @return the error message.
142 */
143 virtual const char* what() const throw();
144
145 /**
146 * Register the point in code (described by the string) in the
147 * 'backtrace' list.
148 * @param w the description of the point in code.
149 */
150 void see(const string& w);
151 /**
152 * Register the point in code in the 'backtrace' list.
153 * @param fi souce file name.
154 * @param fu function name.
155 * @param l line number.
156 * @see CODEPOINT
157 */
158 void see(const string& fi,const string& fu,int l);
159 };
160
161 /**
162 * @brief errno-holding exception.
163 *
164 * The exception object storing the information provided by the errno
165 * variable.
166 */
167 class system_error : public exception {
168 public:
169 /**
170 * The value of errno.
171 */
172 int _errno;
173
174 /**
175 * Construct the exception object storing plain text information on
176 * the point in code.
177 * @param whe the description of point in code.
178 * @param wha the error message.
179 */
180 system_error(const string& whe,const string& wha);
181 /**
182 * Construct the exception object storing elaborate information on
183 * the point in code where it has occured.
184 * @param fi source file name.
185 * @param fu function name.
186 * @param l line number.
187 * @param w the error message.
188 * @see CODEPOINT
189 */
190 system_error(const string& fi,const string& fu,int l,const string& w);
191 virtual ~system_error() throw();
192
193 /**
194 * Return the information on the system error recorded.
195 * @return the string describing the error occured.
196 */
197 virtual const char* why() const throw();
198 };
199
200}
201
202#endif /* __KONFORKA_EXCEPTION_H */
203/* vim:set ft=cpp: */
diff --git a/include/konforka/pointer_map.h b/include/konforka/pointer_map.h
new file mode 100644
index 0000000..d706e71
--- a/dev/null
+++ b/include/konforka/pointer_map.h
@@ -0,0 +1,105 @@
1#ifndef __KONFORKA_POINTER_MAP_H
2#define __KONFORKA_POINTER_MAP_H
3
4#include <typeinfo>
5
6/**
7 * @file
8 * @brief mapping of pointers.
9 *
10 * The support for global mapping of pointers. Useful when using third-party
11 * libraries callbacks when the library does not provide mechanism for passing
12 * along custom context-dependent data.
13 */
14
15namespace konforka {
16
17 /**
18 * @brief internally used actual implementation of mapping pointer.
19 *
20 * @param tf the typeid of the key pointer.
21 * @param pf the key pointer.
22 * @param tt the typeid of the value pointer.
23 * @param pt the value pointer.
24 */
25 void _map_pointer(const type_info& tf,void *pf,const type_info& tt,void *pt);
26 /**
27 * @brief internally used actual implementation of destroying mapped
28 * pointer.
29 *
30 * @param tf the typeid of the key pointer.
31 * @param pf the key pointer.
32 * @param tt the typeid of the value pointer.
33 * @param pt the value pointer.
34 */
35 void _unmap_pointer(const type_info& tf,void *pf,const type_info& tt,void *pt);
36 /**
37 * @brief internally used actual implementation of retrieving mapped
38 * pointer.
39 *
40 * @param tf the typeid of the key pointer.
41 * @param pf the key pointer.
42 * @param tt the typeid of the value pointer.
43 * @return the value.
44 */
45 void *_mapped_pointer(const type_info& tf,void *pf,const type_info& tt);
46
47 /**
48 * @brief the object, maintaining mapped pointer.
49 *
50 * @param from_t the key type.
51 * @param to_t the value type.
52 */
53 template<typename from_t,typename to_t>
54 class map_pointer {
55 public:
56 /**
57 * stored key.
58 */
59 from_t _from;
60 /**
61 * stored value.
62 */
63 to_t _to;
64 /**
65 * flag, specifying that the object is currently mapped.
66 */
67 bool bmapped;
68
69 /**
70 * @brief constructs the object, mapping the key/value pair.
71 *
72 * @param f the key.
73 * @param t the value.
74 */
75 map_pointer(from_t f,to_t t)
76 : _from(f), _to(t), bmapped(false) {
77 _map_pointer(typeid(from_t),_from,typeid(to_t),_to);
78 bmapped=true;
79 }
80 /**
81 * @brief destructor unmaps the key/value pair stored.
82 */
83 ~map_pointer() {
84 if(bmapped)
85 _unmap_pointer(typeid(from_t),_from,typeid(to_t),_to);
86 }
87 };
88
89 /**
90 * The template function for pointer retrieval.
91 *
92 * @param from_t the key type.
93 * @param to_t the value type.
94 * @param f the key.
95 * @return the value.
96 */
97 template<typename from_t,typename to_t>
98 to_t mapped_pointer(from_t f) {
99 return (to_t)_mapped_pointer(typeid(from_t),f,typeid(to_t));
100 }
101
102}
103
104#endif /* __KONFORKA_POINTER_MAP_H */
105/* vim:set ft=cpp: */
diff --git a/include/konforka/pqxx_pile.h b/include/konforka/pqxx_pile.h
new file mode 100644
index 0000000..fdfaed6
--- a/dev/null
+++ b/include/konforka/pqxx_pile.h
@@ -0,0 +1,59 @@
1#ifndef __KONFORKA_PQXX_PILE_H
2#define __KONFORKA_PQXX_PILE_H
3
4#include <pqxx/connection>
5#include <konforka/resource_pile.h>
6
7/**
8 * @file
9 * @brief libpqxx-based postgresql connections pile.
10 */
11
12namespace konforka {
13
14 /**
15 * @brief the base for pqxx-based connection classes.
16 *
17 * @param pqxxc_t the type of libpqxx connection (pqxx::connection,
18 * pqxx::lazyconnection or pqxx::asyncconnection).
19 */
20 template<typename pqxxc_t>
21 class pqxx_piled_connection_base : public resource_pile_base<string,pqxxc_t*, resource_pile_generic_ptr_factory<string,pqxxc_t> > {
22 public:
23 /**
24 * @brief the constractor based on connection info.
25 *
26 * @param ci connection info string.
27 */
28 pqxx_piled_connection_base(const string& ci)
29 : resource_pile_base<string,pqxxc_t*, resource_pile_generic_ptr_factory<string,pqxxc_t> >(ci) { }
30 ~pqxx_piled_connection_base() { this->drop(); }
31
32 /**
33 * @brief cast the object to the reference to the corresponding
34 * libpqxx type.
35 */
36 operator pqxxc_t&(void) { return *this->get_content(); }
37 /**
38 * @brief cast the object to the const reference to the
39 * corresponding libpqxx type.
40 */
41 operator const pqxxc_t&(void) const { return *this->get_content(); }
42 };
43
44 /**
45 * @brief the implementation for piling pqxx::connection objects.
46 */
47 typedef pqxx_piled_connection_base<pqxx::connection> pqxx_piled_connection;
48 /**
49 * @brief the implementation for piling pqxx::lazyconnection objects.
50 */
51 typedef pqxx_piled_connection_base<pqxx::lazyconnection> pqxx_piled_lazy_connection;
52 /**
53 * @brief the implementation for piling pqxx::asyncconnection objects.
54 */
55 typedef pqxx_piled_connection_base<pqxx::asyncconnection> pqxx_piled_async_connection;
56}
57
58#endif /* __KONFORKA_PQXX_PILE_H */
59/* vim:set ft=cpp: */
diff --git a/include/konforka/resource_pile.h b/include/konforka/resource_pile.h
new file mode 100644
index 0000000..d76a07b
--- a/dev/null
+++ b/include/konforka/resource_pile.h
@@ -0,0 +1,118 @@
1#ifndef __KONFORKA_RESOURCE_PILE_H
2#define __KONFORKA_RESOURCE_PILE_H
3
4#include <map>
5#include <konforka/responsible_wrapper.h>
6
7/**
8 * @file
9 * @brief resource-piling base support.
10 *
11 * Base for implementing resource-piling.
12 */
13
14namespace konforka {
15 using std::multimap;
16 using std::pair;
17
18 /**
19 * @brief template base class for resource piling.
20 *
21 * @param key_t the type used for keying resources.
22 * @param value_t the type of resource itself.
23 * @param factory_t the factory class, providing value_t allocate(key_t)
24 * static member.
25 */
26 template<typename key_t,typename value_t,typename factory_t>
27 class resource_pile_base : public responsible_wrapper<value_t> {
28 /**
29 * @brief the type of the pile container itself.
30 */
31 typedef multimap<key_t,value_t> pile_t;
32 /*
33 * @brief the pile of resources.
34 */
35 static pile_t pile;
36 public:
37 /**
38 * @brief stored value for the key associated with the resource
39 * contained.
40 */
41 key_t _key;
42
43 /**
44 * @brief default constructor fetches or allocates resource.
45 *
46 * @param k the key for resource.
47 * @see allocate
48 */
49 resource_pile_base(const key_t& k) {
50 allocate(k);
51 }
52 /**
53 * @brief destructor releases the resource back to pile.
54 */
55 virtual ~resource_pile_base() { this->drop(); }
56
57 /**
58 * @brief this is where the resource is handed back to pile.
59 */
60 void release() {
61 pile.insert(pair<key_t,value_t>(_key,this->opkele));
62 }
63
64 /**
65 * @brief fetch from pile or allocate the resource.
66 *
67 * Try to see if we have a piled resource keyed to the argument. If
68 * we do -- fetch it from the pile, otherwise allocate anew.
69 *
70 * @param k the key for resource.
71 */
72 void allocate(const key_t& k) {
73 this->drop();
74 typename pile_t::iterator i = pile.find(k);
75 if(i==pile.end()) {
76 this->attach(factory_t::allocate(k));
77 }else{
78 this->attach(i->second);
79 try{ pile.erase(i); }catch(...){ _key = k; throw; }
80 }
81 _key = k;
82 }
83
84 };
85
86 template<typename key_t,typename value_t,typename factory_t>
87 typename resource_pile_base<key_t,value_t,factory_t>::pile_t
88 resource_pile_base<key_t,value_t,factory_t>::pile;
89
90 /**
91 * @brief the generic single parameter new-based resource factory.
92 *
93 * The generic resource factory using new as a way to allocate resource
94 * using the single-parameter constructor.
95 *
96 * @param key_t the key type.
97 * @param value_t the resource type.
98 *
99 * @see resource_pile_base
100 */
101 template<typename key_t,typename value_t>
102 struct resource_pile_generic_ptr_factory {
103 public:
104 /**
105 * @brief allocate the resource using new.
106 *
107 * @param k the key.
108 * @return pointer to the newly allocated object.
109 */
110 static value_t *allocate(const key_t& k) {
111 return new value_t(k);
112 }
113 };
114
115}
116
117#endif /* __KONFORKA_RESOURCE_PILE_H */
118/* vim:set ft=cpp: */
diff --git a/include/konforka/responsible_wrapper.h b/include/konforka/responsible_wrapper.h
new file mode 100644
index 0000000..374ad45
--- a/dev/null
+++ b/include/konforka/responsible_wrapper.h
@@ -0,0 +1,99 @@
1#ifndef __KONFORKA_RESPONSIBLE_WRAPPER_H
2#define __KONFORKA_RESPONSIBLE_WRAPPER_H
3
4/**
5 * @file
6 * @brief The konforka::responsible_wrapper class declaration.
7 */
8
9#include <konforka/basic_wrapper.h>
10
11namespace konforka {
12
13 /**
14 * @brief The auto-cleanup wrapper class.
15 *
16 * The wrapper class that may feel responsible for releasing the resources
17 * associated with the content attached.
18 *
19 */
20 template<typename T>
21 class responsible_wrapper : public basic_wrapper<T> {
22 public:
23 /**
24 * The type of wrapped content.
25 */
26 typedef typename basic_wrapper<T>::content_type content_type;
27 /**
28 * Flag indicating whether the object feels responsible for
29 * releasing resources associated with the content.
30 */
31 bool bresponsible;
32
33 /**
34 * Default constructor creates the object with no content
35 * attached.
36 */
37 responsible_wrapper()
38 : basic_wrapper<content_type>() { }
39 /**
40 * Constructor, associating the content with the instance.
41 * @param o the content.
42 * @param br indicates whether resources associated with the
43 * content should be released.
44 */
45 responsible_wrapper(content_type o,bool br=true)
46 : basic_wrapper<content_type>(o), bresponsible(br) { }
47 /**
48 * Destructor releases resources associated with the content
49 * attached (if any), if the instance feels responsible for the
50 * content.
51 */
52 virtual ~responsible_wrapper() { drop(); }
53
54 /**
55 * Attaches the given content to the object.
56 * @param o the content.
57 * @param br indicates whether the object should feel
58 * responsible for releasing the content.
59 */
60 void attach(content_type o,bool br=true) {
61 drop();
62 basic_wrapper<content_type>::attach(o);
63 bresponsible = true;
64 }
65
66 /**
67 * 'empties' object, releasing resources associated with the
68 * content if it feels responsible.
69 */
70 virtual void drop() {
71 if(!this->is())
72 return;
73 if(bresponsible)
74 release();
75 this->bopkele = false;
76 }
77
78 /**
79 * Detaches the content from the object without releasing
80 * resources even if feels responsible for it.
81 * @return the content attached.
82 */
83 virtual content_type detach() {
84 this->ensure();
85 this->bopkele = false;
86 return this->opkele;
87 }
88
89 /**
90 * Pure virtual provided for derived classes to override for
91 * doing whatever it takes to release resources associated with
92 * the content.
93 */
94 virtual void release() = 0;
95 };
96}
97
98#endif /* __KONFORKA_RESPONSIBLE_WRAPPER_H */
99/* vim:set ft=cpp: */