IPKIFColleague.h

Go to the documentation of this file.
00001 
00010 #ifndef __IPKIFColleague_H__
00011 #define __IPKIFColleague_H__
00012 
00013 #include "PKIFdll.h"
00014 #include <vector>
00015 
00016 class IPKIFColleague;
00017 typedef IPKIFColleague IPKIFMediator;
00018 typedef boost::shared_ptr<IPKIFMediator> IPKIFMediatorPtr;
00019 class CPKIFException;
00020 
00032 class CAC_API CAC_NO_VTABLE IPKIFColleague
00033 {
00034 //  friend class IPKIFMediator;
00035 public:
00036     virtual void Initialize();
00037     IPKIFColleague();
00038     virtual ~IPKIFColleague();
00039 
00040     void AddParent(IPKIFMediator* parent);
00041     void RemoveParent(IPKIFMediator* parent);
00042     bool IsParent(IPKIFMediator*) const;
00043     
00044     
00045     // interface for colleagues that are mediators as well
00046     // merged into the general colleague class to work around a circular dependency that grew
00047     // when smart pointers were added.
00048     virtual void InitializeMediator(std::vector<CPKIFException*>* errorInfo = NULL);
00049     virtual void Terminate();
00050     
00051     template<class X> X* GetMediator() const;
00052     void AddMediator(IPKIFMediatorPtr& m);
00053     
00054     //added 12/04/02
00055     void RemoveMediator(IPKIFMediator*);
00056     void RemoveMediatorAssociations();
00057     
00058     template<class X> X* GetMediator(std::vector<const IPKIFColleague *>* tree) const;
00059     void GetMediators(std::vector<IPKIFMediatorPtr>& v) const;
00060     
00078     template <class X>
00079         X* FindMediator(const std::vector<IPKIFMediator*>& v, std::vector<const IPKIFColleague *>* tree) const
00080     {
00081             using namespace std;
00082             //The purpose of this function is to return a pointer to a desired interface.  The 
00083             //interface may be implemented or held by any mediator in a collection of mediators.  
00084             //We don't care where the interface comes from as long as we get it.  The v param
00085             //is the vector that is searched for the interface.  The tree param contains pointers
00086             //to mediators/colleagues already on the call stack so we don't loop.
00087             typename vector<IPKIFMediator*>::const_iterator pos;
00088             const typename vector<IPKIFMediator*>::const_iterator end = v.end();
00089             X* x = NULL;
00090             
00091             //1) See if any of the objects in the vector are the desired interface.
00092             for(pos = v.begin(); pos != end; ++pos)
00093             {
00094                 x = dynamic_cast<X*>(*pos);
00095                 if(NULL != x)
00096                     return x;
00097             }
00098             
00099             //2) See if any of the objects in the vector know anyone with the desired interface.
00100             //const vector<const IPKIFColleague *>::iterator treeBegin = tree->begin();
00101             //const vector<const IPKIFColleague *>::iterator treeEnd = tree->end();
00102             for(pos = v.begin(); pos != end; ++pos)
00103             {
00104                 //moved these down here because the GetMediatorFromParent call below can invalidate the iterators
00105                 //11/9/2003
00106                 const vector<const IPKIFColleague *>::const_iterator treeBegin = tree->begin();
00107                 const vector<const IPKIFColleague *>::const_iterator treeEnd = tree->end();
00108                 
00109                 //see if the current position has already been visited during this search
00110                 if(treeEnd == find(treeBegin, treeEnd, (*pos)))
00111                 {
00112                     //added 10/2/2003
00113                     x = (*pos)->template GetMediatorFromParent<X>(tree);
00114                     if(NULL != x)
00115                         return x;
00116                     //end added 10/2/2003
00117                     
00118                     //if it hasn't ask it for the interface
00119                     x = (*pos)->template GetMediator<X>(tree);
00120                     if(NULL != x)
00121                         return x;
00122                 }
00123             }
00124             
00125             //3) Give up.
00126             return NULL;
00127     }
00128     
00146     template <class X>
00147         X* FindMediator(const std::vector<IPKIFMediatorPtr>& v, std::vector<const IPKIFColleague *>* tree) const
00148     {
00149             using namespace std;
00150             //The purpose of this function is to return a pointer to a desired interface.  The 
00151             //interface may be implemented or held by any mediator in a collection of mediators.  
00152             //We don't care where the interface comes from as long as we get it.  The v param
00153             //is the vector that is searched for the interface.  The tree param contains pointers
00154             //to mediators/colleagues already on the call stack so we don't loop.
00155             typename vector<IPKIFMediatorPtr>::const_iterator pos;
00156             const typename vector<IPKIFMediatorPtr>::const_iterator end = v.end();
00157             X* x = NULL;
00158             
00159             //1) See if any of the objects in the vector are the desired interface.
00160             for(pos = v.begin(); pos != end; ++pos)
00161             {
00162                 x = dynamic_cast<X*>(&(*(*pos)));
00163                 if(NULL != x)
00164                     return x;
00165             }
00166             
00167             //2) See if any of the objects in the vector know anyone with the desired interface.
00168             //const vector<const IPKIFColleague *>::iterator treeBegin = tree->begin();
00169             //const vector<const IPKIFColleague *>::iterator treeEnd = tree->end();
00170             for(pos = v.begin(); pos != end; ++pos)
00171             {
00172                 //moved these down here because the GetMediatorFromParent call below can invalidate the iterators
00173                 //11/9/2003
00174                 const vector<const IPKIFColleague *>::const_iterator treeBegin = tree->begin();
00175                 const vector<const IPKIFColleague *>::const_iterator treeEnd = tree->end();
00176                 
00177                 //see if the current position has already been visited during this search
00178                 IPKIFMediator * tmpMed = &(*(*pos));
00179                 if(treeEnd == find(treeBegin, treeEnd, tmpMed))
00180                 {
00181                     //added 10/2/2003
00182                     x = tmpMed->template GetMediatorFromParent<X>(tree);
00183                     if(NULL != x)
00184                         return x;
00185                     //end added 10/2/2003
00186                     
00187                     //if it hasn't ask it for the interface
00188                     x = tmpMed->template GetMediator<X>(tree);
00189                     if(NULL != x)
00190                         return x;
00191                 }
00192             }
00193             
00194             //3) Give up.
00195             return NULL;
00196     }
00197     
00198     
00199 
00200     //this is implemented here because I could not get it to compile outside of the class declartion
00209     template<class X> X* GetMediatorFromParent() const 
00210     {
00211         //this function should be called when initiating a search.  we create a vector and push this
00212         //instance into it then call the other GetMediatorFromParent function.
00213         std::vector<const IPKIFColleague *> tree; 
00214         return GetMediatorFromParent<X>(&tree);
00215     }
00216 
00217     //this is implemented here because I could not get it to compile outside of the class declartion
00225     template<class X> X* GetMediatorFromParent(std::vector<const IPKIFColleague *>* tree) const 
00226     {
00227         //this function is called when a search is underway.  push this into the vector and call FindMediator.
00228         tree->push_back(this);  
00229         return FindMediator<X>(m_parents, tree);
00230     }
00231 
00232 protected:
00233     std::vector<IPKIFMediator*> m_parents;
00234     std::vector<IPKIFMediatorPtr> m_mediators;
00235 private:
00236     //added 8/21/2004
00238     IPKIFColleague(const IPKIFColleague& copy);
00240     IPKIFColleague& operator=(const IPKIFColleague& rhs); 
00241 };
00242 DECLARE_SMART_POINTERS(IPKIFColleague);
00243 
00281 template<class X> 
00282 X* IPKIFMediator::GetMediator(
00286                               std::vector<const IPKIFColleague *>* tree) const
00287 {
00288     tree->push_back(this);  
00289     
00290     //1) See if any of the associated mediators either are or know where to find the desired interface.
00291     X* x = FindMediator<X>(m_mediators, tree);
00292     if(NULL != x)
00293         return x;
00294     
00295     //2) Still no luck?  See if our parents can help out.
00296     return GetMediatorFromParent<X>(tree);
00297 }
00335 template<class X> 
00336 X* IPKIFMediator::GetMediator() const
00337 {
00338     //first see if this instance is the requested interface (added 12/3/2002)
00339     X* x = NULL;
00340     x = dynamic_cast<X*>(const_cast<IPKIFMediator*>(this));
00341     if(NULL != x)
00342         return x;
00343     
00344     //this function is called when starting a search.  create a vector 
00345     //for the tree then call the other GetMediator function
00346     std::vector<const IPKIFColleague *> tree; 
00347     return GetMediator<X>(&tree);
00348 }
00349 
00350 #endif
00351 

Generated on Mon Nov 15 11:15:51 2010 for PublicKeyInfrastructureFramework(PKIF) by  doxygen 1.5.6