dynamicCodeContext.H 5.12 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8 9
    Copyright (C) 2011-2016 OpenFOAM Foundation
    Copyright (C) 2019 OpenCFD Ltd.
10 11 12 13
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

andy's avatar
andy committed
14 15 16 17
    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 3 of the License, or
    (at your option) any later version.
18 19 20 21 22 23 24

    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
andy's avatar
andy committed
25
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

Class
    Foam::dynamicCodeContext

Description
    Encapsulation of dynamic code dictionaries

SourceFiles
    dynamicCodeContext.C

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

#ifndef dynamicCodeContext_H
#define dynamicCodeContext_H

41
#include <functional>
42
#include "dictionary.H"
43
#include "SHA1.H"
44 45 46 47 48 49 50 51 52 53 54 55

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

namespace Foam
{

/*---------------------------------------------------------------------------*\
                     Class dynamicCodeContext Declaration
\*---------------------------------------------------------------------------*/

class dynamicCodeContext
{
56
    // Private Data
57

58
        //- The parent dictionary context
59
        std::reference_wrapper<const dictionary> dict_;
60

61 62
        //- The SHA1 of the contents
        SHA1 sha1_;
63 64 65 66

        //- Optional "codeOptions" entry
        string options_;

67
        //- Optional "codeLibs" entry
mattijs's avatar
mattijs committed
68 69
        string libs_;

70 71 72 73 74 75 76 77
        //- Optional "codeInclude" entry
        string include_;

        //- Optional "code" entry
        string code_;

        //- Optional "localCode" entry
        string localCode_;
78

79

80 81 82 83
public:

    // Constructors

84 85 86
        //- Construct null
        dynamicCodeContext();

87
        //- Construct from a dictionary
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        explicit dynamicCodeContext(const dictionary& dict);


    // Static Member Functions

        //- Cleanup string and expand with dictionary parameters
        static void inplaceExpand(string& code, const dictionary& dict);

        //- Prefix a \#line directive to code.
        //  The input lineNum is 0-based.
        //  Is a no-op if any of the arguments are invalid
        //  (lineNum is negative, code or file are empty)
        //
        //  \return The change in string length caused by the directive.
        //  This can potentially be used to recover the substring portions.
        static unsigned addLineDirective
        (
            string& code,
            label lineNum,
            const fileName& file
        );
109

110 111 112 113 114 115 116 117 118 119 120
        //- Prefix a \#line directive to code.
        //  The name of the dictionary is used for the 'file' name.
        static unsigned addLineDirective
        (
            string& code,
            label lineNum,
            const dictionary& dict
        );


    // Member Functions
121

122 123 124 125 126
        //- Considered valid if not using dictionary::null as the context
        bool valid() const;

        //- Set code context from a dictionary
        void setCodeContext(const dictionary& dict);
127 128 129 130

        //- Return the parent dictionary context
        const dictionary& dict() const
        {
131
            return dict_.get();
132 133 134 135 136 137 138 139 140 141 142 143 144 145
        }

        //- Return the code-includes
        const string& include() const
        {
            return include_;
        }

        //- Return the code-options
        const string& options() const
        {
            return options_;
        }

mattijs's avatar
mattijs committed
146 147 148 149 150 151
        //- Return the code-libs
        const string& libs() const
        {
            return libs_;
        }

152 153 154 155 156 157
        //- Return the code
        const string& code() const
        {
            return code_;
        }

158 159 160 161 162 163
        //- Return the local (file-scope) code
        const string& localCode() const
        {
            return localCode_;
        }

164 165
        //- Return SHA1 calculated from options, libs, include, code
        const SHA1& sha1() const
166 167 168 169
        {
            return sha1_;
        }

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        //- Add content to SHA1 hashing
        void append(const std::string& str)
        {
            sha1_.append(str);
        }


    // Member Operators

        //- Cast to dictionary
        operator const dictionary&() const
        {
            return dict_.get();
        }

185 186 187 188 189 190 191 192 193 194 195 196
};


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

} // End namespace Foam

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

#endif

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