09236091f4df14d579c65e343b5c0647a337dc82
[framework/web/wrt-commons.git] / modules / core / include / dpl / foreach.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        foreach.h
18  * @author      Bartosz Janiak (b.janiak@samsung.com)
19  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
20  * @version     1.0
21  * @brief       This file is the implementation file of foreach macro for stl containers
22  */
23 #ifndef DPL_FOREACH_H
24 #define DPL_FOREACH_H
25
26 #include <dpl/preprocessor.h>
27
28 namespace DPL
29 {
30 namespace Private
31 {
32
33 /*
34  * Used to detect type of valid reference to value object.
35  */
36 template <typename T>
37 T& ValueReference(T& t)
38 {
39     return(t);
40 }
41
42 template <typename T>
43 const T& ValueReference(const T& t)
44 {
45     return(t);
46 }
47
48
49 } //Private
50 } //DPL
51
52
53 #define DPL_FOREACH_IMPL(temporaryName, iterator, container)            \
54     __typeof__ (DPL::Private::ValueReference((container))) &            \
55     temporaryName = (container);                                        \
56     for (__typeof__ (temporaryName.begin()) iterator =                  \
57              temporaryName.begin();                                     \
58          (iterator) != temporaryName.end(); ++iterator)
59
60 #define FOREACH(iterator, container)                                    \
61     DPL_FOREACH_IMPL(                                                   \
62         DPL_MACRO_CONCAT(foreachContainerReference, __COUNTER__),       \
63                          iterator,                                      \
64                          container)
65
66 #endif // DPL_FOREACH_H