00001 #ifndef s11n_DATA_ALGO_H_INCLUDED
00002 #define s11n_DATA_ALGO_H_INCLUDED
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <string>
00014 #include <list>
00015 #include <iterator>
00016 #include <algorithm>
00017 #include <cassert>
00018
00019 #include <s11n/debuggering_macros.h>
00020 #include <s11n/to_string.h>
00021 #include <s11n/functor.h>
00022 #include <s11n/pointer_stripper.h>
00023
00024 #include <s11n/abstract_creator.h>
00025
00026
00027
00028
00029
00030
00031 namespace s11n {
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 template <typename NodeType, typename ChildType>
00045 void add_child( NodeType & parent, ChildType * ch )
00046 {
00047 parent.children().push_back( ch );
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 template <typename NodeType>
00067 NodeType & create_child( NodeType & parent,
00068 const std::string nodename )
00069 {
00070 NodeType * n = new NodeType;
00071 n->name( nodename );
00072 parent.children().push_back( n );
00073 return *n;
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 template <typename NodeT, typename DestContainerT>
00100 size_t find_children_by_name( const NodeT & parent,
00101 const std::string & name,
00102 DestContainerT & target )
00103 {
00104 size_t c = target.size();
00105 s11n::copy_if( parent.children().begin(),
00106 parent.children().end(),
00107 std::insert_iterator<DestContainerT>( target,
00108 target.begin() ),
00109 s11n::same_name<NodeT>( name )
00110 );
00111
00112 return target.size() - c;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122 template <typename NodeT>
00123 const NodeT *
00124 find_child_by_name( const NodeT & parent, const std::string & name )
00125 {
00126 typedef typename NodeT::child_list_type::const_iterator CIT;
00127 CIT it = std::find_if( parent.children().begin(),
00128 parent.children().end(),
00129 s11n::same_name<NodeT>( name )
00130 );
00131 return (parent.children().end() == it) ? 0 : *it;
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 template <typename NodeT>
00146 NodeT *
00147 find_child_by_name( NodeT & parent, const std::string & name )
00148 {
00149 typedef typename NodeT::child_list_type::iterator IT;
00150 IT it = std::find_if( parent.children().begin(),
00151 parent.children().end(),
00152 s11n::same_name<NodeT>( name )
00153 );
00154 return (parent.children().end() == it) ? 0 : *it;
00155 }
00156
00157
00158 }
00159
00160 #endif // s11n_DATA_ALGO_H_INCLUDED