Scene Lab
An open source project by FPL.
 All Classes Namespaces Files Functions Pages
util.h
Go to the documentation of this file.
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 /// Utility functions that might be useful if you are using Scene Lab.
16 #ifndef SCENE_LAB_UTIL_H
17 #define SCENE_LAB_UTIL_H
18 
19 #include <functional>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 
24 namespace scene_lab {
25 
26 /// @file
27 /// Scan a directory on the file system for all files matching a given file
28 /// extension. Return all files found, along with the "last modified" time for
29 /// each file.
30 ///
31 /// This allows you to check a directory to see if any files have been modified
32 /// since you last used them, which could be useful if you are hypothetically
33 /// updating assets used within an editor tool whenever they change on disk, for
34 /// example.
35 std::unordered_map<std::string, time_t> ScanDirectory(
36  const std::string& directory, const std::string& file_ext);
37 
38 /// AssetLoader struct, basically a tuple of directory, file extension, and
39 /// loader function.
40 ///
41 /// The purpose of this is so you can scan for all the files in a given folder
42 /// matching the file extension, and load them via whatever method you wish.
43 struct AssetLoader {
44  typedef std::function<void(const char* filename)> load_function_t;
45  std::string directory;
46  std::string file_extension;
47  load_function_t load_function;
48  AssetLoader(const std::string& dir, const std::string& file_ext,
49  const load_function_t& load_func)
50  : directory(dir), file_extension(file_ext), load_function(load_func) {}
51 };
52 
53 /// Load assets via the designated asset loaders. Scans through the directory
54 /// specified by each AssetLoader for the given file pattern, and calls the load
55 /// function on each file that's strictly newer than the specified timestamp.
56 ///
57 /// Returns the timestamp of the latest file loaded (so you can pass that into
58 /// the next run of this function), or 0 if no files were loaded.
59 time_t LoadAssetsIfNewer(time_t threshold,
60  const std::vector<AssetLoader>& asset_loaders);
61 
62 /// Scan a directory for assets matching a given file extension. Any files in
63 /// the directory that are strictly newer than the time threshold will be loaded
64 /// via calling the load function passed in.
65 ///
66 /// Returns the timestamp of the latest file loaded (so you can pass that into
67 /// the next run of this function), or 0 if no files were loaded.
68 time_t LoadAssetsIfNewer(time_t threshold, const std::string& directory,
69  const std::string& file_extension,
70  const AssetLoader::load_function_t& load_function);
71 
72 } // namespace scene_lab
73 
74 #endif // SCENE_LAB_UTIL_H
AssetLoader struct, basically a tuple of directory, file extension, and loader function.
Definition: util.h:43
time_t LoadAssetsIfNewer(time_t threshold, const std::vector< AssetLoader > &asset_loaders)
Load assets via the designated asset loaders.