// Bug 547306 - Hard to get a RefPtr<> to this.

// Test case, testing the proposed constructor
// Glib::RefPtr::RefPtr(T_CppObject* pCppObject, bool take_ref);

#include <iostream>
#include <glibmm/refptr.h>

class ARefCountClass
{
public:
  static Glib::RefPtr<ARefCountClass> create()
  {
    return Glib::RefPtr<ARefCountClass>(new ARefCountClass());
  }

  void do_something();
  void do_something() const;

  void print()
  {
    std::cout << "ARefCountClass::print()" << std::endl;
  }

  void print() const
  {
    std::cout << "ARefCountClass::print() const" << std::endl;
  }

  void reference() const
  {
    m_ref_count++;
  }
  void unreference() const
  {
    if (m_ref_count <= 0)
      std::cout << "unreference() called when m_ref_count <= 0!" << std::endl;
    if (--m_ref_count == 0)
      delete this;
  }

  ~ARefCountClass()
  {
    std::cout << "Destructing ARefCountClass" << std::endl;
  }

protected:
  ARefCountClass()
  : m_ref_count(1)
  {
    std::cout << "Constructing ARefCountClass" << std::endl;
  }

private:
  mutable int m_ref_count;
};

class AClass
{
public:
  AClass() {}

  void do_something(const Glib::RefPtr<ARefCountClass>& p)
  {
    p->print();
  }

  void do_something_const(const Glib::RefPtr<const ARefCountClass>& p)
  {
    p->print();
  }
};

void ARefCountClass::do_something()
{
  AClass obj;
  obj.do_something(Glib::RefPtr<ARefCountClass>(this, true));
}

void ARefCountClass::do_something() const
{
  AClass obj;
  obj.do_something_const(Glib::RefPtr<const ARefCountClass>(this, true));
}

int main()
{
  Glib::RefPtr<ARefCountClass> obj = ARefCountClass::create();
  Glib::RefPtr<const ARefCountClass> obj_const = ARefCountClass::create();

  obj->do_something();
  obj_const->do_something();
  
  return 0;
}
