RMF
representation.h
Go to the documentation of this file.
1/**
2 * \file RMF/decorator/representation.h
3 * \brief Helper functions for manipulating RMF files.
4 *
5 * Copyright 2007-2022 IMP Inventors. All rights reserved.
6 *
7 */
8
9#ifndef RMF_DECORATOR_REPRESENTATION_H
10#define RMF_DECORATOR_REPRESENTATION_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 <RMF/internal/paths.h>
20#include <array>
21#include <boost/lexical_cast.hpp>
22
23RMF_ENABLE_WARNINGS
24namespace RMF {
25namespace decorator {
26
27/** See also Representation and RepresentationFactory.
28 */
29class RepresentationConst : public Decorator {
30 friend class RepresentationFactory;
31 friend class Representation;
32 IntsKey representation_;
33 template <class T>
34 std::vector<T> get_it(const Ints &in) const {
35 std::vector<T> ret;
36 for(int i : in) {
37 ret.push_back(get_node().get_file().get_node(NodeID(i)));
38 }
39 return ret;
40 }
41
42 RepresentationConst(NodeConstHandle nh, IntsKey representation)
43 : Decorator(nh), representation_(representation) {}
44
45 public:
46 NodeConstHandles get_representation() const {
47 try {
48 return get_it<NodeConstHandle>(get_node().get_value(representation_));
49 }
50 RMF_DECORATOR_CATCH();
51 }
52 NodeConstHandles get_frame_representation() const {
53 try {
54 return get_it<NodeConstHandle>(
55 get_node().get_frame_value(representation_));
56 }
57 RMF_DECORATOR_CATCH();
58 }
59 NodeConstHandles get_static_representation() const {
60 try {
61 return get_it<NodeConstHandle>(
62 get_node().get_static_value(representation_));
63 }
64 RMF_DECORATOR_CATCH();
65 }
66
67 static std::string get_decorator_type_name() { return "RepresentationConst"; }
68};
69/** See also RepresentationFactory.
70 */
71class Representation : public RepresentationConst {
72 friend class RepresentationFactory;
73 Representation(NodeHandle nh, IntsKey representation)
74 : RepresentationConst(nh, representation) {}
75
76 template <class T>
77 Ints set_it(const std::vector<T> &in) const {
78 Ints ret;
79 ret.reserve(in.size());
80 for(T n : in) { ret.push_back(n.get_id().get_index()); }
81 return ret;
82 }
83 Ints set_it(const NodeIDs &in) {
84 Ints ret;
85 ret.reserve(in.size());
86 for(NodeID n : in) { ret.push_back(n.get_index()); }
87 return ret;
88 }
89
90 public:
91#if !defined(SWIG) && !defined(IMP_DOXYGEN)
92 void set_representation(Ints v) {
93 try {
94 get_node().set_value(representation_, v);
95 }
96 RMF_DECORATOR_CATCH();
97 }
98 void set_frame_representation(Ints v) {
99 try {
100 get_node().set_frame_value(representation_, v);
101 }
102 RMF_DECORATOR_CATCH();
103 }
104 void set_static_representation(Ints v) {
105 try {
106 get_node().set_static_value(representation_, v);
107 }
108 RMF_DECORATOR_CATCH();
109 }
110
111 void set_representation(const NodeConstHandles &v) {
112 try {
113 set_representation(set_it(v));
114 }
115 RMF_DECORATOR_CATCH();
116 }
117 void set_frame_representation(const NodeConstHandles &v) {
118 try {
119 set_frame_representation(set_it(v));
120 }
121 RMF_DECORATOR_CATCH();
122 }
123 void set_static_representation(const NodeConstHandles &v) {
124 try {
125 set_static_representation(set_it(v));
126 }
127 RMF_DECORATOR_CATCH();
128 }
129 void set_representation(const NodeHandles &v) {
130 try {
131 set_representation(set_it(v));
132 }
133 RMF_DECORATOR_CATCH();
134 }
135 void set_frame_representation(const NodeHandles &v) {
136 try {
137 set_frame_representation(set_it(v));
138 }
139 RMF_DECORATOR_CATCH();
140 }
141 void set_static_representation(const NodeHandles &v) {
142 try {
143 set_static_representation(set_it(v));
144 }
145 RMF_DECORATOR_CATCH();
146 }
147#endif
148 void set_representation(const NodeIDs &v) {
149 try {
150 set_representation(set_it(v));
151 }
152 RMF_DECORATOR_CATCH();
153 }
154 void set_frame_representation(const NodeIDs &v) {
155 try {
156 set_frame_representation(set_it(v));
157 }
158 RMF_DECORATOR_CATCH();
159 }
160 void set_static_representation(const NodeIDs &v) {
161 try {
162 set_static_representation(set_it(v));
163 }
164 RMF_DECORATOR_CATCH();
165 }
166
167 NodeHandles get_representation() const {
168 try {
169 return get_it<NodeHandle>(get_node().get_value(representation_));
170 }
171 RMF_DECORATOR_CATCH();
172 }
173 NodeHandles get_frame_representation() const {
174 try {
175 return get_it<NodeHandle>(get_node().get_frame_value(representation_));
176 }
177 RMF_DECORATOR_CATCH();
178 }
179 NodeHandles get_static_representation() const {
180 try {
181 return get_it<NodeHandle>(get_node().get_static_value(representation_));
182 }
183 RMF_DECORATOR_CATCH();
184 }
185
186 static std::string get_decorator_type_name() { return "Representation"; }
187};
188
189/** Create decorators of type Representation.
190 */
191class RepresentationFactory : public Factory {
192 Category cat_;
193 IntsKey representation_;
194
195 public:
196 RepresentationFactory(FileConstHandle fh)
197 : cat_(fh.get_category("feature")),
198 representation_(fh.get_key<IntsTag>(cat_, "representation")) {}
199 RepresentationFactory(FileHandle fh)
200 : cat_(fh.get_category("feature")),
201 representation_(fh.get_key<IntsTag>(cat_, "representation")) {}
202 /** Get a RepresentationConst for nh.*/
204 RMF_USAGE_CHECK((nh.get_type() == RMF::FEATURE),
205 std::string("Bad node type. Got \"") +
206 boost::lexical_cast<std::string>(nh.get_type()) +
207 "\" in decorator type Representation");
208 return RepresentationConst(nh, representation_);
209 }
210 /** Get a Representation for nh.*/
212 RMF_USAGE_CHECK((nh.get_type() == RMF::FEATURE),
213 std::string("Bad node type. Got \"") +
214 boost::lexical_cast<std::string>(nh.get_type()) +
215 "\" in decorator type Representation");
216 return Representation(nh, representation_);
217 }
218 /** Check whether nh has all the attributes required to be a
219 RepresentationConst.*/
220 bool get_is(NodeConstHandle nh) const {
221 return (nh.get_type() == RMF::FEATURE) &&
222 !nh.get_value(representation_).get_is_null();
223 }
224 bool get_is_static(NodeConstHandle nh) const {
225 return (nh.get_type() == RMF::FEATURE) &&
226 !nh.get_static_value(representation_).get_is_null();
227 }
228};
229#ifndef RMF_DOXYGEN
230struct RepresentationConstFactory : public RepresentationFactory {
231 RepresentationConstFactory(FileConstHandle fh) : RepresentationFactory(fh) {}
232 RepresentationConstFactory(FileHandle fh) : RepresentationFactory(fh) {}
233};
234#endif
235
236} /* namespace decorator */
237} /* namespace RMF */
238RMF_DISABLE_WARNINGS
239
240#endif /* RMF_DECORATOR_REPRESENTATION_H */
Mostly empty base classes for decorators and factories.
Declaration for RMF::FileHandle.
Declaration of NodeHandle.
Represent coordinates.
The base class for Factories.
Definition Decorator.h:46
A handle for a read-only RMF file.
A handle for an RMF file.
Definition FileHandle.h:54
A handle for a particular node in a read-only hierarchy.
NodeType get_type() const
get the type of this node
A handle for a particular node in the hierarchy.
Definition NodeHandle.h:60
Representation get(NodeHandle nh) const
RepresentationConst get(NodeConstHandle nh) const
bool get_is(NodeConstHandle nh) const
Various constants.
Various general useful macros for IMP.
See Decorators and Attributes for more information.
The namespace for all functionality in the library.
const NodeType FEATURE
Store information about some feature of the system.
ID< CategoryTag > Category
Definition ID.h:110
ID< NodeTag > NodeID
Definition ID.h:106
std::vector< Int > Ints
Definition types.h:31
std::vector< NodeConstHandle > NodeConstHandles
Pass a list of them.
std::vector< NodeID > NodeIDs
Definition ID.h:113
std::vector< NodeHandle > NodeHandles
Pass a list of them.
Definition NodeHandle.h:49
ID< IntsTag > IntsKey
Definition keys.h:41