\documentclass[a4paper,11pt,final]{article}
\input{config.tex}



\begin{document}
\bbtkGuide[Developer's Guide]





% ==========================================
\tableofcontents
% ==========================================

% ==========================================
\newpage
\hrule

% ==========================================
\section{Introduction}
% ==========================================

% ==========================================
\section{Pipeline processing algorithm}
% ==========================================

% ==========================================
\subsection{Input and output status}
% ==========================================
Each input of a black box has a Status, 
of type IOStatus (defined in bbtkConnection.h) 
which can take one of the three values:
\begin{itemize}
\item UPTODATE (==0): The input did not change since last processing 
\item MODIFIED (==1): [Initial value on construction] The input changed since last processing but is synchronized with the amont box output to which it is connected, if any. This status has only a NOTIFICATION purpose, to let the processing method of the box know which inputs have changed since last time. If it is connected, a MODIFIED input will not call the amont box update during pipeline execution.
\item OUTOFDATE (==2): The input changed since last processing and is NOT synchronized with the amont box output to which it is connected. This means that the pipeline update must recurse to the amont box next time it is executed, in order to update the input value.
\end{itemize}
The status of an input is stored by the class BlackBoxInputConnector.

Each output of a black box instance also has a Status, 
of type IOStatus but which can only take the two values:
\begin{itemize}
\item UPTODATE (==0): The output is up-to-date (the box does not need to be reprocessed).
\item OUTOFDATE (==2): [Initial value on construction] The output is out-of-date (the box needs to be reprocessed).
\end{itemize}
The status of an output is stored by the class BlackBoxOutputConnector.

If a box output 'O' is connected to another box input 'I' then there are some consistency constraints on their I/O statuses:
\begin{itemize}
\item If 'I' is UPTODATE then 'O' is necesseraly UPTODATE
\item If 'I' is MODIFIED then 'O' is necesseraly UPTODATE
\item If 'I' is OUTOFDATE then 'O' can be in any Status (The case 'O' is UPTODATE occurs for example when the amont box was created and executed - hence setting its outputs UPTODATE - *BEFORE* being connected to the aval box)
\item If 'O' is UPTODATE then 'I' can be in any Status
\item If 'O' is OUTOFDATE then 'I' is necessarly OUTOFDATE
\end{itemize}

The status of an input can be modified:
\begin{itemize}
\item By BlackBox::bbSetStatusAndPropagate which is called by:
\begin{itemize}
\item BlackBox::bbSetInput : it is set to MODIFIED
\item BlackBox::bbSignalOutputModification 
\item BlackBox::bbConnectInput which is called when a Connection is connected to the input : it is set to OUTOFDATE 
\item Connection::OnOutputChange
\end{itemize}
\item By BlackBox::bbComputePostProcessStatus which is called after bbProcess in AtomicBlackBox::bbBackwardUpdate : if the input status is MODIFIED then it is changed to UPTODATE.
\item By Connection::BackwardUpdate which is responsible for updating its amont box, transfering the output value to the input and updating the input status. If, after processing the amont box, the output status is UPTODATE then the new input status is MODIFIED, else if the output status is OUTOFDATE then the new input status is OUTOFDATE.
\end{itemize}

The status of an output is modifed in:
\begin{itemize}
\item By BlackBox::bbSetStatusAndPropagate, when propagating the new input status to the outputs. 
\item By BlackBox::bbComputePostProcessStatus, which is called after bbProcess in AtomicBlackBox::bbBackwardUpdate. The new output status is :
\begin{itemize}
\item UPTODATE if all the box inputs are UPTODATE and the BoxProcessMode is *NOT* 'Always'
\item OUTOFDATE in other cases
\end{itemize}
\end{itemize}

% ==========================================
\subsection{Pipeline execution}
% ==========================================
The main execution method of a black box is bbExecute.
bbExecute checks werther the box is not already executing (bbGetExecuting()) 
to prevent reentrance and 
checks werther the execution is not frozen (bbGlobalGetFreezeExecution()).
If it is not the case then it calls bbBackwardUpdate which is 
the main recursive pipeline processing method of a box, giving it a 
NULL Connection::Pointer.

bbBackwardUpdate is defined in AtomicBlackBox:

\bigskip\hrule\begin{verbatim}
void AtomicBlackBox::bbBackwardUpdate ( Connection::Pointer caller )
{
  // Updates the box inputs. Returns the max input IOStatus after update
  IOStatus s = bbUpdateInputs();
  // If at least one input is modified or BoxProcessMode=='Always'
  if ( (s != UPTODATE) || bbBoxProcessModeIsAlways() )
    {
      // User process
      bbProcess();
      // Displays the window (overloaded in widget Blackboxes, WxBlackBox, KWBlackBox, etc.)
      bbShowWindow(caller);
      // Compute the final status of inputs and outputs
      bbComputePostProcessStatus();
    }
}
\end{verbatim}\hrule\bigskip

bbUpdateInputs iterates over the InputConnector of the box and 
calls BackwardUpdate on each one. It returns the max of the final 
status of the input connectors:
\bigskip\hrule\begin{verbatim}
IOStatus BlackBox::bbUpdateInputs()
{
  IOStatus s = UPTODATE;
  InputConnectorMapType::iterator i;
  for ( i = bbGetInputConnectorMap().begin(); 
        i!= bbGetInputConnectorMap().end(); ++i) 
    {
      i->second->BackwardUpdate();
      IOStatus t = i->second->GetStatus();
      if (t > s) s = t;
    }
  return s;    
 
}
\end{verbatim}\hrule\bigskip

\bigskip\hrule\begin{verbatim}
void BlackBoxInputConnector::BackwardUpdate()
{
  // If connected and OUTOFDATE : recursive update
  // Post-update status is updated by the connection 
  // (either MODIFIED or OUTOFDATE)
   if ( mConnection && (mStatus == OUTOFDATE) ) 
    {
      mConnection->BackwardUpdate();
    }
}
\end{verbatim}\hrule\bigskip

\bigskip\hrule\begin{verbatim}
void Connection::BackwardUpdate()
{
  // Calls bbBackwardUpdate on the initial box
  mFrom->bbBackwardUpdate(GetThisPointer<Connection>());
  // Transfers the data from the initial box output to the 
  // final box input adapting it if necessary
  TransferData();
  // Updates the status of the final box input
  IOStatus s = MODIFIED;
  if ( mFrom->bbGetOutputConnector(mOutput).GetStatus() == OUTOFDATE) 
    s = OUTOFDATE;
  mTo->bbGetInputConnector(mInput).SetStatus(s);
}
\end{verbatim}\hrule\bigskip



% ==========================================
\section{Misc}
% ==========================================

\subsection{Displaying messages}

\begin{verbatim}
bbtkMessage("Kind",level,"message "<<"to "<<" display : i="<<i<<std::endl);
bbtkDebugMessage("Kind",level,"message "<<"to "<<" display : i="<<i<<std::endl);
\end{verbatim}

\section{Types and RTTI}



In \bbtk the class conveying the information on a type is 
\begin{verbatim}
bbtk::TypeInfo
\end{verbatim}
which is simply a typedef on 
\begin{verbatim}
const std::type_info&
\end{verbatim}
Remember that all constructors of the std::type\_info class are private, 
hence objects can only be created by the operator \texttt{typeid} 
which returns a const reference on a type\_info. 
Hence the \bbtk type TypeInfo conveys that const reference 
and cannot be itself referenced. 
Any function or method which takes or returns a TypeInfo must take 
or return it \emph{by value} (see e.g. the TypeName function below).
To print the name of a type use one of the template functions 
\begin{verbatim}
template <class T> std::string TypeName();
template <class T> std::string TypeName(const T&);
template <class T> std::string TypeName(bbtk::TypeInfo);
\end{verbatim}


\begin{verbatim}
BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::string,"string");
\end{verbatim}

\end{document} 
