473,508 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interfaces and their implementations, is this code legal/correct?

I am a basically a life-long Win32 C programmer specializing in high-end
winsock servers.

Before I dive into portable C++ programming, I wanted to code up an example
of what I thought would be a workable Collection template library. The idea
is to design API's that operate on interfaces, and provide
multi-implementations to that "single" API. C++ Interfaces and
Implementations, seems to behave like Win32 COM objects?

Please look over the following code sketch, and point out ALL of the flaws
that render it non-portable and/or non-std:

;)
Interfaces.cpp
--------------------
#ifndef NULL
# define NULL 0
#endif
// Collection objects
namespace Collections
{
// Forwards
template< typename T > class ICollection;
template< typename T > class IIterator;
template< typename T > class TNode;
template< typename T > class TStack;


// *** Interfaces ***
// The collection interface
template< typename T >

class ICollection
{

public:

virtual ~ICollection() {}

virtual void Push( T const* pObj, int iTimeout = 0 ) = 0;

virtual T* Pop( int iTimeout = 0 ) = 0;

virtual bool Pop( T const* pObj, int iTimeout = 0 ) = 0;

virtual IIterator< T >* RequestIterator() = 0;

virtual void ReleaseIterator( IIterator< T >* pIterator ) = 0;

};
// The iterator interface
template< typename T >

class IIterator
{

public:

// RAII
class CGuard
{

public:

CGuard( IIterator< T >* pIterator ) : m_pIterator( pIterator ) {}

~CGuard() { m_pIterator->GetCollection()->ReleaseIterator(
m_pIterator ); };
public:

IIterator< T >* operator ->() { return m_pIterator; }
private:

IIterator< T >* m_pIterator;

};
public:

virtual ~IIterator() {}

virtual void Next() = 0;

virtual void Prev() = 0;

virtual void Front() = 0;

virtual void Back() = 0;

virtual bool Eof() = 0;

virtual bool Bof() = 0;

virtual T* GetObj() = 0;

virtual ICollection< T >* GetCollection() = 0;

};


// *** Implmentation ***
// The collection node
template< typename T >

class TNode
{

public:

TNode( T const* pObj = NULL ) : m_pNext( NULL ), m_pObj( pObj ) {}
public:

TNode< T >* m_pNext;

T const* m_pObj;

};
// A stack object
template< typename T >

class TStack : public ICollection< T >
{

class CIterator;
private:

friend class CIterator;

class CIterator : public IIterator< T >
{

public:

CIterator( TStack< T >& Stack )
: m_Stack( Stack ),
m_pCurrent( NULL ),
m_pNext( NULL ),
m_pPrev( NULL ) {}
public:

void Next() {}

void Prev() {}

void Front() {}

void Back() {}
bool Eof()
{
return ( m_pCurrent && m_pCurrent->m_pNext ) ? true : false;
}
bool Bof()
{
return ( m_Stack.m_pFront == m_pCurrent ) ? true : false;
}
T* GetObj()
{
return ( m_pCurrent ) ? const_cast< T* >( m_pCurrent->m_pObj ) :
NULL;
}
ICollection< T >* GetCollection() { return &m_Stack; }
private:

TStack< T >& m_Stack;

TNode< T >* m_pCurrent;

TNode< T >* m_pNext;

TNode< T >* m_pPrev;

};
public:

void Push( T const* pObj, int iTimeout = 0 )
{

}
T* Pop( int iTimeout = 0 )
{
return NULL;
}
bool Pop( T const* pObj, int iTimeout = 0 )
{
return false;
}
IIterator< T >* RequestIterator()
{
return new CIterator( *this );
}
void ReleaseIterator( IIterator< T >* pIterator )
{
delete pIterator;
}
private:

TNode< T >* m_pFront;

};
}
// Test object
class CTest
{

public:

CTest() : m_Val1( 0 ) {}

public:

long m_Val1;

};
int main()
{
// Get the ICollection interface
Collections::ICollection< CTest >* pCol
= new Collections::TStack< CTest >;

{
// Get the ICollection's IIterator interface
Collections::IIterator< CTest >::CGuard
pIterator( pCol->RequestIterator() );
}

delete pCol;

return 0;
}
Thank you! =)

P.S.

I will be implementing some very fast, portable, lock-free algo's that will
use the interfaces posted here. If your interested I can post the code when
it is finished.

--
The designer of the experimental, SMP and HyperThread friendly, AppCore
library.

http://AppCore.home.comcast.net
Jul 19 '05 #1
7 1896
Check out my lock-free pthread semaphore:

http://appcore.home.comcast.net/pthreads/lfsema.cpp

http://appcore.home.comcast.net/pthreads/lfsema.h

This algo. totally skips kernel calls under contention.

Do a performance test, compare my semaphore to the pthread equivalent. Its
not even funny how much better the lock-free algo is.

Enjoy!

--
The designer of the experimental, SMP and HyperThread friendly, AppCore
library.

http://AppCore.home.comcast.net
Jul 19 '05 #2
DOH!

I you would need this in order to compile the semaphore:

http://appcore.home.comcast.net/pthreads/atomic.h

x86 SMP Compatible.
Jul 19 '05 #3
> Uhm, yes: try specific questions instead of "do this work for
me".
I apologize.

* Submit the code to the online Comeau compiler, it's one of the
most standard-conforming around.
Nice. So most compiler portability questions can be skipped, as long as the
code in question compiled without warning in a "strictly conforming"
compiler?

* Use smart-pointers such as std::auto_ptr and boost::shared_ptr
instead of raw C++ pointers.


I was thinking of a templated smart pointer interface, IAutoPtr.

So I can use one reference counted interface, and use it for C pointers and
COM's IUnknown interface.

Does that sound workable?
P.S.

How portable is the _asm keyword and 64-bit integers ( __int64 && long
long ) among popular c++ compilers?

My algos rely on a tiny bit of assembly.

--
The designer of the experimental, SMP and HyperThread friendly, AppCore
library.

http://AppCore.home.comcast.net
Jul 19 '05 #4
On Thu, 28 Aug 2003 22:58:25 GMT, "SenderX" <xx*@xxx.xxx> wrote:
Uhm, yes: try specific questions instead of "do this work for
me".
I apologize.

* Submit the code to the online Comeau compiler, it's one of the
most standard-conforming around.


Nice. So most compiler portability questions can be skipped, as long as the
code in question compiled without warning in a "strictly conforming"
compiler?


No, but error- and warning-free compilation with a compiler such as Comeau
is a good indication that the code is practically clean -- if it weren't,
then the chances are near certainty that the compiler would catch something.

But the only way to ensure portability is to (1) design and code for it,
(2) compile with the relevant compilers and (3) test on the relevant systems.

Systematically.

* Use smart-pointers such as std::auto_ptr and boost::shared_ptr
instead of raw C++ pointers.


I was thinking of a templated smart pointer interface, IAutoPtr.

So I can use one reference counted interface, and use it for C pointers and
COM's IUnknown interface.

Does that sound workable?


Not immediately. COM reference counting keeps the reference count in each
referred object, ordinary C++ smart-pointers keep the count in the pointer
object or in a shared helper object. Generally, nail => hammer, screw =>
screwdriver, and so on.

How portable is the _asm keyword
Not portable at all.

and 64-bit integers ( __int64 && long long ) among popular c++ compilers?
Not at all, but in C99 (and possibly future C++0x) it's another story.
My algos rely on a tiny bit of assembly.


Wrap it behind a clean C++ interface.

Jul 19 '05 #5
> No, but error- and warning-free compilation with a compiler such as Comeau
is a good indication that the code is practically clean
I got a "compile succeeded" using strict mode. So I am currently on the
correct path...

But the only way to ensure portability is to (1) design and code for it,
(2) compile with the relevant compilers and (3) test on the relevant systems.
Systematically.
Yep. ;(

How portable is the _asm keyword
Damn. Any special tricks to get assembly code to compile across different
compilers? ;)

and 64-bit integers ( __int64 && long long ) among popular c++ compilers?
Not at all, but in C99 (and possibly future C++0x) it's another story.


Mmmk.

My algos rely on a tiny bit of assembly.


Wrap it behind a clean C++ interface.


That would allow for a single interface that implements varying assembly
code specific to different processors.

// Interfaces

class IAtomic
-- CompareAndSwap(...) = 0;
-- ExchangeAdd(...) = 0;
// Specific processor implmenataions
class CIA32Atomic : public IAtomic
{
...
};
class CIA64Atomic : public IAtomic
{
...
};
class CPowerPCAtomic : public IAtomic
{
...
};
class CPowerPC64Atomic : public IAtomic
{
...
};
class CAMD64Atomic : public IAtomic
{
...
};

My lock-free code has to be portable across compiler, platform, and
processor. I think C++ and this group can help me.

Thanks.

--
The designer of the experimental, SMP and HyperThread friendly, AppCore
library.

http://AppCore.home.comcast.net
Jul 19 '05 #6
On Fri, 29 Aug 2003 00:12:10 GMT, "SenderX" <xx*@xxx.xxx> wrote:
>How portable is the _asm keyword


Damn. Any special tricks to get assembly code to compile across different
compilers? ;)


The usual "trick" is to write the assembly in -- assembly.

Then link it into the application.

E.g., parts of the C++ runtime library, for most any compiler, is
implemented that way.

Jul 19 '05 #7
In article <lOv3b.287479$Ho3.39520@sccrnsc03>, SenderX <xx*@xxx.xxx> wrote:
* Submit the code to the online Comeau compiler, it's one of the
most standard-conforming around.


Nice. So most compiler portability questions can be skipped,
as long as the code in question compiled without warning in
a "strictly conforming" compiler?


Use one of the "zillion" non-strictly conforming modes
of Comeau then if that is your concern. :)
--
Greg Comeau/4.3.3:Full C++03 core language + more Windows backends
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 19 '05 #8

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

Similar topics

8
3198
by: Shawn Casey | last post by:
Consider the following code: interface IBase { virtual void BaseFunction() = 0; }; interface IDerived : public IBase { virtual void DerivedFunction() = 0;
11
2732
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
7
1319
by: Fred Mellender | last post by:
I would like to make a library (dll) that contains a number of classes and interfaces for use by other implementers. One of the interfaces I want to define is: public interface Foo { Bar...
11
382
by: JJ | last post by:
Can someone explain to me how interfaces are such a big help in creating objects? I was under the impression that an Interface was a template for an Object, just sort of a class with only method ...
5
13931
by: dj | last post by:
I know seasoned OOP-savvy developers will roll their eyes at this one, but can anyone provide a clear explanation as to *why* one would need to use an Interface in a VB.NET application? Every...
6
1813
by: s99999999s2003 | last post by:
hi i come from a non OO environment. now i am learning about classes. can i ask, in JAva, there are things like interface. eg public interface someinterface { public somemethod (); .... ... }...
22
2065
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
27
3819
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software...
5
2991
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7391
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.