1 /* 2 * Copyright 2011-2013 Blender Foundation 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 17 #ifndef __UTIL_TIME_H__ 18 #define __UTIL_TIME_H__ 19 20 #include "util/util_function.h" 21 #include "util/util_string.h" 22 23 CCL_NAMESPACE_BEGIN 24 25 /* Give current time in seconds in double precision, with good accuracy. */ 26 27 double time_dt(); 28 29 /* Sleep for the specified number of seconds. */ 30 31 void time_sleep(double t); 32 33 /* Scoped timer. */ 34 35 class scoped_timer { 36 public: value_(value)37 explicit scoped_timer(double *value = NULL) : value_(value) 38 { 39 time_start_ = time_dt(); 40 } 41 ~scoped_timer()42 ~scoped_timer() 43 { 44 if (value_ != NULL) { 45 *value_ = get_time(); 46 } 47 } 48 get_start()49 double get_start() const 50 { 51 return time_start_; 52 } 53 get_time()54 double get_time() const 55 { 56 return time_dt() - time_start_; 57 } 58 59 protected: 60 double *value_; 61 double time_start_; 62 }; 63 64 class scoped_callback_timer { 65 public: 66 using callback_type = function<void(double)>; 67 scoped_callback_timer(callback_type cb)68 explicit scoped_callback_timer(callback_type cb) : cb(cb) 69 { 70 } 71 ~scoped_callback_timer()72 ~scoped_callback_timer() 73 { 74 if (cb) { 75 cb(timer.get_time()); 76 } 77 } 78 79 protected: 80 scoped_timer timer; 81 callback_type cb; 82 }; 83 84 /* Make human readable string from time, compatible with Blender metadata. */ 85 86 string time_human_readable_from_seconds(const double seconds); 87 double time_human_readable_to_seconds(const string &str); 88 89 CCL_NAMESPACE_END 90 91 #endif 92