jcivelli@chromium.org | c30c76f | 2012-06-27 10:12:24 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
jeremy@chromium.org | 012e999 | 2008-10-30 05:46:04 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "multiprocess_func_list.h" |
| 6 | |
| 7 | #include <map> |
| 8 | |
| 9 | // Helper functions to maintain mapping of "test name"->test func. |
| 10 | // The information is accessed via a global map. |
| 11 | namespace multi_process_function_list { |
maruel@chromium.org | e574344 | 2009-03-05 21:46:38 +0900 | [diff] [blame] | 12 | |
jeremy@chromium.org | 012e999 | 2008-10-30 05:46:04 +0900 | [diff] [blame] | 13 | namespace { |
| 14 | |
jcivelli@chromium.org | c30c76f | 2012-06-27 10:12:24 +0900 | [diff] [blame] | 15 | struct ProcessFunctions { |
| 16 | ProcessFunctions() : main(NULL), setup(NULL) {} |
| 17 | ProcessFunctions(TestMainFunctionPtr main, SetupFunctionPtr setup) |
| 18 | : main(main), |
| 19 | setup(setup) { |
| 20 | } |
| 21 | TestMainFunctionPtr main; |
| 22 | SetupFunctionPtr setup; |
| 23 | }; |
| 24 | |
| 25 | typedef std::map<std::string, ProcessFunctions> MultiProcessTestMap; |
maruel@chromium.org | e574344 | 2009-03-05 21:46:38 +0900 | [diff] [blame] | 26 | |
jeremy@chromium.org | 012e999 | 2008-10-30 05:46:04 +0900 | [diff] [blame] | 27 | // Retrieve a reference to the global 'func name' -> func ptr map. |
jcivelli@chromium.org | c30c76f | 2012-06-27 10:12:24 +0900 | [diff] [blame] | 28 | MultiProcessTestMap& GetMultiprocessFuncMap() { |
jeremy@chromium.org | 012e999 | 2008-10-30 05:46:04 +0900 | [diff] [blame] | 29 | static MultiProcessTestMap test_name_to_func_ptr_map; |
| 30 | return test_name_to_func_ptr_map; |
| 31 | } |
maruel@chromium.org | e574344 | 2009-03-05 21:46:38 +0900 | [diff] [blame] | 32 | |
jeremy@chromium.org | 012e999 | 2008-10-30 05:46:04 +0900 | [diff] [blame] | 33 | } // namespace |
| 34 | |
jcivelli@chromium.org | c30c76f | 2012-06-27 10:12:24 +0900 | [diff] [blame] | 35 | AppendMultiProcessTest::AppendMultiProcessTest( |
| 36 | std::string test_name, |
| 37 | TestMainFunctionPtr main_func_ptr, |
| 38 | SetupFunctionPtr setup_func_ptr) { |
| 39 | GetMultiprocessFuncMap()[test_name] = |
| 40 | ProcessFunctions(main_func_ptr, setup_func_ptr); |
jeremy@chromium.org | 012e999 | 2008-10-30 05:46:04 +0900 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | int InvokeChildProcessTest(std::string test_name) { |
jcivelli@chromium.org | c30c76f | 2012-06-27 10:12:24 +0900 | [diff] [blame] | 44 | MultiProcessTestMap& func_lookup_table = GetMultiprocessFuncMap(); |
jeremy@chromium.org | 012e999 | 2008-10-30 05:46:04 +0900 | [diff] [blame] | 45 | MultiProcessTestMap::iterator it = func_lookup_table.find(test_name); |
| 46 | if (it != func_lookup_table.end()) { |
jcivelli@chromium.org | c30c76f | 2012-06-27 10:12:24 +0900 | [diff] [blame] | 47 | const ProcessFunctions& process_functions = it->second; |
| 48 | if (process_functions.setup) |
| 49 | (*process_functions.setup)(); |
| 50 | if (process_functions.main) |
| 51 | return (*process_functions.main)(); |
jeremy@chromium.org | 012e999 | 2008-10-30 05:46:04 +0900 | [diff] [blame] | 52 | } |
maruel@chromium.org | e574344 | 2009-03-05 21:46:38 +0900 | [diff] [blame] | 53 | |
jeremy@chromium.org | 012e999 | 2008-10-30 05:46:04 +0900 | [diff] [blame] | 54 | return -1; |
| 55 | } |
| 56 | |
| 57 | } // namespace multi_process_function_list |