Doxygen changes
[ACE_TAO.git] / ACE / tests / Dynamic_Test.cpp
blob8e811e98243664fe519961c00d3ee8f0d59ee011
2 //=============================================================================
3 /**
4 * @file Dynamic_Test.cpp
6 * This tests the ACE_Dynamic class
8 * @author Johnny Willemsen <jwillemsen@remedy.nl>
9 */
10 //=============================================================================
13 #include "test_config.h"
14 #include "ace/OS_NS_string.h"
15 #include "ace/Dynamic.h"
16 #include "ace/OS_Memory.h"
20 class A
22 public:
23 A ();
25 void *operator new (size_t n);
27 #if defined (ACE_HAS_NEW_NOTHROW)
28 void *operator new (size_t n, const ACE_nothrow_t&) throw();
29 #if !defined (ACE_LACKS_PLACEMENT_OPERATOR_DELETE)
30 void operator delete (void *p, const ACE_nothrow_t&) throw ();
31 #endif /* ACE_LACKS_PLACEMENT_OPERATOR_DELETE */
32 #endif
34 void * operator new (size_t n, void *p);
36 void operator delete (void *);
38 #if !defined (ACE_LACKS_PLACEMENT_OPERATOR_DELETE)
39 void operator delete (void *, void *);
40 #endif /* ACE_LACKS_PLACEMENT_OPERATOR_DELETE */
42 /// Have we been dynamically created?
43 bool dynamic_;
46 void*
47 A::operator new (size_t n)
49 ACE_Dynamic *const dynamic_instance = ACE_Dynamic::instance ();
51 if (dynamic_instance == 0)
53 // If this ACE_TEST_ASSERT fails, it may be due to running of out TSS
54 // keys. Try using ACE_HAS_TSS_EMULATION, or increasing
55 // ACE_DEFAULT_THREAD_KEYS if already using TSS emulation.
56 ACE_TEST_ASSERT (dynamic_instance != 0);
58 ACE_throw_bad_alloc;
60 else
62 // Allocate the memory and store it (usually in thread-specific
63 // storage, depending on config flags).
64 dynamic_instance->set ();
66 return ::new char[n];
70 #if defined (ACE_HAS_NEW_NOTHROW)
71 void*
72 A::operator new (size_t n, const ACE_nothrow_t&) throw()
74 ACE_Dynamic *const dynamic_instance = ACE_Dynamic::instance ();
76 if (dynamic_instance == 0)
78 // If this ACE_TEST_ASSERT fails, it may be due to running of out TSS
79 // keys. Try using ACE_HAS_TSS_EMULATION, or increasing
80 // ACE_DEFAULT_THREAD_KEYS if already using TSS emulation.
81 ACE_TEST_ASSERT (dynamic_instance != 0);
83 return 0;
85 else
87 // Allocate the memory and store it (usually in thread-specific
88 // storage, depending on config flags).
89 dynamic_instance->set ();
91 return ::new(ACE_nothrow) char[n];
95 #if !defined (ACE_LACKS_PLACEMENT_OPERATOR_DELETE)
96 void
97 A::operator delete (void *p, const ACE_nothrow_t&) throw()
99 ::delete [] static_cast <char *> (p);
101 #endif /* ACE_LACKS_PLACEMENT_OPERATOR_DELETE */
103 #endif /* ACE_HAS_NEW_NOTHROW */
105 void
106 A::operator delete (void *obj)
108 ::delete [] static_cast <char *> (obj);
111 A::A()
113 this->dynamic_ = ACE_Dynamic::instance ()->is_dynamic ();
115 if (this->dynamic_)
116 // Make sure to reset the flag.
117 ACE_Dynamic::instance ()->reset ();
121 run_main (int, ACE_TCHAR *[])
123 ACE_START_TEST (ACE_TEXT ("Dynamic_Test"));
124 A from_stack;
125 A* heap = 0;
126 ACE_NEW_RETURN (heap, A, 1);
127 if (from_stack.dynamic_)
129 ACE_ERROR_RETURN ((LM_ERROR,
130 ACE_TEXT ("dynamic_ is true for an object on the stack\n")),
134 if (!heap->dynamic_)
136 ACE_ERROR_RETURN ((LM_ERROR,
137 ACE_TEXT ("dynamic_ is false for an object from the heap\n")),
141 delete heap;
142 ACE_END_TEST;
143 return 0;