Created
August 2, 2019 17:41
-
-
Save shajeen/bfa406b2d97a5411fdc370bf481429c4 to your computer and use it in GitHub Desktop.
Poco library memory management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// memoryManagement.cpp : Defines the entry point for the console application. | |
// | |
#include <string> | |
#include <iostream> | |
#include <Poco\AutoPtr.h> | |
#include <Poco\RefCountedObject.h> | |
#include <Poco\AutoReleasePool.h> | |
#include <Poco\SharedPtr.h> | |
#include <Poco\DynamicFactory.h> | |
#include <Poco\Buffer.h> | |
#include <Poco\MemoryPool.h> | |
#include <Poco\SingletonHolder.h> | |
// Poco::AutoPtr | |
// Poco::AutoReleasePool | |
// Poco::SharedPtr | |
// Poco::DynamicFactory | |
// Poco::AbstractInstantiator | |
// Poco::Buffer | |
// Poco::MemoryPool | |
// Poco::SingletonHolder | |
// Poco::AutoPtr | |
class int_ : public Poco::RefCountedObject | |
{ | |
public: | |
int_(const int iVal) : st(iVal) | |
{} | |
int get_it() const noexcept | |
{ | |
return st; | |
} | |
protected: | |
~int_() {} | |
private: | |
int st = 0; | |
}; | |
void pocoAutoPtr() | |
{ | |
Poco::AutoPtr<int_> pInteger_1(new int_{ 100 }); | |
Poco::AutoPtr<int_> pInt2; | |
pInt2 = pInteger_1.cast<int_>(); | |
std::cout << "Poco AutoPtr: " << pInt2->get_it() << std::endl; | |
} | |
// Poco::AutoReleasePool | |
void pocoAutoReleasePool() | |
{ | |
Poco::AutoReleasePool<int_> autoReleasePoolObject; | |
auto print = [](const int iVal) { | |
std::cout << "pocoAutoReleasePool: " << iVal << std::endl; | |
}; | |
int_ *poolObj = new int_(10); | |
print(poolObj->get_it()); | |
autoReleasePoolObject.add(poolObj); | |
poolObj = new int_(100); | |
print(poolObj->get_it()); | |
autoReleasePoolObject.add(poolObj); | |
poolObj = new int_(1000); | |
print(poolObj->get_it()); | |
autoReleasePoolObject.add(poolObj); | |
} | |
// Poco::SharedPtr | |
void pocoSharedPtr() | |
{ | |
std::string *pString = new std::string("Poco library"); | |
Poco::SharedPtr<std::string> pSharedPtr1(pString); | |
std::cout << "Rc:" << pSharedPtr1.referenceCount() << std::endl; | |
Poco::SharedPtr<std::string> pSharedPtr2(pSharedPtr1.cast<std::string>()); | |
std::cout << "Rc:" << pSharedPtr1.referenceCount() << std::endl; | |
std::cout << "pocoSharedPtr: " << *pSharedPtr2 << std::endl; | |
} | |
// Poco::DynamicFactory | |
void pocoDynamicFactory() | |
{ | |
class Fruits | |
{ | |
public: | |
virtual std::string get_it() const noexcept = 0; | |
}; | |
class Apple : public Fruits | |
{ | |
public: | |
std::string get_it() const noexcept | |
{ | |
return "Apple"; | |
} | |
}; | |
class Opple : public Fruits | |
{ | |
public: | |
std::string get_it() const noexcept | |
{ | |
return "Opple"; | |
} | |
}; | |
class Jpple : public Fruits | |
{ | |
public : | |
Jpple(const std::string val) :jdk(val) {} | |
std::string get_it() const noexcept | |
{ | |
return jdk; | |
} | |
private: | |
std::string jdk = ""; | |
}; | |
class JppleInstantiator : public Poco::AbstractInstantiator<Fruits> | |
{ | |
public: | |
JppleInstantiator(const std::string val) :jdk(val) {} | |
Fruits* createInstance() const | |
{ | |
return new Jpple(jdk); | |
} | |
private: | |
std::string jdk = ""; | |
}; | |
Poco::DynamicFactory<Fruits> pDynamicFactoryObject; | |
pDynamicFactoryObject.registerClass<Apple>("apple"); | |
pDynamicFactoryObject.registerClass<Opple>("opple"); | |
pDynamicFactoryObject.registerClass("jpple", new JppleInstantiator("Hello World")); | |
Poco::SharedPtr<Fruits> salad = pDynamicFactoryObject.createInstance("apple"); | |
Poco::SharedPtr<Fruits> sadal = pDynamicFactoryObject.createInstance("opple"); | |
Poco::SharedPtr<Fruits> saald = pDynamicFactoryObject.createInstance("jpple"); | |
bool isOk = pDynamicFactoryObject.isClass("apple"); | |
std::cout << std::boolalpha << "class is there? " << isOk << std::endl; | |
std::cout << "pocoDynamicFactory: " << salad->get_it() << std::endl; | |
isOk = pDynamicFactoryObject.isClass("opple"); | |
std::cout << std::boolalpha << "class is there? " << isOk << std::endl; | |
std::cout << "pocoDynamicFactory: " << sadal->get_it() << std::endl; | |
isOk = pDynamicFactoryObject.isClass("jpple"); | |
std::cout << std::boolalpha << "class is there? " << isOk << std::endl; | |
std::cout << "pocoDynamicFactory: " << saald->get_it() << std::endl; | |
} | |
// Poco::Buffer | |
void pocoBuffer() | |
{ | |
Poco::Buffer<char> pocoBufferChar(4); | |
std::cout << "Enter something 4bytes: " << std::endl; | |
std::cin.read(pocoBufferChar.begin(), pocoBufferChar.size()); | |
std::string str(pocoBufferChar.begin(), pocoBufferChar.end()); | |
std::cout << "You have entered in buffer: " << str << std::endl; | |
} | |
// Poco::MemeoryPool | |
void pocoMemoryPool() | |
{ | |
Poco::MemoryPool pocoMemoryPool(1024, 4, 16); | |
std::cout << "poco stock before: " << pocoMemoryPool.available() << std::endl; | |
std::string *chBuffer = reinterpret_cast<std::string*>(pocoMemoryPool.get()); | |
std::cout << "poco stock after: " << pocoMemoryPool.available() << std::endl; | |
pocoMemoryPool.release(chBuffer); | |
} | |
// Poco::SingletonHolder | |
void pocoSingletonHolder() | |
{ | |
class MyPocoClass : public Poco::RefCountedObject | |
{ | |
public: | |
MyPocoClass() {} | |
~MyPocoClass() {} | |
static MyPocoClass* createInstance() | |
{ | |
Poco::SingletonHolder<MyPocoClass> singletonHolder; | |
return singletonHolder.get(); | |
} | |
void setIt(const int iVal) | |
{ | |
it = iVal; | |
} | |
int getIt() const noexcept | |
{ | |
return it; | |
} | |
private: | |
int it = 0; | |
}; | |
Poco::AutoPtr<MyPocoClass> pClassSetter(MyPocoClass::createInstance()); | |
pClassSetter->setIt(1); | |
Poco::AutoPtr<MyPocoClass> pClassGetter(MyPocoClass::createInstance()); | |
std::cout << "poco singetonholder: " << pClassGetter->getIt() << std::endl; | |
} | |
int main() | |
{ | |
pocoAutoPtr(); // Poco::AutoPtr | |
pocoAutoReleasePool(); // Poco::AutoReleasePool | |
pocoSharedPtr(); // Poco::SharedPtr | |
pocoDynamicFactory(); // Poco::DynamicFactory | |
pocoBuffer(); // Poco::Buffer | |
pocoMemoryPool(); // Poco::MemoryPool | |
pocoSingletonHolder(); // Poco::SingletonHolder | |
auto i = 0; | |
std::cin >> i; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment