activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nmitt...@apache.org
Subject svn commit: r397654 [11/12] - in /incubator/activemq/trunk/openwire-cpp: ./ src/ src/command/ src/gram/ src/gram/java/ src/gram/java/org/ src/gram/java/org/apache/ src/gram/java/org/apache/activemq/ src/gram/java/org/apache/activemq/openwire/ src/gram/...
Date Thu, 27 Apr 2006 21:59:44 GMT
Added: incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/array.hpp
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/array.hpp?rev=397654&view=auto
==============================================================================
--- incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/array.hpp (added)
+++ incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/array.hpp Thu Apr 27 14:59:28 2006
@@ -0,0 +1,529 @@
+/*
+ * Copyright (c) 2005-2006, David Fahlander
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer. Redistributions in binary
+ * form must reproduce the above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or other materials provided
+ * with the distribution. Neither the name of slurk.org nor the names
+ * of its contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef IFR_V1_ARRAY_HPP_
+#define IFR_V1_ARRAY_HPP_
+
+#include <cassert> // assert
+#include <cstdlib> // size_t, malloc
+#include <cstring> // memset, memcpy, memcmp
+#include <new>     // bad_alloc
+#include <memory>  // auto_ptr
+#include "p.hpp"
+#include "colltraits.hpp"
+
+/*
+*/
+namespace ifr {
+  namespace v1 {
+    using std::size_t;
+    using std::malloc;
+    using std::memset;
+    using std::bad_alloc;
+    using std::memcpy;
+    using std::memcmp;
+    using std::auto_ptr;
+
+    /* Base of refcounted array types.
+       Defines it's own new and delete operators that
+       allocated memory on heap both for the class itself
+       and for the number of items in the array.
+    */
+    template <class C, typename ItemType> class RCArrayBase : public refcounted {
+    protected:
+      size_t size_;
+    private:
+      // Hide the normal new and delete operators.
+      void* operator new (size_t);
+      void* operator new[] (size_t);
+      void operator delete[] (void*);
+    public:
+      // Allocate space for both this class and the array that it holds.
+      void* operator new (size_t object_size, size_t array_size) throw (bad_alloc) {
+        assert (object_size == sizeof(C));
+        (void) object_size; // Remove warning about unreferenced variable
+
+        C* p = reinterpret_cast<C*> (
+          malloc (sizeof(C) + (array_size * sizeof(ItemType))));
+        if (p == NULL) {
+          throw bad_alloc ();
+        }
+        // This is kind of a pre-constructor that sets the size value since
+        // it is not accessable in constructor.
+        p->size_ = array_size;
+        return p;
+      }
+      void operator delete (void* p, size_t) {
+        free (p);
+      }
+      void operator delete (void* p) {
+        operator delete (p, 0);
+      }
+      virtual void destroy() {
+        delete static_cast<C*> (this);
+      }
+    protected:
+      // Make impossible for end-users to create an instance of us except through create() or
+      // clone() methods on our subclasses.
+      RCArrayBase(){}
+    private:
+      // Make non-copyable since we have a dynamic operator new.
+      // Instead, our subclasses can be cloneable through their clone() method.
+      RCArrayBase(const RCArrayBase& other);
+      RCArrayBase& operator = (const RCArrayBase& other);
+    public:
+      ItemType* c_array() {
+        assert (this != NULL);
+        return reinterpret_cast<ItemType*> (reinterpret_cast<char*> (this) + sizeof(C));
+      }
+      const ItemType* c_array() const {
+        assert (this != NULL);
+        return reinterpret_cast<const ItemType*> (reinterpret_cast<const char*> (this) + sizeof(C));
+      }
+      ItemType& at (size_t pos) {
+        assert (this != NULL);
+        return c_array()[pos];
+      }
+      const ItemType& at (size_t pos) const {
+        assert (this != NULL);
+        return c_array()[pos];
+      }
+      size_t size() const {
+        assert (this != NULL);
+        return size_;
+      }
+    };
+
+    /* Reference counted array template.
+       Used by the Array and array class templates.
+       This general template class is for arrays items of non-primitive types.
+       A specialization exists for array items of primitive types.
+    */
+    template <
+      typename T,
+      typename ItemType = typename colltraits<T>::ItemType,
+      bool     value_based = is_primitive<T>::value >
+    class RCArray
+      : public RCArrayBase<RCArray<T, ItemType, value_based>, ItemType>
+    {
+    public:
+      RCArray() {
+        // Zero fill (instead of calling default constuctor of all items (p<T>))
+        // size_ member was initialized by new operator.
+        memset (
+          this->c_array(),
+          0,
+          this->size_ * sizeof(ItemType));
+      }
+      ~RCArray() {
+        if (this->size_) {
+          // Call destructor of the smart pointers
+          for (size_t i=0; i<this->size_; ++i) {
+            this->c_array()[i].ItemType::~ItemType();
+          }
+        }
+      }
+      static RCArray* create (size_t size) {
+        return new (size) RCArray();
+      }
+      RCArray* clone() const {
+        assert (this != NULL);
+        auto_ptr<RCArray> retval (new (this->size_) RCArray ());
+        ItemType* retarray = retval->c_array();
+        const ItemType* thisarray = this->c_array();
+        // Copy the smart pointers only
+        for (size_t i=0; i<this->size_; ++i) {
+          retarray[i] = thisarray[i];
+        }
+        return retval.release();
+      }
+
+      int compare (const RCArray& other) const {
+        // Compare the smart pointers only
+        for (size_t i=0; i < this->size(); ++i) {
+          if (this->at(i) == other.at(i)) {
+            continue;
+          } else if (this->at(i) < other.at(1)) {
+            return -1;
+          } else {
+            return 1;
+          }
+        }
+        return 0;
+      }
+    };
+
+    /**
+    */
+    enum SecureMode {
+      NOT_SECURE,
+      SECURE
+    };
+
+    /* Reference counted array template specialization for primitive types.
+       Used by the Array and array class templates.
+    */
+    template <typename T, typename ItemType>
+    class RCArray<T, ItemType, true>
+      : public RCArrayBase<RCArray<T, ItemType, true>, ItemType>
+    {
+    private:
+      SecureMode secmode_;
+    public:
+      RCArray(SecureMode secmode = NOT_SECURE) : secmode_(secmode) {}
+      static RCArray* create (size_t size) {
+        return new (size) RCArray();
+      }
+      static RCArray* create (size_t size, SecureMode secmode) {
+        return new (size) RCArray(secmode);
+      }
+      ~RCArray() {
+        if (secmode_ && this->size_) {
+          // Zero memory
+          const void* p = this->c_array();
+          memset (
+            const_cast<void*>(p),
+            0,
+            this->size_ * sizeof(T));
+          // Force no compiler optimization:
+          *(volatile char*)p = *(volatile char*)p;
+        }
+      }
+      RCArray* clone() const {
+        assert (this != NULL);
+        auto_ptr<RCArray> retval (new (this->size_) RCArray (secmode_));
+        memcpy (
+          const_cast<ItemType*>(retval->c_array()),
+          this->c_array(),
+          this->size_ * sizeof(ItemType));
+        return retval.release();
+      }
+      bool isSecure() const {
+        assert (this != NULL);
+        return (secmode_ == SECURE);
+      }
+      int compare (const RCArray& other) const {
+        for (size_t i=0; i < this->size(); ++i) {
+          if (this->at(i) == other.at(i)) {
+            continue;
+          } else if (this->at(i) < other.at(1)) {
+            return -1;
+          } else {
+            return 1;
+          }
+        }
+        return 0;
+      }
+    };
+
+    /* Forward declaration of the array template.
+       Needed for the clone() methods.
+    */
+    template <
+      typename T,
+      typename ItemType = typename colltraits<T>::ItemType,
+      bool is_primitive = is_primitive<T>::value>
+    class array;
+
+    /* Specialization of colltraits for array.
+      Needed for collections where the item type is array,
+      for example array<array<T> >.
+      This specialization states that collections of array<> will
+      store each item as a array<> and not as a p< array<> >.
+    */
+    template <typename T, typename Item_Type, bool is_primitive>
+    struct colltraits<array<T, Item_Type, is_primitive>, false>
+    {
+      typedef array<T, Item_Type, is_primitive> ItemType;
+      typedef const array<T, ItemType, is_primitive>& InArgType;
+    };
+
+    /* Friend class helping to access the private a_ member in array.
+      I want the a_ member to be private so that no one uses it by
+      accident. Still, when people know what the're doing, it should
+      be possible to access the member using this help class.
+    */
+    struct array_help {
+      template <typename T, typename ItemType, bool is_primitive>
+      inline static const p<RCArray<T,ItemType,is_primitive> >& get_array (
+        const array<T, ItemType, is_primitive>& arr)
+      {
+        return arr.a_;
+      }
+
+      template <typename T, typename ItemType, bool is_primitive>
+      inline static p<RCArray<T,ItemType,is_primitive> >& get_array (
+        array<T, ItemType, is_primitive>& arr)
+      {
+        return arr.a_;
+      }
+
+      template <typename T, typename ItemType>
+      inline static int get_ptrdiff (const array<T, ItemType, false>& arr) {
+        return arr.ptrdiff_;
+      }
+
+      template <typename T, typename ItemType>
+      inline static void set_ptrdiff (
+          array<T, ItemType, false>& arr,
+          int ptrdiff)
+      {
+        arr.ptrdiff_ = ptrdiff;        
+      }
+    };
+
+    /* Base help class for the array template. 
+       The class is here to define the common interface between
+       array<primitive type> and array<non primitive type>.
+    */
+    template <typename T, typename ItemType, bool is_primitive>
+    class array_base
+    {
+      friend struct array_help;
+    public:
+      typedef RCArray<T,ItemType,is_primitive> ArrayClass;
+    protected:
+      // The only member - a smart pointer to an array instance.
+      p<ArrayClass> a_;
+    public:
+      // Constructors
+      inline array_base (){}
+      inline array_base (const p<ArrayClass>& other) : a_ (other) {}
+      // Methods
+      inline size_t size() const {
+        return a_->size();
+      }
+      inline int compare (const array_base& other) const {
+        return a_->compare (*other.a_);
+      }
+      // Operators 
+      inline bool operator == (const ArrayClass* arrayClass) const {
+        return a_ == arrayClass;
+      }
+      inline bool operator != (const ArrayClass* arrayClass) const {
+        return a_ != arrayClass;
+      }
+      inline bool operator < (const array_base& other) const {
+        return a_->compare (*other.a_) < 0;
+      }
+      inline bool operator > (const array_base& other) const {
+        return a_->compare (*other.a_) > 0;
+      }
+      inline bool operator == (const array_base& other) const {
+        return a_->compare (*other.a_) == 0;
+      }
+      inline bool operator != (const array_base& other) const {
+        return a_->compare (*other.a_) != 0;
+      }
+    };
+
+    /* The array template.
+       This is the generic array class where all items are p<T>.
+       Note that a specialization exists for primitive types.
+    */
+    template <typename T, typename ItemType, bool is_primitive>
+    class array
+      : public array_base<T, ItemType, is_primitive>
+    {
+      friend struct array_help;
+    public:
+      typedef RCArray<T, ItemType, is_primitive> ArrayClass;
+      typedef array_base<T, ItemType, is_primitive> Base;
+    private:
+      int ptrdiff_;
+    public:
+      // Constructors
+      inline array () : ptrdiff_(0) {}
+      inline explicit array (size_t size) : Base ((size ? ArrayClass::create(size) : NULL)), ptrdiff_(0) {}
+      inline array (const NullClass*) : Base (NULL), ptrdiff_(0) {}
+      //inline array (const p<ArrayClass>& other) : Base (other), ptrdiff_(0) {}
+      inline array (const array& other) : Base (other), ptrdiff_(other.ptrdiff_) {}
+      // Template based Copy Constructor
+      template <typename DerivedItem>
+        inline array (const array<DerivedItem, p<DerivedItem>, false>& other) :
+          Base (reinterpret_cast<const Base&> (other))
+      {
+        // Check compile time that DerivedItem is derived from T
+        T* tmp = reinterpret_cast<DerivedItem*>(NULL);
+        // Check compile-time that T and DerivedItem are compatible
+        smartptr_assert_type_compatibility (tmp, tmp, (DerivedItem*)NULL);
+        // Set ptrdiff_ according to the cast made
+        ptrdiff_ = reinterpret_cast<const array&>(other).ptrdiff_ + ptr_diff<T, DerivedItem>::value();
+      }
+      inline array (const p<ArrayClass>& other, int ptrdiff) : Base (other), ptrdiff_(ptrdiff) {}
+    public:
+      // Template based operator =
+      template <typename DerivedItem>
+        inline array& operator = (const array<DerivedItem, p<DerivedItem>, false>& other)
+      {
+        // Check compile time that DerivedItem is derived from T
+        T* tmp = reinterpret_cast<DerivedItem*>(NULL);
+        // Check compile-time that T and DerivedItem are compatible
+        smartptr_assert_type_compatibility (tmp, tmp, (DerivedItem*)NULL);
+        // Set ptrdiff_ according to the cast made
+        ptrdiff_ = reinterpret_cast<const array&>(other).ptrdiff_ + ptr_diff<T, DerivedItem>::value();
+        this->a_ = reinterpret_cast<const array&>(other).a_;
+        return *this;
+      }
+      inline array& operator = (const NullClass* nullclass) {
+        assert (nullclass == NULL);
+        this->a_ = NULL;
+        return *this;
+      }
+      inline CollItemRef<T> operator [] (size_t pos) const {
+        return CollItemRef<T> (getptr(this->a_), this->a_->c_array() + pos, ptrdiff_);
+      }
+      // Methods
+      inline CollItemRef<T> at (size_t pos) const {
+        return CollItemRef<T> (getptr(this->a_), this->a_->c_array() + pos, ptrdiff_);
+      }
+      inline array clone() const {
+        return array (this->a_->clone(), ptrdiff_);
+      }
+    };
+
+    /* The array template specialized for primitive types.
+    */
+    template <typename T, typename ItemType>
+    class array<T,ItemType,true>
+      : public array_base<T,ItemType,true>
+    {
+    public:
+      typedef RCArray<T,ItemType,true> ArrayClass;
+      typedef array_base<T,ItemType,true> Base;
+    public:
+      // Ordinary Constructors
+      inline array () {}
+      inline explicit array (size_t size) : Base ((size ? ArrayClass::create (size) : NULL)) {}
+      inline array (size_t size, SecureMode secmode) : Base (ArrayClass::create(size, secmode)) {}
+      inline array (const NullClass*) : Base (NULL) {}
+      inline array (const p<ArrayClass>& other) : Base (other) {}
+      inline array (const array& other) : Base (other) {}
+      // Constructor that copies content from supplied C array.
+      array (const ItemType* initialData, size_t size, SecureMode secmode = NOT_SECURE) :
+        Base ((size ? ArrayClass::create (size, secmode) : NULL))
+      {
+        if (size) {
+          memcpy (const_cast<typename remove_const<ItemType>::Type*> (c_array()), initialData, size * sizeof(ItemType));
+        }
+      }
+      inline array& operator = (const NullClass* nullclass) {
+        assert (nullclass == NULL);
+        this->a_ = NULL;
+        return *this;
+      }
+      inline ItemType& operator [] (size_t pos) {
+        return this->a_->at(pos);
+      }
+      inline const ItemType& operator [] (size_t pos) const {
+        return this->a_->at(pos);
+      }
+      // Methods
+      inline ItemType& at (size_t pos) {
+        return this->a_->at (pos);
+      }
+      inline const ItemType& at (size_t pos) const {
+        return this->a_->at (pos);
+      }
+      inline array clone() const {
+        return this->a_->clone();
+      }
+      inline ItemType* c_array() {
+        return this->a_->c_array();
+      }
+      inline const ItemType* c_array() const {
+        return this->a_->c_array();
+      }
+    };
+
+    /**
+    */
+    template <typename T, typename ItemType, bool is_primitive>
+    long getrefcount (const array<T, ItemType, is_primitive>& parray) {
+      return getrefcount (array_help::get_array (parray));
+    }
+
+    /**
+    */
+    template <typename T, typename ItemType, bool is_primitive>
+    void addref (array<T, ItemType, is_primitive>& parray) {
+      addref (array_help::get_array (parray));
+    }
+
+    /**
+    */
+    template <typename T, typename ItemType, bool is_primitive>
+    void release (array<T, ItemType, is_primitive>& parray) {
+      release (array_help::get_array (parray));
+    }
+
+    /** array_cast. Similar to static_cast but for arrays.
+      Usage:
+        array<Base> b;
+        array<Derived> d = array_cast<Derived> (b);
+    */
+    template <typename T, typename Base>
+    inline array<T, p<T>, false> array_cast (const array<Base, p<Base>, false>& baseArray)
+    {
+      // Check compile-time that T and Base are compatible
+      // Also check that the cast is valid using static_cast but allowing casting between const/non-const.
+      smartptr_assert_type_compatibility (
+          (Base*)NULL,
+          (Base*)NULL,
+          static_cast<const T*> ((Base*)(0))) // <-- Checks that cast is valid using static_cast + const_cast
+          ;
+      // Cast and adjust ptrdiff in returned array (making our own static_cast<>-similar implementaion)
+      return array<T> (
+        reinterpret_cast<const p<RCArray<T> >&> (array_help::get_array (baseArray)),
+        array_help::get_ptrdiff (baseArray) - ptr_diff<Base, T>::value());
+    }
+    /** array_cast for arrays of primitive types.
+      Usage:
+        array<char> a;
+        array<const unsigned char> b = array_cast<const unsigned char> (a);
+    */
+    template <typename T, typename T2>
+    inline array<T, T, true>& array_cast (const array<T2, T2, true>& otherArray)
+    {
+      ERROR_CANNOT_CAST_PRIMITIVE_ARRAYS(otherArray); // Not implemented.
+    }
+
+    /**
+    */
+    template <typename T> const T* getptr (const array<T>& a) {
+      return (a != NULL ? a.c_array() : NULL);
+    }
+    /**
+    */
+    template <typename T> T* getptr (array<T>& a) {
+      return (a != NULL ? a.c_array() : NULL);
+    }
+
+  }
+}
+
+#endif // IFR_V1_ARRAY_HPP_

Added: incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/atomic.hpp
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/atomic.hpp?rev=397654&view=auto
==============================================================================
--- incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/atomic.hpp (added)
+++ incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/atomic.hpp Thu Apr 27 14:59:28 2006
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 2005-2006, David Fahlander
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer. Redistributions in binary
+ * form must reproduce the above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or other materials provided
+ * with the distribution. Neither the name of slurk.org nor the names
+ * of its contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+// ----------------------------------------------------------------------------
+//
+//      =====================================================================
+//      Independent Framework (IFR) version 0.1 (BETA)  Stockholm, 2006-04-05
+//      =====================================================================
+//
+//      IFR is a 100% header-only library. It does not require lib-file to
+//      link with. It is licensed with the BSD license (see the license
+//      above).
+//
+//      During this beta stage of IFR, the internal design of IFR may change
+//      until version 1.0 has been released. When version 1.0 is out, the
+//      internal structure is intended to become freezed. Version 1.x is
+//      intended to be a binary contract defined by it's headers. Only
+//      bugfixes that do not affect the internal structure should then be
+//      made. New include files with new classes may be added though. The
+//      aim is making it safe to bundle the IFR headers together with any
+//      other library without risking a mix of versions with other libs that
+//      also bundles IFR. The aim is also to make it safe to build a DLL/SO
+//      API based on IFR 1.x without requiring rebuild of the binaries or
+//      dynamic libraries when newer version of IFR comes out. The binary
+//      contract defined by the IFR include headers must never break even
+//      if the application and the module use different versions of IFR.
+//
+//      If the design or internal structure of IFR must be changed in
+//      future, the future version will be placed under namespace ifr::v2
+//      so that libs that bundles older version of IFR may coexist with
+//      libs that bundles a newer version of IFR.
+//
+//      PUBLIC PART OF THE INTERFACE
+//      ============================
+//
+//      Library users must only include files without the .hpp file
+//      extension. Files with the .hpp file extension are not public. User's
+//      code will break in future if including the hpp files directly. Only
+//      headers without file extensions (#include <ifr/p>, #include
+//      <ifr/array>) are public includes and will in the release version
+//      contain all the content from the hpp files.
+//
+//      User of the library must not use classes within a "nonpublic"
+//      namespace. All symbols defined within a namespace named "nonpublic"
+//      may change and is only intended for use internally by IFR.
+//
+// ----------------------------------------------------------------------------
+#ifndef IFR_V1_ATOMIC_HPP_
+#define IFR_V1_ATOMIC_HPP_
+
+#include "platform.hpp"
+
+namespace ifr {
+  namespace v1 {
+    class atomic {
+    private:
+      // Invalidate copy constructor and assignment operator
+      atomic (const atomic&);
+      atomic& operator= (const atomic&);
+
+#ifdef IFR_USE_PTHREAD
+    protected:
+      long counter_;
+      pthread_mutex_t m_;
+    public:
+      atomic () : counter_ (0) {
+        pthread_mutex_init(&m_, NULL);
+      }
+      atomic (long init_value) : counter_ (init_value) {
+        pthread_mutex_init(&m_, NULL);
+      }
+      ~atomic () {
+        pthread_mutex_destroy(&m_);
+      }
+      void increment() {
+        pthread_mutex_lock(&m_);
+        ++counter_;
+        pthread_mutex_unlock(&m_);
+      }
+      bool decrement() {
+        pthread_mutex_lock(&m_);
+        if (!--counter_) {
+          pthread_mutex_unlock(&m_);
+          return true;
+        } else {
+          pthread_mutex_unlock(&m_);
+          return false;
+        }
+      }
+      long getvalue() const {
+        pthread_mutex_lock(&m_);
+        long retval = counter_;
+        pthread_mutex_unlock(&m_);
+        return retval;
+      }
+      /*void setvalue(long value) {
+        pthread_mutex_lock(&m_);
+        counter_ = value;
+        pthread_mutex_unlock(&m_);
+      }*/
+      static const char* implementation () {
+        return "pthread";
+      }
+#elif defined IFR_USE_GNU_ATOMIC
+    protected:
+      mutable _Atomic_word counter_;
+    public:
+      atomic () : counter_ (0) {}
+      atomic (long init_value) : counter_ (init_value) {}
+      inline void increment() {
+        __atomic_add(&counter_, 1);
+      }
+      inline bool decrement() {
+        return (__exchange_and_add(&counter_, -1) == 1);
+      }
+      inline long getvalue() const {
+        return __exchange_and_add(&counter_, 0);
+      }
+      /*inline void setvalue(long value) {
+        counter_ = value;
+      }*/
+      static const char* implementation () {
+        return "gnu atomic";
+      }
+#elif defined IFR_USE_INTERLOCKED
+    protected:
+      mutable long counter_;
+    public:
+      atomic () : counter_ (0) {}
+      atomic (long init_value) : counter_ (init_value) {}
+      inline void increment() {
+        _InterlockedIncrement (&counter_);
+      }
+      inline bool decrement() {
+        return (_InterlockedDecrement (&counter_) == 0);
+      }
+      inline long getvalue() const {
+        return _InterlockedExchangeAdd (&counter_, 0);
+      }
+      /*inline void setvalue(long value) {
+        counter_ = value;
+      }*/
+      static const char* implementation () {
+        return "windows interlocked";
+      }
+#else
+#error "atomic Operations Unsupported"
+#endif
+    };
+  }
+}
+#endif // IFR_V1_ATOMIC_HPP_
+

Added: incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/cloneable.hpp
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/cloneable.hpp?rev=397654&view=auto
==============================================================================
--- incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/cloneable.hpp (added)
+++ incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/cloneable.hpp Thu Apr 27 14:59:28 2006
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2005-2006, David Fahlander
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer. Redistributions in binary
+ * form must reproduce the above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or other materials provided
+ * with the distribution. Neither the name of slurk.org nor the names
+ * of its contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef IFR_V1_CLONEABLE_HPP_
+#define IFR_V1_CLONEABLE_HPP_
+
+#include "p.hpp"
+namespace ifr {
+  namespace v1 {
+    /*  Deriving from this struct indicates that your class has the methods:
+
+          YourClass* clone() const; // Performs shallow clone.
+          YourClass* deepClone() const; // Performs deep clone.
+
+        A deep clone means that the object and all it's members are cloned.
+        A shallow clone means that only the object are cloned but not
+        nescessarily it's members.
+
+        However, you should normally create your cloning code in the copy
+        constructor, which is the default cloning method in C++. But for
+        classes that of some reason cannot use the copy constructor, the
+        clone method is the alternative. Specifically if the new operator
+        is protected for a class, then it is not possible to clone the
+        object in a normal way, which is the case with some of the
+        collection classes in IFR.
+
+        Examples:
+
+        #ifndef MYLIB_MYCLASS_HPP_
+        #define MYLIB_MYCLASS_HPP_
+
+        #include <ifr/array>
+
+        namespace mylib {
+          using namespace ifr;
+
+          class MyClass : public cloneable
+          {
+          private:
+            p<int> intMember_;
+            array<unsigned char> blobMember_;
+          public:
+            p<MyClass> clone () {
+              // Default clone - use new() and the default copy constructor:
+              return new MyClass (*this);
+            }
+            p<MyClass> deepClone () {
+              // Deep clone - create a copy of all members that are pointers:
+              p<MyClass> retval = new MyClass ();
+              if (intMember_ != NULL) {
+                *retval->intMember_ = *intMember_;
+              }
+              if (blobMember_ != NULL) {
+                *retval->blobMember_ = blobMember_.deepClone();
+              }
+              return retval;
+            }
+          };
+        }
+        #endif //MYLIB_MYCLASS_HPP_
+
+        ----
+
+        #include <ifr/array>
+        #include <mylib/MyClass.hpp>
+
+        using namespace ifr;
+        using namespace mylib;
+
+        int main () {
+          array<MyClass> orig (10);
+          array<MyClass> shallowClone = orig.clone(); // Copies pointers only
+          array<MyClass> deepClone = orig.deepClone(); // Copies instances also
+
+          return 0;
+        }
+
+    */
+    struct cloneable {};
+
+    /** Returns a clone of given object.
+      If given object derives from struct cloneable, this function calls
+      clone() on it.
+      If given object does not derive from struct cloneable, this function
+      calls the copy constructor of given object.
+    */
+    template <typename T> T* get_clone (const T& obj, const void*) {
+      return new T (obj);
+    }
+    template <typename T> T* get_clone (const T& obj, const cloneable*) {
+      return obj.clone();
+    }
+    /** Clones an instance.
+      This function will call the copy constructor of given type unless given class is
+      derived from clonable. If so, clone() is called on the object.
+
+      For classes that do not derive from cloneable, the new operator +
+      copy constructor defines the behavior of both clone() and deelClone().
+
+      Examples:
+        int origInt = 3;
+        int clonedInt = clone (origInt);
+
+        std::string origString = "Hello World";
+        std::string clonedString = clone (origString);
+
+        int* origInt = new int (3);
+        int* clonedInt = clone (origInt);
+
+        p<int> origInt = new int (3);
+        p<int> clonedInt = clone (origInt);
+
+      @param obj Object to clone.
+      @param depth Clone depth. Only applied if the class is derived from cloneable.
+        SHALLOW_CLONE (default) specifies that the instance will be cloned but not
+        nescessarily it's members.
+        DEEP_CLONE specifies that the instance as well as it's members should be cloned.
+    */
+    /*
+    template <typename T> p<T> getclone (const T& obj) {
+      return get_clone (obj, (const T*) NULL);
+    }
+    template <typename T> p<T> getdeepclone (const T& obj) {
+      return get_deep_clone (obj, (const T*) NULL);
+    }
+    template <typename T> p<T> getclone (const p<const T>& obj) {
+      return get_clone (*obj, (const T*) NULL);
+    }
+    template <typename T> p<T> getdeepclone (const p<const T>& obj) {
+      return get_deep_clone (*obj, (const T*) NULL);
+    }*/
+  }
+}
+
+#endif // IFR_V1_CLONEABLE_HPP_

Added: incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/colltraits.hpp
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/colltraits.hpp?rev=397654&view=auto
==============================================================================
--- incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/colltraits.hpp (added)
+++ incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/colltraits.hpp Thu Apr 27 14:59:28 2006
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2005-2006, David Fahlander
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer. Redistributions in binary
+ * form must reproduce the above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or other materials provided
+ * with the distribution. Neither the name of slurk.org nor the names
+ * of its contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef IFR_V1_COLLTRAITS_HPP_
+#define IFR_V1_COLLTRAITS_HPP_
+
+namespace ifr {
+  namespace v1 {
+
+    struct value_true {static const bool value = true;};
+    struct value_false {static const bool value = false;};
+
+    // is_primitive<>. Tells whether T is a primitive type or not.
+    template <typename T> struct is_primitive : value_false{};
+    // Pointers are primitive
+    template <typename T> struct is_primitive<T*> : value_true {};
+    // Enums are primitive
+    //template <enum T> struct is_primitive<T> : colltraits_primitive<T> {};
+    // All integral types are primitive. Here all integral types are listed:
+    template <> struct is_primitive<char> : value_true {};
+    template <> struct is_primitive<signed char> : value_true {};
+    template <> struct is_primitive<unsigned char> : value_true {};
+    template <> struct is_primitive<signed short> : value_true {};
+    template <> struct is_primitive<unsigned short> : value_true {};
+    #if !defined (_MSC_VER) || defined (_NATIVE_WCHAR_T_DEFINED)
+    template <> struct is_primitive<wchar_t> : value_true{};
+    #endif
+    template <> struct is_primitive<signed int> : value_true{};
+    template <> struct is_primitive<unsigned int> : value_true{};
+    template <> struct is_primitive<signed long> : value_true{};
+    template <> struct is_primitive<unsigned long> : value_true{};
+    template <> struct is_primitive<signed long long> : value_true{};
+    template <> struct is_primitive<unsigned long long> : value_true{};
+    // float and double is primitive
+    template <> struct is_primitive<float> : value_true{};
+    template <> struct is_primitive<double> : value_true{};
+    // bool is primitive
+    template <> struct is_primitive<bool> : value_true{};
+    // If const T is primitive, then T is primitive as well
+    template <typename T> struct is_primitive<const T> : is_primitive<T> {};
+    // If volatile T is primitive, then T is primitive as well
+    template <typename T> struct is_primitive<volatile T> : is_primitive<T> {};
+    // If const volatile T is primitive, then T is primitive as well
+    template <typename T> struct is_primitive<const volatile T> : is_primitive<T> {};
+
+    // collection traits for primitive types where ItemType is T
+    template <typename T, bool primitive=is_primitive<T>::value> struct colltraits {
+      typedef T ItemType;
+      typedef const T& InArgType;
+      //static const bool is_primitive = primitive;
+    };
+    // collection traits for non-primitive types where ItemType is p<T>
+    template <typename T> struct colltraits<T, false> {
+      typedef p<T> ItemType;
+      typedef const p<T>& InArgType;
+      //static const bool is_primitive = false;
+    };
+
+/*    // Array
+    template <typename T, typename ItemType, bool is_primitive> class Array;
+    template <typename T, typename ItemType, bool is_primitive>
+    struct colltraits<Array<T, ItemType, is_primitive>, false>
+    {
+      typedef array<T, ItemType, is_primitive> ItemType;
+      typedef const Array<T, ItemType, is_primitive>& InArgType;
+    };
+    // String
+    class String;
+    struct colltraits<String, false>
+    {
+      typedef pString ItemType;
+      typedef const String& InArgType;
+    };
+*/
+
+    template <typename T> struct remove_const {
+      typedef T Type;
+    };
+    template <typename T> struct remove_const<const T> {
+      typedef T Type;
+    };
+    /*template <typename Type1, typename Type2> struct assert_same_size
+    {
+        static const bool value = (sizeof(Type1) == sizeof(Type2));
+    };
+    template <typename Type1, typename Type2> Type1& same_size_cast (Type2& in)
+    {
+        ERROR_CAST_TO_DIFFERENT_SIZE(in);
+    };
+    template <typename Type1, typename Type2>*/
+  }
+}
+
+#endif // IFR_V1_COLLTRAITS_HPP_

Added: incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/endian.hpp
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/endian.hpp?rev=397654&view=auto
==============================================================================
--- incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/endian.hpp (added)
+++ incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/endian.hpp Thu Apr 27 14:59:28 2006
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2005-2006, David Fahlander
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer. Redistributions in binary
+ * form must reproduce the above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or other materials provided
+ * with the distribution. Neither the name of slurk.org nor the names
+ * of its contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// First try - check __BYTE_ORDER macro
+#if !defined IFR_IS_BIG_ENDIAN && !defined IFR_IS_LITTLE_ENDIAN && !defined IFR_IS_DPD_ENDIAN
+# ifdef unix
+#  include <sys/param.h> // defines __BYTE_ORDER (or sometimes __LITTLE_ENDIAN or __BIG_ENDIAN or __PDP_ENDIAN)
+# endif
+# if defined (__GLIBC__)
+#  include <endian.h> // Can also define __BYTE_ORDER
+# endif
+# ifdef __BYTE_ORDER
+#  if __BYTE_ORDER == __LITTLE_ENDIAN
+#   define IFR_IS_LITTLE_ENDIAN
+#  elif __BYTE_ORDER == __BIG_ENDIAN
+#   define IFR_IS_BIG_ENDIAN
+#  elif __BYTE_ORDER == __PDP_ENDIAN
+#   define IFR_IS_PDP_ENDIAN
+#  endif
+# endif
+#endif
+
+// Second try - check __LITTLE_ENDIAN or __BIG_ENDIAN
+#if !defined IFR_IS_BIG_ENDIAN && !defined IFR_IS_LITTLE_ENDIAN && !defined IFR_IS_DPD_ENDIAN
+# if defined __LITTLE_ENDIAN
+#  define IFR_IS_LITTLE_ENDIAN
+# elif defined __BIG_ENDIAN
+#  define IFR_IS_BIG_ENDIAN
+# elif defined __PDP_ENDIAN
+#  define IFR_IS_PDP_ENDIAN
+# endif
+#endif
+
+// Last try - find out from well-known processor types using little endian
+#if !defined IFR_IS_BIG_ENDIAN && !defined IFR_IS_LITTLE_ENDIAN && !defined IFR_IS_DPD_ENDIAN
+# if defined (i386) || defined (__i386__) \
+  || defined (_M_IX86) || defined (vax) \
+  || defined (__alpha) || defined (__alpha__) \
+  || defined (__x86_64__) || defined (__ia64) \
+  || defined (__ia64__) || defined (__amd64__) \
+  || defined (_M_IX86) || defined (_M_IA64) \
+  || defined (_M_ALPHA)
+#  define IFR_IS_LITTLE_ENDIAN
+# else
+#  if defined (__sparc) || defined(__sparc__) \
+  || defined(_POWER) || defined(__powerpc__) \
+  || defined(__ppc__) || defined(__hppa) \
+  || defined(_MIPSEB) || defined(_POWER) \
+  || defined(__s390__)
+#   define IFR_IS_BIG_ENDIAN
+#  endif
+# endif
+#endif
+
+// Show error if we still don't know endianess
+#if !defined IFR_IS_BIG_ENDIAN && !defined IFR_IS_LITTLE_ENDIAN && !defined IFR_IS_DPD_ENDIAN
+#error "Could not determine endianess of your processor type"
+#endif
+

Added: incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/namespace.hpp
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/namespace.hpp?rev=397654&view=auto
==============================================================================
--- incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/namespace.hpp (added)
+++ incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/namespace.hpp Thu Apr 27 14:59:28 2006
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2005-2006, David Fahlander
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer. Redistributions in binary
+ * form must reproduce the above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or other materials provided
+ * with the distribution. Neither the name of slurk.org nor the names
+ * of its contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef IFR_V1_NAMESPACE_HPP_
+#define IFR_V1_NAMESPACE_HPP_
+
+namespace ifr {
+  using namespace v1;
+}
+
+#endif // IFR_V1_NAMESPACE_HPP_

Added: incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/p.hpp
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/p.hpp?rev=397654&view=auto
==============================================================================
--- incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/p.hpp (added)
+++ incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/p.hpp Thu Apr 27 14:59:28 2006
@@ -0,0 +1,741 @@
+/*
+ * Copyright (c) 2005-2006, David Fahlander
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer. Redistributions in binary
+ * form must reproduce the above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or other materials provided
+ * with the distribution. Neither the name of slurk.org nor the names
+ * of its contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+/* TODOS:
+    * Test that p<const T> works as expected.
+*/
+#ifndef IFR_V1_P_HPP_
+#define IFR_V1_P_HPP_
+
+#include <cassert>
+#include <typeinfo>
+#include "atomic.hpp"
+#ifndef NULL
+#define NULL 0
+#endif
+namespace ifr {
+  namespace v1 {
+    // This class is only used for comparing with NULL.
+    class NullClass;
+
+    /** Base class that enhances the use of the p<> template.
+    
+       The p<> template is compatible with any type. However, classes that
+       derives from refcounted gets the following benefits:
+
+       * It becomes faster to create and assign them to a p<> smart pointer.
+       * It becomes faster for the p<> smart pointer to delete them when
+         they are no longer references.
+       * It becomes possible convert ordinary pointers back to
+         p<> smart pointers.
+       
+       The p<> template inspects it's type to see whether it maintains the
+       reference counting mechanism internally or not. By default, the p<>
+       template will assume that it's type does not maintain such reference
+       counting and will therefore create a container object that performs
+       the reference counting. However, if the type is recognized as one
+       that can perform reference counting internally, the p<> template
+       will use the type's reference counting mechanisms and does not need
+       to create a separate reference counting container for the pointer.
+
+       refcounted is an example of a class that handles reference counting
+       internally. By deriving from refcounted, your object will contain
+       a "built-in" thread-safe reference counter and every time you call
+       new() or delete() on your class, only your instance will be created
+       or freed instead of also having to create a separate container
+       object as well.
+
+       Also have a look at struct Interface, which derives from refcounted
+       and is intended to use if your class is subject for multiple
+       inheritance.
+       
+       The internal reference counting gives us another benefit except
+       that it does not have to create a container. Since the class has
+       a built-in reference counter, it can be converted to a ordinary
+       pointer and back again to a smart pointer. Classes that lacks of
+       such an internal reference counter lacks the possibility to be
+       converted back to a smart pointer from an ordinary pointer.
+
+       Converting an ordinary pointer to a smart pointer should be done
+       using the smartify() template function. This function will
+       only compile if the given class is recognized as a class with
+       it's own internal reference counting mechanism.
+
+       If you already have your own base class or other 3rd party
+       base class that has it's own reference counter, you can make
+       the p<> template recognize that class in the same way that it
+       recognizes refcounted. You can do that without changing
+       either the IFR code nor the code of the base class that you
+       want to be recognized. What you do is simply to add some
+       overloaded versions of the smartptr_xxx() functions and make
+       sure that the header file containing these overloaded functions
+       are always included everywhere that you use the p<> template
+       with that class. To see an example of writing such overloaded
+       methods, see how IUnknown is made to recognized by the p<>
+       template using it's overloaded versions of the smartptr_xxx()
+       functions.
+
+       If your class is intended to be used as one of multiple base classes,
+       you must derive virtually from refcounted. Otherwise derived instances
+       will result in having ambigous methods and multiple reference counters.
+
+       Normally, multiple inheritance is useful when implementing interfaces.
+       It is therefore recommended (for simplicity and clean design) that your
+       interfaces derive from Interface rather than from this class directly.
+       The struct Interface derives virtually from refcounted for you.
+
+      @see Interface
+    */
+    class refcounted {
+      friend inline void addref (refcounted*);
+      friend inline void release (refcounted*);
+      friend inline long getrefcount (const refcounted*);
+    private:
+      atomic refcount_;
+    public:
+      virtual ~refcounted() {}
+    protected:
+      virtual void destroy() {
+        delete this;
+      }
+    public:
+      inline refcounted(long refcount) : refcount_ (refcount) {}
+      inline refcounted() : refcount_ (1) {}
+      // When a class derived from refcounted is copied, always reset refcount.
+      inline refcounted(const refcounted&) : refcount_ (1) {}
+      // When a class derived from refcounted is assigned, don't copy refcount.
+      inline refcounted& operator = (const refcounted&) {return *this;}
+    };
+
+    /** Base class for interfaces.
+
+      Structs (or classes) that derive from Interface get the following benefits:
+
+        * They (and their children) does not need to define their own virtual
+          destructor.
+        * They (and their children) can convert their this-pointers to smart
+          pointers by calling smartify(this).
+        * Smart pointers of these classes or structs become more heap-efficiant.
+          (See why in the documentation of refcounted).
+      
+      These benefits are gained because Interface derives from refcounted
+      which has the same benefits. Similar benefits would be gained
+      by deriving from IUnknown or any other class that maintains the
+      reference counting internally and is adopted to the p<> template
+      using the smartptr_xxx () overloaded functions.
+
+      The difference between refcounted and Interface is that Interface is
+      more suited for using where multiple inheritance is used. This is due
+      to that it derives virtual from refcounted. The virtual derivation
+      results in that it can function as base class for any class that is
+      intended to be derived from with multiple inheritance, which is a
+      typical case when using an interface based design.
+
+      Note that the p<> template does not require it's type to be derived
+      from refcounted nor Interface. It's just a help and it gives the
+      mentioned benefits.
+
+      The only thing to keep in mind is to never mix classes that derive
+      (direct or indirectly) from refcounted and classes that do not derive
+      (direct or indirectly) from refcounted in the same class hierarchy.
+      Doing so by mistake will be caught by a compilation
+      error in the function smartptr_assert_type_compatibility().
+    */
+    struct Interface : virtual refcounted
+    {
+    };
+
+
+    inline void addref (refcounted* o) {
+      o->refcount_.increment();
+    }
+    inline void release (refcounted* o) {
+      if (o->refcount_.decrement()) o->destroy();
+    }
+    inline long getrefcount (const refcounted* o) {
+      return o->refcount_.getvalue();
+    }
+
+
+
+    // Forward declaration of p
+    template <typename T> class p;
+    template <typename T> class CollItemRef;
+
+    // This is an internal class that helps getting the private member obj_ from p.
+    // The normal thing would be to use "friend" but that is not possible since
+    // there are template methods and classes that must be friends.
+    struct p_help {
+      template <typename T> inline static void* get_rc (const p<T>& sp) {
+        return const_cast<void*> (sp.rc_);
+      }
+      template <typename T> inline static void set_rc (p<T>& sp, void* rc) {
+        sp.rc_ = rc;
+      }
+      template <typename T> inline static T* get_obj (const p<T>& sp) {
+        return const_cast<T*> (sp.obj_);
+      }
+      template <typename T> inline static void set_obj (p<T>& sp, T* obj) {
+        sp.obj_ = obj;
+      }
+      template <typename T> inline static T* get_obj (const p<T>& sp, int ptr_adjustment) {
+        if (sp.obj_) {
+          return const_cast<T*> (
+            reinterpret_cast<const T*> (
+              reinterpret_cast<const char*>(sp.obj_) + ptr_adjustment));
+        } else {
+          return NULL;
+        }
+      }
+      template <typename T> inline static void adjust_ptr (p<T>& sp, int ptr_adjustment) {
+        if (sp.obj_) *reinterpret_cast<char**>(&sp.obj_) += ptr_adjustment;
+      }
+      template <typename T> inline static T* get_obj (const CollItemRef<T>& cir) {
+        return get_obj (*cir.pitem_, cir.ptrdiff_);
+      }
+      template <typename T> inline static void* get_rc (const CollItemRef<T>& cir) {
+        return cir.pitem_->rc_;
+      }
+    };
+
+    /** Collection Item Reference.
+      Used in collections that must support casting of the collection.
+    */
+    template <typename T> class CollItemRef {
+      friend struct p_help;
+    private:
+      refcounted* collection_;
+      p<T>* pitem_;
+      int ptrdiff_;
+    public:
+      CollItemRef (
+          refcounted* collection,
+          p<T>* pitem,
+          int ptrdiff)
+      : collection_ (collection),
+          pitem_ (pitem),
+          ptrdiff_ (ptrdiff)
+      {
+        // Add refcount on the collection.
+        // This protects pitem_ from the risk of deletion.
+        addref (collection);
+      }
+      ~CollItemRef () {
+        // Release refcount on the collection.
+        release (collection_);
+      }
+    public:
+      void operator = (const p<T>& obj) {
+        *pitem_ = obj;
+        p_help::adjust_ptr (*pitem_, 0 - ptrdiff_);
+      }
+      T* operator -> () const {
+        return p_help::get_obj (*pitem_, ptrdiff_);
+      }
+      T& operator * () const {
+        return *p_help::get_obj (*pitem_, ptrdiff_);
+      }
+    };
+
+    /** Checks that static cast is valid and returns the ptr difference
+        between them.
+    */
+    template <typename T, typename DerivedItem> struct ptr_diff {
+      static int value () {
+        return
+          (int) (reinterpret_cast<const char*> (
+            static_cast<T*> (
+              reinterpret_cast<DerivedItem*>(1))) -
+          reinterpret_cast<const char*> (1));
+      }
+    };
+
+    /** Generic thread-safe reference counted smart pointer for any type.
+
+        This smart pointer works identically to ordinary pointers in almost
+        every way except that it will automatically delete the instance
+        as soon as it is not referenced anymore.
+
+        In similarity of ordinary pointers, this smart pointer supports:
+            * automatic up-cast
+            * explicit down-cast (see p_cast<>)
+
+        Examples of usage:
+
+            // Examples that shows how similar it is to ordinary pointers:
+
+            p<MyClass> myClass = new MyClass();
+            p<int> myInt = new int (3);
+            p<IMyInterface> myInterface = new MyClassThatDerivesFromIMyInterface();
+
+            myClass->doThis();
+            myInterface->doThat();
+            ++(*myInt);
+
+            // Casting examples:
+
+            p<MyBaseClass> myBaseClass = mySubClass; // Automatic up-cast.
+            mySubClass = p_cast<MySubClass> (myBaseClass); // Static down-cast.
+    */
+    template <typename T> class p {
+      friend struct p_help;
+    private:
+      void* rc_;
+      T* obj_;
+    public:
+      /** Default constructor. Initiates pointer and refcounter object to zero.
+      */
+      inline p () : rc_(NULL), obj_ (NULL) {}
+      /** Constructor taking ownership of given pointer. Caller looses the ownership of given object.
+      */
+      inline p (T* obj) {
+        if (obj) {
+          rc_ = smartptr_assign (
+            reinterpret_cast<T*>(obj),
+            reinterpret_cast<T*>(NULL));
+          obj_ = obj;
+        } else {
+          rc_ = NULL;
+          obj_ = NULL;
+        }
+      }
+      p (T*& obj) {
+        // This constructor is not ment to compile!
+        // It is here to generate an error when trying to assign a p<T> from
+        // an lvalue. p<T> must only be assigned directly from the return value
+        // of the new() operator.
+        //
+        // Compiler will not complain about the error text below, but will complain
+        // about "ambigous call to overloaded function"...
+        ERROR_CANNOT_ASSIGN_P_FROM_LVALUE(obj);
+      }
+
+      /** Destructor. Decrements reference count and eventually deletes the object.
+      */
+      inline ~p () {
+        if (rc_) smartptr_release (rc_, reinterpret_cast<T*>(NULL));
+      }
+      /** Copy Constructor. Increments reference count since we also own the object now.
+      */
+      inline p (const p& other) : rc_ (other.rc_), obj_ (other.obj_) {
+        if (other.rc_) smartptr_addref (
+          other.rc_,
+          reinterpret_cast<T*>(NULL));
+      }
+      /** Copy Constructor for p of compatible class.
+        Increments reference count since we also own the object now.
+      */
+      template <class DerivedClass> p (const p<DerivedClass>& other) {
+        // If compilation fails here, your assigning a pointer from
+        // a pointer to a class (DerivedClass) that is not a child of T.
+        obj_ = p_help::get_obj(other);
+        rc_ = p_help::get_rc(other);
+        // Check compile-time that T and DerivedClass are compatible
+        smartptr_assert_type_compatibility ((T*)NULL, (T*)NULL, (DerivedClass*)NULL);
+        // Increment refcounter
+        if (rc_) smartptr_addref (rc_, reinterpret_cast<T*>(NULL));
+      }
+      /** Construct from a Collection Item Reference.
+      */
+      template <class DerivedClass> p (const CollItemRef<DerivedClass>& item)
+      {
+        // If compilation fails here, your assigning a pointer from
+        // a pointer to a class (DerivedClass) that is not a child of T.
+        obj_ = p_help::get_obj(item);
+        rc_ = p_help::get_rc (item);
+        // Check compile-time that T and DerivedClass are compatible
+        smartptr_assert_type_compatibility ((T*)NULL, (T*)NULL, (DerivedClass*)NULL);
+        // Increment refcounter
+        if (rc_) smartptr_addref (rc_, reinterpret_cast<T*>(NULL));
+      }
+      /** Assignment operator. Increments reference count since we also own the object now.
+          Decrements refcount of the old object because we don't own that anymore.
+      */
+      inline p& operator = (const p& other) {
+        if (other.rc_) smartptr_addref (other.rc_, reinterpret_cast<T*>(NULL));
+        if (rc_) smartptr_release (rc_, reinterpret_cast<T*>(NULL));
+        rc_ = other.rc_;
+        obj_ = other.obj_;
+        return *this;
+      }
+      /** Assignment operator for p of compatible class.
+        Increments reference count since we also own the object now. Decrements refcount
+        of the old object because we don't own that anymore.
+      */
+      template <class DerivedClass> p& operator = (const p<DerivedClass>& other) {
+        void* other_rc = p_help::get_rc(other);
+        if (other_rc) smartptr_addref (
+            other_rc,
+            reinterpret_cast<DerivedClass*>(NULL));
+        if (rc_) smartptr_release (
+            rc_,
+            reinterpret_cast<T*>(NULL));
+        // Copy reference counter.
+        rc_ = other_rc;
+        // Check compile time that DerivedClass is derived from T
+        obj_ = p_help::get_obj(other);
+        // Check compile-time that T and DerivedClass are compatible
+        smartptr_assert_type_compatibility ((T*)NULL, (T*)NULL, (DerivedClass*)NULL);
+        return *this;
+      }
+      /** Assign from a Collection Item Reference.
+      */
+      template <class DerivedClass> p& operator = (const CollItemRef<DerivedClass>& other) {
+        void* other_rc = p_help::get_rc(other);
+        if (other_rc) smartptr_addref (
+            other_rc,
+            reinterpret_cast<DerivedClass*>(NULL));
+        if (rc_) smartptr_release (
+            rc_,
+            reinterpret_cast<T*>(NULL));
+        // Copy reference counter.
+        rc_ = other_rc;
+        // Check compile time that DerivedClass is derived from T
+        obj_ = p_help::get_obj(other);
+        // Check compile-time that T and DerivedClass are compatible
+        smartptr_assert_type_compatibility ((T*)NULL, (T*)NULL, (DerivedClass*)NULL);
+        return *this;
+      }
+      /** Assignment operator taking ownership of given pointer.
+        Caller looses the ownership of given object. Decrements refcount
+        of the old object because we don't own that anymore.
+      */
+      p& operator = (T* obj) {
+        if (rc_) smartptr_release (rc_, reinterpret_cast<T*>(NULL));
+        if (obj) {
+          rc_ = smartptr_assign (obj, reinterpret_cast<T*>(NULL));
+          obj_ = obj;
+        } else {
+          rc_ = NULL;
+          obj_ = NULL;
+        }
+        return *this;
+      }
+      p& operator = (T*& obj) {
+        // This constructor is not ment to compile!
+        // It is here to generate an error when trying to assign a p<T> from
+        // an lvalue. p<T> must only be assigned directly from the return value
+        // of the new() operator.
+        //
+        // Compiler will not complain about the error text below, but will complain
+        // about "ambigous call to overloaded function"...
+        ERROR_CANNOT_ASSIGN_P_FROM_LVALUE(obj);
+        return *this;
+      }
+      /** Member reach operator.
+      */
+      inline T* operator -> () const {
+        return obj_;
+      }
+      /** Reference reach operator. 
+      */
+      inline T& operator * () const {
+        return *obj_;
+      }
+      /** Equal Operator. Compares the container's pointer value. Used in order to insert the p in std::maps and std::set.
+      */
+      inline bool operator == (const p& other) const {
+        return rc_ == other.rc_;
+      }
+      /** Equal Operator. Compares the pointer value. Used only to compare with NULL.
+      */
+      inline bool operator == (const T* other) const {
+        return obj_ == other;
+      }
+      /** No-Equal Operator. Compares the container's pointer value. Used in order to insert the p in std collections.
+      */
+      bool operator != (const p& other) const {
+        return rc_ != other.rc_;
+      }
+      /** No-Equal Operator. Compares the pointer value. Used mainly to compare with NULL.
+      */
+      bool operator != (const T* other) const {
+        return obj_ != other;
+      }
+      /** Less Operator. Compares the container's pointer value. Used in order to insert the p in std collections.
+      */
+      bool operator < (const p& other) const {
+        return rc_ < other.rc_;
+      }
+    };
+
+    /**
+    */
+    template <typename T> T* getptr (const p<T>& sp) {
+      return p_help::get_obj(sp);
+    }
+
+    /**
+    */
+    template <typename T> p<T> smartify (T* ptr) {
+      p<T> retval;
+      if (ptr) {
+        p_help::set_rc (retval, smartptr_smartify (ptr, (T*) NULL));
+        p_help::set_obj (retval, ptr);
+      }
+      return retval;
+    }
+    template <typename T> p<const T> smartify (const T* ptr) {
+      p<const T> retval;
+      p_help::set_rc (retval, smartptr_smartify (ptr, (const T*) NULL));
+      p_help::set_obj (retval, ptr);
+      return retval;
+    }
+    template <typename T> T* newptr (T* ptr) {
+      return ptr;
+    }
+
+    /* Used to disable warnings waying "statement has no effect" (gcc warning) in casts.
+    */
+    /*template <typename T> inline T* warning_workaround_get_ptr(T* inout) {
+        return inout;
+    }*/
+
+    /** p_cast. static_cast for smart pointers.
+      Usage:
+        p<Base> b;
+        p<Derived> d = p_cast<Derived> (b);
+
+      See also p_dyncast<>
+    */
+    template <typename T, typename Base>
+    inline p<T> p_cast (const p<Base>& pb)
+    {
+      // Check compile-time that T and Base are compatible
+      smartptr_assert_type_compatibility (
+          (Base*)NULL,
+          (Base*)NULL,
+          (T*)NULL); //static_cast<const T*> ((const Base*)(NULL)));
+      p<T> retval;
+      p_help::set_rc (retval, p_help::get_rc (pb));
+      // Checks that the cast is valid as a static_cast:
+      p_help::set_obj (retval, static_cast<T*> (p_help::get_obj (pb)));
+      if (retval != NULL) addref (retval);
+      return retval;
+    }
+    template <typename T, typename Base>
+    inline p<T> p_cast (const CollItemRef<Base>& pb)
+    {
+      // Check compile-time that T and Base are compatible
+      smartptr_assert_type_compatibility (
+          (Base*)NULL,
+          (Base*)NULL,
+          (T*)NULL); //static_cast<const T*> ((const Base*)(NULL)));
+      p<T> retval;
+      p_help::set_rc (retval, p_help::get_rc (pb));
+      // Checks that the cast is valid as a static_cast:
+      p_help::set_obj (retval, static_cast<T*> (p_help::get_obj (pb)));
+      if (retval != NULL) addref (retval);
+      return retval;
+    }
+
+    /** p_dyncast. dynamic_cast<> for smart pointers.
+      Usage:
+        p<Base> b;
+        p<Derived> d = p_dyncast<Derived> (b);
+    */
+    template <typename T, typename Base>
+    inline p<T> p_dyncast (const p<Base>& pb) throw (std::bad_cast)
+    {
+      // Check compile-time that T and Base are compatible
+      smartptr_assert_type_compatibility (
+          (Base*)NULL,
+          (Base*)NULL,
+          (T*)NULL); //static_cast<const T*> ((const Base*)(NULL)));
+      p<T> retval;
+      // Checks that the cast is valid as a dynamic_cast:
+      p_help::set_obj (retval, dynamic_cast<T*> (p_help::get_obj (pb)));
+      p_help::set_rc (retval, p_help::get_rc (pb));
+      if (retval != NULL) addref (retval);
+      return retval;
+    }
+    template <typename T, typename Base>
+    inline p<T> p_dyncast (const CollItemRef<Base>& pb) throw (std::bad_cast)
+    {
+      // Check compile-time that T and Base are compatible
+      smartptr_assert_type_compatibility (
+          (Base*)NULL,
+          (Base*)NULL,
+          (T*)NULL); //static_cast<const T*> ((const Base*)(NULL)));
+      p<T> retval;
+      // Checks that the cast is valid as a dynamic_cast:
+      p_help::set_obj (retval, dynamic_cast<T*> (p_help::get_obj (pb)));
+      p_help::set_rc (retval, p_help::get_rc (pb));
+      if (retval != NULL) addref (retval);
+      return retval;
+    }
+
+    /**
+    */
+    template <typename T> struct void_refcounter : refcounted {
+      T* obj_;
+      void_refcounter (T* obj, long initcount) : refcounted (initcount), obj_ (obj) {}
+      virtual ~void_refcounter () {
+        delete obj_;
+      }
+    };
+    /* Overloaded reference counting functions for p<T>
+    */
+    template <typename T> inline void addref (const p<T>& sp) {
+      smartptr_addref (
+        p_help::get_rc(sp),
+        reinterpret_cast<T*>(NULL));
+    }
+    template <typename T> inline void release (const p<T>& sp) {
+      smartptr_release (
+        p_help::get_rc(sp),
+        reinterpret_cast<T*>(NULL));
+    }
+    template <typename T> inline long getrefcount (const p<T>& sp) {
+      return smartptr_getrefcount (
+        p_help::get_rc(sp),
+        reinterpret_cast<T*>(NULL));
+    }
+
+
+    /* Overloaded smartptr functions for void
+    */
+    template <typename T> inline void* smartptr_assign (T* obj, const void*) {
+      return static_cast<refcounted*> (new void_refcounter<T> (obj, 1));
+    }
+    template <typename T> inline void smartptr_addref (T* rc, const void*) {
+      addref (const_cast<refcounted*> (reinterpret_cast<const refcounted*> (rc)));
+    }
+    template <typename T> inline void smartptr_release (T* rc, const void*) {
+      release (const_cast<refcounted*> (reinterpret_cast<const refcounted*> (rc)));
+    }
+    // void* is compatible with void*
+    template <typename T> inline void smartptr_assert_type_compatibility (
+        const T*, const void*, const void*)
+    {
+    }
+    template <typename T> long smartptr_getrefcount (const T* rc, const void*) {
+      return getrefcount (reinterpret_cast<const refcounted*>(rc));
+    }
+    template <typename T> void* smartptr_smartify (const T* obj, const void*) {
+      // This constructor is not ment to compile!
+      // It is here to generate an error when trying to call smartify() on
+      // an ordinary pointer that is not possible to convert to a smart pointer.
+      // This error occurs when the class does not derive from Interface,
+      // refcounted, IUnknown or any other class that supports it's own
+      // reference counting mechanism.
+      ERROR_TYPE_NOT_POSSIBLE_TO_SMARTIFY(obj);
+      return NULL;
+    }
+
+
+    /* Overloaded smartptr functions for refcounted
+    */
+    template <typename T> void* smartptr_assign (T* obj, const refcounted*) {
+//#ifdef NDEBUG
+      // Release Version. refcounted's default constructor is set to 1
+      // (as an optimization) so we don't need to call addref() on it.
+      return static_cast<refcounted*> (obj);
+/*#else
+      // Debug version. refcounted's default constructor is set to 0 just
+      // so that we can assert here that p<T>::operator = (T*) is used
+      // to assign a newly created instance. If your asserting complains here,
+      // it is because the instance you are trying to assign from has already
+      // been used in another smart pointer. You can do what you want though
+      // by using smartify(p) instead of p as the argument to p<>'s
+      // constructor or assigment operator.
+
+      //assert (getrefcount(obj) == 0); // Direct use of T* instead of
+                                       // smartify(T*). See explanation
+                                       // above.
+      addref (obj);
+      return static_cast<refcounted*> (obj);
+#endif*/
+    }
+    template <typename T> void* smartptr_smartify (const T* obj, const refcounted*) {
+      addref (const_cast<refcounted*> (static_cast<const refcounted*> (obj)));
+      return const_cast<refcounted*> (static_cast<const refcounted*> (obj));
+    }
+  }
+}
+
+/** IUnknown - Makes p<> hold COM objects according to it's standard.
+*/
+struct IUnknown;
+
+namespace ifr {
+  namespace v1 {
+    /* Overloaded smartptr functions for IUnknown
+    */
+    template <typename T> void* smartptr_assign (T* obj, const IUnknown*) {
+      // Assert this is a newly created instance. If your asserting complains here,
+      // you are probably not assigning a newly created instance but an already
+      // existing instance. If so, you should add the function smartify() around
+      // the instance that you are assigning back as a smart pointer.
+      assert (getrefcount(obj) == 1);
+      return static_cast<IUnknown*> (obj);
+    }
+    template <typename T> inline void smartptr_addref_IUnknown (T* obj) {
+      obj->AddRef();
+    };
+    template <typename T> inline void smartptr_addref (T* rc, const IUnknown*) {
+      smartptr_addref_IUnknown (const_cast<IUnknown*> (reinterpret_cast<const IUnknown*> (rc)));
+    }
+    template <typename T> inline void smartptr_release_IUnknown (T* obj) {
+      return obj->Release();
+    };
+    template <typename T> inline void smartptr_release (T* rc, const IUnknown*) {
+      smartptr_release_IUnknown (const_cast<IUnknown*> (reinterpret_cast<const IUnknown*> (rc)));
+    }
+    // IUnknown* is compatible with IUnknown*
+    template <typename T> inline void smartptr_assert_type_compatibility (const T*, const IUnknown*, const IUnknown*) {}
+
+    // IUnknown* is not compatible with void*
+    template <typename T> inline void smartptr_assert_type_compatibility (const T* tmp, const void*, const IUnknown*) {
+      // This overloaded method must not compile.
+      // It is here to generate an error when trying to mix instances of
+      // IUnknown and non-IUnknown. If you get a compilation error here,
+      // you should derive from IUnknown already in given base
+      // class and not only in given subclass. If that is not possible, your
+      // subclass must not derive from IUnknown.
+      ERROR_CANNOT_MIX_IUNKNOWN_WITH_NON_IUNKNOWN(tmp);
+    }
+    template <typename T> inline void smartptr_assert_type_compatibility (const T*, const IUnknown* a, const void* b) {
+      smartptr_assert_type_compatibility (b, b, a);
+    }
+    template <typename T> long smartptr_getrefcount_IUnknown (T* obj) {
+      obj->AddRef();
+      return obj->Release();
+    }
+    template <typename T> long smartptr_getrefcount (const T* rc, const IUnknown*) {
+      return smartptr_getrefcount_IUnknown (const_cast<IUnknown*> (reinterpret_cast<const IUnknown*>(rc)));
+    }
+    template <typename T> void* smartptr_smartify (const T* obj, const IUnknown*) {
+      smartptr_addref_IUnknown (const_cast<IUnknown*> (static_cast<const IUnknown*> (obj)));
+      return const_cast<IUnknown*> (static_cast<const IUnknown*> (obj));
+    }
+  }
+}
+
+
+#endif // IFR_V1_P_HPP_
+

Added: incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/platform.hpp
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/platform.hpp?rev=397654&view=auto
==============================================================================
--- incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/platform.hpp (added)
+++ incubator/activemq/trunk/openwire-cpp/src/main/cpp/ppr/util/ifr/v1/platform.hpp Thu Apr 27 14:59:28 2006
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2005-2006, David Fahlander
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer. Redistributions in binary
+ * form must reproduce the above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or other materials provided
+ * with the distribution. Neither the name of slurk.org nor the names
+ * of its contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef IFR_V1_PLATFORM_HPP_
+#define IFR_V1_PLATFORM_HPP_
+
+// GCC
+#if defined __GNUC__ 
+#ifndef STDCALL
+#define STDCALL       __attribute__ ((stdcall))
+#endif
+// Microsoft Visual C++
+#elif defined _MSC_VER
+#define STDCALL       __stdcall
+// Disable warning C4290: C++ exception specification ignored except to 
+//                        indicate a function is not __declspec(nothrow).
+#pragma warning( disable : 4290) 
+#endif
+
+#if !defined (_WIN64) && defined (WIN64)
+#define _WIN64
+#endif
+
+#if !defined (_WIN32) && defined (WIN32)
+#define _WIN32
+#endif
+
+#if !defined (_WINDOWS) && (defined (_WIN32) || defined (_WIN64))
+#define _WINDOWS
+#endif
+
+#if (defined __GNUC__)
+#define IFR_USE_GNU_ATOMIC
+#include <bits/c++config.h>
+#include <bits/atomicity.h>
+#ifdef __GLIBCXX__
+using __gnu_cxx::__atomic_add;
+using __gnu_cxx::__exchange_and_add;
+#endif
+#elif (!defined __GNUC__) && ( defined _WINDOWS ) // Non-GCC compiler, windows platform:
+#define IFR_USE_INTERLOCKED
+#else // Compiler or processor for which we got no atomic count support
+#define IFR_USE_PTHREAD
+#include <pthread.h>
+#endif
+
+#ifdef IFR_MODULARITY
+// When defined, each DLL/SO-file is reference counted and released
+// first when it does not own any object anymore.
+#define IFR_REFCOUNT_LIBRARIES
+// When defined, all deallocation (delete) will be performed by the same DLL/SO-file
+// that did allocate the object (new).
+#define IFR_DIFFERENT_HEAPS_AWARE
+#endif
+
+#ifdef _WINDOWS
+
+// Defining Critical Section, needed for windows critical section handling.
+// We dont want to include windows.h since it contains TOO much dirt.
+extern "C" {
+  #ifdef __GNUC__
+  // GNU C Compiler
+  struct _CRITICAL_SECTION;
+  typedef struct _CRITICAL_SECTION CRITICAL_SECTION;
+  #else
+  // Other Windows Compilers. Same structure as in windows.h
+  struct _RTL_CRITICAL_SECTION;
+  typedef struct _RTL_CRITICAL_SECTION CRITICAL_SECTION;
+    #ifdef _MSC_VER
+    // Interlocked functions. Only for MSVC Compiler. GCC uses inline assembler.
+    // Todo: Support Borland and other c++ compilers on windows with either inline
+    // assember or using Interlocked functions.
+    long __cdecl _InterlockedIncrement(volatile long*);
+    long __cdecl _InterlockedDecrement(volatile long*);
+    long __cdecl _InterlockedExchangeAdd (volatile long*, long);
+    #pragma intrinsic( _InterlockedIncrement )
+    #pragma intrinsic( _InterlockedDecrement )
+    #pragma intrinsic( _InterlockedExchangeAdd )
+    #endif
+  #endif
+  // Declare CriticalSection functions:
+  __declspec(dllimport) void STDCALL InitializeCriticalSection (CRITICAL_SECTION*);
+  __declspec(dllimport) void STDCALL EnterCriticalSection (CRITICAL_SECTION*);
+  __declspec(dllimport) void STDCALL LeaveCriticalSection (CRITICAL_SECTION*);
+  __declspec(dllimport) void STDCALL DeleteCriticalSection (CRITICAL_SECTION*);
+
+  namespace ifr {
+    namespace v1 {
+      namespace windows {
+        struct CriticalSection {
+          void* DebugInfo;
+          long LockCount;
+          long RecursionCount;
+          void* OwningThread;
+          void* LockSemaphore;
+          unsigned long SpinCount;
+          inline operator CRITICAL_SECTION* () {
+            return reinterpret_cast<CRITICAL_SECTION*>(this);
+          }
+        };
+      }
+    }
+  }
+}
+#endif
+
+#endif // IFR_V1_PLATFORM_HPP_
+

Added: incubator/activemq/trunk/openwire-cpp/src/test/cpp/TestListener.cpp
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/openwire-cpp/src/test/cpp/TestListener.cpp?rev=397654&view=auto
==============================================================================
--- incubator/activemq/trunk/openwire-cpp/src/test/cpp/TestListener.cpp (added)
+++ incubator/activemq/trunk/openwire-cpp/src/test/cpp/TestListener.cpp Thu Apr 27 14:59:28 2006
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2006 The Apache Software Foundation or its licensors, as
+ * applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "TestListener.hpp"
+
+/*
+ * 
+ */
+TestListener::TestListener()
+{
+}
+
+/*
+ * 
+ */
+TestListener::~TestListener()
+{
+}
+
+/*
+ * 
+ */
+void TestListener::onMessage(p<IMessage> message)
+{
+    try
+    {
+        p<IBytesMessage> msg = p_dyncast<IBytesMessage> (message) ;
+
+        if( msg == NULL )
+            cout << "No message received!" << endl ;
+        else
+        {
+            cout << "Received message with ID: " << msg->getJMSMessageID()->c_str() << endl ;
+            cout << "                 boolean: " << (msg->readBoolean() ? "true" : "false") << endl ;
+            cout << "                 integer: " << msg->readInt() << endl ;
+            cout << "                  string: " << msg->readUTF()->c_str() << endl ;
+        }
+    }
+    catch( exception& e )
+    {
+        cout << "OnMessage caught: " << e.what() << endl ;
+    }
+}

Added: incubator/activemq/trunk/openwire-cpp/src/test/cpp/TestListener.hpp
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/openwire-cpp/src/test/cpp/TestListener.hpp?rev=397654&view=auto
==============================================================================
--- incubator/activemq/trunk/openwire-cpp/src/test/cpp/TestListener.hpp (added)
+++ incubator/activemq/trunk/openwire-cpp/src/test/cpp/TestListener.hpp Thu Apr 27 14:59:28 2006
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2006 The Apache Software Foundation or its licensors, as
+ * applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef TestListener_hpp_
+#define TestListener_hpp_
+
+#include <iostream>
+#include <string>
+
+#include "cms/IMessage.hpp"
+#include "cms/IBytesMessage.hpp"
+#include "cms/IMessageListener.hpp"
+#include "ppr/util/ifr/p"
+
+using namespace apache::cms;
+using namespace ifr;
+using namespace std;
+
+/*
+ * 
+ */
+class TestListener : public IMessageListener
+{
+public:
+    TestListener() ;
+    virtual ~TestListener() ;
+
+    virtual void onMessage(p<IMessage> message) ;
+} ;
+
+#endif /*TestListener_hpp_*/



Mime
View raw message