RMF
reference.h
Go to the documentation of this file.
1 /**
2  * \file RMF/decorator/reference.h
3  * \brief Add a pointer from a node to another reference node.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef RMF_DECORATOR_REFERENCE_H
10 #define RMF_DECORATOR_REFERENCE_H
11 
12 #include <RMF/config.h>
14 #include <RMF/NodeHandle.h>
15 #include <RMF/FileHandle.h>
16 #include <RMF/Decorator.h>
17 #include <RMF/constants.h>
18 #include <RMF/Vector.h>
19 #include <array>
20 #include <boost/lexical_cast.hpp>
21 
22 RMF_ENABLE_WARNINGS
23 namespace RMF {
24 namespace decorator {
25 
26 /** See also Reference and ReferenceFactory.
27  */
28 class ReferenceConst : public Decorator {
29  friend class ReferenceFactory;
30  friend class Reference;
31  IntKey reference_;
33  : Decorator(nh), reference_(reference) {}
34 
35  public:
36  NodeConstHandle get_reference() const {
37  try {
38  return get_node().get_file().get_node(
39  NodeID(get_node().get_value(reference_)));
40  }
41  RMF_DECORATOR_CATCH();
42  }
43  NodeConstHandle get_frame_reference() const {
44  try {
45  return get_node().get_file().get_node(
46  NodeID(get_node().get_frame_value(reference_)));
47  }
48  RMF_DECORATOR_CATCH();
49  }
50  NodeConstHandle get_static_reference() const {
51  try {
52  return get_node().get_file().get_node(
53  NodeID(get_node().get_static_value(reference_)));
54  }
55  RMF_DECORATOR_CATCH();
56  }
57 
58  static std::string get_decorator_type_name() { return "ReferenceConst"; }
59  RMF_SHOWABLE(ReferenceConst, "Reference: " << get_node());
60 };
61 /** See also ReferenceFactory.
62  */
63 class Reference : public ReferenceConst {
64  friend class ReferenceFactory;
65  Reference(NodeHandle nh, IntKey reference)
66  : ReferenceConst(nh, reference) {}
67 
68  public:
69 
70  NodeHandle get_reference() const {
71  try {
72  return get_node().get_file().get_node(
73  NodeID(get_node().get_value(reference_)));
74  }
75  RMF_DECORATOR_CATCH();
76  }
77  NodeHandle get_frame_reference() const {
78  try {
79  return get_node().get_file().get_node(
80  NodeID(get_node().get_frame_value(reference_)));
81  }
82  RMF_DECORATOR_CATCH();
83  }
84  NodeHandle get_static_reference() const {
85  try {
86  return get_node().get_file().get_node(
87  NodeID(get_node().get_static_value(reference_)));
88  }
89  RMF_DECORATOR_CATCH();
90  }
91 
92  void set_reference(NodeConstHandle v) {
93  try {
94  get_node().set_value(reference_, v.get_id().get_index());
95  }
96  RMF_DECORATOR_CATCH();
97  }
98  void set_frame_reference(NodeConstHandle v) {
99  try {
100  get_node().set_frame_value(reference_, v.get_id().get_index());
101  }
102  RMF_DECORATOR_CATCH();
103  }
104  void set_static_reference(NodeConstHandle v) {
105  try {
106  get_node().set_static_value(reference_, v.get_id().get_index());
107  }
108  RMF_DECORATOR_CATCH();
109  }
110 
111  static std::string get_decorator_type_name() { return "Reference"; }
112 };
113 
114 /** Create decorators of type Reference.
115  */
116 class ReferenceFactory : public Factory {
117  Category cat_;
118  IntKey reference_;
119 
120  public:
122  : cat_(fh.get_category("sequence")),
123  reference_(fh.get_key<IntTag>(cat_, "reference")) {}
125  : cat_(fh.get_category("sequence")),
126  reference_(fh.get_key<IntTag>(cat_, "reference")) {}
127  /** Get a ReferenceConst for nh.*/
129  RMF_USAGE_CHECK((nh.get_type() == RMF::REPRESENTATION),
130  std::string("Bad node type. Got \"") +
131  boost::lexical_cast<std::string>(nh.get_type()) +
132  "\" in decorator type Reference");
133  return ReferenceConst(nh, reference_);
134  }
135  /** Get a Reference for nh.*/
136  Reference get(NodeHandle nh) const {
137  RMF_USAGE_CHECK((nh.get_type() == RMF::REPRESENTATION),
138  std::string("Bad node type. Got \"") +
139  boost::lexical_cast<std::string>(nh.get_type()) +
140  "\" in decorator type Reference");
141  return Reference(nh, reference_);
142  }
143  /** Check whether nh has all the attributes required to be a
144  ReferenceConst.*/
145  bool get_is(NodeConstHandle nh) const {
146  return (nh.get_type() == RMF::REPRESENTATION) &&
147  !nh.get_value(reference_).get_is_null();
148  }
149  bool get_is_static(NodeConstHandle nh) const {
150  return (nh.get_type() == RMF::REPRESENTATION) &&
151  !nh.get_static_value(reference_).get_is_null();
152  }
153  RMF_SHOWABLE(ReferenceFactory, "ReferenceFactory");
154 };
155 
156 } /* namespace decorator */
157 } /* namespace RMF */
158 RMF_DISABLE_WARNINGS
159 
160 #endif /* RMF_DECORATOR_REFERENCE_H */
Mostly empty base classes for decorators and factories.
const NodeType REPRESENTATION
Represent part of a molecule.
NodeID get_id() const
bool get_is(NodeConstHandle nh) const
Definition: reference.h:145
A handle for a particular node in the hierarchy.
Definition: NodeHandle.h:60
The base class for decorators.
Definition: Decorator.h:29
NodeType get_type() const
get the type of this node
Various constants.
A handle for a particular node in a read-only hierarchy.
A handle for a read-only RMF file.
void set_value(ID< Tag > k, typename Tag::ArgumentType v) const
Definition: NodeHandle.h:115
A handle for an RMF file.
Definition: FileHandle.h:54
The base class for Factories.
Definition: Decorator.h:46
ID< NodeTag > NodeID
Definition: ID.h:106
Declaration for RMF::FileHandle.
Represent coordinates.
Declaration of NodeHandle.
void set_static_value(ID< Tag > k, typename Tag::ArgumentType v) const
set the value of the attribute k for all frames.
Definition: NodeHandle.h:122
ID< Tag > get_key(Category category, std::string name) const
Various general useful macros for IMP.
NodeHandle get_node(NodeID id) const
Return a NodeHandle from a NodeID.
void set_frame_value(ID< Tag > k, typename Tag::ArgumentType v) const
set the value of the attribute k for this node on the current frame.
Definition: NodeHandle.h:103