[PATCH] libcamera: yaml-parser: Differentiate between empty and empty string

Barnabás Pőcze pobrn at protonmail.com
Wed Sep 18 15:07:29 CEST 2024


Hi


2024. szeptember 18., szerda 11:25 keltezéssel, Stefan Klug <stefan.klug at ideasonboard.com> írta:

> Hi Barnabás,
> 
> On Tue, Sep 17, 2024 at 01:45:13PM +0000, Barnabás Pőcze wrote:
> > Hi
> >
> >
> > 2024. szeptember 17., kedd 00:28 keltezéssel, Stefan Klug <stefan.klug at ideasonboard.com> írta:
> >
> > > When accessing a nonexistent key on a dict the YamlObject returns an
> > > empty element. This element can happily be casted to a string which is
> > > unexpected. For example the following statement:
> > > yamlDict["nonexistant"].get<string>("default") is expected to return
> > > "default" but actually returns "". Fix this by introducing a empty type
> > > to distinguish between an empty YamlObject and a YamlObject of type
> > > value containing an empty string. For completeness add an isEmpty()
> > > function to be able to test for that type.
> > >
> > > Signed-off-by: Stefan Klug <stefan.klug at ideasonboard.com>
> > > ---
> > >  include/libcamera/internal/yaml_parser.h |  5 +++++
> > >  src/libcamera/yaml_parser.cpp            | 20 ++++++++++++++------
> > >  test/yaml-parser.cpp                     |  6 ++++++
> > >  3 files changed, 25 insertions(+), 6 deletions(-)
> > >
> > > diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h
> > > index 16708e488d88..33c3a96fed47 100644
> > > --- a/include/libcamera/internal/yaml_parser.h
> > > +++ b/include/libcamera/internal/yaml_parser.h
> > > @@ -159,6 +159,10 @@ public:
> > >  	{
> > >  		return type_ == Type::Dictionary;
> > >  	}
> > > +	bool isEmpty() const
> > > +	{
> > > +		return type_ == Type::Empty;
> > > +	}
> >
> > I think `constexpr explicit operator bool() const` would be a good addition as well.
> > Then you can just do e.g.
> >
> >   if (auto y = x["something"]) {
> >     // ...
> >   }
> 
> Thanks for the suggestion. I'll add that. One question though. Why the
> constexpr? I can't come up with a practical case were that can be
> resolved at compiletime.

I did it mostly automatically, so I guess you're right. Habit is second nature...


Regards,
Barnabás Pőcze


More information about the libcamera-devel mailing list