Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
crandom.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef CRANDOM_H
6 #define CRANDOM_H 1
7 
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <inttypes.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <assert.h>
16 
17 #include <limits>
18 
19 #include "rofl/common/croflexception.h"
20 #include "rofl/common/cmemory.h"
21 
22 namespace rofl
23 {
24 
25 class eRandomBase : public eMemBase {}; // error base class crandom
26 class eRandomOpenFailed : public eRandomBase {}; // open system-call failed
27 class eRandomReadFailed : public eRandomBase {}; // read system-call failed
28 
29 class crandom : public cmemory {
30 #define DEV_URANDOM "/dev/urandom"
31 public:
32 
36  static double
38 
39  // constructor with default random number length of 4 bytes
40  crandom(size_t vallen = sizeof(uint32_t));
41  // destructor
42  virtual
43  ~crandom();
44  // copy constructor
45  crandom(crandom &r)
46  {
47  *this = r;
48  };
49  // assignment operator
50  crandom&
51  operator=(const crandom &r)
52  {
53  if (this == &r)
54  return *this;
56  return *this;
57  };
58 
61  crandom& rand(size_t length);
64  size_t randlen();
67  uint8_t uint8();
70  uint16_t uint16();
73  uint32_t uint32();
76  uint64_t uint64();
77 
78 public:
79 
80  friend std::ostream&
81  operator<< (std::ostream& os, crandom const& rand) {
82  os << "<crandom ";
83  os << dynamic_cast<cmemory const&>( rand );
84  os << ">";
85  return os;
86  };
87 
88 };
89 
90 }; // end of namespace
91 
92 #endif
Definition: cmemory.h:22
crandom & rand(size_t length)
Definition: crandom.cc:29
static double draw_random_number()
returns a random number between 0 and 1
Definition: crandom.cc:11
Definition: crandom.h:29
uint8_t uint8()
Definition: crandom.cc:62
uint64_t uint64()
Definition: crandom.cc:89
uint16_t uint16()
Definition: crandom.cc:71
C++ abstraction for malloc'ed memory areas.
Definition: cmemory.h:44
cmemory & operator=(cmemory const &m)
Assignment operator.
Definition: cmemory.cc:85
Definition: crandom.h:26
size_t randlen()
Definition: crandom.cc:54
Definition: crandom.h:25
uint32_t uint32()
Definition: crandom.cc:80
Definition: crandom.h:27