OFtutorial06_customClasses 解析

组成

如图

customClass.H

#include "fvCFD.H"

class customClass
{
private:
    label myInt_;

public:
    customClass();
    ~customClass();

    // Access operators - allow the internal value to be set or retrieved.
    // Defined as inline to make the code run faster by avoiding expensive linking.
    // .set() method defined as const as it is not allowed to modify any of the class fields.
    inline label get() const {return myInt_;};
    inline void set(label newInt) {myInt_=newInt;};

    // A basic function
    label basicFunction() const;//basicFunction()函数声明

    // A function which accepts a reference to the mesh and does some operations
    // on it but without modifying it.
    // Passing a reference rather than the entire mesh object instance by value
    // is much more memory efficient - in short, only one mesh object exists
    // and this method only learns to talk to it rather than make a new, separate copy.
    void meshOpFunction(fvMesh& mesh);//meshOpFunction函数声明
};

customClass.C

#include "customClass.H"

customClass::customClass()
{
    myInt_= 0;
}

customClass::~customClass()
{}
//basicFunction()函数定义,返回myInt_*2
label customClass::basicFunction() const
{
    Info << "Calling customClass::basicFunction()" << endl;
    return myInt_*2;
}
//meshOpFunction函数定义,为myInt_赋值mesh.C().size()
void customClass::meshOpFunction(fvMesh& mesh)
{
    Info << "Custom class got a mesh with " << mesh.C().size() << " cells" << endl;
    myInt_ = mesh.C().size();
}

derivedClass.H

#include "fvCFD.H"

// use a standard C++ stringstream object
#include <sstream>

// This class is derived from the IOdictionary, declared in
// $FOAM_SRC/OpenFOAM/db/IOobjects/IOdictionary .
// We derive all the methods by setting maximum access level as public, thus allowing
// all the inherited methods to be accessed in the same way as they were for
// the base class.
class myDict : public IOdictionary
{
public:
    // Main constructor which accepts an IOobject (a file)
    myDict(const IOobject& ioObj);
    ~myDict();

    // custom method which prints the tokens (variables) defined in the file
    void printTokensInTheDict() const;//printTokensInTheDict()函数声明
};

derivedClass.C

#include "derivedClass.H"

myDict::myDict(const IOobject& ioObj)
:
    // Call the base class constructor to make sure all inherited members get
    // initialised as required
    IOdictionary(ioObj)//继承父类
{
// do nothing since we do not have any bespoke fields
}

myDict::~myDict()
{}

void myDict::printTokensInTheDict() const
{
    // retrieve the list of non-space characters in the file using the
    // method defined in dictionary.H, from which the IOdictionary object itself
    // is derived.
    List<token> characters(this->tokens());//获取包含 token 类型对象的列表

    // Create a stream which will hold the message to be printed out.
    // Important to remember about the namespace.
    std::stringstream ss;
    ss << "Tokens in the file:";

    // go over each token in the file
    forAll(characters,i)
        // if the entry is a word, add it to the message
        if (characters[i].isWord())
            ss << "\n" << tab << characters[i].wordToken();

    // print the message - convert to a C-style char array to make sure the
    // printout looks good
    Info << ss.str().c_str() << endl;
}

OFtutorial6.C

源码

头文件

#include "fvCFD.H"

// Include the code for the custom classes declared in .H and defined
// in .C files.
// NOTE: check how the Make/files changed to make sure the additional code gets
// compiled before the main utility.
#include "customClass.H"
#include "derivedClass.H"

主函数

头文件

int main(int argc, char *argv[])
{
    #include "setRootCase.H"
    #include "createTime.H"
    #include "createMesh.H"

创建一个custom实例

    // Create a custom class instance
    customClass customInstance;
    Info << "Default value " << customInstance.get() << endl;

    // Set a new value
    customInstance.set(10);
    Info << "New value " << customInstance.get() << endl;

    // Call a basic function
    customInstance.basicFunction();

    // Pass a reference to the mesh to the custom class and let it do its things
    customInstance.meshOpFunction(mesh);
    Info << "Yet another value " << customInstance.get() << endl;

创建一个myDict实例

    // Now, create an instance of a derived class which inherits from an IOdictionary object
    myDict myTransportProperties
    (
        IOobject
        (
            "transportProperties",
            runTime.constant(),
            mesh,
            IOobject::MUST_READ_IF_MODIFIED,
            IOobject::NO_WRITE
        )
    );
    // Create a viscosity scalar using our new class
    dimensionedScalar nu
    (
        "nu",
        dimViscosity,
        myTransportProperties.lookup("nu")
    );
    Info << "Created a viscosity scalar: " << nu << endl;

    // List the contents of the dictionary using the derived class method
    // implemented specifically for this purpose
    myTransportProperties.printTokensInTheDict();

    Info<< "End\n" << endl;

    return 0;
}

Make

files

源码

值得注意的是,在OFtutorial6.C文件中并未includecustomClass.CderivedClass.C两个文件,这意味这调用函数只需引用其定义,但函数也需要编译

customClass.C
derivedClass.C
OFtutorial6.C

EXE = $(FOAM_USER_APPBIN)/ofTutorial6

options

与此前章节差别不大,不做赘述

Allwmake、Allwclean、testcase

与此前章节差别不大,不做赘述

posted @ 2024-08-13 10:44  ouqiyo  阅读(35)  评论(0)    收藏  举报