Thread: Functor to allow access by callback

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248

    Functor to allow access by callback

    Hi,

    I want to make the two functions to acces the matrix elements in a valarray user-modifyable by using a callback to a functor, which is passed as class-template parameter and instanciated as the data member "accessor" in the class. These are the functions 'at', two versions for const-correctness:
    Code:
        template<typename T, typename AccessorT>
    	inline T Matrix<T,AccessorT> :: at(size_t const& i, size_t const& j) const
    	{
    	    if(i>=this->nb_rows() || j>=this->nb_cols()) {
    		throw MatrixException("index out of bounds");
    	    }
    	    return this->_elms[this->accessor(i,j)];          //   <<-----------------  use of the accessor
    	}
    
        template<typename T, typename AccessorT>
    	inline T& Matrix<T,AccessorT> :: at(size_t const& i, size_t const& j)
    	{
    	    if(i>=this->nb_rows() || j>=this->nb_cols()) {
    		throw MatrixException("index out of bounds");
    	    }
    	    return this->_elms[this->accessor(i,j)];          //   <<-----------------  use of the accessor
    	}
    The functor "accessor" simply translates the two indices 'i,j' in one index 'k' . I have a default accessor defined as:
    Code:
        class MatrixAccessor
        {
        public:
    	virtual size_t operator()(size_t const& i, size_t const& j) = 0;
    	virtual ~MatrixAccessor(){}
        };
    
        class C_Accessor : public MatrixAccessor // for row-oriented memory access
        {
        public:
        C_Accessor() : _nbrows(0), _nbcols(0) {}
        C_Accessor(size_t const& nb_rows) : _nbrows(nb_rows), _nbcols(nb_rows) {}
        C_Accessor(size_t const& nb_rows, size_t const& nb_cols) : _nbrows(nb_rows), _nbcols(nb_cols) {}
    	size_t operator()(size_t const& i, size_t const& j)
    	{
    	    return(i+j*_nbrows);
    	}
    
        private:
    	size_t _nbrows;
    	size_t _nbcols;
        };
    My problem is const-correctness :

    error C3848: expression having type 'const LinearAlgebra::C_Accessor' would lose some const-volatile qualifiers in order to call 'size_t LinearAlgebra::C_Accessor:: operator ()(const size_t &,const size_t &)'
    Is this due to the operator[] of the valarray class? Should the operator() of the functor return a 'const size_t' ?

    I haven't found the operator[] of the valarray class in the documentation, by the way...
    http://publib.boulder.ibm.com/infoce...r_valarray.htm
    http://msdn.microsoft.com/en-us/libr...y8(VS.80).aspx

    Thanks a lot for your help!
    Last edited by MarkZWEERS; 11-11-2008 at 05:59 AM. Reason: typo

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Make operator() const?

    Another thing is that there doesn't seem to be much point in inheriting (and even polymorphically) the accessors: they are used as a template argument after all.
    Last edited by anon; 11-11-2008 at 06:26 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Of course! Thanks...

    The derivation was merely a left-over from a workaround of a problem that did not exist, so yeah, I'd just skip that.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-19-2008, 03:12 PM
  2. callback in a class, what do I do?
    By Yarin in forum C++ Programming
    Replies: 5
    Last Post: 08-17-2007, 11:11 AM
  3. typesafe callback system
    By gandalf_bar in forum C++ Programming
    Replies: 1
    Last Post: 05-22-2004, 06:35 AM
  4. Access violation on class members
    By filler_bunny in forum Windows Programming
    Replies: 2
    Last Post: 05-03-2004, 02:58 AM
  5. Pointer to member function as callback function
    By ninebit in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2002, 05:52 AM