Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. // Each command-line flag has two variables associated with it: one
  2. // with the current value, and one with the default value. However,
  3. // we have a third variable, which is where value is assigned; it's a
  4. // constant. This guarantees that FLAG_##value is initialized at
  5. // static initialization time (e.g. before program-start) rather than
  6. // than global construction time (which is after program-start but
  7. // before main), at least when 'value' is a compile-time constant. We
  8. // use a small trick for the "default value" variable, and call it
  9. // FLAGS_no<name>. This serves the second purpose of assuring a
  10. // compile error if someone tries to define a flag named no<name>
  11. // which is illegal (--foo and --nofoo both affect the "foo" flag).
  12. #define DEFINE_VARIABLE(type, shorttype, name, value, help)
  13. namespace fL##shorttype {
  14. static const type FLAGS_nono##name = value;
  15. /* We always want to export defined variables, dll or no */
  16. GFLAGS_DLL_DEFINE_FLAG type FLAGS_##name = FLAGS_nono##name;
  17. type FLAGS_no##name = FLAGS_nono##name;
  18. static @GFLAGS_NAMESPACE@::FlagRegisterer o_##name(
  19. #name, #type, MAYBE_STRIPPED_HELP(help), __FILE__,
  20. &FLAGS_##name, &FLAGS_no##name);
  21. }
  22. using fL##shorttype::FLAGS_##name
  23.  
  24. class GFLAGS_DLL_DECL FlagRegisterer {
  25. public:
  26. FlagRegisterer(const char* name, const char* type,
  27. const char* help, const char* filename,
  28. void* current_storage, void* defvalue_storage);
  29. };
  30.  
  31.  
  32. // --------------------------------------------------------------------
  33. // FlagRegisterer
  34. // This class exists merely to have a global constructor (the
  35. // kind that runs before main(), that goes an initializes each
  36. // flag that's been declared. Note that it's very important we
  37. // don't have a destructor that deletes flag_, because that would
  38. // cause us to delete current_storage/defvalue_storage as well,
  39. // which can cause a crash if anything tries to access the flag
  40. // values in a global destructor.
  41. // --------------------------------------------------------------------
  42.  
  43. FlagRegisterer::FlagRegisterer(const char* name, const char* type,
  44. const char* help, const char* filename,
  45. void* current_storage, void* defvalue_storage) {
  46. if (help == NULL)
  47. help = "";
  48. // FlagValue expects the type-name to not include any namespace
  49. // components, so we get rid of those, if any.
  50. if (strchr(type, ':'))
  51. type = strrchr(type, ':') + 1;
  52. FlagValue* current = new FlagValue(current_storage, type, false);
  53. FlagValue* defvalue = new FlagValue(defvalue_storage, type, false);
  54. // Importantly, flag_ will never be deleted, so storage is always good.
  55. CommandLineFlag* flag = new CommandLineFlag(name, help, filename,
  56. current, defvalue);
  57. FlagRegistry::GlobalRegistry()->RegisterFlag(flag); // default registry
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement