5 * Created by Bryan Donlan on The Apr 11 2005
6 * Copyright (c) 2005 Bryan Donlan. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
23 #include <cstdlib> // for NULL
25 #include <boost/weak_ptr.hpp>
26 #include <boost/shared_ptr.hpp>
34 boost::weak_ptr
<Agent
> ref
;
35 void checkLife() const;
41 AgentRef(boost::shared_ptr
<Agent
> a
) { ref
= a
; }
42 AgentRef(boost::weak_ptr
<Agent
> a
) { ref
= a
; }
43 AgentRef(Agent
*a
) { set(a
); }
44 AgentRef(const AgentRef
&r
) : ref(r
.ref
) {}
46 void clear() { ref
.reset(); }
48 ~AgentRef() { clear(); };
50 AgentRef
&operator=(const AgentRef
&r
) { ref
= r
.ref
; return *this; }
51 Agent
*operator=(Agent
*a
) { set(a
); return a
; }
52 Agent
&operator*() const { checkLife(); return *ref
.lock().get(); }
53 Agent
*operator->() const { checkLife(); return ref
.lock().get(); }
54 bool operator!() const { return lock().get() == NULL
; }
55 /* This next line breaks builds with MSVC, tossing errors about ambiguous operators.
56 operator bool() const { return ref; } */
57 operator Agent
*() const { return ref
.lock().get(); }
58 bool operator==(const AgentRef
&r
) const { return lock() == r
.lock(); }
59 bool operator==(const Agent
*r
) const { return r
== lock().get(); }
60 bool operator!=(const AgentRef
&r
) const { return !(*this == r
);}
61 bool operator!=(const Agent
*r
) const { return !(*this == r
); }
64 void set(const AgentRef
&r
) { ref
= r
.ref
; }
65 void set(const boost::shared_ptr
<Agent
> &r
) { ref
= r
; }
66 void set(const boost::weak_ptr
<Agent
> &r
) { ref
= r
; }
68 boost::shared_ptr
<Agent
> lock() const;
69 Agent
*get() const { return lock().get(); }