Class utils::Component
Component base class.
The component base class provides quality-of-life functionality for any class derived from it:
- It can be initialized from json via its
configure()
method - It is rendered in "structured" form via
render()
when assigned to anutils::Structure
. - Its class name is accessible via
get_class_name()
- When passed to an ostream (or
LOG
), it is represented as '<ClassName: status>' where the status is given byget_status()
While get_class_name()
works out-of-the-box, the appropriate functionality must be implemented by overloading the others accordingly.
Inheritance
Constructors
Component()
Declaration
utils::Component::Component()
Methods
~Component()
Declaration
virtual utils::Component::~Component()
configure()
configure the object from input
Initialize the object's state from the input utils::Config
. This is done by declaring which required and optional parameters are associated with the fields of this object. During initialization, they are checked for their presence, type and any matchers.
Example:
MyClass : public Component {
public:
void configure(const utils::Json& json) override {
this->param(json, "number", my_number)
.description("some description")
.matches(GreaterEquals(0))
.required();
this->param(json, "name", my_name)
.description("some description")
.matches(SizeIs(GreaterThan(0)))
.default_value("no_name");
}
private:
int my_number;
std::string my_name;
}
MyClass my_object;
my_object.configure(utils::json_from_string(R"(
{
"number": 42,
"name": "hello"
}
)"));
Note
By default, nothing is configured from input. You need to overload this method if you want to use this functionality. Don't forget to call the configure method of the parent class if it also needs to be configured (this is not the case for utils::Component
itself).
HINT: Parameters are not limited to scalars and strings; Any component can be a parameter; in which case it is initialized using its own configure method.
utils::Json
Declaration
void utils::Component::configure(const utils::Json&json)
param()
Assign a parameter from a json field.
Declaration
ParameterBuilder<utils::Component, T>utils::Component::param(const utils::Json&json, const std::string&name, T¶meter)
render()
render the object in structured form
Return a structured representation of the object. This is intended for output purposes. For instance, the solution your solver finds should have a render method which allows it to be returned as part of the result. Example:
{c++}
MySolution : public Component {
public:
// Represent the internal bool vector as +-1 output.
utils::Structure render() const override {
utils::Structure rendered;
for (bool item : solution_) rendered.push_back(item ? 1 : -1);
return rendered;
}
private:
std::vector<bool> solution_;
}
MySolution solution;
std::cout << solution.render().to_string() << std::endl;
Declaration
utils::Structure utils::Component::render() const
get_status()
get_status shows a simplified state representation
Like render
, this produces a structured representation of the object's state. However, it is intended to be simpler in nature with the purpose of rendering the object during stream-output and logging. By default, it will fall back to the full render
, but overloading this allows distinguising how the object looks during LOG vs full output.
Declaration
utils::Structure utils::Component::get_status() const
get_class_name()
get_class_name shows an identifier of the (derived) class name
Its primary use is for type identification during stream output and logging, where a component is rendered as <ClassName: status>
.
Example:
{c++}
MyClass : public Component {}
// This will render as '<MyClass>'
MyClass my_object;
std::cout << my_object << std::endl;
Note
You do not need to overload this method unless you want to change the output of the default implementation.
Declaration
std::string utils::Component::get_class_name() const