Macros.h
1/* Distributed under the Apache License, Version 2.0.
2 See accompanying NOTICE file for details.*/
3
4#pragma once
5
6#define MIN(A,B) (((A) < (B)) ? (A) : (B))
7#define MAX(A,B) (((A) > (B)) ? (A) : (B))
8#define BLIM(input,min,max) (input = (input>max)?max:((input<min)?min:input))
9#define LLIM(input,min) (input = (input<min)?min:input)
10#define ULIM(input,max) (input = (input>max)?max:input)
11#define LIMIT(A,B,C) (((A) < (B)) ? (B) : (((A) > (C)) ? (C) : (A)))
12
13template <class _InIt>
14inline size_t _Hash_value(_InIt _Begin, _InIt _End)
15{ // hash range of elements
16 size_t _Val = 2166136261U;
17
18 while (_Begin != _End)
19 _Val = 16777619U * _Val ^ (size_t)*_Begin++;
20 return (_Val);
21}
22
23/* The following warnings have been disabled while building the protobuf libraries and compiler. You may have to disable some of them in your own project as well, or live with them.
24 C4018 - 'expression' : signed/unsigned mismatch
25 C4146 - unary minus operator applied to unsigned type, result still unsigned
26 C4244 - Conversion from 'type1' to 'type2', possible loss of data.
27 C4251 - 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
28 C4267 - Conversion from 'size_t' to 'type', possible loss of data.
29 C4305 - 'identifier' : truncation from 'type1' to 'type2'
30 C4355 - 'this' : used in base member initializer list
31 C4800 - 'type' : forcing value to bool 'true' or 'false' (performance warning)
32 C4996 - 'function': was declared deprecated
33*/
34
35#define DEFINE_STATIC_STRING(name) static constexpr char const* name = #name;
36#define DEFINE_STATIC_STRING_EX(name,value) static constexpr char const* name = #value;
37
38#define SAFE_DELETE(obj) {delete obj; obj=nullptr;}
39
40#define SAFE_DELETE_ARRAY(ary) {delete [] ary; *ary=nullptr;}
41
42#define INVALIDATE_PROPERTY(name) \
43 if(name!=nullptr) \
44 name->Invalidate();
45
46#define FORCE_INVALIDATE_PROPERTY(name) \
47 if(name!=nullptr) \
48 name->ForceInvalidate();
49
50#define ZERO_SCALAR(name) \
51 if(name!=nullptr && name->IsValid()) \
52 name->SetValue(0);
53
54#define ZERO_UNIT_SCALAR(name) \
55 if(name!=nullptr && name->IsValid()) \
56 name->SetValue(0,*name->GetUnit());
57
58#define COPY_PROPERTY(name) \
59 if(from.Has##name()) \
60 Get##name().Set(*from.m_##name);
61
62#define MERGE_CHILD_SUBS(name, subMgr) \
63 if(from.Has##name()) \
64 Get##name().Merge(*from.m_##name, subMgr);
65
66#define MERGE_CHILD(name) \
67 if(from.Has##name()) \
68 Get##name().Merge(*from.m_##name);
69
70#define CDM_COPY(clazz, from, to) \
71 { \
72 auto* bind = clazz ::Unload(*from); \
73 clazz ::Load(*bind,*to); \
74 delete bind; \
75 }
76
77template<class T>
78inline void Copy(const std::vector<T*>& from, std::vector<T*>& to)
79{
80 to.clear();
81 to.resize(from.size());
82 std::copy(from.begin(), from.end(), to.begin());
83}
84
85template<class T>
86inline void Copy(const std::vector<T>& from, std::vector<T>& to)
87{
88 to.clear();
89 to.resize(from.size());
90 std::copy(from.begin(), from.end(), to.begin());
91}
92
93template<class T>
94inline bool Contains(const std::vector<T*>& v, T& item)
95{
96 return std::find(v.begin(), v.end(), &item) != v.end();
97}
98
99template<class T>
100inline void Remove(std::vector<T*>& v, T* item)
101{
102 v.erase(std::remove(v.begin(), v.end(), item), v.end());
103}
104
105template<class T>
106inline void Copy(const std::vector<T*>& from, std::vector<const T*>& to)
107{
108 to.clear();
109 for (auto i : from)
110 to.push_back(i);
111}
112
113template<class T>
114inline void DELETE_VECTOR(std::vector<T>& vec)
115{
116 for(unsigned int i=0; i<vec.size(); i++)
117 {
118 delete vec.at(i);
119 }
120 vec.clear();
121}
122//This will delete all of the second items in a map and then clear the map to destroy all dangling pointers
123template<class T, class K>
124inline void DELETE_MAP_SECOND(std::map<T,K>& map)
125{
126 for(typename std::map<T,K>::iterator it=map.begin(); it != map.end(); ++it)
127 {
128 delete it->second;
129 }
130 map.clear();
131}
132
133template <typename valueType>
134bool SameSign(valueType x, valueType y)
135{
136 return (x >= 0) ^ (y < 0);
137}

Distributed under the Apache License, Version 2.0.

See accompanying NOTICE file for details.