Changeset 50377a4 for src/Common


Ignore:
Timestamp:
Oct 2, 2017, 4:39:42 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
bf4b4cf
Parents:
a8555c5
Message:

Refactor tree print code to use Indenter

Location:
src/Common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Common/Indenter.h

    ra8555c5 r50377a4  
    1818
    1919struct Indenter {
    20         Indenter( unsigned int amt = 2 ) : amt( amt ) {}
    21         unsigned int amt = 2;  // amount 1 level increases indent by (i.e. how much to increase by in operator++)
    22         unsigned int indent = 0;
     20        static unsigned tabsize;
     21
     22        Indenter( unsigned int amt = tabsize, unsigned int indent = 0 ) : amt( amt ), indent( indent ) {}
     23        unsigned int amt;  // amount 1 level increases indent by (i.e. how much to increase by in operator++)
     24        unsigned int indent;
    2325
    2426        Indenter & operator+=(int nlevels) { indent += amt*nlevels; return *this; }
     
    3032};
    3133
    32 inline std::ostream & operator<<( std::ostream & out, Indenter & indent ) {
     34inline std::ostream & operator<<( std::ostream & out, const Indenter & indent ) {
    3335        return out << std::string(indent.indent, ' ');
    3436}
  • src/Common/utility.h

    ra8555c5 r50377a4  
    2828#include <cassert>
    2929
     30#include "Common/Indenter.h"
     31
    3032template< typename T >
    3133static inline T * maybeClone( const T *orig ) {
     
    7577
    7678template< typename Container >
    77 void printAll( const Container &container, std::ostream &os, int indent = 0 ) {
     79void printAll( const Container &container, std::ostream &os, Indenter indent = {} ) {
    7880        for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
    7981                if ( *i ) {
    80                         os << std::string( indent,  ' ' );
    81                         (*i)->print( os, indent + 2 );
     82                        os << indent;
     83                        (*i)->print( os, indent );
    8284                        // need an endl after each element because it's not easy to know when each individual item should end
    8385                        os << std::endl;
     
    351353template< typename T1, typename T2 >
    352354struct group_iterate_t {
     355private:
     356        std::tuple<T1, T2> args;
     357public:
    353358        group_iterate_t( bool skipBoundsCheck, const T1 & v1, const T2 & v2 ) : args(v1, v2) {
    354359                assertf(skipBoundsCheck || v1.size() == v2.size(), "group iteration requires containers of the same size: <%zd, %zd>.", v1.size(), v2.size());
    355360        };
    356361
     362        typedef std::tuple<decltype(*std::get<0>(args).begin()), decltype(*std::get<1>(args).begin())> value_type;
     363        typedef decltype(std::get<0>(args).begin()) T1Iter;
     364        typedef decltype(std::get<1>(args).begin()) T2Iter;
     365
    357366        struct iterator {
    358                 typedef typename std::remove_reference<T1>::type T1val;
    359                 typedef typename std::remove_reference<T2>::type T2val;
    360                 typedef std::tuple<typename T1val::value_type &, typename T2val::value_type &> value_type;
    361                 typedef typename T1val::iterator T1Iter;
    362                 typedef typename T2val::iterator T2Iter;
    363367                typedef std::tuple<T1Iter, T2Iter> IterTuple;
    364368                IterTuple it;
     
    370374                value_type operator*() const { return std::tie( *std::get<0>(it), *std::get<1>(it) ); }
    371375        };
     376
    372377        iterator begin() { return iterator( std::get<0>(args).begin(), std::get<1>(args).begin() ); }
    373378        iterator end() { return iterator( std::get<0>(args).end(), std::get<1>(args).end() ); }
    374 
    375 private:
    376         std::tuple<T1, T2> args;
    377379};
    378380
Note: See TracChangeset for help on using the changeset viewer.