multiprocess_func_list.cc 1.7 KB

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