////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This is a part of the COM Serialization SDK VC++ Library.
//  
// Copyright 2009 BPM Microsystems
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//                                                                                                                    //
// $Header: //sesca/src/Winprog/COMSerializationSDK/VC++/HeaderLib/ComSerSdk/rcs/StaticVectorServer.h 1.2 2009/06/05 11:53:12 arturoc Exp $
//                                                                                                                    //
///////// 120 columns, Tab = Insert 3 Spaces ///////////////////////////////////////////////////////////////////////////

/**
   This header file defines the CStaticVectorServerBase class, as well as other related helper classes used in its
   implementation.

   The following is an attempt at an explanation of how we allow a variable number of template arguments and for
   each argument we require the user to provide an override of a SetVector() member function.

   1.) We define a template class named CStaticVectorServerBase that takes 18 template arguments, each of them
       defaulted to a single seperate type named "NoArg", and the class has no implementation.  You can see this
       class template declaration for yourself in the code below.  The purpose of this class is to provide a compiler
       error if a user specifies 0 template arguments (since it has no implementation), to allow the user to
       provide a variable number of template arguments, and to act as a primary template for a number of
       specializations.

   2.) We define 17 seperate specializations of the class described in #1 above.  Each of them take a different
       number of template arguments.  When the library-user specifies template arguments to CStaticVectorServerBase,
       they are actually instantiating one of these specializations.  You can see the list of all of these
       specializations below.  Notice that for each of them, we do not have to manually specify "NoArg" for any
       template arguments that are not set.  This is because the primary template (the one with no implementation)
       defaults them to "NoArg" for us.

   3.) Each specialization inherits from a template class named CSSocketVsi.  The letters in its name stand for "Static
       Vector Server Implementation".  CSSocketVsi accepts two template arguments: a static vector type, and another
       specialization of CStaticVectorServerBase.
       For the purposes of this explanation, we will refer to the number of template arguments that each specialization
       accepts as NumTemplateArgs.  The specialization that is provided as the second template argument to CSSocketVsi
       is the one that takes a number of template arguments that is equal to NumTemplateArgs-1.

   4.) CSSocketVsi is the class that declares the SetVector() member function as pure-virtual.  It specifies 
       its first template argument (which is a static vector type) as the parameter type that the SetVector() 
       pure-virtual requires.
       CSSocketVsi is also defined to inherit from its second template argument.  That second template argument,
       as stated above, is a CStaticVectorServerBase specialization that does the same thing as the one that inherited
       from CSSocketVsi.
       
       The CStaticVectorServerBase specialization that takes a single template argument is defined to inherit from
       a CSSocketVsi template where, unlike all other specializations of CStaticVectorServerBase, the second template
       argument is not another specialziation but instead a class named CSSocketVsiRoot.

       Not only does CSSocketVsi specify the SetVector() pure-virtual, it also DEFINES another member function named
       SetVectors() (notice that its name is plural).  SetVectors() is defined in CSSocketVsi to declare an instantiation
       of the static vector object, invoke its SetVector() pure-virtual with that instantiation, and then 
       invoke the base-class SetVectors() (plural).   This chain of calls that originates at one point
       in the heirarchy and ends at the other extreme is how we call all of the SetVector() (singular) pure-virtuals.

       The result is a class heirarchy that looks like this:
       ___________
      | CSSocketVsiRoot |
       -----------
            ^
       _____|______________________________________________________
      | CSSocketVsi (defines pure-virtual SetVector(CStaticVectorType1&))|    
       ------------------------------------------------------------
            ^
       _____|_________________________________________
      | CStaticVectorServerBase (1 arg specialization)|
       -----------------------------------------------
            ^
       _____|______________________________________________________
      | CSSocketVsi (defines pure-virtual SetVector(CStaticVectorType2&))|    
       ------------------------------------------------------------
            ^
       _____|_________________________________________
      | CStaticVectorServerBase (2 arg specialization)|
       -----------------------------------------------
            ^
       _____|______________________________________________________
      | CSSocketVsi (defines pure-virtual SetVector(CStaticVectorType3&))| 
       ------------------------------------------------------------
            ^
       _____|_________________________________________
      | CStaticVectorServerBase (3 arg specialization)|
       -----------------------------------------------
         etc
            
*/
#pragma once

#include "Detail/Server.h"
#include "StaticVector.h"
#include "Constants.h"

namespace ComSerSdk
{
namespace Detail
{

/**
   Svsi = "Static Vector Server Impl".  Declares a pure-virtual "SetVectors" (notice it's plural) member function,
   and overrides CServerCommonBase's GetSerialData() to invoke the SetVectors pure-virtual.  The SetVectors
   pure-virtual is overriden in the CSSocketVsi concrete of this class to invoke the SetVector() pure-virtual that it
   declared with an instance of its static vector, and then invoke its base class' SetVectors (plural).

*/
class CSSocketVsiRoot : public CServerCommonBase
{
      /**
      /override
      Invokes the SetVectors() (plural) pure-virtual to let it begin the chain of calls to all of the library-user's
      SetVector() overrides.
   */
   virtual HRESULT DoGetSocketSerialData(BSTR sCurrentNumber, 
                                         long lSite,
                                         long lSocket, 
                                         BSTR sSiteId,
                                         BSTR sAdapter,
                                         BSTR sSocketId,
                                         IDispatch ** ppoDictionaryAsIDispatch)
   {
      std::string sNextHandle;

      // Obviously, this invokes the SetVectors() override of the most-derived class; that is, the concrete at the
      // very bottom of the heirarchy.  But it is defined in that most-derived, and also in all classes in between,
      // to, after it's done, allow its base-class' SetVectors to be invoked.
      //
      SetVectors(std::string(_bstr_t(sCurrentNumber)), 
                sNextHandle, 
                lSite, 
                lSocket, 
                std::string(_bstr_t(sSiteId)), 
                std::string(_bstr_t(sAdapter)), 
                std::string(_bstr_t(sSocketId)));

      PutItem(Dictionary(), _variant_t("NextHandle"), _variant_t(sNextHandle.c_str()), COMSERSDK_CODELOCATION);
      *ppoDictionaryAsIDispatch = Dictionary();
      return S_OK;
   }

   virtual HRESULT DoGetSerialData(BSTR sCurrentNumber, IDispatch ** ppoDictionaryAsIDispatch)
   {
      return S_OK;
   }

   virtual HRESULT DoGetSerialAddresses(IDispatch ** ppoDictionaryAsIDispatch)
   {
      Scripting::IDictionaryPtr spoDictionary = CServerBase::CreateDictionary();
      //CVectorAddrSetter oAddrSetter(spoDictionary);
      //SetVectorAddrs(oAddrSetter);
      *ppoDictionaryAsIDispatch = spoDictionary.Detach();
      return S_OK;
   }

public:

   /**
      The pure-virtual overriden by CSSocketVsi so that it can instantiate its static vector type, pass it to its
      SetVector() (singular) pure-virtual, and then defer the rest of the execution to its base class.
      Even though this is pure-virtual, we'll give it a body that does nothing to allow the invokation of it
      that the single-vector version of CSSocketVsi does to not result in an unnecessary compile error.
   */
   virtual void SetVectors(const std::string & sCurrentHandle,
                           std::string & sNextHandle, 
                           long lSite,  long lSocket, 
                           const std::string & sSiteId, 
                           const std::string & sAdapter, 
                           const std::string & sSocketId) = 0 {};

};

/**
   Svsi = "Static Vector Server Impl"
*/
template<class TVector, class TBase>
class CSSocketVsi : public TBase
{
   /**
      To be overriden by the library-user so that they can place their logic to specify the data portion of the
      vector object passed in.
   */
   virtual void SetVector(const std::string & sCurrentHandle, 
                          std::string & sNextHandle, 
                          long lSite, 
                          long lSocket, 
                          const std::string & sSiteId, 
                          const std::string & sAdapter, 
                          const std::string & sSocketId, 
                          TVector &) = 0;

protected:
   
   /**
      /override
      Here we instantiate the static vector, pass it to the library-user's SetVector() override, and then invoke
      our base-class' SetVectors(), which will do the exact same thing with its static vector.
   */
   virtual void SetVectors(const std::string & sCurrentHandle,
                           std::string & sNextHandle, 
                           long lSite, 
                           long lSocket, 
                           const std::string & sSiteId, 
                           const std::string & sAdapter, 
                           const std::string & sSocketId)
   {
      Detail::CExceptionPtr spoVectorDestructorException;

      CreateAndSetVector(sCurrentHandle, sNextHandle, spoVectorDestructorException, lSite, lSocket, sSiteId, sAdapter, sSocketId);
      
      if(spoVectorDestructorException.get())
      {
         spoVectorDestructorException->Throw();
      }

      TBase::SetVectors(sCurrentHandle, sNextHandle, lSite, lSocket, sSiteId, sAdapter, sSocketId);
   }


private:

   /**
      Helper for instantiating a static vector and passing it to the library-user's SetVector() override.  The only
      reason to have this function is so that the static vector is created and destroyed in its scope, which optimizes
      memory usage.
   */
   void CreateAndSetVector(const std::string & sCurrentHandle,
                           std::string & sNextHandle,
                           Detail::CExceptionPtr & spoVectorDestructorException,
                           long lSite,  
                           long lSocket, 
                           const std::string & sSiteId, 
                           const std::string & sAdapter, 
                           const std::string & sSocketId)
   {
      TVector oVec(Dictionary(), spoVectorDestructorException);
      SetVector(sCurrentHandle, sNextHandle, lSite, lSocket, sSiteId, sAdapter, sSocketId, oVec);
   }
};

/**
   Type used to act as the CStaticVectorServerBase class' template argument default value.
*/
struct NoArg{};

}// end namespace Detail

/**
   The primary template.  Specializations are immediately below.
*/
template<
      typename T0  = Detail::NoArg, typename T1  = Detail::NoArg, typename T2  = Detail::NoArg,
      typename T3  = Detail::NoArg, typename T4  = Detail::NoArg, typename T5  = Detail::NoArg,
      typename T6  = Detail::NoArg, typename T7  = Detail::NoArg, typename T8  = Detail::NoArg,
      typename T9  = Detail::NoArg, typename T10 = Detail::NoArg, typename T11 = Detail::NoArg,
      typename T12 = Detail::NoArg, typename T13 = Detail::NoArg, typename T14 = Detail::NoArg,
      typename T15 = Detail::NoArg, typename T16 = Detail::NoArg, typename T17 = Detail::NoArg,
      typename T18 = Detail::NoArg, typename T19 = Detail::NoArg
    >
class CStaticSocketVectorServerBase;

/**
  CStaticVectorServerBase specializations.  Implementations are forwarded to their templated base classes.
*/

template<class T0>
class CStaticSocketVectorServerBase<T0> : 
   public Detail::CSSocketVsi<T0, Detail::CSSocketVsiRoot>{};

template<class T0, class T1>
class CStaticSocketVectorServerBase<T1,                         T0> : 
   public Detail::CSSocketVsi<      T1, CStaticSocketVectorServerBase<T0>>{};

template<class T0, class T1, class T2>
class CStaticSocketVectorServerBase<T2,                         T1,T0> : 
   public Detail::CSSocketVsi<      T2, CStaticSocketVectorServerBase<T1,T0>>{};

template<class T0, class T1, class T2, class T3>
class CStaticSocketVectorServerBase<T3,                         T2,T1,T0> : 
   public Detail::CSSocketVsi<      T3, CStaticSocketVectorServerBase<T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4>
class CStaticSocketVectorServerBase<T4,                         T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T4, CStaticSocketVectorServerBase<T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5>
class CStaticSocketVectorServerBase<T5,                         T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T5, CStaticSocketVectorServerBase<T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6>
class CStaticSocketVectorServerBase<T6,                         T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T6, CStaticSocketVectorServerBase<T5,T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7>
class CStaticSocketVectorServerBase<T7,                         T6,T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T7, CStaticSocketVectorServerBase<T6,T5,T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
class CStaticSocketVectorServerBase<T8,                         T7,T6,T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T8, CStaticSocketVectorServerBase<T7,T6,T5,T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9>
class CStaticSocketVectorServerBase<T9,                         T8,T7,T6,T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T9, CStaticSocketVectorServerBase<T8,T7,T6,T5,T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9,
         class T10>
class CStaticSocketVectorServerBase<T10,                         T9,T8,T7,T6,T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T10, CStaticSocketVectorServerBase<T9,T8,T7,T6,T5,T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9,
         class T10, class T11>
class CStaticSocketVectorServerBase<T11,                         T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T11, CStaticSocketVectorServerBase<T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9,
         class T10, class T11, class T12>
class CStaticSocketVectorServerBase<T12,                         T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T12, CStaticSocketVectorServerBase<T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9,
         class T10, class T11, class T12, class T13>
class CStaticSocketVectorServerBase<T13,                         T12,T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T13, CStaticSocketVectorServerBase<T12,T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9,
         class T10, class T11, class T12, class T13, class T14>
class CStaticSocketVectorServerBase<T14,                         T13,T12,T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T14, CStaticSocketVectorServerBase<T13,T12,T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9,
         class T10, class T11, class T12, class T13, class T14, class T15>
class CStaticSocketVectorServerBase<T15,                         T14,T13,T12,T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T15, CStaticSocketVectorServerBase<T14,T13,T12,T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9,
         class T10, class T11, class T12, class T13, class T14, class T15, class T16>
class CStaticSocketVectorServerBase<T16,                         T15,T14,T13,T12,T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T16, CStaticSocketVectorServerBase<T15,T14,T13,T12,T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9,
         class T10, class T11, class T12, class T13, class T14, class T15, class T16, class T17>
class CStaticSocketVectorServerBase<T17,                         T16,T15,T14,T13,T12,T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<      T17, CStaticSocketVectorServerBase<T16,T15,T14,T13,T12,T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0>>{};

template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9,
         class T10, class T11, class T12, class T13, class T14, class T15, class T16, class T17, class T18>
class CStaticSocketVectorServerBase<T18,T17,T16,T15,T14,T13,T12,T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0> : 
   public Detail::CSSocketVsi<T18, CStaticSocketVectorServerBase<T17,T16,T15,T14,T13,T12,T11,T10,T9,T8,T7,T6,T5,T4,T3,T2,T1,T0>>{};

}// end namespace ComSerSdk