1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2014 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Alessandro Tasora, Radu Serban
13 // =============================================================================
14 
15 #ifndef CHOBJECT_H
16 #define CHOBJECT_H
17 
18 #include <cfloat>
19 #include <cmath>
20 #include <cstdlib>
21 #include <iostream>
22 #include <memory>
23 #include <string>
24 
25 #include "chrono/core/ChLog.h"
26 #include "chrono/core/ChMath.h"
27 
28 #include <vector>
29 
30 namespace chrono {
31 
32 /// @addtogroup chrono_physics
33 /// @{
34 
35 // Forward references
36 class ChVar;
37 class ChTag;
38 
39 /// Base class for items which can be named, deleted, copied. etc. as in the editor of a 3d modeler.
40 class ChApi ChObj {
41   private:
42     std::string m_name;  ///< name of object
43     int m_identifier;    ///< object identifier
44 
45   protected:
46     double ChTime;  ///< the time of simulation for the object
47 
48   public:
49     ChObj();
50     ChObj(const ChObj& other);
~ChObj()51     virtual ~ChObj() {}
52 
53     /// "Virtual" copy constructor.
54     /// Concrete derived classes must implement this.
55     virtual ChObj* Clone() const = 0;
56 
57     /// Gets the numerical identifier of the object.
GetIdentifier()58     int GetIdentifier() const { return m_identifier; }
59     /// Sets the numerical identifier of the object.
SetIdentifier(int id)60     void SetIdentifier(int id) { m_identifier = id; }
61 
62     /// Gets the simulation time of this object
GetChTime()63     double GetChTime() const { return ChTime; }
64     /// Sets the simulation time of this object.
SetChTime(double m_time)65     void SetChTime(double m_time) { ChTime = m_time; }
66 
67     /// Gets the name of the object as C Ascii null-terminated string -for reading only!
GetName()68     const char* GetName() const { return m_name.c_str(); }
69     /// Sets the name of this object, as ascii string
SetName(const char myname[])70     void SetName(const char myname[]) { m_name = myname; }
71 
72     /// Gets the name of the object as C Ascii null-terminated string.
GetNameString()73     std::string GetNameString() const { return m_name; }
74     /// Sets the name of this object, as std::string
SetNameString(const std::string & myname)75     void SetNameString(const std::string& myname) { m_name = myname; }
76 
77     // Set-get generic LONG flags, passed as reference
78 
MFlagsSetAllOFF(int & mflag)79     void MFlagsSetAllOFF(int& mflag) { mflag = 0; }
MFlagsSetAllON(int & mflag)80     void MFlagsSetAllON(int& mflag) {
81         mflag = 0;
82         mflag = ~mflag;
83     }
MFlagSetON(int & mflag,int mask)84     void MFlagSetON(int& mflag, int mask) { mflag |= mask; }
MFlagSetOFF(int & mflag,int mask)85     void MFlagSetOFF(int& mflag, int mask) { mflag &= ~mask; }
MFlagGet(int & mflag,int mask)86     int MFlagGet(int& mflag, int mask) { return (mflag & mask); }
87 
88     /// Method to allow serialization of transient data to archives.
89     virtual void ArchiveOUT(ChArchiveOut& marchive);
90 
91     /// Method to allow de-serialization of transient data from archives.
92     virtual void ArchiveIN(ChArchiveIn& marchive);
93 
94     // Method to allow mnemonic names in (de)serialization of containers (std::vector, arrays, etc.)
ArchiveContainerName()95     virtual std::string& ArchiveContainerName() { return m_name; }
96 };
97 
98 CH_CLASS_VERSION(ChObj, 0)
99 
100 // Functions to manipulate STL containers of ChObj objects
101 
102 template <class T, class Iterator>
ChContainerSearchFromName(const char * m_name,Iterator from,Iterator to)103 T ChContainerSearchFromName(const char* m_name, Iterator from, Iterator to) {
104     Iterator iter = from;
105     while (iter != to) {
106         if (!strcmp(m_name, (*iter)->GetName()))
107             return (*iter);
108         iter++;
109     }
110     return T(0);
111 }
112 
113 template <class T, class Iterator>
ChContainerSearchFromID(int myID,Iterator from,Iterator to)114 T ChContainerSearchFromID(int myID, Iterator from, Iterator to) {
115     Iterator iter = from;
116     while (iter != to) {
117         if (myID == (*iter)->GetIdentifier())
118             return (*iter);
119         iter++;
120     }
121     return T(0);
122 }
123 
124 /// @} chrono_physics
125 
126 }  // end namespace chrono
127 
128 #endif
129