From 56cf23dc21b5787a4b567afe5b83c3e5504071e6 Mon Sep 17 00:00:00 2001 From: Ted Ross Date: Mon, 19 Jul 2010 19:51:47 +0000 Subject: QPID-2589 - Patch from Chuck Rolke This patch cleans up or adds the copy constructors and the copy assignment operators for the binding classes. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@965603 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/bindings/qpid/dotnet/src/Address.cpp | 10 +++- cpp/bindings/qpid/dotnet/src/Address.h | 21 +++++++- cpp/bindings/qpid/dotnet/src/Connection.cpp | 8 +++ cpp/bindings/qpid/dotnet/src/Connection.h | 34 ++++++++++++ cpp/bindings/qpid/dotnet/src/Duration.h | 6 +-- cpp/bindings/qpid/dotnet/src/Message.cpp | 6 +-- cpp/bindings/qpid/dotnet/src/Message.h | 21 +++++++- cpp/bindings/qpid/dotnet/src/Receiver.cpp | 7 +-- cpp/bindings/qpid/dotnet/src/Receiver.h | 22 +++++++- cpp/bindings/qpid/dotnet/src/Sender.cpp | 8 +-- cpp/bindings/qpid/dotnet/src/Sender.h | 28 +++++++++- cpp/bindings/qpid/dotnet/src/Session.cpp | 9 ++-- cpp/bindings/qpid/dotnet/src/Session.h | 26 ++++++++- .../dotnet/test/messaging.test/messaging.test.cs | 62 ++++++++++++++++++++++ 14 files changed, 245 insertions(+), 23 deletions(-) (limited to 'cpp/bindings/qpid/dotnet') diff --git a/cpp/bindings/qpid/dotnet/src/Address.cpp b/cpp/bindings/qpid/dotnet/src/Address.cpp index 2da40e6416..2b6dbb4647 100644 --- a/cpp/bindings/qpid/dotnet/src/Address.cpp +++ b/cpp/bindings/qpid/dotnet/src/Address.cpp @@ -64,7 +64,7 @@ namespace Messaging { Type = ""; } - + // Create with options and type Address::Address(System::String ^ name, System::String ^ subject, System::Collections::Generic::Dictionary< @@ -78,8 +78,15 @@ namespace Messaging { Type = type; } + // Copy constructor + Address::Address(const Address ^ address) + : addressp(new ::qpid::messaging::Address( + *(const_cast
(address)->NativeAddress))) + { + } // Create from received address + // The new Address object consumes the unmanaged pointer Address::Address(::qpid::messaging::Address * addrp) : addressp(addrp) { @@ -110,7 +117,6 @@ namespace Messaging { } } - // // ToString // diff --git a/cpp/bindings/qpid/dotnet/src/Address.h b/cpp/bindings/qpid/dotnet/src/Address.h index 9f940d67ea..1f2c3fe161 100644 --- a/cpp/bindings/qpid/dotnet/src/Address.h +++ b/cpp/bindings/qpid/dotnet/src/Address.h @@ -65,12 +65,31 @@ namespace Messaging { System::String ^, System::Object ^> ^ options, System::String ^ type); + // copy constructor + Address(const Address ^ address); + // Create from received address + // The new Address object consumes the unmanaged pointer Address(::qpid::messaging::Address * addrp); ~Address(); !Address(); -// Address(const Address % rhs); + + // assignment operator + Address % operator=(const Address % rhs) + { + if (this == %rhs) + { + // Self assignment, do nothing + } + else + { + delete addressp; + addressp = new ::qpid::messaging::Address( + *(const_cast
(rhs).NativeAddress) ); + } + return *this; + } property ::qpid::messaging::Address * NativeAddress { diff --git a/cpp/bindings/qpid/dotnet/src/Connection.cpp b/cpp/bindings/qpid/dotnet/src/Connection.cpp index 0e59c4196e..50755302f3 100644 --- a/cpp/bindings/qpid/dotnet/src/Connection.cpp +++ b/cpp/bindings/qpid/dotnet/src/Connection.cpp @@ -68,6 +68,14 @@ namespace Messaging { } + // Copy constructor + Connection::Connection(const Connection ^ connection) + : connectionp(new ::qpid::messaging::Connection( + *(const_cast(connection)->NativeConnection))) + { + } + + // Destructor Connection::~Connection() { diff --git a/cpp/bindings/qpid/dotnet/src/Connection.h b/cpp/bindings/qpid/dotnet/src/Connection.h index 8e0f40f803..6a0caf1a16 100644 --- a/cpp/bindings/qpid/dotnet/src/Connection.h +++ b/cpp/bindings/qpid/dotnet/src/Connection.h @@ -56,9 +56,43 @@ namespace Messaging { System::String ^, System::Object ^> ^ options); Connection(System::String ^ url, System::String ^ options); + + // copy constructor + Connection(const Connection ^ connection); + ~Connection(); !Connection(); + // assignment operator + Connection % operator=(const Connection % rhs) + { + if (this == %rhs) + { + // Self assignment, do nothing + } + else + { + delete connectionp; + connectionp = new ::qpid::messaging::Connection( + *(const_cast(rhs).NativeConnection) ); + } + return *this; + } + + property ::qpid::messaging::Connection * NativeConnection + { + ::qpid::messaging::Connection * get () { return connectionp; } + } + + property System::String ^ NPAddress + { + System::String ^ get () + { + System::IntPtr i((void *)connectionp); + return gcnew System::String(i.ToString()); + } + } + void SetOption(System::String ^ name, System::Object ^ value); void Open(); diff --git a/cpp/bindings/qpid/dotnet/src/Duration.h b/cpp/bindings/qpid/dotnet/src/Duration.h index 8bbfa561f0..213c338a59 100644 --- a/cpp/bindings/qpid/dotnet/src/Duration.h +++ b/cpp/bindings/qpid/dotnet/src/Duration.h @@ -45,13 +45,13 @@ namespace Messaging { Duration(const Duration % rhs) : milliseconds(rhs.milliseconds) { - }; + } explicit Duration(System::UInt64 mS) : - milliseconds(mS) {}; + milliseconds(mS) {} Duration() : - milliseconds(System::UInt64::MaxValue) {}; + milliseconds(System::UInt64::MaxValue) {} property System::UInt64 Milliseconds { diff --git a/cpp/bindings/qpid/dotnet/src/Message.cpp b/cpp/bindings/qpid/dotnet/src/Message.cpp index f620a099ca..def3051d75 100644 --- a/cpp/bindings/qpid/dotnet/src/Message.cpp +++ b/cpp/bindings/qpid/dotnet/src/Message.cpp @@ -141,10 +141,10 @@ namespace Messaging { } // Copy constructor - // TODO: prevent copy - Message::Message(const Message % rhs) + Message::Message(const Message ^ message) + : messagep(new ::qpid::messaging::Message( + *(const_cast(message)->NativeMessage))) { - messagep = rhs.messagep; } // Destroys kept object diff --git a/cpp/bindings/qpid/dotnet/src/Message.h b/cpp/bindings/qpid/dotnet/src/Message.h index d5b4beb406..c7f6092a40 100644 --- a/cpp/bindings/qpid/dotnet/src/Message.h +++ b/cpp/bindings/qpid/dotnet/src/Message.h @@ -76,8 +76,27 @@ namespace Messaging { !Message(); // Copy constructor - Message(const Message % rhs); + Message(const Message ^ message); + // assignment operator + Message % operator=(const Message % rhs) + { + if (this == %rhs) + { + // Self assignment, do nothing + } + else + { + delete messagep; + messagep = new ::qpid::messaging::Message( + *(const_cast(rhs).NativeMessage) ); + } + return *this; + } + + // + // NativeMessage + // property ::qpid::messaging::Message * NativeMessage { ::qpid::messaging::Message * get () { return messagep; } diff --git a/cpp/bindings/qpid/dotnet/src/Receiver.cpp b/cpp/bindings/qpid/dotnet/src/Receiver.cpp index 96df8cc53d..1e2f30d996 100644 --- a/cpp/bindings/qpid/dotnet/src/Receiver.cpp +++ b/cpp/bindings/qpid/dotnet/src/Receiver.cpp @@ -66,10 +66,11 @@ namespace Messaging { // Copy constructor - Receiver::Receiver(const Receiver ^ rhs) + Receiver::Receiver(const Receiver ^ receiver) + : receiverp(new ::qpid::messaging::Receiver( + *(const_cast(receiver)->NativeReceiver))), + parentSession(receiver->parentSession) { - receiverp = rhs->receiverp; - parentSession = rhs->parentSession; } diff --git a/cpp/bindings/qpid/dotnet/src/Receiver.h b/cpp/bindings/qpid/dotnet/src/Receiver.h index 436f3f2668..68cfa6fec7 100644 --- a/cpp/bindings/qpid/dotnet/src/Receiver.h +++ b/cpp/bindings/qpid/dotnet/src/Receiver.h @@ -63,9 +63,29 @@ namespace Messaging { public: Receiver(::qpid::messaging::Receiver * r, Session ^ sessRef); + + // copy constructor + Receiver(const Receiver ^ receiver); + ~Receiver(); !Receiver(); - Receiver(const Receiver ^ rhs); + + // assignment operator + Receiver % operator=(const Receiver % rhs) + { + if (this == %rhs) + { + // Self assignment, do nothing + } + else + { + delete receiverp; + receiverp = new ::qpid::messaging::Receiver( + *(const_cast(rhs).NativeReceiver)); + parentSession = rhs.parentSession; + } + return *this; + } property ::qpid::messaging::Receiver * NativeReceiver { diff --git a/cpp/bindings/qpid/dotnet/src/Sender.cpp b/cpp/bindings/qpid/dotnet/src/Sender.cpp index 0d394f8ef9..bcc2407841 100644 --- a/cpp/bindings/qpid/dotnet/src/Sender.cpp +++ b/cpp/bindings/qpid/dotnet/src/Sender.cpp @@ -61,12 +61,14 @@ namespace Messaging { } // Copy constructor - Sender::Sender(const Sender % rhs) + Sender::Sender(const Sender ^ sender) + : senderp(new ::qpid::messaging::Sender( + *(const_cast(sender)->NativeSender))), + parentSession(sender->parentSession) { - senderp = rhs.senderp; - parentSession = rhs.parentSession; } + // Destroys kept object // TODO: add lock void Sender::Cleanup() diff --git a/cpp/bindings/qpid/dotnet/src/Sender.h b/cpp/bindings/qpid/dotnet/src/Sender.h index de114ab2f1..670f11fe30 100644 --- a/cpp/bindings/qpid/dotnet/src/Sender.h +++ b/cpp/bindings/qpid/dotnet/src/Sender.h @@ -61,9 +61,35 @@ namespace Messaging { public: Sender(::qpid::messaging::Sender * s, Session ^ sessRef); + + // copy constructor + Sender(const Sender ^ sender); + ~Sender(); !Sender(); - Sender(const Sender % rhs); + + // assignment operator + Sender % operator=(const Sender % rhs) + { + if (this == %rhs) + { + // Self assignment, do nothing + } + else + { + delete senderp; + senderp = new ::qpid::messaging::Sender( + *(const_cast(rhs).NativeSender)); + parentSession = rhs.parentSession; + } + return *this; + } + + property ::qpid::messaging::Sender * NativeSender + { + ::qpid::messaging::Sender * get () { return senderp; } + } + // Send(message) void Send(Message ^ mmsgp); diff --git a/cpp/bindings/qpid/dotnet/src/Session.cpp b/cpp/bindings/qpid/dotnet/src/Session.cpp index bafc9b3c76..438c9c4d00 100644 --- a/cpp/bindings/qpid/dotnet/src/Session.cpp +++ b/cpp/bindings/qpid/dotnet/src/Session.cpp @@ -67,11 +67,12 @@ namespace Messaging { Cleanup(); } - // copy constructor - Session::Session(const Session % rhs) + // Copy constructor + Session::Session(const Session ^ session) + : sessionp(new ::qpid::messaging::Session( + *(const_cast(session)->NativeSession))), + parentConnectionp(session->parentConnectionp) { - sessionp = rhs.sessionp; - parentConnectionp = rhs.parentConnectionp; } diff --git a/cpp/bindings/qpid/dotnet/src/Session.h b/cpp/bindings/qpid/dotnet/src/Session.h index a5affc67b6..e07725247b 100644 --- a/cpp/bindings/qpid/dotnet/src/Session.h +++ b/cpp/bindings/qpid/dotnet/src/Session.h @@ -67,9 +67,33 @@ namespace Messaging { public: Session(::qpid::messaging::Session * sessionp, Connection ^ connRef); + + // copy constructor + Session(const Session ^ session); + ~Session(); !Session(); - Session(const Session % rhs); + + // assignment operator + Session % operator=(const Session % rhs) + { + if (this == %rhs) + { + // Self assignment, do nothing + } + else + { + delete sessionp; + sessionp = new ::qpid::messaging::Session( + *(const_cast(rhs).NativeSession) ); + } + return *this; + } + + property ::qpid::messaging::Session * NativeSession + { + ::qpid::messaging::Session * get () { return sessionp; } + } void Close(); void Commit(); diff --git a/cpp/bindings/qpid/dotnet/test/messaging.test/messaging.test.cs b/cpp/bindings/qpid/dotnet/test/messaging.test/messaging.test.cs index c1b3035470..9280dfa8f2 100644 --- a/cpp/bindings/qpid/dotnet/test/messaging.test/messaging.test.cs +++ b/cpp/bindings/qpid/dotnet/test/messaging.test/messaging.test.cs @@ -139,6 +139,68 @@ namespace Org.Apache.Qpid.Messaging guidReadback = myGuid.ToByteArray(); Console.WriteLine("GuidReadback len = {0}", guidReadback.Length); + + // + // Set/Get some properties of a message + // + Message msgGetSet = new Message("12345"); + + msgGetSet.Subject = "Subject"; + msgGetSet.MessageId = "MessageId"; + msgGetSet.UserId = "UserId"; + msgGetSet.CorrelationId = "CorrelationId"; + msgGetSet.Ttl = DurationConstants.SECOND; + msgGetSet.Priority = (byte)'z'; + msgGetSet.Durable = false; + msgGetSet.Redelivered = true; + + Dictionary props = new Dictionary(); + props.Add("firstProperty", 1); + props.Add("secondProperty", 2); + msgGetSet.Properties = props; + + + + Console.WriteLine("Message Subject = {0}", msgGetSet.Subject); + Console.WriteLine("Message ContentType = {0}", msgGetSet.ContentType); + Console.WriteLine("Message MessageId = {0}", msgGetSet.MessageId); + Console.WriteLine("Message UserId = {0}", msgGetSet.UserId); + Console.WriteLine("Message CorrelationId = {0}", msgGetSet.CorrelationId); + Console.WriteLine("Message Ttl mS = {0}", msgGetSet.Ttl.Milliseconds); + Console.WriteLine("Message Priority = {0}", msgGetSet.Priority); + Console.WriteLine("Message Durable = {0}", msgGetSet.Durable); + Console.WriteLine("Message Redelivered = {0}", msgGetSet.Redelivered); + + Dictionary gotProps = msgGetSet.Properties; + foreach (KeyValuePair kvp in gotProps) + { + Console.WriteLine("Message Property {0} = {1}", kvp.Key, kvp.Value.ToString()); + } + + Console.WriteLine("Cycle through a million address copy constructions..."); + Address a1 = new Address("abc"); + Address a2 = new Address("def"); + Address a3 = new Address(a2); + for (int i = 0; i < 1000000; i++) + { + a1 = a2; + a2 = a3; + a3 = new Address(a1); + } + Console.WriteLine(" ...done."); + + Console.WriteLine("Use each object's copy constructor"); + + Address Address1 = new Address("abc"); + Address Address2 = new Address(Address1); + + Connection Connection1 = new Connection("abc"); + Connection Connection2 = new Connection(Connection1); + + Message Message1 = new Message("abc"); + Message Message2 = new Message(Message1); + + } } } -- cgit v1.2.1