Statistics
| Branch: | Revision:

root / src / scave / scaveutils.h @ e1750c09

History | View | Annotate | Download (2.69 KB)

1
//=========================================================================
2
//  SCAVEUTILS.H - part of
3
//                  OMNeT++/OMNEST
4
//           Discrete System Simulation in C++
5
//
6
//  Author: Tamas Borbely
7
//
8
//=========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16

    
17
#ifndef _SCAVEUTILS_H_
18
#define _SCAVEUTILS_H_
19

    
20
#include <string>
21
#include <set>
22
#include <functional>
23
#include "intxtypes.h"
24
#include "scavedefs.h"
25
#include "timeutil.h"
26
#include "commonutil.h"
27

    
28
NAMESPACE_BEGIN
29

    
30
SCAVE_API bool parseInt(const char *str, int &dest);
31
SCAVE_API bool parseLong(const char *str, long &dest);
32
SCAVE_API bool parseInt64(const char *str, int64 &dest);
33
SCAVE_API bool parseDouble(const char *str, double &dest);
34
SCAVE_API bool parseSimtime(const char *str, simultime_t &dest);
35
SCAVE_API std::string unquoteString(const char *str);
36

    
37
// simple profiling macro
38
// var is a long variable collecting the execution time of stmt in usec
39
#define TIME(var,stmt) { timeval start,end; \
40
                         gettimeofday(&start,NULL); \
41
                         stmt; \
42
                         gettimeofday(&end,NULL); \
43
                         var += timeval_diff_usec(end,start); }
44

    
45
template <class Operation>
46
class FlipArgs
47
    : public std::binary_function<typename Operation::second_argument_type,
48
                                    typename Operation::first_argument_type,
49
                                    typename Operation::result_type>
50
{
51
    public:
52
        typedef typename Operation::second_argument_type first_argument_type;
53
        typedef typename Operation::first_argument_type second_argument_type;
54
        typedef typename Operation::result_type result_type;
55
        FlipArgs(const Operation & _Func) : op(_Func) {};
56
        result_type operator()(const first_argument_type& _Left, const second_argument_type& _Right)
57
        {
58
            return op(_Right, _Left);
59
        }
60
    protected:
61
        Operation op;
62
};
63

    
64
template<class Operation>
65
inline FlipArgs<Operation> flipArgs(const Operation& op)
66
{
67
    return FlipArgs<Operation>(op);
68
}
69

    
70
class StringPool
71
{
72
    private:
73
        std::set<std::string> pool;
74
        const std::string *lastInsertedPtr;
75
    public:
76
        StringPool() : lastInsertedPtr(NULL) {}
77
        const std::string *insert(const std::string& str);
78
        const std::string *find(const std::string& str) const;
79
        void clear() { lastInsertedPtr = NULL; pool.clear(); }
80
};
81

    
82
NAMESPACE_END
83

    
84

    
85
#endif
86