Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #ifndef INCLUDED_OUTPUT_WITH_DEPTH
21 : : #define INCLUDED_OUTPUT_WITH_DEPTH
22 : :
23 : : #include <vector>
24 : : #include <iostream>
25 : :
26 : : namespace writerfilter
27 : : {
28 : :
29 : : using namespace ::std;
30 : :
31 : : template <typename T>
32 : : class OutputWithDepth
33 : : {
34 : : typedef ::std::vector<T> Group_t;
35 : : Group_t mGroup;
36 : :
37 : : unsigned int mnCurrentDepth;
38 : : unsigned int mnGroupDepth;
39 : :
40 : : T mOpenTag;
41 : : T mCloseTag;
42 : :
43 : : protected:
44 : : virtual void output(const T & aItem) const = 0;
45 : : void outputGroup();
46 : : void finalize();
47 : :
48 : : public:
49 : : OutputWithDepth(const T & aOpenTag, const T & aCloseTag);
50 : : virtual ~OutputWithDepth();
51 : :
52 : : void openGroup();
53 : : void closeGroup();
54 : : void addItem(const T & aItem);
55 : : void setDepth(unsigned int nDepth);
56 : : };
57 : :
58 : : template <typename T>
59 : 64 : OutputWithDepth<T>::OutputWithDepth(const T & aOpenTag, const T & aEndTag)
60 [ + - ][ + - ]: 64 : : mOpenTag(aOpenTag), mCloseTag(aEndTag)
61 : : {
62 : 64 : }
63 : :
64 : : template <typename T>
65 : 64 : OutputWithDepth<T>::~OutputWithDepth()
66 : : {
67 [ - + ]: 64 : }
68 : :
69 : : template <typename T>
70 : 32 : void OutputWithDepth<T>::finalize()
71 : : {
72 : 32 : outputGroup();
73 : 32 : }
74 : :
75 : : template <typename T>
76 : 0 : void OutputWithDepth<T>::openGroup()
77 : : {
78 : 0 : outputGroup();
79 : 0 : mnGroupDepth = 0;
80 : 0 : }
81 : :
82 : : template <typename T>
83 : 0 : void OutputWithDepth<T>::closeGroup()
84 : : {
85 [ # # ]: 0 : if (mnGroupDepth > mnCurrentDepth)
86 [ # # ]: 0 : for (unsigned int i = 0; i < mnGroupDepth - mnCurrentDepth; ++i)
87 : 0 : output(mOpenTag);
88 [ # # ]: 0 : else if (mnGroupDepth < mnCurrentDepth)
89 [ # # ]: 0 : for (unsigned int i = 0; i < mnCurrentDepth - mnGroupDepth; ++i)
90 : 0 : output(mCloseTag);
91 : :
92 : 0 : outputGroup();
93 : :
94 : 0 : mnCurrentDepth = mnGroupDepth;
95 : 0 : }
96 : :
97 : : template <typename T>
98 : 0 : void OutputWithDepth<T>::addItem(const T & aItem)
99 : : {
100 : 0 : mGroup.push_back(aItem);
101 : 0 : }
102 : :
103 : : template <typename T>
104 : 0 : void OutputWithDepth<T>::setDepth(unsigned int nDepth)
105 : : {
106 : 0 : mnGroupDepth = nDepth;
107 : 0 : }
108 : :
109 : : template <typename T>
110 : 64 : void OutputWithDepth<T>::outputGroup()
111 : : {
112 : 64 : typename Group_t::iterator aItEnd = mGroup.end();
113 : :
114 [ # # ][ + - ]: 64 : for (typename Group_t::iterator aIt = mGroup.begin(); aIt != aItEnd; aIt++)
[ - + ]
115 : : {
116 [ # # ][ # # ]: 0 : output(*aIt);
117 : : }
118 : :
119 : 64 : mGroup.clear();
120 : 64 : }
121 : : }
122 : : #endif // INCLUDED_OUTPUT_WITH_DEPTH
123 : :
124 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|