0026922: Huge performance issue writing data to the output stream
[occt.git] / src / QANCollection / QANCollection_Alloc.cxx
CommitLineData
ed9161a4
RL
1// Created on: 2004-03-05
2// Created by: Mikhail KUZMITCHEV
973c2be1 3// Copyright (c) 2004-2014 OPEN CASCADE SAS
ed9161a4 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
ed9161a4 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
ed9161a4 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
ed9161a4
RL
15
16#include <QANCollection.hxx>
17#include <Draw_Interpretor.hxx>
18
19#include <NCollection_StdAllocator.hxx>
20#include <NCollection_IncAllocator.hxx>
8b381bc3 21#include <Standard_Assert.hxx>
22
ed9161a4
RL
23#include <list>
24#include <vector>
25
26//=======================================================================
27//function : QANColStdAllocator1
16347bb8 28//purpose :
ed9161a4
RL
29//=======================================================================
30static Standard_Integer QANColStdAllocator1(Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
31{
32 if ( argc != 1) {
33 di << "Usage : " << argv[0] << "\n";
34 return 1;
35 }
36
37 //type definitions
857ffd5e 38 typedef Handle(Standard_Transient) elem_type;
ed9161a4 39 typedef NCollection_StdAllocator<elem_type> allocator_type;
8b381bc3 40 Standard_STATIC_ASSERT (sizeof (allocator_type::value_type) == sizeof (elem_type));
41 Standard_STATIC_ASSERT (sizeof (allocator_type::pointer) == sizeof (void*));
42 Standard_STATIC_ASSERT (sizeof (allocator_type::const_pointer) == sizeof (void*));
ed9161a4
RL
43
44 elem_type aDummy;
45 allocator_type::reference aRef = aDummy;
105aae76 46 (void)aRef; // avoid compiler warning on unused
ed9161a4 47 allocator_type::const_reference aConstRef = aDummy;
105aae76 48 (void)aConstRef; // avoid compiler warning on unused
8b381bc3 49 Standard_STATIC_ASSERT (sizeof (allocator_type::size_type) == sizeof (size_t));
50 Standard_STATIC_ASSERT (sizeof (allocator_type::difference_type) == sizeof (ptrdiff_t));
ed9161a4
RL
51
52 typedef int other_elem_type;
8b381bc3 53 Standard_STATIC_ASSERT (sizeof (allocator_type::rebind<other_elem_type>::other::value_type) == sizeof (other_elem_type));
ed9161a4
RL
54
55 return 0;
56}
57
58//=======================================================================
59//function : QANColStdAllocator2
16347bb8 60//purpose :
ed9161a4
RL
61//=======================================================================
62static Standard_Integer QANColStdAllocator2(Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
63{
64 if ( argc != 1) {
65 di << "Usage : " << argv[0] << "\n";
66 return 1;
67 }
68
69 //create incremental allocator outside the scope of objects it will manage
70 Handle(NCollection_IncAllocator) anIncAlloc = new NCollection_IncAllocator();
71
72 {
73 //typed allocator
74 NCollection_StdAllocator<int> aSAlloc (anIncAlloc);
75 std::list<int, NCollection_StdAllocator<int> > aL (aSAlloc);
76 aL.push_back (2);
77 if ( aL.size() == size_t (1) ) {
78 di << "Test1 : OK\n";
79 } else {
80 di << "Test1 : Error\n";
81 }
82
83 //type cast
84 NCollection_StdAllocator<char> aCAlloc;
85 std::vector<int, NCollection_StdAllocator<int> > aV (aCAlloc);
86 aV.push_back (1);
87 if ( aV.size() == size_t (1) ) {
88 di << "Test2 : OK\n";
89 } else {
90 di << "Test2 : Error\n";
91 }
92
93 //using void-specialization allocator
16347bb8 94 NCollection_StdAllocator<void> aVAlloc;
95 std::vector<int, NCollection_StdAllocator<int> > aV2 (aVAlloc);
96
ed9161a4
RL
97 aV2.resize (10);
98 aV2.push_back (-1);
99 if ( aV2.size() == size_t (11) ) {
100 di << "Test3 : OK\n";
101 } else {
102 di << "Test3 : Error\n";
103 }
104
105 //equality of allocators
106 if ( aSAlloc != aCAlloc ) {
107 di << "Test4 : OK\n";
108 } else {
109 di << "Test4 : Error\n";
110 }
111 NCollection_StdAllocator<int> anIAlloc (anIncAlloc);
112 if ( aSAlloc == anIAlloc ) {
113 di << "Test5 : OK\n";
114 } else {
115 di << "Test5 : Error\n";
116 }
117
118 }
119
120 return 0;
121}
122
3a01a933 123void QANCollection::CommandsAlloc(Draw_Interpretor& theCommands) {
ed9161a4
RL
124 const char *group = "QANCollection";
125
16347bb8 126 theCommands.Add("QANColStdAllocator1", "QANColStdAllocator1", __FILE__, QANColStdAllocator1, group);
127 theCommands.Add("QANColStdAllocator2", "QANColStdAllocator2", __FILE__, QANColStdAllocator2, group);
ed9161a4
RL
128
129 return;
130}