OpenFrames
FrameManager.hpp
Go to the documentation of this file.
1 /***********************************
2  Copyright 2018 Ravishankar Mathur
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 ***********************************/
16 
21 #ifndef _OF_FRAMEMANAGER_
22 #define _OF_FRAMEMANAGER_
23 
24 #include <OpenFrames/Export.h>
26 #include <OpenThreads/Mutex>
27 #include <osg/Referenced>
28 #include <osg/ref_ptr>
29 
30 namespace OpenFrames
31 {
52  class OF_EXPORT FrameManager : public osg::Referenced
53  {
54  public:
55  FrameManager(ReferenceFrame *frame = NULL) : _frame(frame) {}
56 
57  enum Priority
58  {
59  LOW_PRIORITY, // For frequent users, e.g. rendering threads
60  HIGH_PRIORITY // For one-off users, e.g. modifying the scene
61  };
62 
63  void setFrame(ReferenceFrame *frame)
64  {
65  lock();
66  _frame = frame;
67  unlock();
68  }
69 
70  inline ReferenceFrame* getFrame() {return _frame.get();}
71  inline osg::Group* getData()
72  {
73  if(_frame.valid()) return _frame->getGroup();
74  else return NULL;
75  }
76 
77  inline int lock(Priority priority = HIGH_PRIORITY)
78  {
79  if(priority == LOW_PRIORITY) _mutexLP.lock();
80  _mutexNext.lock();
81  int val = _mutexData.lock();
82  _mutexNext.unlock();
83  return val;
84  }
85 
87  inline int unlock(Priority priority = HIGH_PRIORITY)
88  {
89  int val = _mutexData.unlock();
90  if(priority == LOW_PRIORITY) _mutexLP.unlock();
91  return val;
92  }
93 
94  inline int trylock(Priority priority = HIGH_PRIORITY)
95  {
96  if(priority == LOW_PRIORITY) _mutexLP.lock();
97  _mutexNext.lock();
98  int val = _mutexData.trylock();
99  _mutexNext.unlock();
100  return val;
101  }
102 
103  protected:
104  virtual ~FrameManager() {}
105 
106  osg::ref_ptr<ReferenceFrame> _frame;
107  OpenThreads::Mutex _mutexData, _mutexNext, _mutexLP;
108  };
109 
110 }
111 
112 #endif
This class allows priority access to a ReferenceFrame heirarchy.
Definition: FrameManager.hpp:52
Definition: CoordinateAxes.hpp:29
int unlock(Priority priority=HIGH_PRIORITY)
Users must specify the same priority as they did for lock/trylock.
Definition: FrameManager.hpp:87
Definition: ReferenceFrame.hpp:54