473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Something about template , static interface and Concept.

I think the boost::concept_check is too complex.
Here is my solution about static interface and concept.

The static interface can be used as function param. It
is just a proxy of the real implementation.
And the Concept can be used when building the class
hierarchy. It is almost a private inherit, but for the sake of
providing an uniform look with the static interface version,
the private member is used.

In my opinion, the two versions of interface can be optimized
by the compiler and introduce no run-time burden.

My code need test and re-view.
Can anyone give me some advice?
Can anyone tell me whether there is a more graceful solution?
Any suggestion is welcome.
////////////////////////////////////////////////////////////////////
// Here is my code.
namespace common_utils_
{
template< typename T_Impl, bool T_UseRef >
struct si_root ;

template< typename T_Impl struct si_root< T_Impl, true >
{
typedef T_Impl impl_t ;

si_root( void )
: m_pimpl( NULL )
{
}

void set_impl( impl_t* p )
{
m_pimpl = p ;
}

protected:
impl_t& impl( void )
{
assert( m_pimpl ) ;
return *m_pimpl ;
}

private:
impl_t* m_pimpl ;
} ;
template< typename T_Impl struct si_root< T_Impl, false >
{
typedef T_Impl impl_t ;

protected:
impl_t& impl( void )
{
return m_impl ;
}

private:
impl_t m_impl ;
} ;

//
#define STATIC_INTERFACE_1(impl,T0) \
T0< si_root<impl,true

#define STATIC_INTERFACE_2(impl,T0,T1) \
T0< STATIC_INTERFACE_1(impl,T1) >

#define STATIC_INTERFACE_3(impl,T0,T1,T2) \
T0< STATIC_INTERFACE_2(impl,T1,T2) >

#define STATIC_INTERFACE_4(impl,T0,T1,T2,T3) \
T0< STATIC_INTERFACE_3(impl,T1,T2,T3) >

#define STATIC_CONCEPT_1(impl,T0) \
T0< si_root<impl,false

#define STATIC_CONCEPT_2(impl,T0,T1) \
T0< STATIC_CONCEPT_1(impl,T1) >

#define STATIC_CONCEPT_3(impl,T0,T1,T2) \
T0< STATIC_CONCEPT_2(impl,T1,T2) >

#define STATIC_CONCEPT_4(impl,T0,T1,T2,T3) \
T0< STATIC_CONCEPT_3(impl,T1,T2,T3) >

// exampls
namespace
{
template< typename T_Base struct si_0 : public T_Base
{
typedef typename T_Base::impl_t::type_0 type_0 ;
void function0( void )
{
T_Base::impl().function0() ;
}
} ;
template< typename T_Base struct si_1 : public T_Base
{
typedef typename T_Base::impl_t::type_1 type_1 ;
void function1( void )
{
T_Base::impl().function1() ;
}
} ;
template< typename T_Base struct si_2 : public T_Base
{
typedef typename T_Base::impl_t::type_2 type_2 ;
void function2( void )
{
T_Base::impl().function2() ;
}
} ;
template< typename T_Base struct si_3 : public T_Base
{
typedef typename T_Base::impl_t::type_3 type_3 ;
void function3( void )
{
T_Base::impl().function3() ;
}
} ;

struct TTTT
{
typedef int type_0 ;
typedef long type_1 ;
typedef float type_2 ;
typedef double type_3 ;
void function0( void )
{
std::cout << __LINE__ << ": " << __FUNCTION__ << " Is called."
<< std::endl ;
}
void function1( void )
{
std::cout << __LINE__ << ": " << __FUNCTION__ << " Is called."
<< std::endl ;
}
void function2( void )
{
std::cout << __LINE__ << ": " << __FUNCTION__ << " Is called."
<< std::endl ;
}
void function3( void )
{
std::cout << __LINE__ << ": " << __FUNCTION__ << " Is called."
<< std::endl ;
}
} ;

typedef STATIC_INTERFACE_4( TTTT, si_0, si_1, si_2, si_3 )
my_interface_t ;
typedef STATIC_CONCEPT_4( TTTT, si_0, si_1, si_2, si_3 )
my_concept_t ;

void interface_test( void )
{
TTTT obj ;
my_interface_t i ;
my_concept_t c ;

i.set_impl( &obj ) ;

i.function0() ;
i.function1() ;
i.function2() ;
i.function3() ;

c.function0() ;
c.function1() ;
c.function2() ;
c.function3() ;
}
} // anonymous

} // namespace common_utils_

Jul 25 '07 #1
3 1677
On 25 Jul, 02:22, goodmen <goodme...@gmail.comwrote:
I think the boost::concept_check is too complex.
Here is my solution about static interface and concept.

The static interface can be used as function param. It
is just a proxy of the real implementation.
And the Concept can be used when building the class
hierarchy. It is almost a private inherit, but for the sake of
providing an uniform look with the static interface version,
the private member is used.

In my opinion, the two versions of interface can be optimized
by the compiler and introduce no run-time burden.

My code need test and re-view.
Can anyone give me some advice?
Can anyone tell me whether there is a more graceful solution?
Any suggestion is welcome.
<...>

Its difficult to understand from the code what you are trying to do.
Is there a particular problem that you are tring to solve?

regards
Andy Little


Jul 25 '07 #2
On 25 Jul, 13:12, goodmen <goodme...@gmail.comwrote:
6, Now, here is the use of the "static interface" :
FWIW Here is my understanding of static interface. The commented out
exprs fail because they don't meet the requirements. There is another
term "named conformance" (? I think) (versus "structural conformance")
to an interface. Though technically below the names have to be
typenames rather than object or function names.

regards
Andy Little

//-----

struct a{
typedef int a_type;
};

struct b{
typedef int b_type;
};

struct ab : a,b{};
struct c {
typedef double a_type;
typedef double b_type;
};

template <typename T>
void fun_b( T const & t)
{
typedef typename T::b_type type;

type t1(1);
}

template <typename T>
void fun_a( T const & t)
{
typedef typename T::a_type type;

type t1(1);
}

int main()
{
fun_a( a());
// fun_a(b()); // error no b::a_type
fun_a(ab());
fun_a(c());

// fun_b(a()); // error n a::b_type
fun_b(b());
fun_b(ab());
fun_b(c());
}
Jul 25 '07 #3
On Jul 26, 4:11 am, kwikius <a...@servocomm.freeserve.co.ukwrote:
On 25 Jul, 13:12, goodmen <goodme...@gmail.comwrote:
6, Now, here is the use of the "static interface" :

FWIW Here is my understanding of static interface. The commented out
exprs fail because they don't meet the requirements. There is another
term "named conformance" (? I think) (versus "structural conformance")
to an interface. Though technically below the names have to be
typenames rather than object or function names.

regards
Andy Little
hehe , I agree with you.
Jul 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: bearophileHUGS | last post by:
Adding Optional Static Typing to Python looks like a quite complex thing, but useful too: http://www.artima.com/weblogs/viewpost.jsp?thread=85551 I have just a couple of notes: Boo...
11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
0
by: Gianni Mariani | last post by:
I'm trying to make a generic factory that provides a placement new as well as the accompanying destructor. To do a down-cast I used a static_cast but it turns out that this is not always a...
1
by: Aaron Walker | last post by:
Hey folks, I've got some code for an options class that stores user run time options (as static members) so that they are availably any time an instance is created; currently the code has a...
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
2
by: David O | last post by:
I am using the CRTP (Curiously Recurring Template Pattern) to provide a set of low-level functions customizable by environment. Using CRTP ensures that all the function have the same signatures,...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
28
by: jmDesktop | last post by:
Studying OOP and noticed that Python does not have Interfaces. Is that correct? Is my schooling for nought on these OOP concepts if I use Python. Am I losing something if I don't use the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.