Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   recursive dereferencing library (http://www.velocityreviews.com/forums/t593015-recursive-dereferencing-library.html)

sebastian 02-20-2008 06:21 PM

recursive dereferencing library
 
I have a dereferencing template that I believe to be implemented
correctly and complete, but I would like to have it checked for
correctness to be sure. is this the right forum for such a thing?

jason.cipriani@gmail.com 02-20-2008 07:29 PM

Re: recursive dereferencing library
 
Sure, give it a whirl. At the bare minimum you'll get lots of good
advice about undefined gotchas and other weird stuff. As far actually
checking the syntax (not necessarily correctness of your
implementation), you can also try compiling your code with Comeau's
online compiler here:

http://www.comeaucomputing.com/tryitout/

Jason

"sebastian" <smgarth2u@yahoo.com> wrote in message
news:
62973aa1-2f16-490f-9a71-103387610fee...oglegroups.com...
>I have a dereferencing template that I believe to be implemented
> correctly and complete, but I would like to have it checked for
> correctness to be sure. is this the right forum for such a thing?


Gianni Mariani 02-20-2008 07:33 PM

Re: recursive dereferencing library
 
sebastian wrote:
> I have a dereferencing template that I believe to be implemented
> correctly and complete, but I would like to have it checked for
> correctness to be sure. is this the right forum for such a thing?


If it's a question about the proper use of C++ then it is appropriate.

sebastian 02-21-2008 01:08 AM

Re: recursive dereferencing library
 
// extract.hpp

#ifndef XTD_EXTRACT_HPP
#define XTD_EXTRACT_HPP

namespace xtd {

/*
template to be specialized for user defined types
*/

template < typename Type >
struct content
{
typedef Type type;
typedef type & reference;

static inline
reference
extractor( reference object )
{
return object;
}
};

/*
user entry point
*/

template < typename Type >
inline
typename content< Type >::reference
extract( Type & object )
{
return content< Type >::extractor( object );
}

/*
specializations for references and pointers
*/

template < typename Type >
struct content< Type & >
{
typedef typename content< Type >::type type;
typedef type & reference;

static inline
reference
extractor( Type & object )
{
return extract( object );
}
};

template < typename Type >
struct content< Type * >
{
typedef typename content< Type >::type type;
typedef type & reference;

static inline
reference
extractor( Type * object )
{
return extract( *object );
}
};

template < typename Type >
struct content< Type * const >
{
typedef typename content< Type >::type type;
typedef type & reference;

static inline
reference
extractor( Type * const object )
{
return extract( *object );
}
};

/*
generic function object
*/

struct extractor
{
template < typename Type >
inline
typename content< Type >::reference
operator ( ) ( Type & object )
{
return extract( object );
}
};

} // namespace xtd

#endif // XTD_EXTRACT_HPP

// pointer_example.cpp

#include <iostream>
#include "extract.hpp"

using namespace std;
using namespace xtd;

int
main( void )
{
int
i = 1024,
* p = &i,
** pp = &p,
*** ppp = &pp;
extract( ppp )++;
cout << extract( ppp ) << endl;
return 0;
}

// auto_pointer_specialization_example.cpp

#include <iostream>
#include <memory>
#include "extract.hpp"

namespace xtd {

template < typename Type >
struct content< std::auto_ptr< Type > >
{
typedef typename content< Type >::type type;
typedef type & reference;

static inline
reference
extractor( std::auto_ptr< Type > & object )
{
return extract( *object.get( ) );
}
};

template < typename Type >
struct content< std::auto_ptr< Type > const >
{
typedef typename content< Type >::type type;
typedef type & reference;

static inline
reference
extractor( std::auto_ptr< Type > const & object )
{
return extract( *object.get( ) );
}
};

} // namespace xtd

using namespace std;
using namespace xtd;

int
main( void )
{
auto_ptr< auto_ptr< int > >
ap( new auto_ptr< int >( new int( 1024 ) ) );
extract( ap )++;
cout << extract( ap ) << endl;
return 0;
}

Gianni Mariani 02-21-2008 11:40 PM

Re: recursive dereferencing library
 
sebastian wrote:
> // extract.hpp


.... what problem are you trying to solve ?

It's very clever but I'm not sure it's really needed.

sebastian 02-22-2008 01:45 PM

Re: recursive dereferencing library
 

>> ... what problem are you trying to solve ?


the sole purpose of the library is to simplify complex dereferencing
accesses. also, it often allows the underlying storage of data to
change without necessarily changing the accessor code.

>> but I'm not sure it's really needed.


its necessity or usefulness depends on the individual, I suppose.



All times are GMT. The time now is 01:14 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.