OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Handle.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/Handle.cc
3  *
4  * Copyright (C) 2002-2007 Novamente LLC
5  * Copyright (C) 2013 Linas Vepstas <linas@linas.org>
6  * All Rights Reserved
7  *
8  * Written by Thiago Maia <thiago@vettatech.com>
9  * Andre Senna <senna@vettalabs.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU Affero General Public License v3 as
13  * published by the Free Software Foundation and including the exceptions
14  * at http://opencog.org/wiki/Licenses
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU Affero General Public License
22  * along with this program; if not, write to:
23  * Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26 
27 #include <climits>
29 #include <opencog/atomspace/Atom.h>
31 
32 using namespace opencog;
33 
34 const Handle Handle::UNDEFINED(ULONG_MAX);
36 
37 Handle::Handle(const AtomPtr& atom) : _uuid(atom->_uuid), _ptr(atom) {}
38 
40 {
41  this->_uuid = a->_uuid;
42  this->_ptr = a;
43  return *this;
44 }
45 
46 // ===================================================
47 // Handle resolution stuff.
48 
49 // Its a vector, not a set, because its priority ranked.
50 std::vector<const AtomTable*> Handle::_resolver;
51 
53 {
54  _resolver.push_back(tab);
55 }
56 
58 {
59  auto it = std::find(_resolver.begin(), _resolver.end(), tab);
60  if (it != _resolver.end())
61  _resolver.erase(it);
62 }
63 
64 // Search several atomspaces, in order. First one to come up with
65 // the atom wins. Seems to work, for now.
66 inline AtomPtr Handle::do_res(const Handle* hp)
67 {
68  for (const AtomTable* at : _resolver) {
69  AtomPtr a(at->getHandle((Handle&)(*hp))._ptr);
70  if (a.get()) return a;
71  }
72  return NULL;
73 }
74 
76 {
77  AtomPtr a(do_res(this));
78  _ptr.swap(a);
79  return _ptr.get();
80 }
81 
83 {
84  return do_res(this).get();
85 }
86 
88 {
89  AtomPtr a(do_res(this));
90  _ptr.swap(a);
91  return _ptr;
92 }
static AtomPtr do_res(const Handle *)
Definition: Handle.cc:66
AtomPtr resolve_ptr()
Definition: Handle.cc:87
static std::vector< const AtomTable * > _resolver
Definition: Handle.h:68
std::shared_ptr< Atom > AtomPtr
Definition: Handle.h:48
Handle & operator=(const Handle &h)
Definition: Handle.h:89
UUID _uuid
Definition: Handle.h:62
static const Handle UNDEFINED
Definition: Handle.h:77
AtomPtr _ptr
Definition: Handle.h:63
Atom * cresolve() const
Definition: Handle.cc:82
static void set_resolver(const AtomTable *)
Definition: Handle.cc:52
static void clear_resolver(const AtomTable *)
Definition: Handle.cc:57
Atom * resolve()
Definition: Handle.cc:75
static const AtomPtr NULL_POINTER
Definition: Handle.h:74