ccdevnet / openc2e

openc2e

This URL has Read+Write access

openc2e / src / AgentRef.h
100644 76 lines (60 sloc) 2.309 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* AgentRef.h
* openc2e
*
* Created by Bryan Donlan on The Apr 11 2005
* Copyright (c) 2005 Bryan Donlan. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
 
#ifndef AGENTREF_H
#define AGENTREF_H 1
 
#include <cstdlib> // for NULL
#include <iostream>
#include <boost/weak_ptr.hpp>
#include <boost/shared_ptr.hpp>
 
class Agent;
 
class AgentRef {
friend class Agent;
 
protected:
boost::weak_ptr<Agent> ref;
void checkLife() const;
 
public:
void dump() const;
 
AgentRef() { }
AgentRef(boost::shared_ptr<Agent> a) { ref = a; }
AgentRef(boost::weak_ptr<Agent> a) { ref = a; }
AgentRef(Agent *a) { set(a); }
AgentRef(const AgentRef &r) : ref(r.ref) {}
 
void clear() { ref.reset(); }
 
~AgentRef() { clear(); };
 
AgentRef &operator=(const AgentRef &r) { ref = r.ref; return *this; }
Agent *operator=(Agent *a) { set(a); return a; }
Agent &operator*() const { checkLife(); return *ref.lock().get(); }
Agent *operator->() const { checkLife(); return ref.lock().get(); }
bool operator!() const { return lock().get() == NULL; }
/* This next line breaks builds with MSVC, tossing errors about ambiguous operators.
operator bool() const { return ref; } */
operator Agent *() const { return ref.lock().get(); }
bool operator==(const AgentRef &r) const { return lock() == r.lock(); }
bool operator==(const Agent *r) const { return r == lock().get(); }
bool operator!=(const AgentRef &r) const { return !(*this == r);}
bool operator!=(const Agent *r) const { return !(*this == r); }
 
void set(Agent *a);
void set(const AgentRef &r) { ref = r.ref; }
void set(const boost::shared_ptr<Agent> &r) { ref = r; }
void set(const boost::weak_ptr<Agent> &r) { ref = r; }
 
boost::shared_ptr<Agent> lock() const;
Agent *get() const { return lock().get(); }
};
 
#endif
 
/* vim: set noet: */