You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2012/07/11 02:27:26 UTC

svn commit: r1359962 [2/2] - in /incubator/mesos/trunk/third_party/libprocess: ./ include/stout/

Added: incubator/mesos/trunk/third_party/libprocess/include/stout/time.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/include/stout/time.hpp?rev=1359962&view=auto
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/include/stout/time.hpp (added)
+++ incubator/mesos/trunk/third_party/libprocess/include/stout/time.hpp Wed Jul 11 00:27:25 2012
@@ -0,0 +1,311 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __STOUT_TIME_HPP__
+#define __STOUT_TIME_HPP__
+
+struct hours;
+struct minutes;
+struct seconds;
+struct milliseconds;
+struct microseconds;
+struct nanoseconds;
+
+
+struct hours
+{
+  explicit hours(double _value) : value(_value) {}
+  inline operator minutes () const;
+  inline operator seconds () const;
+  inline operator milliseconds () const;
+  inline operator microseconds () const;
+  inline operator nanoseconds () const;
+  double mins() const { return value * 60; }
+  double secs() const { return value * 3600; }
+  double millis() const { return value * 3600 * 1000; }
+  double micros() const { return value * 3600 * 1000000; }
+  double nanos() const { return value * 3600 * 1000000000; }
+  const double value;
+};
+
+
+struct minutes
+{
+  explicit minutes(double _value) : value(_value) {}
+  inline operator hours () const;
+  inline operator seconds () const;
+  inline operator milliseconds () const;
+  inline operator microseconds () const;
+  inline operator nanoseconds () const;
+  double hrs() const {return value / 60; }
+  double secs() const { return value * 60; }
+  double millis() const { return value * 60 * 1000; }
+  double micros() const { return value * 60 * 1000000; }
+  double nanos() const { return value * 60 * 1000000000; }
+  const double value;
+};
+
+
+struct seconds
+{
+  explicit seconds(double _value) : value(_value) {}
+  inline operator hours () const;
+  inline operator minutes () const;
+  inline operator milliseconds () const;
+  inline operator microseconds () const;
+  inline operator nanoseconds () const;
+  double hrs() const { return value / 3600; }
+  double mins() const { return value / 60; }
+  double millis() const { return value * 1000; }
+  double micros() const { return value * 1000000; }
+  double nanos() const { return value * 1000000000; }
+  const double value;
+};
+
+
+struct milliseconds
+{
+  explicit milliseconds(double _value) : value(_value) {}
+  inline operator hours () const;
+  inline operator minutes () const;
+  inline operator seconds () const;
+  inline operator microseconds () const;
+  inline operator nanoseconds () const;
+  double hrs() const { return value / (3600 * 1000); }
+  double mins() const { return value / (60 * 1000); }
+  double secs() const { return value / 1000; }
+  double micros() const { return value * 1000; }
+  double nanos() const { return value * 1000000; }
+  const double value;
+};
+
+
+struct microseconds
+{
+  explicit microseconds(double _value) : value(_value) {}
+  inline operator hours () const;
+  inline operator minutes () const;
+  inline operator seconds () const;
+  inline operator milliseconds () const;
+  inline operator nanoseconds () const;
+  double hrs() const { return value / (3600 * 1000000); }
+  double mins() const { return value / (60 * 1000000); }
+  double secs() const { return value / 1000000; }
+  double millis() const { return value / 1000; }
+  double nanos() const { return value * 1000; }
+  const double value;
+};
+
+
+struct nanoseconds
+{
+  explicit nanoseconds(double _value) : value(_value) {}
+  inline operator hours () const;
+  inline operator minutes () const;
+  inline operator seconds () const;
+  inline operator milliseconds () const;
+  inline operator microseconds () const;
+  double hrs() const { return value / (3600 * 1000000000); }
+  double mins() const { return value / (60 * 1000000000); }
+  double secs() const { return value / 1000000000; }
+  double millis() const { return value / 1000000; }
+  double micros() const { return value / 1000; }
+  const double value;
+};
+
+
+inline hours::operator minutes () const
+{
+  return minutes(mins());
+}
+
+
+inline hours::operator seconds () const
+{
+  return seconds(secs());
+}
+
+
+inline hours::operator milliseconds () const
+{
+  return milliseconds(millis());
+}
+
+
+inline hours::operator microseconds () const
+{
+  return microseconds(micros());
+}
+
+
+inline hours::operator nanoseconds () const
+{
+  return nanoseconds(nanos());
+}
+
+
+inline minutes::operator hours () const
+{
+  return hours(hrs());
+}
+
+
+inline minutes::operator seconds () const
+{
+  return seconds(secs());
+}
+
+
+inline minutes::operator milliseconds () const
+{
+  return milliseconds(millis());
+}
+
+
+inline minutes::operator microseconds () const
+{
+  return microseconds(micros());
+}
+
+
+inline minutes::operator nanoseconds () const
+{
+  return nanoseconds(nanos());
+}
+
+
+inline seconds::operator hours () const
+{
+  return hours(hrs());
+}
+
+
+inline seconds::operator minutes () const
+{
+  return minutes(mins());
+}
+
+
+inline seconds::operator milliseconds () const
+{
+  return milliseconds(millis());
+}
+
+
+inline seconds::operator microseconds () const
+{
+  return microseconds(micros());
+}
+
+
+inline seconds::operator nanoseconds () const
+{
+  return nanoseconds(nanos());
+}
+
+
+inline milliseconds::operator hours () const
+{
+  return hours(hrs());
+}
+
+
+inline milliseconds::operator minutes () const
+{
+  return minutes(mins());
+}
+
+
+inline milliseconds::operator seconds () const
+{
+  return seconds(secs());
+}
+
+
+inline milliseconds::operator microseconds () const
+{
+  return microseconds(micros());
+}
+
+
+inline milliseconds::operator nanoseconds () const
+{
+  return nanoseconds(nanos());
+}
+
+
+inline microseconds::operator hours () const
+{
+  return hours(hrs());
+}
+
+
+inline microseconds::operator minutes () const
+{
+  return minutes(mins());
+}
+
+
+inline microseconds::operator seconds () const
+{
+  return seconds(secs());
+}
+
+
+inline microseconds::operator milliseconds () const
+{
+  return milliseconds(millis());
+}
+
+
+inline microseconds::operator nanoseconds () const
+{
+  return nanoseconds(nanos());
+}
+
+
+inline nanoseconds::operator hours () const
+{
+  return hours(hrs());
+}
+
+
+inline nanoseconds::operator minutes () const
+{
+  return minutes(mins());
+}
+
+
+inline nanoseconds::operator seconds () const
+{
+  return seconds(secs());
+}
+
+
+inline nanoseconds::operator milliseconds () const
+{
+  return milliseconds(millis());
+}
+
+
+inline nanoseconds::operator microseconds () const
+{
+  return microseconds(micros());
+}
+
+#endif // __STOUT_TIME_HPP__

Added: incubator/mesos/trunk/third_party/libprocess/include/stout/timer.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/include/stout/timer.hpp?rev=1359962&view=auto
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/include/stout/timer.hpp (added)
+++ incubator/mesos/trunk/third_party/libprocess/include/stout/timer.hpp Wed Jul 11 00:27:25 2012
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __STOUT_TIMER_HPP__
+#define __STOUT_TIMER_HPP__
+
+#include <time.h>
+
+#ifdef __MACH__
+#include <mach/clock.h>
+#include <mach/mach.h>
+#endif // __MACH__
+
+#include <sys/time.h>
+
+#include "time.hpp"
+
+class Timer
+{
+public:
+  Timer() : running(false) { started.tv_sec = 0; started.tv_nsec = 0; }
+
+  void start()
+  {
+    started = now();
+    running = true;
+  }
+
+  void stop()
+  {
+    stopped = now();
+    running = false;
+  }
+
+  nanoseconds elapsed()
+  {
+    if (!running) {
+      return nanoseconds(diff(stopped, started));
+    }
+
+    return nanoseconds(diff(now(), started));
+  }
+
+private:
+  static timespec now()
+  {
+    timespec ts;
+#ifdef __MACH__
+    // OS X does not have clock_gettime, use clock_get_time.
+    clock_serv_t cclock;
+    mach_timespec_t mts;
+    host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
+    clock_get_time(cclock, &mts);
+    mach_port_deallocate(mach_task_self(), cclock);
+    ts.tv_sec = mts.tv_sec;
+    ts.tv_nsec = mts.tv_nsec;
+#else
+    clock_gettime(CLOCK_REALTIME, &ts);
+#endif // __MACH__
+    return ts;
+  }
+
+  static uint64_t diff(const timespec& from, const timespec& to)
+  {
+    return ((from.tv_sec - to.tv_sec) * 1000000000LL)
+      + (from.tv_nsec - to.tv_nsec);
+  }
+
+  bool running;
+  timespec started, stopped;
+};
+
+#endif // __STOUT_TIMER_HPP__

Added: incubator/mesos/trunk/third_party/libprocess/include/stout/try.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/include/stout/try.hpp?rev=1359962&view=auto
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/include/stout/try.hpp (added)
+++ incubator/mesos/trunk/third_party/libprocess/include/stout/try.hpp Wed Jul 11 00:27:25 2012
@@ -0,0 +1,146 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __STOUT_TRY_HPP__
+#define __STOUT_TRY_HPP__
+
+#include <assert.h>
+
+#include <string>
+
+
+template <typename T>
+class Try
+{
+public:
+  static Try<T> some(const T& t)
+  {
+    return Try<T>(SOME, new T(t));
+  }
+
+  static Try<T> error(const std::string& message)
+  {
+    return Try<T>(ERROR, NULL, message);
+  }
+
+  Try(const T& _t) : state(SOME), t(new T(_t)) {}
+
+  Try(const Try<T>& that)
+  {
+    state = that.state;
+    if (that.t != NULL) {
+      t = new T(*that.t);
+    } else {
+      t = NULL;
+    }
+    message = that.message;
+  }
+
+  ~Try()
+  {
+    delete t;
+  }
+
+  Try<T>& operator = (const Try<T>& that)
+  {
+    if (this != &that) {
+      state = that.state;
+      if (that.t != NULL) {
+        t = new T(*that.t);
+      } else {
+        t = NULL;
+      }
+      message = that.message;
+    }
+
+    return *this;
+  }
+
+  bool isSome() const { return state == SOME; }
+  bool isError() const { return state == ERROR; }
+
+  T get() const { assert(state == SOME); return *t; }
+
+  std::string error() const { assert(state == ERROR); return message; }
+
+private:
+  enum State {
+    SOME,
+    ERROR
+  };
+
+  Try(State _state, T* _t = NULL, const std::string& _message = "")
+    : state(_state), t(_t), message(_message) {}
+
+  State state;
+  T* t;
+  std::string message;
+};
+
+
+template <>
+class Try<void>
+{
+public:
+  static Try<void> some()
+  {
+    return Try<void>(SOME);
+  }
+
+  static Try<void> error(const std::string& message)
+  {
+    return Try<void>(ERROR, message);
+  }
+
+  Try(const Try<void>& that)
+  {
+    state = that.state;
+    message = that.message;
+  }
+
+  ~Try() {}
+
+  Try<void>& operator = (const Try<void>& that)
+  {
+    if (this != &that) {
+      state = that.state;
+      message = that.message;
+    }
+
+    return *this;
+  }
+
+  bool isSome() const { return state == SOME; }
+  bool isError() const { return state == ERROR; }
+
+  std::string error() const { assert(state == ERROR); return message; }
+
+private:
+  enum State {
+    SOME,
+    ERROR
+  };
+
+  Try(State _state, const std::string& _message = "")
+    : state(_state), message(_message) {}
+
+  State state;
+  std::string message;
+};
+
+#endif // __STOUT_TRY_HPP__

Added: incubator/mesos/trunk/third_party/libprocess/include/stout/uuid.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/include/stout/uuid.hpp?rev=1359962&view=auto
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/include/stout/uuid.hpp (added)
+++ incubator/mesos/trunk/third_party/libprocess/include/stout/uuid.hpp Wed Jul 11 00:27:25 2012
@@ -0,0 +1,74 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __STOUT_UUID_HPP__
+#define __STOUT_UUID_HPP__
+
+#include <assert.h>
+
+#include <sstream>
+#include <string>
+
+#include <boost/uuid/uuid.hpp>
+#include <boost/uuid/uuid_io.hpp>
+#include <boost/uuid/uuid_generators.hpp>
+
+
+struct UUID : boost::uuids::uuid
+{
+public:
+  static UUID random()
+  {
+    return UUID(boost::uuids::random_generator()());
+  }
+
+  static UUID fromBytes(const std::string& s)
+  {
+    boost::uuids::uuid uuid;
+    memcpy(&uuid, s.data(), s.size());
+    return UUID(uuid);
+  }
+
+  static UUID fromString(const std::string& s)
+  {
+    boost::uuids::uuid uuid;
+    std::istringstream in(s);
+    in >> uuid;
+    return UUID(uuid);
+  }
+
+  std::string toBytes() const
+  {
+    assert(sizeof(data) == size());
+    return std::string(reinterpret_cast<const char*>(data), sizeof(data));
+  }
+
+  std::string toString() const
+  {
+    std::ostringstream out;
+    out << *this;
+    return out.str();
+  }
+
+private:
+  explicit UUID(const boost::uuids::uuid& uuid)
+    : boost::uuids::uuid(uuid) {}
+};
+
+
+#endif // __STOUT_UUID_HPP__