Oracle® Fusion Middleware C++ API Reference for Oracle Coherence
12c (12.2.1.4.0)

E90870-01

Managed Class Template Reference

#include <coherence/lang/Managed.hpp>

Inherits Object.

List of all members.


Detailed Description

template<class T>
class coherence::lang::Managed< T >

Managed is an adaptor class which transforms a pre-existing class into a Coherence managed 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:

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:

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;

See also:
coherence::io::pof::PofReader

coherence::io::pof::PofWriter

coherence::io::pof::SystemPofContext

Author:
mf 2007.07.05

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:

  • It is reflexive: for any non-null handle h, h->equals(h) must return true.
  • It is symmetric: for any non-null handles h1 and h2, h1->equals(h2) should return true if and only if h2->equals(h1) returns true.
  • It is transitive: for any non-null handles h1, h2, and h3, if h1->equals(h2) returns true and h2->equals(h3) returns true, then h1->equals(h3) should return true.
  • It is consistent: for any non-null handles 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.
  • If the supplied handle is NULL then false must be returned.

The default implementation is a reference equality comparison.

Parameters:
v the Object::View to compare against, may be NULL
Returns:
true iff the given handle references an Object that is "equal" to this Object
See also:
equals(Object::View v1, Object::View v2)

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);
     }

Returns:
a string representation of this object

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 hashCode is:

  • Whenever it is invoked on the same Object more than once during an execution of an application, the 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.
  • If two Objects are equal according to the equals method, then calling the hashCode method on each of the two Objects must produce the same value.
  • It is not required that if two Objects are unequal according to the 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.

Returns:
a hash code value for this Object


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.

Constructor & Destructor Documentation

Managed (  )  [inline, protected]

Create a new Managed<T> instance with the default initial T value.

Returns:
the new Managed<T>

Managed ( const T &  t  )  [inline, protected]

Create a new Managed<T> instance.

Parameters:
t the initial value for the templated type


Member Function Documentation

virtual T& getManagedObject (  )  [inline, protected, virtual]

Return the reference to the managed T object.

Returns:
the managed object

virtual const T& getManagedObject (  )  const [inline, protected, virtual]

Return the constant reference to managed T object.

Returns:
the managed 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:

The default implementation is a reference equality comparison.

Parameters:
v the Object::View to compare against, may be NULL
Returns:
true iff the given handle references an Object that is "equal" to this Object
See also:
equals(Object::View v1, Object::View v2)

Parameters:
v the object to compare against
This method delegates to the custom types equality operator.

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);
     }

Returns:
a string representation of this object

Parameters:
out the stream to write to
This method delegates to the custom types stream operator. (operator<<)

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:

The default implementation is identity based.

Returns:
a hash code value for this Object

This method delegates to the global hash function specialized for the custom type.

Reimplemented from Object.


The documentation for this class was generated from the following file:
Copyright © 2000, 2019, Oracle and/or its affiliates. All rights reserved.