JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
Manager.H
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
18// This code is inspired by the Neuromorphic Robotics Toolkit (http://nrtkit.org)
19
20#pragma once
21
23#include <jevois/Types/Enum.H>
24
25#include <vector>
26
27// BEGIN_JEVOIS_CODE_SNIPPET manager1.C
28namespace jevois
29{
30 namespace manager
31 {
32 static ParameterCategory const ParamCateg("General Options");
33
34 //! Parameter \relates jevois::Manager
35 JEVOIS_DECLARE_PARAMETER(help, bool, "Print this help message", false, ParamCateg);
36
37#ifdef JEVOIS_LDEBUG_ENABLE
38 //! Enum for Parameter \relates jevois::Manager
39 JEVOIS_DEFINE_ENUM_CLASS(LogLevel, (fatal) (error) (info) (debug) );
40#else
41 //! Enum for Parameter \relates jevois::Manager
42 JEVOIS_DEFINE_ENUM_CLASS(LogLevel, (fatal) (error) (info) );
43#endif
44
45 //! Parameter \relates jevois::Manager
46 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(loglevel, LogLevel, "Set the minimum log level to display",
47 LogLevel::info, LogLevel_Values, ParamCateg);
48
49 //! Parameter \relates jevois::Manager
50 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(tracelevel, unsigned int, "Set the minimum trace level to display",
51 0, ParamCateg);
52
53 //! Parameter \relates jevois::Manager
54 JEVOIS_DECLARE_PARAMETER(nickname, std::string, "Nickname associated with this camera, useful when multiple "
55 "JeVois cameras are connected to a same USB bus", "jevois", ParamCateg);
56 }
57
58 // ######################################################################
59 //! Manager of a hierarchy of Component objects
60 /*! A Manager should be the top-level Component of any hierarchy of Components. It is primarily responsible for
61 handling the setting of Parameter values via the command-line or otherwise.
62
63 Users should only need to construct a Manager (including Engine, which derives from Manager), add any Component to
64 it, and then call init() on the Manager, which will parse all command line options, bind them to the relevant
65 Parameters, and call init() on all subComponents (which in turn calls init() on all of their subComponents,
66 etc.). See the documentation of Component for more information about the init() flow.
67
68 The parameter \p nickname is not internally used by the Manager. It can be set, for example, in \b initscript.cfg
69 to a different value for each camera, in systems that use multiple JeVois cameras connected to a single USB bus.
70
71 \ingroup component */
72 class Manager : public Component,
73 public Parameter<manager::help, manager::loglevel, manager::tracelevel, manager::nickname>
74 {
75 public:
76 // END_JEVOIS_CODE_SNIPPET
77
78 //! @name Manager construction, destruction, parsing command-line arguments
79 //! @{
80
81 //! Constructor without command-line args
82 /*! The command-line args should be passed later using setArgs(), before you init(), otherwise the manager will
83 issue a non-fatal warning. */
84 Manager(std::string const & instance = "TheManager");
85
86 //! Constructor
87 /*! Creates the Manager, and optionally takes in the command line arguments. */
88 Manager(int argc, char const* argv[], std::string const & instance = "TheManager");
89
90 //! Set the command-line arguments, call this before start() if args were not passed at construction
91 void setCommandLineArgs(int argc, char const* argv[]);
92
93 //! Destructor
94 virtual ~Manager();
95
96 //! @}
97
98 //! @name Component hierarchies under the Manager
99 //! @{
100
101 //! Pseudo-constructor: construct a top-level Component
102 /*! A component of type Comp (which must derive from jevois::Component) will be created and added as a
103 sub-component of the manager (making it a so-called top-level component). The child logically "belongs" to the
104 Manager, and will automatically be deleted when the Manager is deleted. In addition to construction, the
105 component will be brought to the same initialized state as the Manager. */
106 template <class Comp, typename... Args>
107 std::shared_ptr<Comp> addComponent(std::string const & instanceName, Args && ...args);
108
109 //! Use addComponent() on the Manager as opposed to jevois::Component::addSubComponent()
110 template <class Comp, typename... Args>
111 std::shared_ptr<Comp> addSubComponent(std::string const & instanceName, Args && ...args) = delete;
112
113 //! Remove a top-level Component from the Manager, by shared_ptr
114 /*! \note Beware that the passed shared_ptr is invalidated in the process. A warning is issued if the use_count is
115 not down to zero after that (i.e., there are additional shared_ptr pointers to this Component floating around,
116 which prevent it from actually being deleted. */
117 template <class Comp>
118 void removeComponent(std::shared_ptr<Comp> & component);
119
120 //! Use removeComponent() on the Manager as opposed to jevois::Component::removeSubComponent()
121 template <class Comp>
122 void removeSubComponent(std::shared_ptr<Comp> & component) = delete;
123
124 //! Remove a top-level Component from the Manager, by instance name
125 void removeComponent(std::string const & instanceName, bool warnIfNotFound = true);
126
127 //! Use removeComponent() on the Manager as opposed to jevois::Component::removeSubComponent()
128 void removeSubComponent(std::string const & instanceName, bool warnIfNotFound) = delete;
129
130 //! Get a top-level component by instance name
131 /*! This method does a dynamic_pointer_cast to Comp if it is not the default (jevois::Component). Throws if
132 component is not found by instance name, or it is found but not of type Comp (if Comp is specified). Note that
133 once you hold a shared_ptr to a Component, it is guaranteed that the component will not be destroyed until
134 that shared_ptr is released. If the JeVois system tries to destroy the component, e.g., someone calls
135 removeComponent(), the component will be un-initialized and its parent will be unset, so it will not be
136 fully operational, and will be actually deleted when the last shared_ptr to it runs out of scope. */
137 template <class Comp = jevois::Component>
138 std::shared_ptr<Comp> getComponent(std::string const & instanceName) const;
139
140 //! Use getComponent() on the Manager as opposed to jevois::Component::getSubComponent()
141 template <class Comp>
142 std::shared_ptr<Comp> getSubComponent(std::string const & instanceName) const = delete;
143
144 //! @}
145
146 //! @name Manager runtime
147 //! @{
148
149 //! Users can call init() on the Manager to initialize the whole Component tree
150 /*! This will, in particular, trigger parsing of command-line arguments. */
151 using Component::init;
152
153 //! Users can call uninit() on the Manager to un-initialize the whole Component tree
154 /*! If started(), this will first call stop() */
155 using Component::uninit;
156
157 //! Get the remaining arguments that were not parsed by the command line
158 /*! Any command line arguments after a lone '--' will be available in remainingArgs() */
159 std::vector<std::string> const & remainingArgs() const;
160
161 //! @}
162
163 protected:
164 // BEGIN_JEVOIS_CODE_SNIPPET manager2.C
165
166 //! Parameter callback
167 void onParamChange(manager::loglevel const & param, manager::LogLevel const & newval) override;
168
169 //! Parameter callback
170 void onParamChange(manager::tracelevel const & param, unsigned int const & newval) override;
171 // END_JEVOIS_CODE_SNIPPET
172
173 //! Calls parseCommandLine()
174 void preInit() override;
175
176 //! Checks for the --help flag
177 void postInit() override;
178
179 //! Constructs a help message from all parameters in the model, and outputs it to 'out'
180 void constructHelpMessage(std::ostream & out) const;
181
182 //! Constructs a help message and tries to send it to /usr/bin/less
183 void printHelpMessage() const;
184
185 private:
186 //! Parse the command line and internally store the resulting remaining args
187 void doParseCommandLine();
188
189 //! Parses the command line, and applies settings to all Parameters
190 /*! See the definition of Parameter for details on how to specify them from the command line. */
191 std::vector<std::string> const parseCommandLine(std::vector<std::string> const & commandLineArgs);
192
193 //! The original command line arguments
194 std::vector<std::string> itsCommandLineArgs;
195
196 //! Did we get command-line args (even though there may be none, just to detect if forgot)
197 bool itsGotArgs;
198
199 //! Any command line arguments not used by the model
200 std::vector<std::string> itsRemainingArgs;
201 };
202} // namespace jevois
203
204// Include implementation details
205#include <jevois/Component/details/ManagerImpl.H>
A component of a model hierarchy.
Definition Component.H:182
std::string const & instanceName() const
The instance name of this component.
Definition Component.C:50
friend class Manager
Definition Component.H:513
Manager of a hierarchy of Component objects.
Definition Manager.H:74
JEVOIS_DEFINE_ENUM_CLASS(LogLevel,(fatal)(error)(info)(debug))
Enum for Parameter.
void removeSubComponent(std::string const &instanceName, bool warnIfNotFound)=delete
Use removeComponent() on the Manager as opposed to jevois::Component::removeSubComponent()
std::shared_ptr< Comp > addComponent(std::string const &instanceName, Args &&...args)
Pseudo-constructor: construct a top-level Component.
void setCommandLineArgs(int argc, char const *argv[])
Set the command-line arguments, call this before start() if args were not passed at construction.
Definition Manager.C:38
void removeComponent(std::shared_ptr< Comp > &component)
Remove a top-level Component from the Manager, by shared_ptr.
std::shared_ptr< Comp > getSubComponent(std::string const &instanceName) const =delete
Use getComponent() on the Manager as opposed to jevois::Component::getSubComponent()
void onParamChange(manager::loglevel const &param, manager::LogLevel const &newval) override
Parameter callback.
void postInit() override
Checks for the –help flag.
Definition Manager.C:58
std::shared_ptr< Comp > addSubComponent(std::string const &instanceName, Args &&...args)=delete
Use addComponent() on the Manager as opposed to jevois::Component::addSubComponent()
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(tracelevel, unsigned int, "Set the minimum trace level to display", 0, ParamCateg)
Parameter.
virtual ~Manager()
Destructor.
Definition Manager.C:45
JEVOIS_DECLARE_PARAMETER(nickname, std::string, "Nickname associated with this camera, useful when multiple " "JeVois cameras are connected to a same USB bus", "jevois", ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(help, bool, "Print this help message", false, ParamCateg)
Parameter.
void onParamChange(manager::tracelevel const &param, unsigned int const &newval) override
Parameter callback.
void constructHelpMessage(std::ostream &out) const
Constructs a help message from all parameters in the model, and outputs it to 'out'.
Definition Manager.C:86
std::vector< std::string > const & remainingArgs() const
Get the remaining arguments that were not parsed by the command line.
Definition Manager.C:172
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(loglevel, LogLevel, "Set the minimum log level to display", LogLevel::info, LogLevel_Values, ParamCateg)
Parameter.
std::shared_ptr< Comp > getComponent(std::string const &instanceName) const
Get a top-level component by instance name.
void printHelpMessage() const
Constructs a help message and tries to send it to /usr/bin/less.
Definition Manager.C:80
void preInit() override
Calls parseCommandLine()
Definition Manager.C:49
void removeSubComponent(std::shared_ptr< Comp > &component)=delete
Use removeComponent() on the Manager as opposed to jevois::Component::removeSubComponent()
JEVOIS_DEFINE_ENUM_CLASS(CameraSensor,(any)(imx290)(os08a10)(ar0234))
Enum for different sensor models.
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2