00001
00002
00003
#ifndef s11n_CHILDRENHOLDER_H_INCLUDED
00004
#define s11n_CHILDRENHOLDER_H_INCLUDED 1
00005
00006
#include <string>
00007
#include <list>
00008
#include <map>
00009
#include <vector>
00010
00011
00012
namespace s11n
00013 {
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 template <
class PType,
class CType >
class children_holder
00032 {
00033
public:
00034
00035
00036
00037
00038 typedef PType
parent_type;
00039
00040
00041
00042
00043
00044 typedef CType
child_type;
00045
00046
00047
00048
00049 typedef std::list < child_type * >
list_type;
00050
00051
00052
00053
00054 typedef children_holder < parent_type, child_type >
ThisType;
00055
00056
00057
00058
00059 typedef typename list_type::iterator
iterator;
00060
00061
00062
00063 typedef typename list_type::const_iterator
const_iterator;
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 static ThisType::list_type *
child_list(
const ThisType::parent_type * parent,
int creationPolicy = 0 )
00084 {
00085
if ( !parent )
return NULL;
00086
static ThisType::map_type & cmap = parentChildMap();
00087
typename map_type::const_iterator it = cmap.find( parent );
00088
if ( cmap.end() != it )
return ( *it ).second;
00089
if ( 0 == creationPolicy )
return NULL;
00090
list_type *cl =
new list_type();
00091 cmap[parent] = cl;
00092
return cl;
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 static bool unmap_parent(
const ThisType::parent_type * parent )
00112 {
00113
if ( !parent )
return false;
00114
static ThisType::map_type & cmap = parentChildMap();
00115
typename ThisType::map_type::iterator it = cmap.find( parent );
00116
if ( it == cmap.end() )
return false;
00117 cmap.erase( parent );
00118
return true;
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 static bool cleanup_parent(
const ThisType::parent_type * parent )
00131 {
00132
if ( !parent )
00133
return false;
00134
static ThisType::map_type & cmap = parentChildMap();
00135
typename ThisType::map_type::iterator it = cmap.find( parent );
00136
if ( it == cmap.end() )
00137 {
00138
00139
return false;
00140 }
00141
typename ThisType::list_type * li = ( *it ).second;
00142
if ( !unmap_parent( parent ) )
00143 {
00144
return false;
00145 }
00146
typename ThisType::list_type::iterator vit;
00147
typename ThisType::child_type * child = 0;
00148
for ( vit = li->begin(); li->begin() != li->end(); )
00149 {
00150
00151
00152 child = ( *vit );
00153 li->erase( vit );
00154
delete( child );
00155 child = 0;
00156 }
00157
delete( li );
00158
return true;
00159 }
00160
00161
00162
private:
00163
typedef std::map < const ThisType::parent_type *, ThisType::list_type * > map_type;
00164
00165
00166
00167
00168
00169
static map_type & parentChildMap()
00170 {
00171
static map_type meyers;
00172
return meyers;
00173 }
00174
00175
00176 };
00177
00178
00179 };
00180
#endif // s11n_CHILDRENHOLDER_H_INCLUDED