Macros.h
1 /**************************************************************************************
2 Copyright 2015 Applied Research Associates, Inc.
3 Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 this file except in compliance with the License. You may obtain a copy of the License
5 at:
6 http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software distributed under
8 the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9 CONDITIONS OF ANY KIND, either express or implied. See the License for the
10 specific language governing permissions and limitations under the License.
11 **************************************************************************************/
12 
13 #pragma once
14 #include <map>
15 #include <vector>
16 #include <algorithm>
17 
18 namespace biogears {
19 #define BLIM(input, min, max) (input = (input > max) ? max : ((input < min) ? min : input))
20 #define LLIM(input, min) (input = (input < min) ? min : input)
21 #define ULIM(input, max) (input = (input > max) ? max : input)
22 #define LIMIT(A, B, C) (((A) < (B)) ? (B) : (((A) > (C)) ? (C) : (A)))
23 
24 template <class _InIt>
25 inline size_t _Hash_value(_InIt _Begin, _InIt _End)
26 { // hash range of elements
27  size_t _Val = 2166136261U;
28 
29  while (_Begin != _End)
30  _Val = 16777619U * _Val ^ (size_t)*_Begin++;
31  return (_Val);
32 }
33 
34 #define DEFINE_STATIC_STRING(name) static constexpr char const* name = #name;
35 #define DEFINE_STATIC_STRING_EX(name, value) static constexpr char const* name = #value;
36 
37 #define SAFE_DELETE(obj) \
38  { \
39  delete obj; \
40  obj = nullptr; \
41  }
42 
43 #define SAFE_DELETE_ARRAY(ary) \
44  { \
45  delete[] ary; \
46  *ary = nullptr; \
47  }
48 
49 #define INVALIDATE_PROPERTY(name) \
50  if (name != nullptr) \
51  name->Invalidate();
52 
53 #define COPY_PROPERTY(name) \
54  if (from.Has##name()) \
55  Get##name().Set(*from.m_##name);
56 
57 #define MERGE_CHILD(name) \
58  if (from.Has##name()) \
59  Get##name().Merge(*from.m_##name);
60 
61 #define CDM_COPY(from, to) \
62  { \
63  auto* bind = from->Unload(); \
64  to->Load(*bind); \
65  delete bind; \
66  }
67 
68 template <class T>
69 inline void Copy(const std::vector<T*>& from, std::vector<T*>& to)
70 {
71  to.clear();
72  to.resize(from.size());
73  std::copy(from.begin(), from.end(), to.begin());
74 }
75 
76 template <class T>
77 inline void Copy(const std::vector<T>& from, std::vector<T>& to)
78 {
79  to.clear();
80  to.resize(from.size());
81  std::copy(from.begin(), from.end(), to.begin());
82 }
83 
84 template <class T>
85 inline bool Contains(const std::vector<T*>& v, T& item)
86 {
87  return std::find(v.begin(), v.end(), &item) != v.end();
88 }
89 
90 template <class T>
91 inline void Remove(std::vector<T*>& v, T* item)
92 {
93  v.erase(std::remove(v.begin(), v.end(), item), v.end());
94 }
95 
96 template <class T>
97 inline void Copy(const std::vector<T*>& from, std::vector<const T*>& to)
98 {
99  to.clear();
100  for (auto i : from)
101  to.push_back(i);
102 }
103 
104 template <class T>
105 inline void DELETE_VECTOR(std::vector<T>& vec)
106 {
107  for (unsigned int i = 0; i < vec.size(); i++) {
108  delete vec.at(i);
109  }
110  vec.clear();
111 }
112 //This will delete all of the second items in a map and then clear the map to destroy all dangling pointers
113 template <class T, class K>
114 inline void DELETE_MAP_SECOND(std::map<T, K>& map)
115 {
116  for (typename std::map<T, K>::iterator it = map.begin(); it != map.end(); ++it) {
117  delete it->second;
118  }
119  map.clear();
120 }
121 
122 template <typename valueType>
123 bool SameSign(valueType x, valueType y)
124 {
125  return (x >= 0) ^ (y < 0);
126 }
127 }
void Copy(const std::vector< T * > &from, std::vector< T * > &to)
Definition: Macros.h:69
void DELETE_VECTOR(std::vector< T > &vec)
Definition: Macros.h:105
bool SameSign(valueType x, valueType y)
Definition: Macros.h:123
void Remove(std::vector< T * > &v, T *item)
Definition: Macros.h:91
size_t _Hash_value(_InIt _Begin, _InIt _End)
Definition: Macros.h:25
void DELETE_MAP_SECOND(std::map< T, K > &map)
Definition: Macros.h:114
bool Contains(const std::vector< T * > &v, T &item)
Definition: Macros.h:85
Definition: SEElectricalCircuit.h:18