blob: 30309400d7ae79f4777da657c4f754f3f8e87746 [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- 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"
24#include "sync-diff-leaf.h"
25
26#include <boost/make_shared.hpp>
27#include <boost/foreach.hpp>
28#include <boost/assert.hpp>
29
30using namespace boost;
31
32namespace Sync {
33
34DiffState::DiffState ()
35{
36}
37
38DiffState::~DiffState ()
39{
40}
41
42DiffStatePtr
43DiffState::diff () const
44{
45 DiffStatePtr ret = make_shared<DiffState> ();
46
47 DiffStatePtr state = m_next;
48 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{
60 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
75 return *this;
76}
77
78// from State
79boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/>
80DiffState::update (NameInfoConstPtr info, const SeqNo &seq)
81{
82 m_leaves.erase (info);
83
84 DiffLeafPtr leaf = make_shared<DiffLeaf> (info, cref (seq));
85 m_leaves.insert (leaf);
86
87 return make_tuple (true, false, SeqNo ());
88}
89
90bool
91DiffState::remove (NameInfoConstPtr info)
92{
93 m_leaves.erase (info);
94
95 DiffLeafPtr leaf = make_shared<DiffLeaf> (info);
96 m_leaves.insert (leaf);
97
98 return true;
99}
100
101} // ns3