// -*- mode:c++ -*- // // Header file int_wrapper.hpp // // Copyright (c) 2003 Raoul M. Gough // // Use, modification and distribution is subject to the Boost Software // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy // at http://www.boost.org/LICENSE_1_0.txt) // // History // ======= // 2003/ 9/10 rmg File creation as IntWrapper.hpp // 2003/10/15 rmg Renamed int_wrapper.hpp (Boost naming convention) // // $Id$ // #ifndef int_wrapper_rmg_20030910_included #define int_wrapper_rmg_20030910_included #include #include #include struct int_wrapper { static bool our_trace_flag; static unsigned our_object_counter; int m_obj_number; int m_int; inline int_wrapper (); inline explicit int_wrapper (int i); inline int_wrapper (int_wrapper const &other); inline int_wrapper &operator= (int_wrapper const &other); inline ~int_wrapper (); inline void increment (int); inline std::string repr () const; inline static void setTrace (bool onoff); }; inline bool operator== (int_wrapper const &lhs, int_wrapper const &rhs); inline bool operator!= (int_wrapper const &lhs, int_wrapper const &rhs); inline bool operator< (int_wrapper const &lhs, int_wrapper const &rhs); inline int compare (int_wrapper const &lhs, int_wrapper const &rhs); // // This is ugly. Put all of this stuff inline to avoid needing a // separate object file for testing (don't know how to do this in // Jam). Client module will have to define the following variables: // // bool int_wrapper::our_trace_flag = true; // unsigned int_wrapper::our_object_counter = 0; // int_wrapper::int_wrapper () : m_obj_number (our_object_counter++) , m_int (0) { if (our_trace_flag) { printf ("int_wrapper %u ()\n", m_obj_number); } } int_wrapper::int_wrapper (int i) : m_obj_number (our_object_counter++) , m_int (i) { if (our_trace_flag) { printf ("int_wrapper %u (%d)\n" , m_obj_number , m_int); } } int_wrapper::int_wrapper (int_wrapper const &other) : m_obj_number (our_object_counter++) , m_int (other.m_int) { if (our_trace_flag) { printf ("int_wrapper %u (int_wrapper %u)\n" , m_obj_number , other.m_obj_number); } } int_wrapper &int_wrapper::operator= (int_wrapper const &other) { if (our_trace_flag) { printf ("int_wrapper %u = int_wrapper %u\n" , m_obj_number , other.m_obj_number); } m_int = other.m_int; return *this; } int_wrapper::~int_wrapper () { if (our_trace_flag) { printf ("~int_wrapper %u\n", m_obj_number); } m_int = 0xbaaaaaad; } void int_wrapper::increment(int change) { if (our_trace_flag) { printf ("int_wrapper %u::increment(%d)\n", m_obj_number, change); } m_int += change; } std::string int_wrapper::repr () const { std::stringstream temp; temp << m_int; return temp.str(); } void int_wrapper::setTrace (bool onoff) { our_trace_flag = onoff; } bool operator== (int_wrapper const &lhs, int_wrapper const &rhs) { return lhs.m_int == rhs.m_int; } bool operator!= (int_wrapper const &lhs, int_wrapper const &rhs) { return lhs.m_int != rhs.m_int; } bool operator< (int_wrapper const &lhs, int_wrapper const &rhs) { return lhs.m_int < rhs.m_int; } int compare (int_wrapper const &lhs, int_wrapper const &rhs) { if (lhs < rhs) { return -1; } else if (rhs < lhs) { return 1; } else { return 0; } } #endif // int_wrapper_rmg_20030910_included