On Tue, Jul 15, 2003 at 09:18:47AM +0200, Andre Poenitz spake thusly:

...

> Theoretically, I'd expect this to be undefined behaviour as 'it' is
> invalid after its 'pointee' is erased.
> 
> Practically, there is a  std::list<>::remove_if(Pred) somewhere.
> 
> Andre'

Thanks! Ok, see attached, which fails to compile with the usual
obtuse string of messages from gcc :-(

BranchList.C: In method `void BranchList::remove(const string &)':
BranchList.C:133: implicit declaration of function `int remove_if(...)'
BranchList.C:133: warning: cannot pass objects of type `binder2nd<pfn>' through `...'

I'm sure it's trivial for those in the know...

Regards Martin

// -*- C++ -*- 
/* This file is part of
 * ======================================================
 *
 *           LyX, The Document Processor
 *
 *           Copyright 1995 Matthias Ettrich
 *           Copyright 1995-2001 The LyX Team.
 *
 *
 * ====================================================== */

/**
 * \class Branch
 *
 * A class describing a 'branch', i.e., a named alternative for
 * selectively outputting some parts of a document while suppressing
 * other parts.
 *
 * A branch has a name, can either be selected or not, and uses a
 * user-specifyable background colour. All these can be set and
 * queried.
 * 
 * \class BranchList
 *
 * A class containing a vector of all defined branches within a
 * document. Has methods for selecting or deselecting branches by
 * name, for outputting a '|'-separated string of all elements or only
 * the selected ones, and for adding and removing elements.
 */


#ifndef BRANCHES_H
#define BRANCHES_H

#include "LString.h"
#include <vector>

class Branch {
public:
        ///
        string getBranch() const;
        ///
        void setBranch(string const &);
        ///
        bool getSelected() const;
        ///
        void setSelected(bool);
        /// 
        string getColor() const;
        ///
        void setColor(string const &);


private:
        ///
        string branch_;
        ///
        bool selected_;
        ///
        string color_;
};



class pfn {
public:
        typedef Branch first_argument_type;
        typedef string second_argument_type;
        typedef bool result_type;

        bool operator()(Branch & br, string & s) const {
                return (br.getBranch() == s);
        }
};



class BranchList {
public:
        ///
        BranchList() { separator_ = "|"; };
        
        ///
        typedef std::vector<Branch>     List;

        ///
        void clear();
        ///
        Branch branch(string const &);
        /// Select/deselect multiple branches given in '|'-separated string
        void setSelected(string const &, bool);
        /// Add multiple branches to list
        void add(string const &);
        /// Assign color names to branches in order given
        void setColors(string const &);
        /// remove multiple branches from list
        void remove(string const &);
        /// return whether this branch is selected
        bool selected(string const &);
        /// return, as a '|'-separated string, all branch names
        string allBranches() const;
        /// 
        string allSelected() const;
        ///
        string allColors() const;
        ///
        string separator() const;
        
private:
        ///
        List list;
        ///
        string separator_;
};

#endif
/* This file is part of
 * ======================================================
 *
 *           LyX, The Document Processor
 *
 *           Copyright 1995 Matthias Ettrich
 *           Copyright 1995-2001 The LyX Team.
 *
 *
 * ====================================================== */


#include "BranchList.h"
#include "support/LAssert.h"
#include "debug.h"

#include <functional>

using std::vector;
using std::bind2nd;
using std::remove_if;
using namespace lyx::support;


string Branch::getBranch() const
{
        return branch_;
}


void Branch::setBranch(string const & s)
{
        branch_ = s;
}


bool Branch::getSelected() const
{
        return selected_;
}


void Branch::setSelected(bool b)
{
        selected_ = b;
}


string Branch::getColor() const
 
{
        return color_;
}


void Branch::setColor(string const & c)
{
        color_ = c;
}


void BranchList::clear()
{
        list.clear();
}


Branch BranchList::branch(string const & s)
{
        List::iterator it = list.begin();
        List::iterator end = list.end();
        for (; it != end; it++) {
                if (s == it->getBranch()) 
                return *it;
        }
        Assert(true);
        return *end;
}


void BranchList::setSelected(string const & s, bool val)
{
        List::iterator it = list.begin();
        List::iterator end = list.end();
        for (; it != end; it++) {
                if (s.find(it->getBranch(), 0) != string::npos)
                        it->setSelected(val);
        }       
}


void BranchList::add(string const & s)
{
        string name;
        size_t i = 0;
        size_t j = 0;
        Branch br;
        while (j < string::npos) {
lyxerr << "add:j=" << j << endl;
                j = s.find_first_of(separator(), i);
                name = s.substr(i, j - i);
                br.setBranch(name);
                br.setSelected(false);
                br.setColor("none");
                list.push_back(br);
                i = j; 
                i++;
        }
}


void BranchList::setColors(string const & s)
{
        string name;
        size_t i = 0;
        size_t j = 0;
        List::iterator it = list.begin();
        while (j < string::npos) {
lyxerr << "col:j=" << j << endl;
                size_t j = s.find_first_of(separator(), i);
                name = s.substr(i, j - i);
                it->setColor(name);
                it++;
                Assert(it != list.end());
                i = j; 
                i++;
        }
}


void BranchList::remove(string const & s)
{
        remove_if(list.begin(), list.end(), bind2nd(pfn(), s));
}


bool BranchList::selected(string const & s)
{
        List::iterator it = list.begin();
        List::iterator end = list.end();
        for (; it != end; it++) {
                if (s == it->getBranch()) 
                        return it->getSelected();
        }
        return false;
}


string BranchList::allBranches() const
{
        List::const_iterator it = list.begin();
        List::const_iterator end = list.end();
        string ret;
        for (; it != end; it++) {
                ret += it->getBranch() + separator();
        }
        // remove final '|':
        size_t i = ret.find_last_of(separator());
        if (i != string::npos)
                ret.erase(i);
        return ret;
}


string BranchList::allSelected() const
{       
        List::const_iterator it = list.begin();
        List::const_iterator end = list.end();
        string ret;
        for (; it != end; it++) {
                if (it->getSelected()) 
                        ret += it->getBranch() + separator();
        }
        // remove final '|':
        size_t i = ret.find_last_of(separator());
        if (i != string::npos)
                ret.erase(i);
        return ret;
}


string BranchList::allColors() const
{
        List::const_iterator it = list.begin();
        List::const_iterator end = list.end();
        string ret;
        for (; it != end; it++) {
                ret += it->getColor() + separator();
        }
        // remove final '|':
        size_t i = ret.find_last_of(separator());
        if (i != string::npos)
                ret.erase(i);
        return ret;
}


string BranchList::separator() const
{
        return separator_;
}

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to