timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
tim::argparse::argument_parser::argument Struct Reference

#include "timemory/utility/argparse.hpp"

+ Collaboration diagram for tim::argparse::argument_parser::argument:

Public Types

enum  Position : int {
  LastArgument = -1 ,
  IgnoreArgument = -2
}
 
enum  Count : int { ANY = -1 }
 
using callback_t = std::function< void(void *&)>
 

Public Member Functions

 ~argument ()
 
argumentname (const std::string &name)
 
argumentnames (const std::vector< std::string > &names)
 
argumentdescription (const std::string &description)
 
argumentdtype (const std::string &_dtype)
 
argumentrequired (bool req)
 
argumentposition (int position)
 
argumentmax_count (int count)
 
argumentmin_count (int count)
 
argumentcount (int count)
 
template<typename T >
argumentset_default (const T &val)
 
template<typename T >
argumentset_default (T &val)
 
template<typename T >
argumentchoices (const std::initializer_list< T > &_choices)
 
template<template< typename... > class ContainerT, typename T , typename... ExtraT, typename ContT = ContainerT<T, ExtraT...>, enable_if_t< helpers::is_container< ContT >::value > = 0>
argumentchoices (const ContainerT< T, ExtraT... > &_choices)
 
template<typename ActionFuncT >
argumentaction (ActionFuncT &&_func)
 
bool found () const
 
template<typename T >
std::enable_if_t< helpers::is_container< T >::value, T > get ()
 
template<typename T >
std::enable_if_t< !helpers::is_container< T >::value &&!std::is_same< T, bool >::value, T > get ()
 
template<typename T >
std::enable_if_t< std::is_same< T, bool >::value, T > get ()
 
size_t size () const
 
std::string get_name () const
 

Friends

struct argument_parser
 
std::ostream & operator<< (std::ostream &os, const argument &arg)
 

Detailed Description

Definition at line 360 of file argparse.hpp.

Member Typedef Documentation

◆ callback_t

using tim::argparse::argument_parser::argument::callback_t = std::function<void(void*&)>

Definition at line 362 of file argparse.hpp.

Member Enumeration Documentation

◆ Count

Enumerator
ANY 

Definition at line 370 of file argparse.hpp.

◆ Position

Enumerator
LastArgument 
IgnoreArgument 

Definition at line 364 of file argparse.hpp.

Constructor & Destructor Documentation

◆ ~argument()

tim::argparse::argument_parser::argument::~argument ( )
inline

Definition at line 375 of file argparse.hpp.

375{ m_destroy(m_default); }

Member Function Documentation

◆ action()

template<typename ActionFuncT >
argument & tim::argparse::argument_parser::argument::action ( ActionFuncT &&  _func)
inline

Definition at line 495 of file argparse.hpp.

496 {
497 m_actions.push_back(std::forward<ActionFuncT>(_func));
498 return *this;
499 }

Referenced by tim::tsettings< Tp, Vp >::add_argument().

◆ choices() [1/2]

template<template< typename... > class ContainerT, typename T , typename... ExtraT, typename ContT = ContainerT<T, ExtraT...>, enable_if_t< helpers::is_container< ContT >::value > = 0>
argument & tim::argparse::argument_parser::argument::choices ( const ContainerT< T, ExtraT... > &  _choices)
inline

Definition at line 483 of file argparse.hpp.

484 {
485 for(auto&& itr : _choices)
486 {
487 std::stringstream ss;
488 ss << itr;
489 m_choices.insert(ss.str());
490 }
491 return *this;
492 }

◆ choices() [2/2]

template<typename T >
argument & tim::argparse::argument_parser::argument::choices ( const std::initializer_list< T > &  _choices)
inline

Definition at line 469 of file argparse.hpp.

470 {
471 for(auto&& itr : _choices)
472 {
473 std::stringstream ss;
474 ss << itr;
475 m_choices.insert(ss.str());
476 }
477 return *this;
478 }

Referenced by tim::tsettings< Tp, Vp >::add_argument().

◆ count()

argument & tim::argparse::argument_parser::argument::count ( int  count)
inline

Definition at line 435 of file argparse.hpp.

436 {
437 m_count = count;
438 return *this;
439 }

References count().

Referenced by tim::tsettings< Tp, Vp >::add_argument(), count(), max_count(), and min_count().

◆ description()

argument & tim::argparse::argument_parser::argument::description ( const std::string &  description)
inline

Definition at line 390 of file argparse.hpp.

391 {
392 m_desc = description;
393 return *this;
394 }
argument & description(const std::string &description)
Definition: argparse.hpp:390

References description().

Referenced by tim::argparse::argument_parser::add_argument(), and description().

◆ dtype()

argument & tim::argparse::argument_parser::argument::dtype ( const std::string &  _dtype)
inline

Definition at line 396 of file argparse.hpp.

397 {
398 m_dtype = _dtype;
399 return *this;
400 }

◆ found()

bool tim::argparse::argument_parser::argument::found ( ) const
inline

Definition at line 501 of file argparse.hpp.

501{ return m_found; }

Referenced by get().

◆ get() [1/3]

template<typename T >
std::enable_if_t< helpers::is_container< T >::value, T > tim::argparse::argument_parser::argument::get ( )
inline

Definition at line 504 of file argparse.hpp.

505 {
506 T t = T{};
507 typename T::value_type vt;
508 for(auto& s : m_values)
509 {
510 std::istringstream in(s);
511 in >> vt;
512 t.insert(t.end(), vt);
513 }
514 if(m_values.empty() && m_default &&
515 m_default_tidx == std::type_index{ typeid(T) })
516 t = (*static_cast<T*>(m_default));
517 return t;
518 }

◆ get() [2/3]

template<typename T >
std::enable_if_t< !helpers::is_container< T >::value &&!std::is_same< T, bool >::value, T > tim::argparse::argument_parser::argument::get ( )
inline

Definition at line 523 of file argparse.hpp.

524 {
525 auto inp = get<std::string>();
526 std::istringstream iss{ inp };
527 T t = T{};
528 iss >> t >> std::ws;
529 if(inp.empty() && m_default && m_default_tidx == std::type_index{ typeid(T) })
530 t = (*static_cast<T*>(m_default));
531 return t;
532 }

◆ get() [3/3]

template<typename T >
std::enable_if_t< std::is_same< T, bool >::value, T > tim::argparse::argument_parser::argument::get ( )
inline

Definition at line 535 of file argparse.hpp.

536 {
537 if(m_count == 0)
538 return found();
539
540 auto inp = get<std::string>();
541 if(inp.empty() && m_default && m_default_tidx == std::type_index{ typeid(T) })
542 return (*static_cast<T*>(m_default));
543 else if(inp.empty())
544 return found();
545
546 return get_bool(inp, found());
547 }
bool get_bool(const std::string &strbool, bool _default) noexcept
Definition: utility.cpp:75

References found(), and tim::get_bool().

◆ get_name()

std::string tim::argparse::argument_parser::argument::get_name ( ) const
inline

Definition at line 551 of file argparse.hpp.

552 {
553 std::stringstream ss;
554 for(const auto& itr : m_names)
555 ss << "/" << itr;
556 return ss.str().substr(1);
557 }

◆ max_count()

argument & tim::argparse::argument_parser::argument::max_count ( int  count)
inline

Definition at line 423 of file argparse.hpp.

424 {
425 m_max_count = count;
426 return *this;
427 }

References count().

Referenced by tim::tsettings< Tp, Vp >::add_argument().

◆ min_count()

argument & tim::argparse::argument_parser::argument::min_count ( int  count)
inline

Definition at line 429 of file argparse.hpp.

430 {
431 m_min_count = count;
432 return *this;
433 }

References count().

◆ name()

argument & tim::argparse::argument_parser::argument::name ( const std::string &  name)
inline

Definition at line 377 of file argparse.hpp.

378 {
379 m_names.push_back(name);
380 return *this;
381 }
argument & name(const std::string &name)
Definition: argparse.hpp:377

References name().

Referenced by name().

◆ names()

argument & tim::argparse::argument_parser::argument::names ( const std::vector< std::string > &  names)
inline

Definition at line 383 of file argparse.hpp.

384 {
385 for(const auto& itr : names)
386 m_names.push_back(itr);
387 return *this;
388 }
argument & names(const std::vector< std::string > &names)
Definition: argparse.hpp:383

References names().

Referenced by tim::argparse::argument_parser::add_argument(), tim::argparse::argument_parser::enable_help(), and names().

◆ position()

argument & tim::argparse::argument_parser::argument::position ( int  position)
inline

Definition at line 408 of file argparse.hpp.

409 {
410 if(position != Position::LastArgument)
411 {
412 // position + 1 because technically argument zero is the name of the
413 // executable
414 m_position = position + 1;
415 }
416 else
417 {
418 m_position = position;
419 }
420 return *this;
421 }
argument & position(int position)
Definition: argparse.hpp:408

References position().

Referenced by position().

◆ required()

argument & tim::argparse::argument_parser::argument::required ( bool  req)
inline

Definition at line 402 of file argparse.hpp.

403 {
404 m_required = req;
405 return *this;
406 }

Referenced by tim::argparse::argument_parser::add_argument().

◆ set_default() [1/2]

template<typename T >
argument & tim::argparse::argument_parser::argument::set_default ( const T &  val)
inline

Definition at line 442 of file argparse.hpp.

443 {
444 m_found = true;
445 m_default_tidx = std::type_index{ typeid(decay_t<T>) };
446 m_callback = [&](void*& obj) {
447 m_destroy(obj);
448 if(!obj)
449 obj = (void*) new T{};
450 (*static_cast<T*>(obj)) = val;
451 };
452 m_destroy = [](void*& obj) {
453 if(obj)
454 delete static_cast<T*>(obj);
455 };
456 return *this;
457 }

◆ set_default() [2/2]

template<typename T >
argument & tim::argparse::argument_parser::argument::set_default ( T &  val)
inline

Definition at line 460 of file argparse.hpp.

461 {
462 m_found = true;
463 m_default_tidx = std::type_index{ typeid(decay_t<T>) };
464 m_callback = [&](void*& obj) { obj = (void*) &val; };
465 return *this;
466 }

◆ size()

size_t tim::argparse::argument_parser::argument::size ( ) const
inline

Definition at line 549 of file argparse.hpp.

549{ return m_values.size(); }

Friends And Related Function Documentation

◆ argument_parser

friend struct argument_parser
friend

Definition at line 608 of file argparse.hpp.

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
const argument arg 
)
friend

Definition at line 591 of file argparse.hpp.

592 {
593 std::stringstream ss;
594 ss << "names: ";
595 for(const auto& itr : arg.m_names)
596 ss << itr << " ";
597 ss << ", index: " << arg.m_index << ", count: " << arg.m_count
598 << ", min count: " << arg.m_min_count << ", max count: " << arg.m_max_count
599 << ", found: " << std::boolalpha << arg.m_found
600 << ", required: " << std::boolalpha << arg.m_required
601 << ", position: " << arg.m_position << ", values: ";
602 for(const auto& itr : arg.m_values)
603 ss << itr << " ";
604 os << ss.str();
605 return os;
606 }
const std::string std::ostream * os

The documentation for this struct was generated from the following file: