/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 1991-2005 OpenCFD Ltd.
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the
    Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM; if not, write to the Free Software Foundation,
    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Class
    writer

Description
    base class for graphics format writing. Entry points are
    - write(..). Write to an Ostream a table of points with corresponding
    values.

    - write(scalar/vector/tensor). Write single scalar/vector/tensor.
    Default is to write space separated components.

    Example:

        // Construct writer of xmgr type
        autoPtr<writer<scalar> > scalarFormatter(writer<scalar>::New("xmgr"));

        // Output list of points and corresponding values
        scalarFormatter().write
        (
            coordSet
            (
                points,         // sample coordinates
                "someLine",     // name of coordSet
                "distance",     // write coordinates as distance to refPoint
                points[0]       // reference point
            ),
            "U.component(0)",   // name of values
            vals                // values
        );

        
SourceFiles
    writer.C

\*---------------------------------------------------------------------------*/

#ifndef writer_H
#define writer_H

#include "fileName.H"
#include "wordList.H"
#include "vector.H"
#include "tensor.H"
#include "typeInfo.H"
#include "runTimeSelectionTables.H"
#include "autoPtr.H"
#include "Field.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

// Class forward declarations
class coordSet;

/*---------------------------------------------------------------------------*\
                           Class writer Declaration
\*---------------------------------------------------------------------------*/

template<class Type>
class writer
{

protected:

    //- Generates filename from coordSet and sampled fields
    fileName getBaseName(const coordSet&, const wordList&) const;
    
    void writeCoord
    (
        const coordSet& samples,
        const label sampleI,
        Ostream& os
    ) const;

    //- Writes single-column ascii write. Column 1 is coordSet coordinate,
    //  columns 2 is the value. Uses write() function
    //  to write coordinate in correct format.
    void writeTable
    (
        const coordSet&,
        const List<Type>&,
        Ostream& os
    ) const;

    //- Writes multi-column ascii write. Column 1 is coordSet coordinate,
    //  columns 2..n are the values. Uses write() function
    //  to write coordinate in correct format.
    void writeTable
    (
        const coordSet&,
        const List<const List<Type>*>&,
        Ostream& os
    ) const;


public:

    //- Runtime type information
    TypeName("writer");

    // Declare run-time constructor selection table

        declareRunTimeSelectionTable
        (
            autoPtr,
            writer,
            word,
            (),
            ()
        );


    // Selectors

        //- Return a reference to the selected writer
        static autoPtr<writer> New
        (
            const word& writeFormat
        );


    // Constructors

        //- Construct null
        writer();


    // Destructor

        virtual ~writer() = 0;


    // Member Functions

        // Access

        // Write

            //- Generate file name with correct extension
            virtual fileName getFileName
            (
                const coordSet&,
                const wordList&
            ) const = 0;

            //- General entry point for writing.
            //  The data is organized in a set of point with one or
            //  more values per point
            virtual void write
            (
                const coordSet&,
                const wordList&,
                const List<const Field<Type>*>&,
                Ostream&
            ) const = 0;

            //- Write scalar as ascii
            virtual Ostream& write(const scalar, Ostream&) const;

            //- Write vector. Tab separated ascii
            virtual Ostream& write(const vector&, Ostream&) const;

            //- Write tensor. Tab separated ascii
            virtual Ostream& write(const tensor&, Ostream&) const;
};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#ifdef NoRepository
#   include "writer.C"
#endif

// Only used internally
#define makeTypeWritersTypeName(type)                       \
                                                            \
defineNamedTemplateTypeNameAndDebug(type, 0);

// Used externally sometimes
#define makeWritersTypeName(typeWriter)                     \
                                                            \
makeTypeWritersTypeName(typeWriter##ScalarWriter);          \
makeTypeWritersTypeName(typeWriter##VectorWriter);          \
makeTypeWritersTypeName(typeWriter##TensorWriter);

// Define type info for single template instantiation (e.g. vector)
#define makeWriterTypes(WriterType, type)                   \
                                                            \
defineNamedTemplateTypeNameAndDebug(type, 0);               \
                                                            \
addToRunTimeSelectionTable                                  \
(                                                           \
    WriterType, type, word                                  \
);


// Define type info info for scalar, vector etc. instantiations
#define makeWriters(typeWriter)                             \
                                                            \
makeWriterTypes(scalarWriter, typeWriter##ScalarWriter);    \
makeWriterTypes(vectorWriter, typeWriter##VectorWriter);    \
makeWriterTypes(tensorWriter, typeWriter##TensorWriter);


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

// ************************************************************************* //
