CASM  1.1.0
A Clusters Approach to Statistical Mechanics
CASM::InputParser< T > Class Template Reference

#include <InputParser.hh>

+ Inheritance diagram for CASM::InputParser< T >:

Detailed Description

template<typename T>
class CASM::InputParser< T >

Constructs values from JSON and collects error and warning messages for easy printing, without throwing.

To use InputParser for a type, T, you must write: void parse(InputParser<T> &parser, ... any other required input ...);

The parse function should call methods of InputParser (i.e. require, optional, subparse, etc.) to automate the handling of errors and capture of error and warning messages.

The parse function is called when InputParser<T> is constructed from input json: jsonParser json_input = ...; InputParser<T> parser {json_input, ... any other required input ...};

When parsing complex objects, subparsers may be called and they will store their errors and warnings in a map of (path to the JSON subobject being parsed) : (string with an error or warning message).

To check if parsing was successful and conditionally print errors & warnings, with the path to the location in the json where the issue occurred, do: Log& log = ...; if(!parser.valid()) { jsonParser report = make_report(parser); print_errors(parser, log); log << std::endl << report << std::endl << std::endl; ... handle error or throw ... } if(parser.all_warnings().size()) { jsonParser report = make_report(parser); print_warnings(parser, log); log << std::endl << report << std::endl << std::endl; }

To do the above and throw an exception if the parser has any errors, use: MyExceptionType error {"... message ..."}; report_and_throw_if_invalid(parser, log, error);

Get the set of errors and warnings, including all subparsers, use: std::set<std::string> warnings = parser.all_warnings(); std::set<std::string> errors = parser.all_errors();

If parsing could not occur, then the parse function may leave parser.value as an empty unique_ptr<T>, otherwise parser.value should hold the resulting value. So if parsing was successful you can get the constructed value with *parser.value.

Definition at line 230 of file InputParser.hh.

Public Types

typedef std::multimap< fs::path, std::shared_ptr< KwargsParser > > map_type
 

Public Member Functions

template<typename... Args>
 InputParser (jsonParser const &_input, Args &&... args)
 Construct parser and use parse(*this) More...
 
template<typename... Args>
 InputParser (jsonParser const &_input, fs::path _path, bool _required, Args &&... args)
 
template<typename CustomParse >
 InputParser (CustomParse f_parse, jsonParser const &_input)
 Construct parser and use custom parse function, f_parse(*this) More...
 
template<typename CustomParse >
 InputParser (CustomParse f_parse, jsonParser const &_input, fs::path _path, bool _required)
 
template<typename RequiredType , typename... Args>
std::unique_ptr< RequiredType > require (fs::path option, Args &&... args)
 
template<typename RequiredType , typename... Args>
void require (RequiredType &value, fs::path option, Args &&... args)
 
template<typename RequiredType , typename... Args>
std::unique_ptr< RequiredType > optional (fs::path option, Args &&... args)
 
template<typename RequiredType , typename... Args>
void optional (RequiredType &value, fs::path option, Args &&... args)
 
template<typename RequiredType , typename... Args>
RequiredType optional_else (fs::path option, const RequiredType &_default, Args &&... args)
 
template<typename RequiredType , typename... Args>
void optional_else (RequiredType &value, fs::path option, const RequiredType &_default, Args &&... args)
 
template<typename RequiredType , typename... Args>
std::shared_ptr< InputParser< RequiredType > > subparse (fs::path option, Args &&... args)
 
template<typename RequiredType , typename... Args>
std::shared_ptr< InputParser< RequiredType > > subparse_if (fs::path option, Args &&... args)
 
template<typename RequiredType , typename... Args>
std::shared_ptr< InputParser< RequiredType > > subparse_else (fs::path option, const RequiredType &_default, Args &&... args)
 
template<typename RequiredType , typename... Args>
std::shared_ptr< InputParser< RequiredType > > parse_as (Args &&... args)
 
const jsonParserparent () const
 
fs::path parent_path () const
 
std::string name () const
 Name of this->self, equivalent to this->path.filename().string() More...
 
bool exists () const
 
fs::path relpath (const fs::path &val) const
 Return this->path / val, ensuring the result is a relative path. More...
 
map_type::const_iterator begin () const
 Begin iterator over subparsers. More...
 
map_type::const_iterator end () const
 End iterator over subparsers. More...
 
bool valid () const
 Return true if this and and all subparsers are valid. More...
 
std::map< fs::path, std::set< std::string > > all_warnings () const
 Return warning messages from this and all subparsers. More...
 
std::map< fs::path, std::set< std::string > > all_errors () const
 Return error messages from this and all subparsers. More...
 
void insert (fs::path path, const std::shared_ptr< KwargsParser > &subparser)
 
Validatorinsert (const Validator &other)
 
Validatorinsert (const Validator &other)
 
void insert_error (fs::path option, std::string message)
 Insert a subparser at location option with a single error message More...
 
void insert_warning (fs::path option, std::string message)
 Insert a subparser at location option with a single warning message More...
 
bool warn_unnecessary (const std::set< std::string > &expected)
 Insert a warning if any unexpected JSON attributes are found in self. More...
 
void clear ()
 

Public Attributes

std::unique_ptr< T > value
 
jsonParser const & input
 Reference to the top of the JSON document being parsed. More...
 
fs::path path
 
jsonParser const & self
 
bool required
 If this->input.at(this->path) is required to exist. More...
 
std::string type_name
 
std::set< std::string > error
 
std::set< std::string > warning
 

Private Types

typedef map_type::value_type PairType
 

Private Attributes

map_type m_subparsers
 

Member Typedef Documentation

◆ map_type

typedef std::multimap<fs::path, std::shared_ptr<KwargsParser> > CASM::KwargsParser::map_type
inherited

Definition at line 134 of file InputParser.hh.

◆ PairType

typedef map_type::value_type CASM::KwargsParser::PairType
privateinherited

Definition at line 172 of file InputParser.hh.

Constructor & Destructor Documentation

◆ InputParser() [1/4]

template<typename T >
template<typename... Args>
CASM::InputParser< T >::InputParser ( jsonParser const &  _input,
Args &&...  args 
)

Construct parser and use parse(*this)

Definition at line 15 of file InputParser_impl.hh.

◆ InputParser() [2/4]

template<typename T >
template<typename... Args>
CASM::InputParser< T >::InputParser ( jsonParser const &  _input,
fs::path  _path,
bool  _required,
Args &&...  args 
)

Construct parser and use parse(*this, std::forward<Args>(args)...) if _path exists

Definition at line 24 of file InputParser_impl.hh.

◆ InputParser() [3/4]

template<typename T >
template<typename CustomParse >
CASM::InputParser< T >::InputParser ( CustomParse  f_parse,
jsonParser const &  _input 
)

Construct parser and use custom parse function, f_parse(*this)

Definition at line 35 of file InputParser_impl.hh.

◆ InputParser() [4/4]

template<typename T >
template<typename CustomParse >
CASM::InputParser< T >::InputParser ( CustomParse  f_parse,
jsonParser const &  _input,
fs::path  _path,
bool  _required 
)

Construct parser and use custom parse function, f_parse(*this), if _path exists

Definition at line 44 of file InputParser_impl.hh.

Member Function Documentation

◆ all_errors()

std::map< fs::path, std::set< std::string > > CASM::KwargsParser::all_errors ( ) const
inherited

Return error messages from this and all subparsers.

Definition at line 77 of file InputParser.cc.

◆ all_warnings()

std::map< fs::path, std::set< std::string > > CASM::KwargsParser::all_warnings ( ) const
inherited

Return warning messages from this and all subparsers.

Definition at line 61 of file InputParser.cc.

◆ begin()

KwargsParser::map_type::const_iterator CASM::KwargsParser::begin ( ) const
inherited

Begin iterator over subparsers.

Definition at line 48 of file InputParser.cc.

◆ clear()

void CASM::Validator::clear ( )
inlineinherited

Definition at line 14 of file Validator.hh.

◆ end()

KwargsParser::map_type::const_iterator CASM::KwargsParser::end ( ) const
inherited

End iterator over subparsers.

Definition at line 52 of file InputParser.cc.

◆ exists()

bool CASM::KwargsParser::exists ( ) const
inherited

Check if this->input.find_at(this->path) exists

When KwargsParser is contructed with a path that does not exist, then this->self=this->input.

Definition at line 44 of file InputParser.cc.

◆ insert() [1/3]

Validator& CASM::Validator::insert
inlineinherited

Definition at line 19 of file Validator.hh.

◆ insert() [2/3]

Validator& CASM::Validator::insert ( const Validator other)
inlineinherited

Definition at line 19 of file Validator.hh.

◆ insert() [3/3]

void CASM::KwargsParser::insert ( fs::path  path,
const std::shared_ptr< KwargsParser > &  subparser 
)
inherited

Insert a subparser

Subparsers are stored in a map of path (from this->input) to the subparser. After being inserted, the subparser's errors and warnings are included in validation checks (valid) and in parser output (make_report, print_warnings, print_errors, etc.).

Definition at line 92 of file InputParser.cc.

◆ insert_error()

void CASM::KwargsParser::insert_error ( fs::path  option,
std::string  message 
)
inherited

Insert a subparser at location option with a single error message

Definition at line 97 of file InputParser.cc.

◆ insert_warning()

void CASM::KwargsParser::insert_warning ( fs::path  option,
std::string  message 
)
inherited

Insert a subparser at location option with a single warning message

Definition at line 104 of file InputParser.cc.

◆ name()

std::string CASM::KwargsParser::name ( ) const
inherited

Name of this->self, equivalent to this->path.filename().string()

Definition at line 42 of file InputParser.cc.

◆ optional() [1/2]

template<typename T >
template<typename RequiredType , typename... Args>
std::unique_ptr< RequiredType > CASM::InputParser< T >::optional ( fs::path  option,
Args &&...  args 
)

Check that if self.find_at(option) exists it can constructed as type RequiredType, returning result in unique_ptr

Failing to parse the component will result in errors of type:

If self.find_at(option) does not exist, return empty unique_ptr

Definition at line 105 of file InputParser_impl.hh.

◆ optional() [2/2]

template<typename T >
template<typename RequiredType , typename... Args>
void CASM::InputParser< T >::optional ( RequiredType &  value,
fs::path  option,
Args &&...  args 
)

Check that if self.find_at(option) exists it can constructed as type RequiredType, assigning result to value

Failing to parse the component will result in errors of type:

If self.find_at(option) does not exist, do not change value and do not insert an error.

Definition at line 133 of file InputParser_impl.hh.

◆ optional_else() [1/2]

template<typename T >
template<typename RequiredType , typename... Args>
RequiredType CASM::InputParser< T >::optional_else ( fs::path  option,
const RequiredType &  _default,
Args &&...  args 
)

Check for self.find_at(option), return value or default, error if cannot be constructed

Failing to parse the component will result in errors of type:

If self.find_at(option) does not exist, return _default and do not insert an error.

Definition at line 162 of file InputParser_impl.hh.

◆ optional_else() [2/2]

template<typename T >
template<typename RequiredType , typename... Args>
void CASM::InputParser< T >::optional_else ( RequiredType &  value,
fs::path  option,
const RequiredType &  _default,
Args &&...  args 
)

Check for self.find_at(option), assign result or default, error if cannot be constructed

Failing to parse the component will result in errors of type:

If self.find(option) does not exist, assign _default to value and do not insert an error.

Definition at line 191 of file InputParser_impl.hh.

◆ parent()

const jsonParser & CASM::KwargsParser::parent ( ) const
inherited

Return a const reference to the parent JSON object of this->self

If self==input, returns self.

Definition at line 33 of file InputParser.cc.

◆ parent_path()

fs::path CASM::KwargsParser::parent_path ( ) const
inherited

Return a fs::path from the top-level to the parent JSON object of this->self

The result satisfies: this->parent()==this->input.find_at(this->parent_path())

Definition at line 40 of file InputParser.cc.

◆ parse_as()

template<typename T >
template<typename RequiredType , typename... Args>
std::shared_ptr< InputParser< RequiredType > > CASM::InputParser< T >::parse_as ( Args &&...  args)

Parse this->self as RequiredType

Parameters
argsArguments forwared to the parse method

Definition at line 256 of file InputParser_impl.hh.

◆ relpath()

fs::path CASM::KwargsParser::relpath ( const fs::path &  val) const
inlineinherited

Return this->path / val, ensuring the result is a relative path.

Definition at line 128 of file InputParser.hh.

◆ require() [1/2]

template<typename T >
template<typename RequiredType , typename... Args>
std::unique_ptr< RequiredType > CASM::InputParser< T >::require ( fs::path  option,
Args &&...  args 
)

Require self.find_at(option) of type RequiredType, returning result in unique_ptr

Failing to parse the component will result in errors of type:

Definition at line 54 of file InputParser_impl.hh.

◆ require() [2/2]

template<typename T >
template<typename RequiredType , typename... Args>
void CASM::InputParser< T >::require ( RequiredType &  value,
fs::path  option,
Args &&...  args 
)

Require self.find_at(option) of type RequiredType, assigning result to value

Failing to parse the component will result in errors of type:

Definition at line 79 of file InputParser_impl.hh.

◆ subparse()

template<typename T >
template<typename RequiredType , typename... Args>
std::shared_ptr< InputParser< RequiredType > > CASM::InputParser< T >::subparse ( fs::path  option,
Args &&...  args 
)

Run an InputParser on the JSON subobject at this->path / option, collecting errors and warnings

Will:

  • Subparser errors and warnings are stored using this->insert

Equivalent to:

auto subparser = std::make_shared<InputParser<RequiredType>>(
this->input, this->relpath(option), true, std::forward<Args>(args)...);
this->insert(subparser->path, subparser);
return subparser;
Validator & insert(const Validator &other)
Definition: Validator.hh:19
fs::path relpath(const fs::path &val) const
Return this->path / val, ensuring the result is a relative path.
Definition: InputParser.hh:128
jsonParser const & input
Reference to the top of the JSON document being parsed.
Definition: InputParser.hh:68

Definition at line 222 of file InputParser_impl.hh.

◆ subparse_else()

template<typename T >
template<typename RequiredType , typename... Args>
std::shared_ptr< InputParser< RequiredType > > CASM::InputParser< T >::subparse_else ( fs::path  option,
const RequiredType &  _default,
Args &&...  args 
)

Subparse, if this->path / option exists, else the result->value will be copy-constructed from _default

Definition at line 244 of file InputParser_impl.hh.

◆ subparse_if()

template<typename T >
template<typename RequiredType , typename... Args>
std::shared_ptr< InputParser< RequiredType > > CASM::InputParser< T >::subparse_if ( fs::path  option,
Args &&...  args 
)

Subparse, if this->path / option exists

If the JSON subobject does not exist, the result->value will be empty

Definition at line 233 of file InputParser_impl.hh.

◆ valid()

bool CASM::KwargsParser::valid ( ) const
inherited

Return true if this and and all subparsers are valid.

Definition at line 56 of file InputParser.cc.

◆ warn_unnecessary()

bool CASM::KwargsParser::warn_unnecessary ( const std::set< std::string > &  expected)
inherited

Insert a warning if any unexpected JSON attributes are found in self.

Definition at line 111 of file InputParser.cc.

Member Data Documentation

◆ error

std::set<std::string> CASM::Validator::error
inherited

Definition at line 11 of file Validator.hh.

◆ input

jsonParser const& CASM::KwargsParser::input
inherited

Reference to the top of the JSON document being parsed.

Definition at line 68 of file InputParser.hh.

◆ m_subparsers

map_type CASM::KwargsParser::m_subparsers
privateinherited

Used to store sub-parsers. Allows code re-use w/ storage of all errors & warnings. Can use static_cast to get values from InputParser subparsers if they are included.

Definition at line 178 of file InputParser.hh.

◆ path

fs::path CASM::KwargsParser::path
inherited

Path to the JSON component to be parsed from the opt of the JSON document. If it exists, this->self = this->input.at(this->path))

Definition at line 72 of file InputParser.hh.

◆ required

bool CASM::KwargsParser::required
inherited

If this->input.at(this->path) is required to exist.

Definition at line 84 of file InputParser.hh.

◆ self

jsonParser const& CASM::KwargsParser::self
inherited

Reference to the JSON component to be parsed, if it exists, otherwise set to this->input

Note:

  • if path.empty(), or path not found in input, self = input;
    • else: self = *input.find_at(path)
  • Use this->exists() to check if the JSON component exists in the document

Definition at line 81 of file InputParser.hh.

◆ type_name

std::string CASM::KwargsParser::type_name
inherited

Default empty, can be used to differentiate between parsers when multiple values are parsed from a single JSON object

Definition at line 88 of file InputParser.hh.

◆ value

template<typename T >
std::unique_ptr<T> CASM::InputParser< T >::value

Store the object being read from JSON, use unique_ptr so default constructor not necessary

Definition at line 234 of file InputParser.hh.

◆ warning

std::set<std::string> CASM::Validator::warning
inherited

Definition at line 12 of file Validator.hh.


The documentation for this class was generated from the following files: