<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_COMMON_SCOPED_CO_MEM_H_
#define CHROME_COMMON_SCOPED_CO_MEM_H_
#pragma once

#include <objbase.h>

#include "base/basictypes.h"

namespace chrome {
namespace common {

// Simple scoped memory releaser class for COM allocated memory.
// Example:
//   chrome::common::ScopedCoMem<ITEMIDLIST> file_item;
//   SHGetSomeInfo(&file_item, ...);
//   ...
//   return;  <-- memory released
template<typename T>
class ScopedCoMem {
 public:
  explicit ScopedCoMem() : mem_ptr_(NULL) {}

  ~ScopedCoMem() {
    if (mem_ptr_)
      CoTaskMemFree(mem_ptr_);
  }

  T** operator&() {  // NOLINT
    return &mem_ptr_;
  }

  operator T*() {
    return mem_ptr_;
  }

 private:<commit_msg>Make ScopedCoMem class/struct friendly by adding operator->(). Also adding a Reset method and a DCHECK to catch memory leaks. Review URL: http://codereview.chromium.org/8356003<commit_after>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_COMMON_SCOPED_CO_MEM_H_
#define CHROME_COMMON_SCOPED_CO_MEM_H_
#pragma once

#include <objbase.h>

#include "base/basictypes.h"
#include "base/logging.h"

namespace chrome {
namespace common {

// Simple scoped memory releaser class for COM allocated memory.
// Example:
//   chrome::common::ScopedCoMem<ITEMIDLIST> file_item;
//   SHGetSomeInfo(&file_item, ...);
//   ...
//   return;  <-- memory released
template<typename T>
class ScopedCoMem {
 public:
  ScopedCoMem() : mem_ptr_(NULL) {}
  ~ScopedCoMem() {
    Reset(NULL);
  }

  T** operator&() {  // NOLINT
    DCHECK(mem_ptr_ == NULL);  // To catch memory leaks.
    return &mem_ptr_;
  }

  operator T*() {
    return mem_ptr_;
  }

  T* operator->() {
    DCHECK(mem_ptr_ != NULL);
    return mem_ptr_;
  }

  const T* operator->() const {
    DCHECK(mem_ptr_ != NULL);
    return mem_ptr_;
  }

  void Reset(T* ptr) {
    if (mem_ptr_)
      CoTaskMemFree(mem_ptr_);
    mem_ptr_ = ptr;
  }

 private:
{'fraction_non_alphanumeric': 0.12256897331524197, 'fraction_numerical': 0.0067842605156037995, 'mean_word_length': 3.3976143141153083, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4865736', 'n_tokens_mistral': 794, 'n_tokens_neox': 760, 'n_words': 209}