Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
frame.cc
Go to the documentation of this file.
1 
18 #include "ion/gfxutils/frame.h"
19 
20 #include <algorithm>
21 
22 #include "ion/base/logging.h"
23 
24 namespace ion {
25 namespace gfxutils {
26 
28  : counter_(0),
29  in_frame_(false),
30  pre_frame_callbacks_(*this),
31  post_frame_callbacks_(*this) {}
32 
33 Frame::~Frame() {}
34 
35 void Frame::Begin() {
36  if (in_frame_) {
37  LOG(ERROR) << "Frame::Begin() called while already in a frame.";
38  } else {
40  for (CallbackMap::const_iterator it = pre_frame_callbacks_.begin();
41  it != pre_frame_callbacks_.end(); ++it)
42  it->second(*this);
43  in_frame_ = true;
44  }
45 }
46 
47 void Frame::End() {
48  if (!in_frame_) {
49  LOG(ERROR) << "Frame::End() called while not in a frame.";
50  } else {
52  for (CallbackMap::const_iterator it = post_frame_callbacks_.begin();
53  it != post_frame_callbacks_.end(); ++it)
54  it->second(*this);
55  in_frame_ = false;
56  ++counter_;
57  }
58 }
59 
60 void Frame::AddPreFrameCallback(const std::string& key,
61  const Callback& callback) {
62  if (callback)
63  pre_frame_callbacks_[key] = callback;
64 }
65 
66 void Frame::AddPostFrameCallback(const std::string& key,
67  const Callback& callback) {
68  if (callback)
69  post_frame_callbacks_[key] = callback;
70 }
71 
72 bool Frame::RemovePreFrameCallback(const std::string& key) {
73  CallbackMap::iterator it = pre_frame_callbacks_.find(key);
74  if (it != pre_frame_callbacks_.end()) {
75  pre_frame_callbacks_.erase(it);
76  return true;
77  }
78  return false;
79 }
80 
81 bool Frame::RemovePostFrameCallback(const std::string& key) {
82  CallbackMap::iterator it = post_frame_callbacks_.find(key);
83  if (it != post_frame_callbacks_.end()) {
84  post_frame_callbacks_.erase(it);
85  return true;
86  }
87  return false;
88 }
89 
90 } // namespace gfxutils
91 } // namespace ion
void AddPreFrameCallback(const std::string &key, const Callback &callback)
Adds a callback to be invoked when Begin() or End() is called.
Definition: frame.cc:60
bool RemovePreFrameCallback(const std::string &key)
Removes a callback added previously with AddPreFrameCallback() or AddPostFrameCallback(), identified by the key.
Definition: frame.cc:72
void Begin()
Begins a new frame.
Definition: frame.cc:35
void AddPostFrameCallback(const std::string &key, const Callback &callback)
Definition: frame.cc:66
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
void End()
Ends the current frame.
Definition: frame.cc:47
Frame()
The constructor initializes the frame counter to 0.
Definition: frame.cc:27
Copyright 2016 Google Inc.
std::function< void(const Frame &frame)> Callback
Callback that can be invoked at the beginning or end of a frame.
Definition: frame.h:37
bool RemovePostFrameCallback(const std::string &key)
Definition: frame.cc:81