OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TypeNode.h
Go to the documentation of this file.
1 /*
2  * opencog/atoms/TypeNode.h
3  *
4  * Copyright (C) 2015 Linas Vepstas
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License v3 as
9  * published by the Free Software Foundation and including the exceptions
10  * at http://opencog.org/wiki/Licenses
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program; if not, write to:
19  * Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #ifndef _OPENCOG_TYPE_NODE_H
24 #define _OPENCOG_TYPE_NODE_H
25 
28 #include <opencog/atomspace/Node.h>
29 
30 namespace opencog
31 {
40 class TypeNode : public Node
41 {
42 protected:
44 
45 public:
46  TypeNode(const std::string& s,
49  // Convert to number and back to string to avoid miscompares.
50  : Node(TYPE_NODE, s, tv, av),
52  {
53  if (NOTYPE == value)
54  throw InvalidParamException(TRACE_INFO,
55  "Not a valid typename: '%s'", s.c_str());
56  }
57 
61  : Node(TYPE_NODE, classserver().getTypeName(t), tv, av),
62  value(t)
63  {}
64 
66  : Node(n),
68  {
69  OC_ASSERT(TYPE_NODE == n.getType(), "Bad TypeNode constructor!");
70 
71  if (NOTYPE == value)
72  throw InvalidParamException(TRACE_INFO,
73  "Not a valid typename: '%s'", n.getName().c_str());
74  }
75 
76  static void validate(const std::string& str)
77  {
78  Type t = classserver().getType(str);
79  if (NOTYPE == t)
80  throw InvalidParamException(TRACE_INFO,
81  "Not a valid typename: '%s'", str.c_str());
82  }
83 
84  Type get_value(void) { return value; }
85 };
86 
87 typedef std::shared_ptr<TypeNode> TypeNodePtr;
88 static inline TypeNodePtr TypeNodeCast(const Handle& h)
89  { AtomPtr a(h); return std::dynamic_pointer_cast<TypeNode>(a); }
91  { return std::dynamic_pointer_cast<TypeNode>(a); }
92 
93 // XXX temporary hack ...
94 #define createTypeNode std::make_shared<TypeNode>
95 
97 }
98 
99 #endif // _OPENCOG_TYPE_NODE_H
static TypeNodePtr TypeNodeCast(const Handle &h)
Definition: TypeNode.h:88
std::shared_ptr< Atom > AtomPtr
Definition: Handle.h:48
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
std::shared_ptr< AttentionValue > AttentionValuePtr
Type get_value(void)
Definition: TypeNode.h:84
Type getType() const
Definition: Atom.h:197
TypeNode(Node &n)
Definition: TypeNode.h:65
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
static void validate(const std::string &str)
Definition: TypeNode.h:76
TypeNode(Type t, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV())
Definition: TypeNode.h:58
const std::string & getName() const
Definition: Node.h:87
static TruthValuePtr DEFAULT_TV()
Definition: TruthValue.cc:52
std::shared_ptr< TypeNode > TypeNodePtr
Definition: TypeNode.h:87
static AttentionValuePtr DEFAULT_AV()
to be used as default attention value
Type getType(const std::string &typeName)
Definition: ClassServer.cc:138
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
TypeNode(const std::string &s, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV())
Definition: TypeNode.h:46