LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_util.h
Go to the documentation of this file.
1 /*
2  LibNRG - Networking for Real-time Games
3 
4  Copyright (C) 2012-2014 Alex Baines <alex@abaines.me.uk>
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute it
12  freely, subject to the following restrictions:
13 
14  1. The origin of this software must not be misrepresented; you must not
15  claim that you wrote the original software. If you use this software
16  in a product, an acknowledgment in the product documentation would be
17  appreciated but is not required.
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20  3. This notice may not be removed or altered from any source distribution.
21 */
25 #ifndef NRG_UTIL_H
26 #define NRG_UTIL_H
27 #include "nrg_core.h"
28 #include <vector>
29 #include <algorithm>
30 #include <functional>
31 #include <memory>
32 #include <utility>
33 
34 namespace nrg {
35 
37 template< class T, class... Args >
38 std::unique_ptr<T> make_unique( Args&&... args ){
39  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
40 }
41 
43 template<class ID>
44 class IDAssigner {
45 public:
46  IDAssigner(const ID& start = 0) : max_id(start){}
47  ID acquire(){
48  if(reusable_ids.empty()){
49  return max_id++;
50  } else {
51  ID id = reusable_ids.back();
52  reusable_ids.pop_back();
53  return id;
54  }
55  }
56  void release(const ID& id){
57  reusable_ids.push_back(id);
58  std::sort(reusable_ids.begin(), reusable_ids.end(), std::greater<ID>());
59  }
60 private:
61  std::vector<ID> reusable_ids;
62  ID max_id;
63 };
64 
66 struct ClientStats {
67 
69  virtual size_t getNumSnapshotStats() const = 0;
70 
72  virtual int getSnapshotStat(size_t index) const = 0;
73 
75  virtual size_t getNumInterpStats() const = 0;
76 
78  virtual float getInterpStat(size_t index) const = 0;
79 
81  virtual uint8_t* toRGBATexture(uint32_t (&tex)[64*64]) const = 0;
82 
83  virtual ~ClientStats(){}
84 };
85 
86 namespace detail {
87 
89 template<size_t N>
90 struct min_sizeof {
91  static const size_t val
92  = (N < 256U) ? 1 // pow(2,8)
93  : (N < 65536U) ? 2 // pow(2,16)
94  : (N < 4294967296U) ? 4 // pow(2,32)
95  : 8
96  ;
97 };
98 
100 template<int N> struct size2type {};
101 template<> struct size2type<1> { typedef uint8_t type;};
102 template<> struct size2type<2> { typedef uint16_t type;};
103 template<> struct size2type<4> { typedef uint32_t type;};
104 template<> struct size2type<8> { typedef uint64_t type;};
105 
106 }
107 
108 }
109 
110 #endif
111 
virtual size_t getNumSnapshotStats() const =0
Get the number of available snapshot statistics.
Client statistics interface.
Definition: nrg_util.h:66
Definition: nrg_util.h:44
virtual uint8_t * toRGBATexture(uint32_t(&tex)[64 *64]) const =0
Create a Lagometer texture from the stats.
virtual size_t getNumInterpStats() const =0
Get the number of available interpolation statistics.
virtual int getSnapshotStat(size_t index) const =0
Get a snapshot statistic which shows the latency at which snapshots were received, -1 means it was dropped.
Common defines and includes used by all the other nrg header files.
virtual float getInterpStat(size_t index) const =0
Get a statistic showing how far between snapshots the client is interpolating, >1 means it is having ...