Ophidian
 All Classes Namespaces Functions
entity.h
1 /*
2  * Copyright 2016 Ophidian
3 Licensed to the Apache Software Foundation (ASF) under one
4 or more contributor license agreements. See the NOTICE file
5 distributed with this work for additional information
6 regarding copyright ownership. The ASF licenses this file
7 to you under the Apache License, Version 2.0 (the
8 "License"); you may not use this file except in compliance
9 with the License. You may obtain a copy of the License at
10 
11  http://www.apache.org/licenses/LICENSE-2.0
12 
13 Unless required by applicable law or agreed to in writing,
14 software distributed under the License is distributed on an
15 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 KIND, either express or implied. See the License for the
17 specific language governing permissions and limitations
18 under the License.
19  */
20 
21 #ifndef OPHIDIAN_SRC_ENTITY_SYSTEM_ENTITY_H
22 #define OPHIDIAN_SRC_ENTITY_SYSTEM_ENTITY_H
23 
24 #include <limits>
25 #include <cstdint>
26 #include <boost/serialization/strong_typedef.hpp>
27 
28 namespace ophidian{
29 namespace entity_system{
30 
31 //I adapted the boost strong typed to ensure a default initialization with std::numeric_limits<T>::max()
32 #define ENTITY_SYSTEM_STRONG_TYPEDEF(T, D) \
33 struct D \
34  : boost::totally_ordered1< D \
35  , boost::totally_ordered2< D, T \
36  > > \
37 { \
38  T t; \
39  explicit D(const T t_) : t(t_) {}; \
40  D(): t(std::numeric_limits<T>::max()) {}; \
41  D(const D & t_) : t(t_.t){} \
42  D & operator=(const D & rhs) { t = rhs.t; return *this;} \
43  D & operator=(const T & rhs) { t = rhs; return *this;} \
44  operator const T & () const {return t; } \
45  operator T & () { return t; } \
46  bool operator==(const D & rhs) const { return t == rhs.t; } \
47  bool operator<(const D & rhs) const { return t < rhs.t; } \
48 };
49 
50 ENTITY_SYSTEM_STRONG_TYPEDEF(uint32_t, entity)
51 ENTITY_SYSTEM_STRONG_TYPEDEF(uint32_t, entity_index)
52 
53 static const entity_index invalid_entity_index = static_cast<entity_index>(std::numeric_limits<uint32_t>::max());
54 static const entity invalid_entity = static_cast<entity>(std::numeric_limits<uint32_t>::max());
55 
56 } /* namespace entity system */
57 } /* namespace ophidian */
58 
59 //adding to std namespace hash operators for entity and pair of entities
60 namespace std
61 {
62 template<> struct hash<ophidian::entity_system::entity>
63 {
64  std::size_t operator()(const ophidian::entity_system::entity & e) const{
65  return std::hash<std::uint32_t>()(e);
66  }
67 };
68 
69 template<> struct hash<std::pair<ophidian::entity_system::entity,ophidian::entity_system::entity>>
70 {
71  std::size_t operator () (const std::pair<ophidian::entity_system::entity,ophidian::entity_system::entity> &p) const {
72  return std::hash<uint32_t>{}(p.first) ^ std::hash<uint32_t>{}(p.second);
73 }
74 };
75 }
76 
77 #endif //OPHIDIAN_SRC_ENTITY_SYSTEM_ENTITY_H