Sfoglia il codice sorgente

Cleaned up class heirarchy and removed storage of string identifier, using constexpr instead.

master
Trevor Irons 8 anni fa
parent
commit
a3dc462b7d
5 ha cambiato i file con 223 aggiunte e 9 eliminazioni
  1. 9
    2
      examples/DEMGrain.cpp
  2. 104
    0
      include/DEMGrain.h
  3. 9
    5
      include/DEMParticle.h
  4. 99
    0
      src/DEMGrain.cpp
  5. 2
    2
      src/DEMParticle.cpp

+ 9
- 2
examples/DEMGrain.cpp Vedi File

@@ -28,12 +28,19 @@ int main()
28 28
     std::cout << *Grain << std::endl;
29 29
     YAML::Node ng = Grain->Serialize();
30 30
 
31
+    auto Grain3 = DEMGrain::NewSP();
32
+    std::cout << Grain3->GetName() << std::endl;
33
+    std::cout << ((DEMParticle*)(Grain3.get()))->GetName() << std::endl;
34
+
35
+    auto Grain4 = DEMParticle::NewSP();
36
+    std::cout << Grain4->GetName() << std::endl;
37
+
31 38
     //Vector3r pos; pos << 2,3,4;
32 39
 
33 40
     //Grain->SetCentreMass( pos );
34 41
 
35
-    auto Grain2 = DEMGrain::DeSerialize(ng);
36
-    std::cout << *Grain2 << std::endl;
42
+    //auto Grain2 = DEMParticle::DeSerialize(ng);
43
+    //std::cout << *Grain2 << std::endl;
37 44
 
38 45
 /*
39 46
     if ( *Grain2 != *Grain) {

+ 104
- 0
include/DEMGrain.h Vedi File

@@ -0,0 +1,104 @@
1
+/* This file is part of Lemma, a geophysical modelling and inversion API.
2
+ * More information is available at http://lemmasoftware.org
3
+ */
4
+
5
+/* This Source Code Form is subject to the terms of the Mozilla Public
6
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
7
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
+ */
9
+
10
+/**
11
+ * @file
12
+ * @date      06/16/2016 02:10:28 PM
13
+ * @version   $Id$
14
+ * @author    Trevor Irons (ti)
15
+ * @email     tirons@egi.utah.edu
16
+ * @copyright Copyright (c) 2016, University of Utah
17
+ * @copyright Copyright (c) 2016, Trevor Irons & Lemma Software, LLC
18
+ */
19
+
20
+#ifndef  DEMGRAIN_INC
21
+#define  DEMGRAIN_INC
22
+
23
+#pragma once
24
+#include "DEMParticle.h"
25
+
26
+namespace Lemma {
27
+
28
+    /**
29
+      \brief
30
+      \details
31
+     */
32
+    class DEMGrain : public DEMParticle {
33
+
34
+        friend std::ostream &operator<<(std::ostream &stream, const DEMGrain &ob);
35
+
36
+        public:
37
+
38
+        // ====================  LIFECYCLE     =======================
39
+
40
+        /*
41
+         *  Factory method for generating concrete class.
42
+         *  @return a std::shared_ptr of type DEMGrain
43
+         */
44
+        static std::shared_ptr< DEMGrain > NewSP();
45
+
46
+        /**
47
+         *  Uses YAML to serialize this object.
48
+         *  @return a YAML::Node
49
+         *   @see DEMGrain::DeSerialize
50
+         */
51
+        YAML::Node Serialize() const;
52
+
53
+        /**
54
+         *   Constructs an DEMGrain object from a YAML::Node.
55
+         *   @see DEMGrain::Serialize
56
+         */
57
+        static std::shared_ptr<DEMGrain> DeSerialize(const YAML::Node& node);
58
+
59
+        // ====================  OPERATORS     =======================
60
+
61
+        // ====================  OPERATIONS    =======================
62
+
63
+        // ====================  ACCESS        =======================
64
+
65
+        // ====================  INQUIRY       =======================
66
+
67
+        virtual inline std::string GetName () const {
68
+            return std::string(this->CName);
69
+        }
70
+
71
+        protected:
72
+
73
+        // ====================  LIFECYCLE     =======================
74
+
75
+        /**
76
+         * Default protected constructor, use NewSP methods to construct
77
+         * @see DEMGrain::NewSP
78
+         */
79
+        DEMGrain ( );
80
+
81
+        /**
82
+         * Protected DeDerializing constructor, use factory DeSerialize  method.
83
+         * @see DEMGrain::DeSerialize
84
+         */
85
+        DEMGrain (const YAML::Node& node);
86
+
87
+        /** Default protected destructor, use smart pointers (std::shared_ptr) */
88
+        ~DEMGrain ();
89
+
90
+        // ====================  DATA MEMBERS  =========================
91
+
92
+        private:
93
+
94
+        static constexpr auto CName = "DEMGrain";
95
+
96
+    }; // -----  end of class  DEMGrain  -----
97
+}  // -----  end of namespace Lemma ----
98
+
99
+/* vim: set tabstop=4 expandtab: */
100
+/* vim: set filetype=cpp: */
101
+
102
+
103
+#endif   // ----- #ifndef DEMGRAIN_INC  -----
104
+

+ 9
- 5
include/DEMParticle.h Vedi File

@@ -54,8 +54,11 @@ namespace Lemma {
54 54
         Vector3r GetCentreMass(  );
55 55
 
56 56
         // ====================  INQUIRY       =======================
57
+        /** Returns the name of the underlying class, similiar to Python's type */
58
+        virtual inline std::string GetName() const {
59
+            return this->CName;
60
+        }
57 61
 
58
-#ifdef HAVE_YAMLCPP
59 62
         /**
60 63
          *  Uses YAML to serialize this object.
61 64
          *  @return a YAML::Node
@@ -66,25 +69,26 @@ namespace Lemma {
66 69
          *   Constructs an object from a YAML::Node.
67 70
          */
68 71
         static std::shared_ptr< DEMParticle > DeSerialize(const YAML::Node& node);
69
-#endif
70 72
 
71 73
         protected:
72 74
 
73 75
         // ====================  LIFECYCLE     =======================
74 76
 
75 77
         /** Default protected constructor, use New */
76
-        DEMParticle (const std::string& name);
78
+        DEMParticle ( );
77 79
 
78
-#ifdef HAVE_YAMLCPP
79 80
         /** Protected DeDerializing constructor, use factory DeSerialize  method*/
80 81
         DEMParticle (const YAML::Node& node);
81
-#endif
82 82
 
83 83
         /** Default protected destructor, use Delete */
84 84
         ~DEMParticle ();
85 85
 
86 86
         private:
87 87
 
88
+
89
+        /** ASCII string representation of the class name */
90
+        static constexpr auto CName = "DEMParticle";
91
+
88 92
         // ====================  DATA MEMBERS  =========================
89 93
 
90 94
         Vector3r    centreMass;

+ 99
- 0
src/DEMGrain.cpp Vedi File

@@ -0,0 +1,99 @@
1
+/* This file is part of Lemma, a geophysical modelling and inversion API.
2
+ * More information is available at http://lemmasoftware.org
3
+ */
4
+
5
+/* This Source Code Form is subject to the terms of the Mozilla Public
6
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
7
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
+ */
9
+
10
+/**
11
+ * @file
12
+ * @date      06/16/2016 02:11:21 PM
13
+ * @version   $Id$
14
+ * @author    Trevor Irons (ti)
15
+ * @email     tirons@egi.utah.edu
16
+ * @copyright Copyright (c) 2016, University of Utah
17
+ * @copyright Copyright (c) 2016, Trevor Irons & Lemma Software, LLC
18
+ */
19
+
20
+
21
+#include "DEMGrain.h"
22
+
23
+namespace Lemma {
24
+
25
+    // ====================  FRIEND METHODS  =====================
26
+
27
+    std::ostream &operator << (std::ostream &stream, const DEMGrain &ob) {
28
+        stream << ob.Serialize()  << "\n---\n"; // End of doc --- as a direct stream should encapulste thingy
29
+        return stream;
30
+    }
31
+
32
+    // ====================  LIFECYCLE     =======================
33
+
34
+    //--------------------------------------------------------------------------------------
35
+    //       Class:  DEMGrain
36
+    //      Method:  DEMGrain
37
+    // Description:  constructor (protected)
38
+    //--------------------------------------------------------------------------------------
39
+    DEMGrain::DEMGrain ( ) : DEMParticle( ) {
40
+
41
+    }  // -----  end of method DEMGrain::DEMGrain  (constructor)  -----
42
+
43
+    //--------------------------------------------------------------------------------------
44
+    //       Class:  DEMGrain
45
+    //      Method:  DEMGrain
46
+    // Description:  DeSerializing constructor (protected)
47
+    //--------------------------------------------------------------------------------------
48
+    DEMGrain::DEMGrain (const YAML::Node& node) : DEMParticle(node) {
49
+
50
+    }  // -----  end of method DEMGrain::DEMGrain  (constructor)  -----
51
+
52
+    //--------------------------------------------------------------------------------------
53
+    //       Class:  DEMGrain
54
+    //      Method:  NewSP()
55
+    // Description:  public constructor returing a shared_ptr
56
+    //--------------------------------------------------------------------------------------
57
+    std::shared_ptr< DEMGrain >  DEMGrain::NewSP() {
58
+        std::shared_ptr< DEMGrain > sp(new  DEMGrain( ), LemmaObjectDeleter() );
59
+        return sp;
60
+    }
61
+
62
+    //--------------------------------------------------------------------------------------
63
+    //       Class:  DEMGrain
64
+    //      Method:  ~DEMGrain
65
+    // Description:  destructor (protected)
66
+    //--------------------------------------------------------------------------------------
67
+    DEMGrain::~DEMGrain () {
68
+
69
+    }  // -----  end of method DEMGrain::~DEMGrain  (destructor)  -----
70
+
71
+    //--------------------------------------------------------------------------------------
72
+    //       Class:  DEMGrain
73
+    //      Method:  Serialize
74
+    //--------------------------------------------------------------------------------------
75
+    YAML::Node  DEMGrain::Serialize (  ) const {
76
+        YAML::Node node = DEMParticle::Serialize();
77
+        node.SetTag( GetName() );
78
+        // FILL IN CLASS SPECIFICS HERE
79
+        return node;
80
+    }		// -----  end of method DEMGrain::Serialize  -----
81
+
82
+    //--------------------------------------------------------------------------------------
83
+    //       Class:  DEMGrain
84
+    //      Method:  DeSerialize
85
+    //--------------------------------------------------------------------------------------
86
+    std::shared_ptr<DEMGrain> DEMGrain::DeSerialize ( const YAML::Node& node  ) {
87
+        if (node.Tag() !=  "DEMGrain" ) {
88
+            throw  DeSerializeTypeMismatch( "DEMGrain", node.Tag());
89
+        }
90
+        std::shared_ptr< DEMGrain > Object(new DEMGrain(node), LemmaObjectDeleter() );
91
+        return Object ;
92
+    }		// -----  end of method DEMGrain::DeSerialize  -----
93
+
94
+} // ----  end of namespace Lemma  ----
95
+
96
+/* vim: set tabstop=4 expandtab: */
97
+/* vim: set filetype=cpp: */
98
+
99
+

+ 2
- 2
src/DEMParticle.cpp Vedi File

@@ -35,7 +35,7 @@ std::ostream &operator << (std::ostream &stream, const DEMParticle &ob) {
35 35
 //      Method:  DEMParticle
36 36
 // Description:  constructor (protected)
37 37
 //--------------------------------------------------------------------------------------
38
-DEMParticle::DEMParticle (const std::string& name) : LemmaObject(name) {
38
+DEMParticle::DEMParticle ( ) : LemmaObject( ) {
39 39
 
40 40
 }  // -----  end of method DEMParticle::DEMParticle  (constructor)  -----
41 41
 
@@ -54,7 +54,7 @@ DEMParticle::DEMParticle (const YAML::Node& node) : LemmaObject(node) {
54 54
 // Description:  public constructor
55 55
 //--------------------------------------------------------------------------------------
56 56
 std::shared_ptr< DEMParticle > DEMParticle::NewSP() {
57
-    std::shared_ptr<DEMParticle> sp(new  DEMParticle("DEMParticle"), LemmaObjectDeleter() );
57
+    std::shared_ptr<DEMParticle> sp(new  DEMParticle( ), LemmaObjectDeleter() );
58 58
     return sp;
59 59
 }
60 60
 

Loading…
Annulla
Salva