Teamcenter C++ API Reference  2312
BusinessObjectRef.hxx
Go to the documentation of this file.
1 //Copyright 2022 Siemens Digital Industries Software
2 //==================================================
3 //Copyright $2007.
4 //Siemens Product Lifecycle Management Software Inc.
5 //All Rights Reserved.
6 //==================================================
7 //Copyright 2022 Siemens Digital Industries Software
8 
9 /*====================================================================================================
10 File description:
11 
12  Filename: BusinessObjectRef.hxx
13  Module : metaframework
14 
15  POMRef and EIMRef are DEPRICATED forms of BusinessObjectRef and should not be used in new code.
16 
17  Usage:
18 
19  tag_t objTag = fromSomewhere();
20  ...
21  BusinessObjectRef<persistBusObjClass> persistRef = busTag; // still just a tag_t...
22  busRef->getXYZ(...);
23 
24 
25 
26 ==================================================================================================
27  Date Name Description of Change
28 
29 $HISTORY$
30 ==================================================================================================*/
31 
32 
33 #ifndef TCBUSINESSOBJECTREF_HXX
34 
35 #define TCBUSINESSOBJECTREF_HXX
36 
37 #include <unidefs.h>
39 #include <base_utils/IFail.hxx>
42 #include <cxpom/cxpom_errors.h>
45 #include <typeinfo>
46 #include <type_traits>
47 #include <pom/pom/pom.h>
48 
49 template <typename T> class BusinessObjectRef
50 {
51  static_assert( std::is_base_of_v<::Teamcenter::RootObject, T> );
52 
53 public:
54 
55  // default constructor
57  // constructor
59  : m_tag( tag ), m_pointee( 0 )
60  {}
61 
62  BusinessObjectRef( T* object )
63  : m_tag( 0 ), m_pointee( 0 )
64  {
65  if ( object != 0 )
66  {
67  m_pointee = object;
68  m_tag = object->getTag();
69  }
70  }
71 
72  template<typename S> BusinessObjectRef(const BusinessObjectRef<S> &other)
73  {
74  m_tag = other.tag();
75  m_pointee = other.m_pointee ? dynamic_cast<T*>(other.m_pointee) : 0;
76  }
77 
79  {
80 
81  if ( this != &other )
82  {
83  m_pointee = other.m_pointee;
84  m_tag = other.m_tag;
85  }
86  return *this;
87  }
88 
90  {
91  m_pointee = 0;
92  m_tag = tag;
93  return *this;
94  }
95 
96  T* operator->() { return getPointee(); }
97 
98  tag_t tag() const { return m_tag; }
99  operator tag_t() const { return m_tag; }
100 
101  operator T*() { return getPointee(); }
102 
103  const T& operator*() const { return *getPointee(); }
104 
105  template <typename U> bool isInstanceOf() const
106  {
107  U* pt = 0;
109  try
110  {
111  pt = dynamic_cast<U*>(getPointee());
112  }
113  catch ( const IFail& )
114  {
115  }
116  return pt ? true : false;
117  }
118 
120  {
121  m_pointee = 0;
122  }
123 
124 private:
125 
126  T* getPointee() const
127  {
128  if ( m_tag && !m_pointee )
129  {
131  }
132  if ( !m_pointee )
133  {
134  std::string typeName;
135  std::string expectedTypeName = typeid(T).name();
136 
137  try
138  {
139  tag_t objectType = NULLTAG;
140  logical verdict = false;
141  size_t pos = expectedTypeName.find("::");
142 
143  if (pos != std::string::npos)
144  {
145  expectedTypeName = expectedTypeName.substr(pos + 2);
146  }
147 
148  if (POM_is_tag_valid(m_tag, &verdict) == POM_ok && verdict )
149  {
151  PomResultStatus pstatus = POM_class_of_instance(m_tag, &objectType);
152  Teamcenter::scoped_smptr<char> tempTypeName;
153  pstatus = POM_name_of_class(objectType, &tempTypeName);
154  typeName.assign(tempTypeName.getString());
155  }
156  else
157  {
158  typeName.assign("Runtime Class");
159  }
160  }
161  catch ( const IFail& )
162  {
163  typeName.assign("unknown");
164  }
165 
166  throw IFail( CXPOM_wrong_class, typeName.c_str(), expectedTypeName.c_str() );
167  }
168  return m_pointee;
169  }
170 
171  template<typename S> friend class BusinessObjectRef;
172  // the object tag
173  tag_t m_tag;
174 
175  // Pointer is mutable as it is essentially a cache
176  // of the Tag.object call
177  mutable T* m_pointee;
178 };
179 
180 
181 #endif