IMP  2.1.0
The Integrative Modeling Platform
kernel/optimizer_state_macros.h
Go to the documentation of this file.
1 /**
2  * \file IMP/kernel/optimizer_state_macros.h
3  * \brief Various general useful macros for IMP.
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_OPTIMIZER_STATE_MACROS_H
10 #define IMPKERNEL_OPTIMIZER_STATE_MACROS_H
11 #include <IMP/kernel/kernel_config.h>
12 #include <IMP/base/value_macros.h>
14 #include "internal/utility.h"
15 #include "OptimizerState.h"
16 
17 /** \deprecated_at{2.1} Declare the methods directly.
18 */
19 #define IMP_OPTIMIZER_STATE(Name) \
20  IMPKERNEL_DEPRECATED_MACRO(2.1, "Declare the methods youself."); \
21  virtual void update() IMP_OVERRIDE; \
22  IMP_OBJECT_NO_WARNING(Name)
23 
24 /** \deprecated_at{2.1} Functionality is built into OptimizerState.
25 */
26 #define IMP_PERIODIC_OPTIMIZER_STATE(Name) \
27  IMPKERNEL_DEPRECATED_MACRO(2.1, "Use IMP::core::PeriodicOptimizerState."); \
28  virtual void update() { \
29  IMP_OBJECT_LOG; \
30  ++call_number_; \
31  if (call_number_ % (skip_ + 1) == 0) { \
32  do_update(update_number_); \
33  ++update_number_; \
34  } \
35  } \
36  IMP_PROTECTED_METHOD(void, do_update, (unsigned int call_number), , ); \
37  \
38  public: /** Called when an optimization begins. It resets the current call \
39  number \
40  as well as making sure that the last frame is written.*/ \
41  void set_is_optimizing(bool tf) { \
42  if (!tf) { \
43  do_update(update_number_); \
44  ++update_number_; \
45  } else \
46  call_number_ = 0; \
47  } \
48  IMP_NO_DOXYGEN(void set_skip_steps(unsigned int k) { set_period(k + 1); }); \
49  void set_period(unsigned int p) { \
50  IMP_USAGE_CHECK(p > 0, "Period must be positive."); \
51  skip_ = p - 1; \
52  call_number_ = 0; \
53  } \
54  unsigned int get_period() const { return skip_ + 1; } \
55  void reset() { \
56  call_number_ = 0; \
57  update_number_ = 0; \
58  } \
59  IMP_OBJECT_NO_WARNING(Name); \
60  \
61  private: \
62  ::IMP::kernel::internal::Counter skip_, call_number_, update_number_
63 
64 //! Define a pair of classes to handle saving the model
65 /** This macro defines a class:
66  - NameOptimizerState
67  to handling saving the model in the specified way during
68  optimization and on failures, respectively.
69  \param[in] Name The name to prefix the class names
70  \param[in] args The parentesized arguments to the constructor. The
71  last one should be a string called file_name.
72  \param[in] vars The needed member variables.
73  \param[in] constr The body of the constructor.
74  \param[in] functs Any functions (declaration and definition) to
75  go in the class bodies.
76  \param[in] save_action The action to take to perform the save. The
77  name to save to is know as file_name
78  */
79 #define IMP_MODEL_SAVE(Name, args, vars, constr, functs, save_action) \
80  IMPKERNEL_DEPRECATED_MACRO(2.1, "Just expand in place."); \
81  class Name##OptimizerState : public OptimizerState { \
82  std::string file_name_; \
83  vars virtual void do_update(unsigned int update_number) IMP_OVERRIDE { \
84  std::ostringstream oss; \
85  bool formatted = false; \
86  try { \
87  oss << boost::format(file_name_) % update_number; \
88  formatted = true; \
89  } \
90  catch (...) { \
91  } \
92  if (formatted) { \
93  write(oss.str(), update_number, false); \
94  } else { \
95  write(file_name_, update_number, update_number != 0); \
96  } \
97  } \
98  \
99  public: /** Write to a file generated from the passed filename every \
100  skip_steps steps. The file_name constructor argument should contain \
101  "%1%" if you don't want to write the same file each time. \
102  */ \
103  Name##OptimizerState args \
104  : OptimizerState(std::string("Writer to ") + file_name), \
105  file_name_(file_name) { \
106  constr \
107  } \
108  functs void write(std::string file_name, unsigned int call = 0, \
109  bool append = false) const { \
110  IMP_UNUSED(call); \
111  IMP_UNUSED(append); \
112  save_action \
113  } \
114  \
115  private: \
116  IMP_OBJECT_METHODS(Name##OptimizerState); \
117  }; \
118  IMP_OBJECTS(Name##OptimizerState, Name##OptimizerStates);
119 
120 #endif /* IMPKERNEL_OPTIMIZER_STATE_MACROS_H */
Shared optimizer state.
Various general useful macros for IMP.
Various general useful macros for IMP.