switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / PacketInterpreter.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief PacketInterpreter non-inline non-template implementation */
30
31 //#include "PacketInterpreter.ih"
32
33 // Custom includes
34 #include "Packets.hh"
35
36 //#include "PacketInterpreter.mpp"
37 #define prefix_
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 // senf::PacketInterpreterBase
42
43 // structors and default members
44
45 prefix_  senf::PacketInterpreterBase::~PacketInterpreterBase()
46 {}
47
48 prefix_ senf::PacketInterpreterBase::ptr senf::PacketInterpreterBase::clone()
49 {
50     detail::PacketImpl::Guard p (new detail::PacketImpl(begin(),end()));
51     ptr pi (appendClone(p.p,begin(),p.p->begin()));
52     for (ptr i (next()); i; i = i->next())
53         i->appendClone(p.p,begin(),p.p->begin());
54     pi->impl().assignAnnotations( impl());
55     return pi;
56 }
57
58 // Interpreter chain access
59
60 prefix_ senf::PacketInterpreterBase::ptr senf::PacketInterpreterBase::append(ptr packet)
61 {
62     if (next())
63         impl().truncateInterpreters(next().get());
64
65     optional_range r (nextPacketRange());
66     if (!r)
67         throw InvalidPacketChainException();
68
69     ptr rv (packet->appendClone(&impl(), *r));
70     rv->data().resize(packet->data().size());
71     std::copy(packet->data().begin(), packet->data().end(), rv->data().begin());
72
73     for (ptr p (packet->next()) ; p ; p = p->next())
74         p->appendClone(&impl(), packet->data().begin(), rv->data().begin());
75
76     return rv;
77 }
78
79 prefix_ void senf::PacketInterpreterBase::reparse()
80 {
81     if (next())
82         impl().truncateInterpreters(next().get());
83 }
84
85 // Access to the abstract interface
86
87 prefix_ void senf::PacketInterpreterBase::dump(std::ostream & os)
88 {
89     try {
90         if (detail::AnnotationRegistry::instance().begin()
91             != detail::AnnotationRegistry::instance().end()) {
92             os << "Annotations:\n";
93             impl().dumpAnnotations(os);
94         }
95         v_dump(os);
96         for (ptr i (next()); i; i = i->next())
97             i->v_dump(os);
98     }
99     catch (senf::Exception & e) {
100         os << "[Exception: " << e.message() << "]\n";
101     }
102 }
103
104 prefix_ void senf::PacketInterpreterBase::finalizeThis()
105 {
106     v_finalize();
107 }
108
109 prefix_ void senf::PacketInterpreterBase::finalizeTo(ptr other)
110 {
111     for (ptr i (other); i.get() != this && i.get(); i = i->prev())
112         i->finalizeThis();
113     finalizeThis();
114 }
115
116 // reference/memory management
117
118 prefix_ void senf::PacketInterpreterBase::add_ref()
119 {
120     if (impl_ && !refcount())
121         impl_->add_ref();
122     intrusive_refcount_t<PacketInterpreterBase>::add_ref();
123 }
124
125 prefix_ void senf::PacketInterpreterBase::release()
126 {
127     if (impl_ && refcount()==1)
128         // This call will set impl_ to 0 if we just removed the last reference ...
129         impl_->release();
130     if (intrusive_refcount_t<PacketInterpreterBase>::release() && !impl_)
131         delete this;
132 }
133
134 //-/////////////////////////////////////////////////////////////////////////////////////////////////
135 // senf::PacketInterpreterBase::Factory
136
137 prefix_  senf::PacketInterpreterBase::Factory::~Factory()
138 {}
139
140 //-/////////////////////////////////////////////////////////////////////////////////////////////////
141 #undef prefix_
142 //#include "PacketInterpreter.mpp"
143
144 \f
145 // Local Variables:
146 // mode: c++
147 // fill-column: 100
148 // c-file-style: "senf"
149 // indent-tabs-mode: nil
150 // ispell-local-dictionary: "american"
151 // compile-command: "scons -u test"
152 // comment-column: 40
153 // End: