wiki:WritingNewClasses

Writing a new Lemma Class

This guideline provides a way to add a new class to Lemma. First you need to decide what Module you will be contributing to. For this example, we will be adding a new class RectilinearGridVTKImporter to LemmaCore. We provide some vim tools to help with boilerplate which are located at Lemma/vim. Keep in mind the LemmaStyleGuide.

  1. Create a new file with your class name. Note that Lemma uses mixed case filenames, and most POSIX systems are case sensitive. As such we create new files RectilinearGridVTKImporter.h and RectilinearGridVTKImporter.cpp which are located in the Lemma/Modules/LemmaCore/include and Lemma/Modules/LemmaCore/src directories of Lemma.
  2. Use standard include guards, in the case that your class has a intrinsic dependency on VTK you may also add such an include guard for that.
    #ifndef  RECTILINEARGRIDVTKIMPORTER_INC
    #define  RECTILINEARGRIDVTKIMPORTER_INC
    
    #ifdef  LEMMAUSEVTK
    
    #endif      // -----  LEMMAUSEVTK  -----
    
    #endif   // ----- #ifndef RECTILINEARGRIDVTKIMPORTER_INC  -----
    
  1. Use the c-support vim extension to write your class definition, this is found under C/C++ --> C++ --> Lemma class. The extension will query you for the module, base class, and class name; in this case LemmaCore, LemmaObject, and RectilinearGridVTKImporter respectively. This will populate the class with most of the boilerplate and some basic Doxygen documentation.
#ifndef  RECTILINEARGRIDVTKIMPORTER_INC
#define  RECTILINEARGRIDVTKIMPORTER_INC

#ifdef  LEMMAUSEVTK

#pragma once
#include "LemmaObject.h"

namespace Lemma {

    /**
     * \ingroup LemmaCore
     * \brief
     * \details
     */
    class RectilinearGridVTKImporter : public LemmaObject {

        friend std::ostream &operator<<(std::ostream &stream, const RectilinearGridVTKImporter &ob);

        protected:
        /*
         *  This key is used to lock the constructor. It is protected so that inhereted
         *  classes also have the key to contruct their base class.
         */
        struct ctor_key {};

        public:

        // ====================  LIFECYCLE     =======================

        /**
         * Default constructor.
         * @note This method is locked, and cannot be called directly.
         *       The reason that the method is public is to enable the use
         *       of make_shared whilst enforcing the use of shared_ptr,
         *       in c++-17, this curiosity may be resolved.
         * @see RectilinearGridVTKImporter::NewSP
         */
        explicit RectilinearGridVTKImporter ( const ctor_key& );

        /**
         * DeSerializing constructor.
         * @note This method is locked, and cannot be called directly.
         *       The reason that the method is public is to enable the use
         *       of make_shared whilst enforcing the use of shared_ptr,
         *       in c++-17, this curiosity may be resolved.
         * @see RectilinearGridVTKImporter::DeSerialize
         */
        RectilinearGridVTKImporter ( const YAML::Node& node, const ctor_key& );

        /**
         * Default destructor.
         * @note This method should never be called due to the mandated
         *       use of smart pointers. It is necessary to keep the method
         *       public in order to allow for the use of the more efficient
         *       make_shared constructor.
         */
        virtual ~RectilinearGridVTKImporter ();

        /**
         *  Uses YAML to serialize this object.
         *  @return a YAML::Node
         *  @see RectilinearGridVTKImporter::DeSerialize
         */
        virtual YAML::Node Serialize() const;

        /*
         *  Factory method for generating concrete class.
         *  @return a std::shared_ptr of type RectilinearGridVTKImporter
         */
        static std::shared_ptr< RectilinearGridVTKImporter > NewSP();

        /**
         *   Constructs an RectilinearGridVTKImporter object from a YAML::Node.
         *   @see RectilinearGridVTKImporter::Serialize
         */
        static std::shared_ptr<RectilinearGridVTKImporter> DeSerialize(const YAML::Node& node);

        // ====================  OPERATORS     =======================

        // ====================  OPERATIONS    =======================

        // ====================  ACCESS        =======================

        // ====================  INQUIRY       =======================
        /**
         *  Returns the name of the underlying class, similiar to Python's type
         *  @return string of class name
         */
        virtual inline std::string GetName() const {
            return CName;
        }

        protected:

        // ====================  LIFECYCLE     =======================

        /** Copy is disabled */
        RectilinearGridVTKImporter( const RectilinearGridVTKImporter& ) = delete;

        // ====================  DATA MEMBERS  =========================

        private:

        /** ASCII string representation of the class name */
        static constexpr auto CName = "RectilinearGridVTKImporter";

    }; // -----  end of class  RectilinearGridVTKImporter  -----

}  // -----  end of namespace Lemma ----

/* vim: set tabstop=4 expandtab: */
/* vim: set filetype=cpp: */

#endif      // -----  LEMMAUSEVTK  -----

#endif   // ----- #ifndef RECTILINEARGRIDVTKIMPORTER_INC  -----
  1. Unless your class is header only, open the implementation file RectilinearGridVTKImporter.cpp, and fill the class with boilerplate using C/C++ --> C++ --> IMPLEMENTATION --> Lemma class.
  1. Register your new class in CMakeLists.txt in the source directory.
  1. Test compile your code, if it compiles, start hacking.
Last modified 5 months ago Last modified on Jul 29, 2018 1:33:37 AM