#include <coherence/lang/Managed.hpp>
Inherits Object.
The resulting object will be usable both as the supplied data type and as a Coherence managed object. As a managed object it is suitable for storage in Coherence caches.
The managed object must be created using its associated static create methods, which support either default construction, or copy construction from the custom type. The managed object's life-cycle is dictated by reference counting, and it may not be manually deleted.
To be compatible with the Managed template the following set of functions must be defined for the supplied type:
CustomType::CustomType()
CustomType::CustomType(const CustomType&)
bool operator==(const CustomType&, const CustomType&)
template<typename Char, typename Traits> std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out, const CustomType&)
size_t hash_value(const CustomType&)
A example of a conforming class would be:
class Address { public: Address(const std::string& sCity, const std::String& sState, int nZip) : m_sCity(sCity), m_sState(sState), m_nZip(nZip) {} Address(const Address& that) : m_sCity(that.m_sCity), m_sState(that.m_sState), m_nZip(that.m_nZip) {} protected: Address() : m_nZip(0) {} public: std::string getCity() const {return m_sCity;} std::string getState() const {return m_sState;} int getZip() const {return m_nZip;} private: const std::string m_sCity; const std::string m_sState; const int m_nZip; }; bool operator==(const Address& addra, const Address& addrb) { return addra.getZip() == addrb.getZip() && addra.getState() == addrb.getState() && addra.getCity() == addrb.getCity(); } template<typename Char, typename Traits, class T> std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out, const Address& addr) { out << addr.getCity() << ", " << addr.getState() << " " << addr.getZip(); return out; } size_t hash_value(const Address& addr) { return (size_t) addr.getZip(); }
Serialization support may be added by specializing the following free-functions:
void serialize<CustomType>(coherence::io::pof::PofWriter::Handle, const CustomType&)
CustomType deserialize<CustomType>(coherence::io::pof::PofReader::Handle)
The serialization functions do not need to be defined within the source file of the original data type. They only need to be linked into the application, and registered with the SystemPofContext via the COH_REGISTER_MANAGED_CLASS macro.
#include "coherence/io/pof/SystemPofContext.hpp" #include "Address.hpp" using namespace coherence::io::pof; COH_REGISTER_MANAGED_CLASS(1234, Address); template<> void serialize<Address>(PofWriter::Handle hOut, const Address& addr) { hOut->writeString(0, addr.getCity()); hOut->writeString(1, addr.getState()); hOut->writeInt32 (2, addr.getZip()); } template<> Address deserialize<Address>(PofReader::Handle hIn) { std::string sCity = hIn->readString(0); std::string sState = hIn->readString(1); int nZip = hIn->readInt32 (2); return Address(sCity, sState, nZip); }
An example usage of the resulting managed type would be:
// construct the non-managed version as usual Address office("Redwood Shores", "CA", 94065); // the managed version can be initialized from the non-managed version // the result is a new object, which does not reference the original Managed<Address>::View vOffice = Managed<Address>::create(office); String::View vKey = "Oracle"; // the managed version is suitable for use with caches hCache->put(vKey, vAddr); vOffice = cast<Managed<Address>::View>(hCache->get(vKey)); // the non-managed class's public methods/fields remain accessible assert(vOffice->getCity() == office.getCity()); assert(vOffice->getState() == office.getState()); assert(vOffice->getZip() == office.getZip()); // conversion back to the non-managed type may be performed using the // non-managed class's copy constructor. Address officeOut = *vOffice;
coherence::io::pof::PofWriter
coherence::io::pof::SystemPofContext
Public Types | ||||
typedef spec::Handle | Handle | |||
Managed<T> Handle definition. | ||||
typedef spec::View | View | |||
Managed<T> View definition. | ||||
typedef spec::Holder | Holder | |||
Managed<T> Holder definition. | ||||
typedef T | BoxedType | |||
The boxed class type. | ||||
Public Member Functions | ||||
virtual bool | equals (Object::View v) const | |||
Return true iff the specified Object is "equal" to this Object. This method implements an equivalence relation on Objects:
The default implementation is a reference equality comparison.
| ||||
virtual TypedHandle < const String > | toString () const | |||
Output a human-readable description of this Object to the given stream. Note that when overriding this method the return type must be TypedHandle<const String> rather then String::View. These two types are assignment compatible but not equivalent and declaring the override with String::View will not be a compatible override. coherence::lang::operator<<(std::ostream, Object::View) is defined and will call into the toString method, to output Objects. If a managed String object is desired, the COH_TO_STRING macro can be used to build up a String from streamable contents and is generally how toString() will be implemented. Object::View vKey = ... Object::View vValue = ... std::cout << vKey << " = " << vValue << std::endl; String::View vs = COH_TO_STRING(vKey << " = " << vValue); The COH_TO_STRING macro is also the most common way to implement the toString method. For example: virtual TypedHandle<const String> Person::toString() const { return COH_TO_STRING("Name: " << f_sName << " SSN: " << f_nSSN); }
| ||||
virtual size32_t | hashCode () const | |||
Return a hash code value for the Object. This method is supported for the benefit of hash-based containers. The general contract of
The default implementation is identity based.
| ||||
Protected Member Functions | ||||
Managed () | ||||
Create a new Managed<T> instance with the default initial T value. | ||||
Managed (const T &t) | ||||
Create a new Managed<T> instance. | ||||
Managed (const Managed< T > &that) | ||||
Copy constructor. | ||||
virtual T & | getManagedObject () | |||
Return the reference to the managed T object. | ||||
virtual const T & | getManagedObject () const | |||
Return the constant reference to managed T object. |
Managed | ( | ) | [inline, protected] |
Create a new Managed<T> instance with the default initial T value.
Managed | ( | const T & | t | ) | [inline, protected] |
Create a new Managed<T> instance.
t | the initial value for the templated type |
virtual T& getManagedObject | ( | ) | [inline, protected, virtual] |
Return the reference to the managed T object.
virtual const T& getManagedObject | ( | ) | const [inline, protected, virtual] |
Return the constant reference to managed T object.
virtual bool equals | ( | Object::View | v | ) | const [inline, virtual] |
Return true iff the specified Object is "equal" to this Object.
This method implements an equivalence relation on Objects:
h
, h->equals(h)
must return true
. h1
and h2
, h1->equals(h2)
should return true
if and only if h2->equals(h1)
returns true
. h1
, h2
, and h3
, if h1->equals(h2)
returns true
and h2->equals(h3)
returns true
, then h1->equals(h3)
should return true
. h1
and h2
, multiple invocations of h1->equals(h2)
consistently return true
or consistently return false
, provided no information used in comparisons on the objects is modified. NULL
then false
must be returned. The default implementation is a reference equality comparison.
v | the Object::View to compare against, may be NULL |
v | the object to compare against |
Reimplemented from Object.
virtual TypedHandle<const String> toString | ( | ) | const [inline, virtual] |
Output a human-readable description of this Object to the given stream.
Note that when overriding this method the return type must be TypedHandle<const String> rather then String::View. These two types are assignment compatible but not equivalent and declaring the override with String::View will not be a compatible override.
coherence::lang::operator<<(std::ostream, Object::View) is defined and will call into the toString method, to output Objects. If a managed String object is desired, the COH_TO_STRING macro can be used to build up a String from streamable contents and is generally how toString() will be implemented.
Object::View vKey = ... Object::View vValue = ... std::cout << vKey << " = " << vValue << std::endl; String::View vs = COH_TO_STRING(vKey << " = " << vValue);
The COH_TO_STRING macro is also the most common way to implement the toString method. For example:
virtual TypedHandle<const String> Person::toString() const { return COH_TO_STRING("Name: " << f_sName << " SSN: " << f_nSSN); }
out | the stream to write to |
Reimplemented from Object.
virtual size32_t hashCode | ( | ) | const [inline, virtual] |
Return a hash code value for the Object.
This method is supported for the benefit of hash-based containers.
The general contract of hashCode
is:
hashCode
method must consistently return the same value, provided no information used in equals
comparisons on the object is modified. This value need not remain consistent from one execution of an application to another execution of the same application. equals
method, then calling the hashCode
method on each of the two Objects must produce the same value. equals
method, then calling the hashCode
method on each of the two objects must produce distinct results. However, the programmer should be aware that producing distinct results for unequal objects may improve the performance of hash-based containers. The default implementation is identity based.
This method delegates to the global hash function specialized for the custom type.
Reimplemented from Object.