2 Replies Latest reply: Apr 9, 2013 2:34 AM by JoeHolder RSS

    Using existing C++  class with managed template if class in namespace

    JoeHolder
      Hi,

      does anyone know make an existing C++ class that is declared within nested namespaces into a Coherence managed template class?

      I have a class and declared within reuters::transactions::tds::cache

      called CacheD3ContextTerminalData.

      I declare the <<, == and hash functions for it.

      I've tried variants on either calling 'using namespace reuters::transactions::tds::cache;' or passing in the fully qualified class name to these functions.

      If I use the 'using namespace' the binary operator is not found in Management.hpp.

      If I try passing in the fully qualified class name everywhere I get the same problem - an addtionally if I try and pass it into COH_REGISTER_MANAGED_CLASS the macro seems to truncate the classname at 'reuters'

      If I declare CacheD3ContextTerminalData outside of any namespaces it is ok.


      But isn't the idea to support existing classes?

      Many thanks if anyone knows how to do this.

      Joe
        • 1. Re: Using existing C++  class with managed template if class in namespace
          Mfalco-Oracle
          Hi Joe,

          Yes, Managed is meant to work with namespaced classes, please try the following:

          - declare the three free functions in the reuters::transactions::tds::cache namespace
          - issue the COH_REGISTER_MANAGED_CLASS with the short class name, thus you either do it from within the reuters::transactions::tds::cache namespace or after a corresponding "using namespace"

          In your application code when using the Managed wrapper you should be able to either use Managed<CacheD3ContextTerminalData> directly if you've also done a "using namespace reuters::transactions::tds::cache" from within that file, or the fully qualified Managed<reuters::transactions::tds::cache::CacheD3ContextTerminalData> if you have not.

          If this does not work for you, please reply with the Coherence version, and compiler and version you are using, and if possible a small reproducer.

          thanks,

          Mark
          Oracle Coherence
          • 2. Re: Using existing C++  class with managed template if class in namespace
            JoeHolder
            Hi Mark,

            Thanks very much for the tips.

            I found it worked for me if I did the following :

            *1) declare the free functions in the namespace*

            namespace reuters
            {
                 namespace transactions
                 {
                      namespace tds
                      {
                           namespace cache
                           {
                                class CacheD3ContextFactory;

                                class CacheD3Context : public reuters::transactions::tds::ID3Context
                                {
                                     // -class goes here

                                };
                                // ----- free functions -----------------------------------------------------

                                /**
                                * Output this CacheD3Context to the stream
                                */
                                std::ostream& operator<<(std::ostream& out, const CacheD3Context& info);

                                /**
                                * Perform an equality test on two CacheD3Context objects
                                * @return true if the objects are equal
                                */
                                bool operator==(const CacheD3Context& cdA, const CacheD3Context& cdB);

                                /**
                                * Return the hash for the CacheD3Context.
                                */
                                size_t hash_value(const CacheD3Context& cd);
                           }
                      }
                 }
            }


            *2) In the corresponding .CPP file define the body of the free functions in the namespace*


            namespace reuters
            {
                 namespace transactions
                 {
                      namespace tds
                      {
                           namespace cache
                           {
                                std::ostream& operator<<(std::ostream& out, const CacheD3Context& cc)
                                {
                                     out << "CacheD3Context("
                                               << "remoteTerminalData="          << cc.getRemoteTerminalData()
            ...etc....
                                               << ", type="                         << cc.getType()
                                               << ')';
                                     return out;
                                }

                                bool operator==(const CacheD3Context& ccA, const CacheD3Context& ccB)
                                {
                                     return     ccA.getRemoteTerminalData()          == ccA.getRemoteTerminalData()          &&
            ...etc....
                                               ccA.getType()                         == ccB.getType();
                                }

                                size_t hash_value(const CacheD3Context& cc)
                                {
                                     return size_t(&cc); // identity hash (note: not suitable for cache keys)
                                }
                           }
                      }
                 }
            }


            *3) In the serialize CPP files use the namespace and declare with the short name*

            using namespace reuters::transactions::tds::cache;

            COH_REGISTER_MANAGED_CLASS(1012, CacheD3Context);



            Thanks again

            Joe