template<typename T_Type, typename T_Holding, typename T_Access> class DReference { . . . IDSharedMemoryHolder *_holder; void retain(IDSharedMemoryHolder *inHolder) { . . . } void release() { . . . } . . . ~DReference() { release(); } template<typename T_OtherType, typename T_OtherHolding, typename T_OtherAccess> DReference( const DReference<T_OtherType, T_OtherHolding, T_OtherAccess> & inLReference) : _holder(NULL), _holding(), _access() { retain(inLReference._holder); } . . . }
DStrongReference<DPlugIn> DPlugInManager::createPlugIn( const DPlugInDescriptor &inDescriptor) { . . . DStrongReference<DPlugIn> thePlugInReference = internalCreatePlugIn(inDescriptor); . . . return thePlugInReference; } ... DStrongReference<DPlugIn> DPlugInManager::internalCreatePlugIn( const DPlugInDescriptor &inDescriptor) { for (IDPlugInStorage *thePlugInStorage : _storages) { if (thePlugInStorage->getPlugInStatus(inDescriptor)) return thePlugInStorage->createPlugIn(inDescriptor); } return DStrongReference<DPlugIn>(); } ... class DPlugInStorageImpl : public IDPlugInStorage { public: virtual ~DPlugInStorageImpl() { } virtual DStrongReference<DPlugIn> createPlugIn( const DPlugInDescriptor &inDescriptor); };
#include <iostream> #include <stdio.h> class TestClass { private: int _state; public: TestClass(int inState) : _state(inState) { std::cout << "State based: " << inState << std::endl; } TestClass() : _state(1) { std::cout << "Default" << std::endl; } TestClass(const TestClass &inObject0) : _state(2) { std::cout << "Const Copy" << std::endl; } TestClass(TestClass &inObject0) : _state(3) { std::cout << "Copy" << std::endl; } TestClass(const TestClass &&inObject0) : _state(4) { std::cout << "Const Move" << std::endl; } TestClass(TestClass &&inObject0) : _state(5) { std::cout << "Move" << std::endl; } ~TestClass() { std::cout << "Destroy" << std::endl; } void call() { std::cout << "Call " << _state << std::endl; } }; /////////////////////////////////////////////////////////////////////////////// int main() { TestClass theTestObject = TestClass(); theTestObject.call(); fflush(stdout); return 0; }
#include <iostream> #include <stdio.h> class TestClass { private: int _state; public: TestClass(int inState) : _state(inState) { std::cout << "State based: " << inState << std::endl; } TestClass() : _state(1) { std::cout << "Default" << std::endl; } explicit TestClass(const TestClass &inObject0) : _state(2) { std::cout << "Const Copy" << std::endl; } } ~TestClass() { std::cout << "Destroy" << std::endl; } void call() { std::cout << "Call " << _state << std::endl; } }; /////////////////////////////////////////////////////////////////////////////// int main() { TestClass theTestObject = TestClass(); theTestObject.call(); fflush(stdout); return 0; }
template<typename T_Type, typename T_Holding, typename T_Access> class DReference { . . . IDSharedMemoryHolder *_holder; void retain(IDSharedMemoryHolder *inHolder) { . . . } void release() { . . . } . . . ~DReference() { release(); } //NB: Workaround for Copy elision explicit DReference( const OwnType &inLReference) : _holder(NULL), _holding(), _access() { // Call for some magic cases retain(inLReference._holder); } template<typename T_OtherType, typename T_OtherHolding, typename T_OtherAccess> DReference( const DReference<T_OtherType, T_OtherHolding, T_OtherAccess> & inLReference) : _holder(NULL), _holding(), _access() { retain(inLReference._holder); } . . . }
#include <iostream> #include <stdio.h> class TestClass { private: int _state; public: TestClass(int inState) : _state(inState) { std::cout << "State based: " << inState << std::endl; } TestClass() : _state(1) { std::cout << "Default" << std::endl; } explicit TestClass(const TestClass &inObject0) : _state(2) { std::cout << "Const Copy" << std::endl; } template<typename T> TestClass(const T &inObject0) : _state(13) { std::cout << "Template Copy" << std::endl; } ~TestClass() { std::cout << "Destroy" << std::endl; } void call() { std::cout << "Call " << _state << std::endl; } }; /////////////////////////////////////////////////////////////////////////////// int main() { TestClass theTestObject = TestClass(); theTestObject.call(); fflush(stdout); return 0; }
/////////////////////////////////////////////////////////////////////////////// #include <iostream> /////////////////////////////////////////////////////////////////////////////// template<typename T_Type> class TestTemplateClass { private: typedef TestTemplateClass<T_Type> OwnType; T_Type _state; public: TestTemplateClass() : _state() { std::cout << "Default constructor" << std::endl; } TestTemplateClass(int inState) : _state(inState) { std::cout << "State constructor" << std::endl; } TestTemplateClass(const OwnType &inValue) { std::cout << "Copy constructor" << std::endl; } template<typename T_OtherType> TestTemplateClass(const TestTemplateClass<T_OtherType> &inValue) { std::cout << "Template-copy constructor" << std::endl; } template<typename T_OtherType> void operator = (const TestTemplateClass<T_OtherType> &inValue) { std::cout << "Operator" << std::endl; } ~TestTemplateClass() { std::cout << "Destructor" << std::endl; } }; /////////////////////////////////////////////////////////////////////////////// TestTemplateClass<int> createFunction() { return TestTemplateClass<int>(); } /////////////////////////////////////////////////////////////////////////////// int main() { TestTemplateClass<int> theReference = createFunction(); std::cout << "Finished" << std::endl; return 0; } ///////////////////////////////////////////////////////////////////////////////
Default constructor Copy constructor Destructor Copy constructor Destructor Finished Destructor
template<typename T_Type, typename T_Holding, typename T_Access> class DReference { . . .
DReference<MyType, DReferenceStrongHolding<MyType>, DReferenceCachingAccess< MyType > > theReference;
template<typename T_Type> class DStrongReference : public DReference< T_Type, DReferenceStrongHolding<MyType>, DReferenceCachingAccess< MyType > > { . . .
/////////////////////////////////////////////////////////////////////////////// #include <iostream> /////////////////////////////////////////////////////////////////////////////// template<typename T_Type, typename T_Strategy> class TestTemplateClass { private: typedef TestTemplateClass<T_Type, T_Strategy> OwnType; T_Type _state; T_Strategy _strategy; public: TestTemplateClass() : _state(), _strategy() { std::cout << "Default constructor: " << this << std::endl; } TestTemplateClass(int inState) : _state(inState), _strategy() { std::cout << "State constructor: " << this << std::endl; } TestTemplateClass(const OwnType &inValue) : _state(), _strategy() { std::cout << "Copy constructor: " << this << " from " << &inValue << std::endl; } template<typename T_OtherType, typename T_OtherStrategy> TestTemplateClass( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) : _state(), _strategy() { std::cout << "Template-copy constructor: " << this << std::endl; } void operator = (const OwnType &inValue) { std::cout << "Assigning: " << this << " from " << inValue << std::endl; } template<typename T_OtherType, typename T_OtherStrategy> void operator = ( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) { std::cout << "Assigning: " << this << " from " << &inValue << std::endl; } ~TestTemplateClass() { std::cout << "Destructor: " << this << std::endl; } }; /////////////////////////////////////////////////////////////////////////////// template<typename T_Type> class TestTemplateClassIntStrategy : public TestTemplateClass<T_Type, int> { private: //- Types typedef TestTemplateClassIntStrategy<T_Type> OwnType; typedef TestTemplateClass<T_Type, int> ParentType; public: TestTemplateClassIntStrategy() : ParentType() { } TestTemplateClassIntStrategy(int inState) : ParentType(inState) { } TestTemplateClassIntStrategy(const OwnType &inValue) : ParentType(inValue) { } template<typename T_OtherType, typename T_OtherStrategy> TestTemplateClassIntStrategy( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) : ParentType(inValue) { } //- Operators void operator = (const OwnType &inValue) { ParentType::operator =(inValue); } template<typename T_OtherType, typename T_OtherStrategy> void operator = ( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) { ParentType::operator =(inValue); } }; /////////////////////////////////////////////////////////////////////////////// TestTemplateClassIntStrategy<int> createFunction() { return TestTemplateClassIntStrategy<int>(); } int main() { TestTemplateClassIntStrategy<int> theReference = createFunction(); std::cout << "Finished" << std::endl; return 0; }
Default constructor: 0x28fed8 Copy constructor: 0x28ff08 from 0x28fed8 Destructor: 0x28fed8 Copy constructor: 0x28ff00 from 0x28ff08 Destructor: 0x28ff08 Finished Destructor: 0x28ff00
. . . int main() { TestTemplateClassIntStrategy<int> theReference; theReference = createFunction(); std::cout << "Finished" << std::endl; return 0; }
Default constructor: 0x28ff00 Default constructor: 0x28fed8 Copy constructor: 0x28ff08 from 0x28fed8 Destructor: 0x28fed8 Assigning: 0x28ff00 from 0x28ff08 Destructor: 0x28ff08 Finished Destructor: 0x28ff00
DStrongReference<Type> theStrongReference; // . : // // template<typename Type, typename Owning, typename Holding> // DWeakReference::DWeakReference(const DReference<Type, Owning, Holding> &ref) : Parent(ref) { } // // , DStrongReference DReference. // DWeakReference<Type> theWeakReference = theStrongReference;
//============================================================================ // Name : demiurg_application_example.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ /////////////////////////////////////////////////////////////////////////////// #include <iostream> /////////////////////////////////////////////////////////////////////////////// template<typename T_Type, typename T_Strategy> class TestTemplateClass { private: typedef TestTemplateClass<T_Type, T_Strategy> OwnType; T_Type _state; T_Strategy _strategy; public: TestTemplateClass() : _state(), _strategy() { std::cout << "Default constructor: " << this << std::endl; } TestTemplateClass(int inState) : _state(inState), _strategy() { std::cout << "State constructor: " << this << std::endl; } TestTemplateClass(const OwnType &inValue) : _state(), _strategy() { std::cout << "Copy constructor: " << this << " from " << &inValue << std::endl; } template<typename T_OtherType, typename T_OtherStrategy> TestTemplateClass( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) : _state(), _strategy() { std::cout << "Template-copy constructor: " << this << std::endl; } void operator = (const OwnType &inValue) { std::cout << "Assigning: " << this << " from " << &inValue << std::endl; } template<typename T_OtherType, typename T_OtherStrategy> void operator = ( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) { std::cout << "Assigning: " << this << " from " << &inValue << std::endl; } ~TestTemplateClass() { std::cout << "Destructor: " << this << std::endl; } }; /////////////////////////////////////////////////////////////////////////////// //- Integer strategy template<typename T_Type> class TestTemplateClassIntStrategy : public TestTemplateClass<T_Type, int> { private: //- Types typedef TestTemplateClassIntStrategy<T_Type> OwnType; typedef TestTemplateClass<T_Type, int> ParentType; public: TestTemplateClassIntStrategy() : ParentType() { } TestTemplateClassIntStrategy(int inState) : ParentType(inState) { } TestTemplateClassIntStrategy(const OwnType &inValue) : ParentType(inValue) { } template<typename T_OtherType, typename T_OtherStrategy> TestTemplateClassIntStrategy( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) : ParentType(inValue) { } //- Operators void operator = (const OwnType &inValue) { ParentType::operator =(inValue); } template<typename T_OtherType, typename T_OtherStrategy> void operator = ( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) { ParentType::operator =(inValue); } }; //- Boolean strategy template<typename T_Type> class TestTemplateClassBoolStrategy : public TestTemplateClass<T_Type, bool> { private: //- Types typedef TestTemplateClassBoolStrategy<T_Type> OwnType; typedef TestTemplateClass<T_Type, bool> ParentType; public: TestTemplateClassBoolStrategy() : ParentType() { } TestTemplateClassBoolStrategy(int inState) : ParentType(inState) { } TestTemplateClassBoolStrategy(const OwnType &inValue) : ParentType(inValue) { } template<typename T_OtherType, typename T_OtherStrategy> TestTemplateClassBoolStrategy( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) : ParentType(inValue) { } //- Operators void operator = (const OwnType &inValue) { ParentType::operator =(inValue); } template<typename T_OtherType, typename T_OtherStrategy> void operator = ( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) { ParentType::operator =(inValue); } }; /////////////////////////////////////////////////////////////////////////////// TestTemplateClassBoolStrategy<int> createFunction() { return TestTemplateClassBoolStrategy<int>(); } int main() { TestTemplateClassIntStrategy<int> theReference; theReference = createFunction(); std::cout << "Finished" << std::endl; return 0; }
Default constructor: 0x28fed8 Copy constructor: 0x28ff08 from 0x28fed8 Destructor: 0x28fed8 Copy constructor: 0x28ff00 from 0x28ff08 Destructor: 0x28ff08 Finished Destructor: 0x28ff00
Source: https://habr.com/ru/post/252885/
All Articles