blob: 1c6a41c4e61590875f0cf75113cd409aa7402d6d [file] [log] [blame]
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include "sync-diff-state.h"
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080024#include "sync-diff-leaf.h"
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080025
26#include <boost/make_shared.hpp>
Alexander Afanasyev64d50692012-03-07 20:48:35 -080027#include <boost/foreach.hpp>
28#include <boost/assert.hpp>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080029
30using namespace boost;
31
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080032namespace Sync {
33
34DiffState::DiffState ()
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080035{
36}
37
38DiffState::~DiffState ()
39{
40}
41
42DiffStatePtr
43DiffState::diff () const
44{
45 DiffStatePtr ret = make_shared<DiffState> ();
Alexander Afanasyeva36b7512012-03-12 16:03:03 -070046
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080047 DiffStatePtr state = m_next;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080048 while (state != 0)
49 {
50 *ret += *state;
51 state = state->m_next;
52 }
53
54 return ret;
55}
56
57DiffState &
58DiffState::operator += (const DiffState &state)
59{
Alexander Afanasyeva36b7512012-03-12 16:03:03 -070060 BOOST_FOREACH (LeafConstPtr _leaf, state.getLeaves ())
61 {
62 DiffLeafConstPtr leaf = dynamic_pointer_cast<const DiffLeaf> (_leaf);
63 BOOST_ASSERT (leaf != 0);
64
65 if (leaf->getOperation () == UPDATE)
66 update (leaf->getInfo (), leaf->getSeq ());
67 else if (leaf->getOperation () == REMOVE)
68 remove (leaf->getInfo ());
69 else
70 {
71 BOOST_ASSERT (false);
72 }
73 }
74
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080075 return *this;
76}
77
78// from State
Alexander Afanasyev750d1872012-03-12 15:33:56 -070079boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080080DiffState::update (NameInfoConstPtr info, const SeqNo &seq)
81{
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070082 m_leaves.erase (info);
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080083
84 DiffLeafPtr leaf = make_shared<DiffLeaf> (info, cref (seq));
85 m_leaves.insert (leaf);
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070086
Alexander Afanasyev750d1872012-03-12 15:33:56 -070087 return make_tuple (true, false, SeqNo ());
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080088}
89
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070090bool
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080091DiffState::remove (NameInfoConstPtr info)
92{
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070093 m_leaves.erase (info);
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080094
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080095 DiffLeafPtr leaf = make_shared<DiffLeaf> (info);
96 m_leaves.insert (leaf);
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070097
98 return true;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080099}
100
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800101} // ns3