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 
23 RMF_ENABLE_WARNINGS
24 namespace RMF {
25 namespace decorator {
26 
27 /** See also Representation and RepresentationFactory.
28  */
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  */
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  */
192  Category cat_;
193  IntsKey representation_;
194 
195  public:
197  : cat_(fh.get_category("feature")),
198  representation_(fh.get_key<IntsTag>(cat_, "representation")) {}
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.*/
211  Representation get(NodeHandle nh) const {
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
230 struct 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 */
238 RMF_DISABLE_WARNINGS
239 
240 #endif /* RMF_DECORATOR_REPRESENTATION_H */
Mostly empty base classes for decorators and factories.
const NodeType FEATURE
Store information about some feature of the system.
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.
std::vector< NodeHandle > NodeHandles
Pass a list of them.
Definition: NodeHandle.h:46
A handle for a read-only RMF file.
void set_value(ID< Tag > k, typename Tag::ArgumentType v) const
Definition: NodeHandle.h:115
std::vector< Int > Ints
Definition: types.h:31
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
std::vector< NodeConstHandle > NodeConstHandles
Pass a list of them.
ID< Tag > get_key(Category category, std::string name) const
bool get_is(NodeConstHandle nh) const
Various general useful macros for IMP.
std::vector< NodeID > NodeIDs
Definition: ID.h:113
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