00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00031 #ifndef _API_DELTA_H_
00032 #define _API_DELTA_H_
00033
00034 #include <api_deltabase.h>
00035 #include <search_node.h>
00036 #include <dbtools.h>
00037 #include <db_method>
00038
00039 namespace coco {
00040
00046 static std::string ___empty_string;
00047
00049
00054 class delta_get_action : public vdbl::method<const std::string&>
00055 {
00056 private:
00058 typedef vdbl::method<const std::string&> _Base;
00060 vdbl::colid cid;
00062 const vdbl::row* _r;
00063
00064 public:
00066 typedef vdbl::context context;
00068 typedef std::string return_type;
00069
00071 delta_get_action(vdbl::colid _c) : _Base(), cid(_c), _r() {}
00073 delta_get_action(const delta_get_action& _i) : _Base(_i), cid(_i.cid),
00074 _r(_i._r) {}
00076 virtual ~delta_get_action() {}
00077
00079 const std::string& operator() () const;
00081 const std::string& def() const { return operator()(); }
00083 void setcontext(const context* c, const vdbl::row* r) { _r = r; }
00084 };
00085
00086
00087
00088 inline bool delta_base::apply3(work_node& _x, const work_node& _y,
00089 undelta_base*& _u, const delta_id& _d) const
00090 { _x = _y; return apply(_x, _u, _d); }
00091
00092
00093
00094 inline bool undelta_base::unapply3(work_node& _x, const work_node& _y,
00095 const delta_id& _d) const
00096 { _x = _y; return unapply(_x, _d); }
00097
00098 inline const std::string& delta_get_action::operator() () const
00099 {
00100 if(!_r)
00101 return ___empty_string;
00102 bool error;
00103 const vdbl::col& _c(_r->get_col(cid, error));
00104 if(error)
00105 return ___empty_string;
00106 const delta* _d;
00107 _c.get_ptr(_d);
00108 if(_d == NULL)
00109 return ___empty_string;
00110 return _d->get_action();
00111 }
00112 inline delta::delta(const delta_base& __d)
00113 { _d = __d.new_copy(); }
00114 inline delta::delta(const delta& __d)
00115 { _d = __d._d->new_copy();
00116 #if DEBUG_DELTA
00117 std::cerr << "Called delta copy constructor" << std::endl;
00118 #endif
00119 }
00120 inline delta::~delta()
00121 { if(_d) delete _d;
00122 #if DEBUG_DELTA
00123 std::cerr << "Called delta destructor" << std::endl;
00124 #endif
00125 }
00126 inline delta& delta::operator=(const delta& __d)
00127 {
00128 if(_d) delete _d;
00129 _d = __d._d->new_copy();
00130 #if DEBUG_DELTA
00131 std::cerr << "Called delta assignment operator" << std::endl;
00132 #endif
00133 return *this;
00134 }
00135
00136 inline const std::string& delta::get_action() const { return _d->get_action(); }
00137 inline const delta_base* delta::get_base() const { return _d; }
00138
00139 inline bool delta::apply(work_node& _x, const delta_id& _i) const
00140 { undelta_base* _u(NULL);
00141 if(_d->apply(_x, _u, _i))
00142 {
00143 _x.undeltas.insert(std::make_pair(_i,undelta(_u)));
00144
00145
00146
00147 if ( _x.deltanew_it == _x.deltas.end() )
00148 _x.deltanew_it = _x.deltas.insert(_x.deltanew_it,_i);
00149 else
00150 _x.deltas.push_back(_i);
00151
00152
00153 return true;
00154 }
00155 else
00156 return false;
00157 }
00158
00159 inline bool delta::apply3(work_node& _x, const work_node& _y,
00160 const delta_id& _i) const
00161 { undelta_base* _u(NULL);
00162 if(_d->apply3(_x, _y, _u, _i))
00163 {
00164 _x.undeltas.insert(std::make_pair(_i,undelta(_u)));
00165
00166
00167
00168 if ( _x.deltanew_it == _x.deltas.end() )
00169 _x.deltanew_it = _x.deltas.insert(_x.deltanew_it,_i);
00170 else
00171 _x.deltas.push_back(_i);
00172
00173
00174 return true;
00175 }
00176 else
00177 return false;
00178 }
00179
00180 inline void delta::convert(work_node& _x)
00181 {
00182 delta_base* _do = _d;
00183 _do->convert(_x, _d);
00184 if(_do != _d)
00185 delete _do;
00186 }
00187
00188 inline void delta::unkeep() { _d->unkeep(); }
00189
00190 inline bool delta::operator==(const delta& _t) const
00191 { return _d->get_action() == _t._d->get_action()
00192 && _d->operator==(*_t._d); }
00193 inline bool delta::operator!=(const delta& _t) const
00194 { return _d->get_action() != _t._d->get_action()
00195 || _d->operator!=(*_t._d); }
00196
00197 inline undelta::undelta(const undelta& __d)
00198 { _d = __d._d->new_copy();
00199 #if DEBUG_DELTA
00200 std::cerr << "Called undelta copy constructor" << std::endl;
00201 #endif
00202 }
00203 inline undelta::~undelta()
00204 { if(_d) delete _d;
00205 #if DEBUG_DELTA
00206 std::cerr << "Called undelta destructor" << std::endl;
00207 #endif
00208 }
00209 inline undelta& undelta::operator=(const undelta& __u)
00210 {
00211 if(_d) delete _d;
00212 _d = __u._d->new_copy();
00213 #if DEBUG_DELTA
00214 std::cerr << "Called undelta assignment operator" << std::endl;
00215 #endif
00216 return *this;
00217 }
00218 inline const undelta_base* undelta::get_base() const { return _d; }
00219
00220 inline bool undelta::unapply(work_node& _x, const delta_id& _i) const
00221 { return _d->unapply(_x, _i); }
00222
00223 inline bool undelta::unapply3(work_node& _x, const work_node& _y,
00224 const delta_id& _i) const
00225 { return _d->unapply3(_x, _y, _i); }
00226
00227 }
00228
00229 #endif