SysInfo

来源:互联网 发布:知乎崩坏3rd画面 编辑:程序博客网 时间:2024/05/16 19:24

class SysInfo {

 public:

  // Return the number of logical processors/cores on the current machine.

  // WARNING: On POSIX, this method uses static variables and is not threadsafe

  // until it's been initialized by being called once without a race.

  static int NumberOfProcessors();

 

  // Return the number of bytes of physical memory on the current machine.

  static int64 AmountOfPhysicalMemory();

 

  // Return the number of megabytes of physical memory on the current machine.

  static int AmountOfPhysicalMemoryMB() {

    return static_cast<int>(AmountOfPhysicalMemory() / 1024 / 1024);

  }

 

  // Return the available disk space in bytes on the volume containing |path|,

  // or -1 on failure.

  static int64 AmountOfFreeDiskSpace(const std::wstring& path);

 

  // Return true if the given environment variable is defined.

  // TODO: find a better place for HasEnvVar.

  static bool HasEnvVar(const wchar_t* var);

 

  // Return the value of the given environment variable

  // or an empty string if not defined.

  // TODO: find a better place for GetEnvVar.

  static std::wstring GetEnvVar(const wchar_t* var);

 

  // Returns the name of the host operating system.

  static std::string OperatingSystemName();

 

  // Returns the version of the host operating system.

  static std::string OperatingSystemVersion();

 

  // Retrieves detailed numeric values for the OS version.

  // WARNING: On OS X, this method uses static variables and is not threadsafe

  // until it's been initialized by being called once without a race.

  // TODO(port): Implement a Linux version of this method and enable the

  // corresponding unit test.

  static void OperatingSystemVersionNumbers(int32 *major_version,

                                            int32 *minor_version,

                                            int32 *bugfix_version);

 

  // Returns the CPU architecture of the system. Exact return value may differ

  // across platforms.

  static std::string CPUArchitecture();

 

  // Returns the pixel dimensions of the primary display via the

  // width and height parameters.

  static void GetPrimaryDisplayDimensions(int* width, int* height);

 

  // Return the number of displays.

  static int DisplayCount();

 

  // Return the smallest amount of memory (in bytes) which the VM system will

  // allocate.

  static size_t VMAllocationGranularity();

 

#if defined(OS_MACOSX)

  // Under the OS X Sandbox, our access to the system is limited, this call

  // caches the system info on startup before we turn the Sandbox on.

  // The above functions are all wired up to return the cached value so the rest

  // of the code can call them in the Sandbox without worrying.

  static void CacheSysInfo();

#endif

};

// static
int SysInfo::NumberOfProcessors() {
  SYSTEM_INFO info;
  GetSystemInfo(&info);
  return static_cast<int>(info.dwNumberOfProcessors);
}
// static
int64 SysInfo::AmountOfPhysicalMemory() {
  MEMORYSTATUSEX memory_info;
  memory_info.dwLength = sizeof(memory_info);
  if (!GlobalMemoryStatusEx(&memory_info)) {
    NOTREACHED();
    return 0;
  }
  int64 rv = static_cast<int64>(memory_info.ullTotalPhys);
  if (rv < 0)
    rv = kint64max;
  return rv;
}
// static
int64 SysInfo::AmountOfFreeDiskSpace(const std::wstring& path) {
  ULARGE_INTEGER available, total, free;
  if (!GetDiskFreeSpaceExW(path.c_str(), &available, &total, &free)) {
    return -1;
  }
  int64 rv = static_cast<int64>(available.QuadPart);
  if (rv < 0)
    rv = kint64max;
  return rv;
}
// static
bool SysInfo::HasEnvVar(const wchar_t* var) {
  return GetEnvironmentVariable(var, NULL, 0) != 0;
}
// static
std::wstring SysInfo::GetEnvVar(const wchar_t* var) {
  DWORD value_length = GetEnvironmentVariable(var, NULL, 0);
  if (value_length == 0) {
    return L"";
  }
  scoped_array<wchar_t> value(new wchar_t[value_length]);
  GetEnvironmentVariable(var, value.get(), value_length);
  return std::wstring(value.get());
}
// static
std::string SysInfo::OperatingSystemName() {
  return "Windows NT";
}
// static
std::string SysInfo::OperatingSystemVersion() {
  OSVERSIONINFO info = {0};
  info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  GetVersionEx(&info);
  return StringPrintf("%lu.%lu", info.dwMajorVersion, info.dwMinorVersion);
}
// TODO: Implement OperatingSystemVersionComplete, which would include
// patchlevel/service pack number. See chrome/browser/views/bug_report_view.cc,
// BugReportView::SetOSVersion.
// static
std::string SysInfo::CPUArchitecture() {
  // TODO: Make this vary when we support any other architectures.
  return "x86";
}
// static
void SysInfo::GetPrimaryDisplayDimensions(int* width, int* height) {
  if (width)
    *width = GetSystemMetrics(SM_CXSCREEN);
  if (height)
    *height = GetSystemMetrics(SM_CYSCREEN);
}
// static
int SysInfo::DisplayCount() {
  return GetSystemMetrics(SM_CMONITORS);
}
// static
size_t SysInfo::VMAllocationGranularity() {
  SYSTEM_INFO sysinfo;
  GetSystemInfo(&sysinfo);
  return sysinfo.dwAllocationGranularity;
}
// static
void SysInfo::OperatingSystemVersionNumbers(int32 *major_version,
                                            int32 *minor_version,
                                            int32 *bugfix_version) {
  OSVERSIONINFO info = {0};
  info.dwOSVersionInfoSize = sizeof(info);
  GetVersionEx(&info);
  *major_version = info.dwMajorVersion;
  *minor_version = info.dwMinorVersion;
  *bugfix_version = 0;
}

原创粉丝点击