Savarese Software Research Corporation
BasicServiceOptions.h
Go to the documentation of this file.
1 /* Copyright 2006-2013 Savarese Software Research Corporation.
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.savarese.com/software/ApacheLicense-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 
21 #ifndef __SSRC_WISP_UTILITY_BASIC_SERVICE_OPTIONS_H
22 #define __SSRC_WISP_UTILITY_BASIC_SERVICE_OPTIONS_H
23 
24 #include <ssrc/wisp-packages.h>
25 
26 #include <boost/program_options.hpp>
27 
29 
31 public:
32  // Allocate 64k for message. That's below the max Spread message limit
33  // and hopefully large enough to accommodate anything we throw at it.
34  enum { DefaultServiceMessageCapacity = 1u << 16 };
35 
36 private:
37  boost::program_options::options_description basic_description;
38 
39 protected:
40 
42  basic_description("Basic"), description("Options"),
43  help(false), connection(),
45  call_timeout(600),
46  locale()
47  {
48  using namespace boost::program_options;
49 
50  basic_description.add_options()
51  ("help,h", "Display this help message and exit.")
52  ("connection,c", value<std::string>(&connection),
53  "Specify the name of the Spread daemon to connect to.")
54  ("name,n", value<std::string>(&name),
55  "The Spread private name to assign the service.")
56  ("capacity,C",
57  value<unsigned int>(&capacity)->default_value(DefaultServiceMessageCapacity),
58  "The ssrc::spread::Message capacity the service requires for message "
59  "sends. This value should equal the size in bytes of the largest "
60  "message the service will ever send.")
61  ("call-timeout,t",
62  value<unsigned int>(&call_timeout)->default_value(600),
63  "The number of seconds to wait before cancelling an asynchronous call "
64  "that has not yet returned.")
65  ("locale,l", value<std::string>(&locale),
66  "The locale to use for ctype string interpretation.");
67 
68  description.add(basic_description);
69  }
70 
71  virtual void store(int argc, char *argv[]) {
72  namespace po = boost::program_options;
73  po::variables_map vm;
74  po::store(po::parse_command_line(argc, argv, description), vm);
75  notify(vm);
76  po::notify(vm);
77  }
78 
79  virtual void notify(boost::program_options::variables_map & vm) {
80  namespace po = boost::program_options;
81  help = (vm.count("help") > 0);
82  if(!help) {
83  // In order to set help when no options are specified, we can't
84  // check vm.empty() because default values add to vm.size().
85  // Therefore, we check that no non-default values are specified.
86  po::variables_map::iterator it = vm.begin();
87  while(it != vm.end()) {
88  if(!it->second.empty() && !it->second.defaulted())
89  break;
90  ++it;
91  }
92  help = (it == vm.end());
93  }
94  }
95 
96 public:
97  boost::program_options::options_description description;
98  bool help;
99  std::string connection;
100  std::string name;
101  unsigned int capacity;
102  unsigned int call_timeout;
103  std::string locale;
104 
105  virtual ~BasicServiceOptions() = default;
106 };
107 
109 
110 #endif

Savarese Software Research Corporation
Copyright © 2006-2012 Savarese Software Research Corporation. All rights reserved.