Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ObserverCache.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file core/pose/datacache/ObserverCache.cc
11 /// @brief A DataCache storing objects derived from
12 /// core::pose::datacache::CacheableData.
13 /// @author Yih-En Andrew Ban (yab@u.washington.edu)
14 
15 // unit headers
17 
18 // project headers
19 // AUTO-REMOVED #include <core/pose/Pose.hh>
20 
22 #include <utility/vector1.hh>
23 #include <iostream>
24 
25 
26 
27 namespace core {
28 namespace pose {
29 namespace datacache {
30 
31 
32 /// @brief constructor
33 /// @param[in] n_types The number of slots for this ObserverCache.
34 /// @param[in] pose The Pose that will be watched by the Observers in this cache.
36  Size const n_slots,
37  Pose & pose
38 ) :
39  Super( n_slots ),
40  pose_( &pose )
41 {}
42 
43 
44 /// @brief default destructor
46  detach();
47 }
48 
49 
50 /// @brief copy assignment
52  if ( this != &rval ) {
53  // detach all existing observers
54  detach();
55 
56  // copy assign (all observers cloned)
57  Super::operator =( rval );
58 
59  // attach the observers that were already attached in rval
60  for ( Size i = 1, ie = rval.data().size(); i <= ie; ++i ) {
61  if ( rval.is_attached( i ) ) {
62  attach( i );
63  }
64  }
65  }
66  return *this;
67 }
68 
69 
70 /// @brief clear all the observers
72  detach(); // safety
73  Super::clear();
74 }
75 
76 
77 /// @brief clear the observer in a selected slot
78 void ObserverCache::clear( Size const slot ) {
79  detach( slot ); // safety
80  Super::clear( slot );
81 }
82 
83 
84 /// @brief store a copy of the observer in the given slot and attach it to
85 /// the Pose
86 /// @param[in] The slot to use.
87 /// @param[in] observer The Observer to clone() and store.
88 /// @remarks this function exists to ensure the base class version is
89 /// overridden
91  Size const slot,
92  CacheableObserverOP observer
93 )
94 {
95  set( slot, observer, true );
96 }
97 
98 
99 /// @brief store a copy of the observer in the given slot
100 /// @param[in] The slot to use.
101 /// @param[in] observer The Observer to clone() and store.
102 /// @param[in] auto_attach Attach the observer to the Pose?
104  Size const slot,
105  CacheableObserverOP observer,
106  bool const auto_attach
107 )
108 {
109  detach( slot ); // safety
110 
111  if ( observer.get() != 0 ) {
112  data()[ slot ] = observer->clone();
113 
114  if ( auto_attach ) {
115  attach( slot );
116  }
117  } else {
118  data()[ slot ] = 0;
119  }
120 }
121 
122 
123 /// @brief is the observer in the slot attached to the Pose?
124 /// @return true if attached, false if not attached or no observer
125 /// exists in the slot
126 bool ObserverCache::is_attached( Size const slot ) const {
127  if ( has( slot ) ) {
128  return data()[ slot ]->is_attached();
129  }
130 
131  return false;
132 }
133 
134 
135 /// @brief attach all stored observers to the Pose
137  for ( Size i = 1, ie = data().size(); i <= ie; ++i ) {
138  attach( i );
139  }
140 }
141 
142 
143 /// @brief detach all observers from the Pose
145  for ( Size i = 1, ie = data().size(); i <= ie; ++i ) {
146  detach( i );
147  }
148 }
149 
150 
151 /// @brief attach an observer in a particular slot to the Pose
152 /// @param[in] slot Attach the observer in this slot.
153 void ObserverCache::attach( Size const slot ) {
154  if ( has( slot ) ) {
155  data()[ slot ]->attach_to( *pose_ );
156  }
157 }
158 
159 
160 /// @brief detach an observer in a particular slot to the Pose
161 /// @param[in] slot Detach the observer in this slot.
162 void ObserverCache::detach( Size const slot ) {
163  if ( has( slot ) ) {
164  data()[ slot ]->detach_from();
165  }
166 }
167 
168 
169 } // namespace datacache
170 } // namespace pose
171 } // namespace core