MongoDB  2.7.0
goodies.h
1 // @file goodies.h
2 // miscellaneous
3 
4 /* Copyright 2009 10gen Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <iostream>
22 #include <sstream>
23 
24 #include <boost/detail/endian.hpp>
25 #include <boost/intrusive_ptr.hpp>
26 #include <boost/scoped_array.hpp>
27 #include <boost/scoped_ptr.hpp>
28 #include <boost/shared_ptr.hpp>
29 
30 #include "mongo/bson/util/misc.h"
31 
32 namespace mongo {
33 
34  /* @return a dump of the buffer as hex byte ascii output */
35  string hexdump(const char *data, unsigned len);
36 
37  template<class T>
38  inline string ToString(const T& t) {
39  std::stringstream s;
40  s << t;
41  return s.str();
42  }
43 
44  bool isPrime(int n);
45  int nextPrime(int n);
46 
47  inline void dumpmemory(const char *data, int len) {
48  if ( len > 1024 )
49  len = 1024;
50  try {
51  const char *q = data;
52  const char *p = q;
53  while ( len > 0 ) {
54  for ( int i = 0; i < 16; i++ ) {
55  if ( *p >= 32 && *p <= 126 )
56  std::cout << *p;
57  else
58  std::cout << '.';
59  p++;
60  }
61  std::cout << " ";
62  p -= 16;
63  for ( int i = 0; i < 16; i++ )
64  std::cout << (unsigned) ((unsigned char)*p++) << ' ';
65  std::cout << std::endl;
66  len -= 16;
67  }
68  }
69  catch (...) {
70  }
71  }
72 
73 // PRINT(2+2); prints "2+2: 4"
74 #define MONGO_PRINT(x) std::cout << #x ": " << (x) << std::endl
75 #define PRINT MONGO_PRINT
76 // PRINTFL; prints file:line
77 #define MONGO_PRINTFL std::cout << __FILE__ ":" << __LINE__ << std::endl
78 #define PRINTFL MONGO_PRINTFL
79 #define MONGO_FLOG log() << __FILE__ ":" << __LINE__ << std::endl
80 #define FLOG MONGO_FLOG
81 
82  inline bool startsWith(const char *str, const char *prefix) {
83  size_t l = strlen(prefix);
84  if ( strlen(str) < l ) return false;
85  return strncmp(str, prefix, l) == 0;
86  }
87  inline bool startsWith(const std::string& s, const std::string& p) {
88  return startsWith(s.c_str(), p.c_str());
89  }
90 
91  inline bool endsWith(const char *p, const char *suffix) {
92  size_t a = strlen(p);
93  size_t b = strlen(suffix);
94  if ( b > a ) return false;
95  return strcmp(p + a - b, suffix) == 0;
96  }
97 
98  inline unsigned long swapEndian(unsigned long x) {
99  return
100  ((x & 0xff) << 24) |
101  ((x & 0xff00) << 8) |
102  ((x & 0xff0000) >> 8) |
103  ((x & 0xff000000) >> 24);
104  }
105 
106 #if defined(BOOST_LITTLE_ENDIAN)
107  inline unsigned long fixEndian(unsigned long x) {
108  return x;
109  }
110 #elif defined(BOOST_BIG_ENDIAN)
111  inline unsigned long fixEndian(unsigned long x) {
112  return swapEndian(x);
113  }
114 #else
115 #error no boost endian header defined
116 #endif
117 
118 #if !defined(_WIN32)
119  typedef int HANDLE;
120  inline void strcpy_s(char *dst, unsigned len, const char *src) {
121  verify( strlen(src) < len );
122  strcpy(dst, src);
123  }
124 #else
125  typedef void *HANDLE;
126 #endif
127 
128 
133  class ThreadSafeString : boost::noncopyable {
134  public:
135  ThreadSafeString( size_t size=256 )
136  : _size( size ) , _buf( new char[size] ) {
137  memset( _buf , 0 , _size );
138  }
139 
140  ~ThreadSafeString() {
141  delete[] _buf;
142  }
143 
144  string toString() const {
145  string s = _buf;
146  return s;
147  }
148 
149  ThreadSafeString& operator=( const char * str ) {
150  size_t s = strlen(str);
151  if ( s >= _size - 2 )
152  s = _size - 2;
153  strncpy( _buf , str , s );
154  _buf[s] = 0;
155  return *this;
156  }
157 
158  bool empty() const {
159  return _buf == 0 || _buf[0] == 0;
160  }
161 
162  private:
163  const size_t _size;
164  char *const _buf;
165  };
166 
167  std::ostream& operator<<(std::ostream &s, const ThreadSafeString &o);
168 
175  template <typename T>
176  struct ptr {
177 
178  ptr() : _p(NULL) {}
179 
180  // convert to ptr<T>
181  ptr(T* p) : _p(p) {} // needed for NULL
182  template<typename U> ptr(U* p) : _p(p) {}
183  template<typename U> ptr(const ptr<U>& p) : _p(p) {}
184  template<typename U> ptr(const boost::shared_ptr<U>& p) : _p(p.get()) {}
185  template<typename U> ptr(const boost::scoped_ptr<U>& p) : _p(p.get()) {}
186  //template<typename U> ptr(const auto_ptr<U>& p) : _p(p.get()) {}
187 
188  // assign to ptr<T>
189  ptr& operator= (T* p) { _p = p; return *this; } // needed for NULL
190  template<typename U> ptr& operator= (U* p) { _p = p; return *this; }
191  template<typename U> ptr& operator= (const ptr<U>& p) { _p = p; return *this; }
192  template<typename U> ptr& operator= (const boost::shared_ptr<U>& p) { _p = p.get(); return *this; }
193  template<typename U> ptr& operator= (const boost::scoped_ptr<U>& p) { _p = p.get(); return *this; }
194  //template<typename U> ptr& operator= (const auto_ptr<U>& p) { _p = p.get(); return *this; }
195 
196  // use
197  T* operator->() const { return _p; }
198  T& operator*() const { return *_p; }
199 
200  // convert from ptr<T>
201  operator T* () const { return _p; }
202 
203  private:
204  T* _p;
205  };
206 
207 
208 
209  using boost::shared_ptr;
210  using boost::scoped_ptr;
211  using boost::scoped_array;
212  using boost::intrusive_ptr;
213  using boost::dynamic_pointer_cast;
214 } // namespace mongo
215 
A generic pointer type for function arguments.
Definition: goodies.h:176
this is a thread safe string you will never get a bad pointer, though data may be mungedd ...
Definition: goodies.h:133