<commit_before>//===--------------------------- new.cpp ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <stdlib.h>

#include "new"
#include "include/atomic_support.h"

#if defined(_LIBCPP_ABI_MICROSOFT)
#   if !defined(_LIBCPP_ABI_VCRUNTIME)
#       include "support/runtime/new_handler_fallback.ipp"
#   endif
#elif defined(LIBCXX_BUILDING_LIBCXXABI)
#   include <cxxabi.h>
#elif defined(LIBCXXRT)
#   include <cxxabi.h>
#   include "support/runtime/new_handler_fallback.ipp"
#elif defined(__GLIBCXX__)
    // nothing to do
#elif !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
#   include "support/runtime/new_handler_fallback.ipp"
#endif

namespace std
{

#ifndef __GLIBCXX__
const nothrow_t nothrow{};
#endif

#ifndef LIBSTDCXX

void
__throw_bad_alloc()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    throw bad_alloc();
#else
    _VSTD::abort();
#endif
}

#endif // !LIBSTDCXX

}  // std

#if !defined(__GLIBCXX__) &&                                                   \
    !defined(_LIBCPP_ABI_VCRUNTIME) &&      \
    !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)

// Implement all new and delete operators as weak definitions
// in this shared library, so that they can be overridden by programs
// that define non-weak copies of the functions.

_LIBCPP_WEAK
void *
operator new(std::size_t size) _THROW_BAD_ALLOC
{
    if (size == 0)
        size = 1;
    void* p;
    while ((p = ::malloc(size)) == 0)
    {
        // If malloc fails and there is a new_handler,
        // call it to try free up memory.
        std::new_handler nh = std::get_new_handler();
        if (nh)
            nh();
        else
#ifndef _LIBCPP_NO_EXCEPTIONS
            throw std::bad_alloc();
#else
            break;
#endif
    }
    return p;
}

_LIBCPP_WEAK
void*
operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
{
    void* p = 0;
#ifndef _LIBCPP_NO_EXCEPTIONS
    try
    {
#endif  // _LIBCPP_NO_EXCEPTIONS
        p = ::operator new(size);
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
    return p;
}

_LIBCPP_WEAK
void*
operator new[](size_t size) _THROW_BAD_ALLOC
{
    return ::operator new(size);
}

_LIBCPP_WEAK
void*
operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
{
    void* p = 0;
#ifndef _LIBCPP_NO_EXCEPTIONS
    try
    {
#endif  // _LIBCPP_NO_EXCEPTIONS
        p = ::operator new[](size);
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
    return p;
}

_LIBCPP_WEAK
void
operator delete(void* ptr) _NOEXCEPT
{
    ::free(ptr);
}

_LIBCPP_WEAK
void
operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
{
    ::operator delete(ptr);
}

_LIBCPP_WEAK
void
operator delete(void* ptr, size_t) _NOEXCEPT
{
    ::operator delete(ptr);
}

_LIBCPP_WEAK
void
operator delete[] (void* ptr) _NOEXCEPT
{
    ::operator delete(ptr);
}

_LIBCPP_WEAK
void
operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
{
    ::operator delete[](ptr);
}

_LIBCPP_WEAK
void
operator delete[] (void* ptr, size_t) _NOEXCEPT
{
    ::operator delete[](ptr);
}

#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)

_LIBCPP_WEAK
void *
operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
{
    if (size == 0)
        size = 1;
    if (static_cast<size_t>(alignment) < sizeof(void*))
      alignment = std::align_val_t(sizeof(void*));
    void* p;
#if defined(_LIBCPP_MSVCRT_LIKE)
    while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
#else
    while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
#endif
    {
        // If posix_memalign fails and there is a new_handler,
        // call it to try free up memory.
        std::new_handler nh = std::get_new_handler();
        if (nh)
            nh();
        else {
#ifndef _LIBCPP_NO_EXCEPTIONS
            throw std::bad_alloc();
#else
            p = nullptr; // posix_memalign doesn't initialize 'p' on failure
            break;
#endif
        }
    }
    return p;
}

_LIBCPP_WEAK
void*
operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
{
    void* p = 0;
#ifndef _LIBCPP_NO_EXCEPTIONS
    try
    {
#endif  // _LIBCPP_NO_EXCEPTIONS
        p = ::operator new(size, alignment);
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
    return p;
}

_LIBCPP_WEAK
void*
operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
{
    return ::operator new(size, alignment);
}

_LIBCPP_WEAK
void*
operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
{
    void* p = 0;
#ifndef _LIBCPP_NO_EXCEPTIONS
    try
    {
#endif  // _LIBCPP_NO_EXCEPTIONS
        p = ::operator new[](size, alignment);
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
    return p;
}

_LIBCPP_WEAK
void
operator delete(void* ptr, std::align_val_t) _NOEXCEPT
{
#if defined(_LIBCPP_MSVCRT_LIKE)
    ::_aligned_free(ptr);
#else
    ::free(ptr);
#endif
}

_LIBCPP_WEAK
void
operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
{
    ::operator delete(ptr, alignment);
}

_LIBCPP_WEAK
void
operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
{
    ::operator delete(ptr, alignment);
}

_LIBCPP_WEAK
void
operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
{
    ::operator delete(ptr, alignment);
}

_LIBCPP_WEAK
void
operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
{
    ::operator delete[](ptr, alignment);
}

_LIBCPP_WEAK
void
operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
{
    ::operator delete[](ptr, alignment);
}

#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
#endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
<commit_msg>[libc++][CMake] Remove unnecessary conditional for defining new handlers<commit_after>//===--------------------------- new.cpp ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <stdlib.h>

#include "new"
#include "include/atomic_support.h"

#if defined(_LIBCPP_ABI_MICROSOFT)
#   if !defined(_LIBCPP_ABI_VCRUNTIME)
#       include "support/runtime/new_handler_fallback.ipp"
#   endif
#elif defined(LIBCXX_BUILDING_LIBCXXABI)
#   include <cxxabi.h>
#elif defined(LIBCXXRT)
#   include <cxxabi.h>
#   include "support/runtime/new_handler_fallback.ipp"
#elif defined(__GLIBCXX__)
    // nothing to do
#else
#   include "support/runtime/new_handler_fallback.ipp"
#endif

namespace std
{

#ifndef __GLIBCXX__
const nothrow_t nothrow{};
#endif

#ifndef LIBSTDCXX

void
__throw_bad_alloc()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    throw bad_alloc();
#else
    _VSTD::abort();
#endif
}

#endif // !LIBSTDCXX

}  // std

#if !defined(__GLIBCXX__) &&                                                   \
    !defined(_LIBCPP_ABI_VCRUNTIME) &&      \
    !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)

// Implement all new and delete operators as weak definitions
// in this shared library, so that they can be overridden by programs
// that define non-weak copies of the functions.

_LIBCPP_WEAK
void *
operator new(std::size_t size) _THROW_BAD_ALLOC
{
    if (size == 0)
        size = 1;
    void* p;
    while ((p = ::malloc(size)) == 0)
    {
        // If malloc fails and there is a new_handler,
        // call it to try free up memory.
        std::new_handler nh = std::get_new_handler();
        if (nh)
            nh();
        else
#ifndef _LIBCPP_NO_EXCEPTIONS
            throw std::bad_alloc();
#else
            break;
#endif
    }
    return p;
}

_LIBCPP_WEAK
void*
operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
{
    void* p = 0;
#ifndef _LIBCPP_NO_EXCEPTIONS
    try
    {
#endif  // _LIBCPP_NO_EXCEPTIONS
        p = ::operator new(size);
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
    return p;
}

_LIBCPP_WEAK
void*
operator new[](size_t size) _THROW_BAD_ALLOC
{
    return ::operator new(size);
}

_LIBCPP_WEAK
void*
operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
{
    void* p = 0;
#ifndef _LIBCPP_NO_EXCEPTIONS
    try
    {
#endif  // _LIBCPP_NO_EXCEPTIONS
        p = ::operator new[](size);
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
    return p;
}

_LIBCPP_WEAK
void
operator delete(void* ptr) _NOEXCEPT
{
    ::free(ptr);
}

_LIBCPP_WEAK
void
operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
{
    ::operator delete(ptr);
}

_LIBCPP_WEAK
void
operator delete(void* ptr, size_t) _NOEXCEPT
{
    ::operator delete(ptr);
}

_LIBCPP_WEAK
void
operator delete[] (void* ptr) _NOEXCEPT
{
    ::operator delete(ptr);
}

_LIBCPP_WEAK
void
operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
{
    ::operator delete[](ptr);
}

_LIBCPP_WEAK
void
operator delete[] (void* ptr, size_t) _NOEXCEPT
{
    ::operator delete[](ptr);
}

#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)

_LIBCPP_WEAK
void *
operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
{
    if (size == 0)
        size = 1;
    if (static_cast<size_t>(alignment) < sizeof(void*))
      alignment = std::align_val_t(sizeof(void*));
    void* p;
#if defined(_LIBCPP_MSVCRT_LIKE)
    while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
#else
    while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
#endif
    {
        // If posix_memalign fails and there is a new_handler,
        // call it to try free up memory.
        std::new_handler nh = std::get_new_handler();
        if (nh)
            nh();
        else {
#ifndef _LIBCPP_NO_EXCEPTIONS
            throw std::bad_alloc();
#else
            p = nullptr; // posix_memalign doesn't initialize 'p' on failure
            break;
#endif
        }
    }
    return p;
}

_LIBCPP_WEAK
void*
operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
{
    void* p = 0;
#ifndef _LIBCPP_NO_EXCEPTIONS
    try
    {
#endif  // _LIBCPP_NO_EXCEPTIONS
        p = ::operator new(size, alignment);
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
    return p;
}

_LIBCPP_WEAK
void*
operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
{
    return ::operator new(size, alignment);
}

_LIBCPP_WEAK
void*
operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
{
    void* p = 0;
#ifndef _LIBCPP_NO_EXCEPTIONS
    try
    {
#endif  // _LIBCPP_NO_EXCEPTIONS
        p = ::operator new[](size, alignment);
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
    return p;
}

_LIBCPP_WEAK
void
operator delete(void* ptr, std::align_val_t) _NOEXCEPT
{
#if defined(_LIBCPP_MSVCRT_LIKE)
    ::_aligned_free(ptr);
#else
    ::free(ptr);
#endif
}

_LIBCPP_WEAK
void
operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
{
    ::operator delete(ptr, alignment);
}

_LIBCPP_WEAK
void
operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
{
    ::operator delete(ptr, alignment);
}

_LIBCPP_WEAK
void
operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
{
    ::operator delete(ptr, alignment);
}

_LIBCPP_WEAK
void
operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
{
    ::operator delete[](ptr, alignment);
}

_LIBCPP_WEAK
void
operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
{
    ::operator delete[](ptr, alignment);
}

#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
#endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
<|endoftext|>


<commit_before>// Copyright (c) 2012 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.

#include "ash/system/ime/tray_ime.h"

#include <vector>

#include "ash/shell.h"
#include "ash/system/tray/system_tray.h"
#include "ash/system/tray/system_tray_delegate.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_item_more.h"
#include "ash/system/tray/tray_views.h"
#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "grit/ash_strings.h"
#include "grit/ui_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/font.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"

namespace ash {
namespace internal {

namespace tray {

class IMEDefaultView : public TrayItemMore {
 public:
  explicit IMEDefaultView(SystemTrayItem* owner)
      : TrayItemMore(owner) {
    ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();

    SetImage(bundle.GetImageNamed(
        IDR_AURA_UBER_TRAY_IME).ToSkBitmap());

    IMEInfo info;
    Shell::GetInstance()->tray_delegate()->GetCurrentIME(&info);
    UpdateLabel(info);
  }

  virtual ~IMEDefaultView() {}

  void UpdateLabel(const IMEInfo& info) {
    SetLabel(info.name);
    SetAccessibleName(info.name);
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(IMEDefaultView);
};

class IMEDetailedView : public views::View,
                        public ViewClickListener {
 public:
  IMEDetailedView(SystemTrayItem* owner, user::LoginStatus login)
      : login_(login),
        header_(NULL) {
    SetLayoutManager(new views::BoxLayout(
        views::BoxLayout::kVertical, 0, 0, 0));
    set_background(views::Background::CreateSolidBackground(kBackgroundColor));
    SystemTrayDelegate* delegate = Shell::GetInstance()->tray_delegate();
    IMEInfoList list;
    delegate->GetAvailableIMEList(&list);
    IMEPropertyInfoList property_list;
    delegate->GetCurrentIMEProperties(&property_list);
    Update(list, property_list);
  }

  virtual ~IMEDetailedView() {}

  void Update(const IMEInfoList& list,
              const IMEPropertyInfoList& property_list) {
    RemoveAllChildViews(true);

    header_ = NULL;

    AppendHeaderEntry();
    AppendIMEList(list);
    if (!property_list.empty())
      AppendIMEProperties(property_list);
    if (login_ != user::LOGGED_IN_NONE && login_ != user::LOGGED_IN_LOCKED)
      AppendSettings();

    Layout();
    SchedulePaint();
  }

 private:
  void AppendHeaderEntry() {
    header_ = CreateDetailedHeaderEntry(IDS_ASH_STATUS_TRAY_IME, this);
    AddChildView(header_);
  }

  void AppendIMEList(const IMEInfoList& list) {
    ime_map_.clear();
    views::View* imes = new views::View;
    imes->SetLayoutManager(new views::BoxLayout(
        views::BoxLayout::kVertical, 0, 0, 1));
    for (size_t i = 0; i < list.size(); i++) {
      HoverHighlightView* container = new HoverHighlightView(this);
      container->set_fixed_height(kTrayPopupItemHeight);
      container->AddLabel(list[i].name,
          list[i].selected ? gfx::Font::BOLD : gfx::Font::NORMAL);
      imes->AddChildView(container);
      ime_map_[container] = list[i].id;
    }
    imes->set_border(views::Border::CreateSolidSidedBorder(1, 0, 1, 0,
        kBorderLightColor));
    AddChildView(imes);
  }

  void AppendIMEProperties(const IMEPropertyInfoList& property_list) {
    property_map_.clear();
    views::View* properties = new views::View;
    properties->SetLayoutManager(new views::BoxLayout(
        views::BoxLayout::kVertical, 0, 0, 1));
    for (size_t i = 0; i < property_list.size(); i++) {
      HoverHighlightView* container = new HoverHighlightView(this);
      container->set_fixed_height(kTrayPopupItemHeight);
      container->AddLabel(
          property_list[i].name,
          property_list[i].selected ? gfx::Font::BOLD : gfx::Font::NORMAL);
      properties->AddChildView(container);
      property_map_[container] = property_list[i].key;
    }
    properties->set_border(views::Border::CreateSolidSidedBorder(
        0, 0, 1, 0, kBorderLightColor));
    AddChildView(properties);
  }

  void AppendSettings() {
    HoverHighlightView* container = new HoverHighlightView(this);
    container->set_fixed_height(kTrayPopupItemHeight);
    container->AddLabel(ui::ResourceBundle::GetSharedInstance().
        GetLocalizedString(IDS_ASH_STATUS_TRAY_IME_SETTINGS),
        gfx::Font::NORMAL);
    AddChildView(container);
    settings_ = container;
  }

  // Overridden from ViewClickListener.
  virtual void ClickedOn(views::View* sender) OVERRIDE {
    SystemTrayDelegate* delegate = Shell::GetInstance()->tray_delegate();
    if (sender == header_) {
      Shell::GetInstance()->tray()->ShowDefaultView();
    } else if (sender == settings_) {
      delegate->ShowIMESettings();
    } else {
      std::map<views::View*, std::string>::const_iterator ime_find;
      ime_find = ime_map_.find(sender);
      if (ime_find != ime_map_.end()) {
        std::string ime_id = ime_find->second;
        delegate->SwitchIME(ime_id);
        GetWidget()->Close();
      } else {
        std::map<views::View*, std::string>::const_iterator prop_find;
        prop_find = property_map_.find(sender);
        if (prop_find != property_map_.end()) {
          const std::string key = prop_find->second;
          delegate->ActivateIMEProperty(key);
          GetWidget()->Close();
        }
      }
    }
  }

  user::LoginStatus login_;

  std::map<views::View*, std::string> ime_map_;
  std::map<views::View*, std::string> property_map_;
  views::View* header_;
  views::View* settings_;

  DISALLOW_COPY_AND_ASSIGN(IMEDetailedView);
};

}  // namespace tray

TrayIME::TrayIME() {
}

TrayIME::~TrayIME() {
}

void TrayIME::UpdateTrayLabel(const IMEInfo& current, size_t count) {
  tray_label_->SetText(current.short_name);
  tray_label_->SetVisible(count > 1);
}

views::View* TrayIME::CreateTrayView(user::LoginStatus status) {
  tray_label_.reset(new views::Label);
  SetupLabelForTray(tray_label_.get());
  tray_label_->set_border(
      views::Border::CreateEmptyBorder(0, 2, 0, 2));
  return tray_label_.get();
}

views::View* TrayIME::CreateDefaultView(user::LoginStatus status) {
  default_.reset(new tray::IMEDefaultView(this));
  return default_.get();
}

views::View* TrayIME::CreateDetailedView(user::LoginStatus status) {
  detailed_.reset(new tray::IMEDetailedView(this, status));
  return detailed_.get();
}

void TrayIME::DestroyTrayView() {
  tray_label_.reset();
}

void TrayIME::DestroyDefaultView() {
  default_.reset();
}

void TrayIME::DestroyDetailedView() {
  detailed_.reset();
}

void TrayIME::OnIMERefresh() {
  SystemTrayDelegate* delegate = Shell::GetInstance()->tray_delegate();
  IMEInfoList list;
  IMEInfo current;
  IMEPropertyInfoList property_list;
  delegate->GetCurrentIME(&current);
  delegate->GetAvailableIMEList(&list);
  delegate->GetCurrentIMEProperties(&property_list);

  UpdateTrayLabel(current, list.size());

  if (default_.get())
    default_->UpdateLabel(current);
  if (detailed_.get())
    detailed_->Update(list, property_list);
}

}  // namespace internal
}  // namespace ash
<commit_msg>Popup detailed view for IME switch when the tray icon is not visible.<commit_after>// Copyright (c) 2012 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.

#include "ash/system/ime/tray_ime.h"

#include <vector>

#include "ash/shell.h"
#include "ash/system/tray/system_tray.h"
#include "ash/system/tray/system_tray_delegate.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_item_more.h"
#include "ash/system/tray/tray_views.h"
#include "ash/wm/shelf_layout_manager.h"
#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "grit/ash_strings.h"
#include "grit/ui_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/font.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"

namespace ash {
namespace internal {

namespace tray {

class IMEDefaultView : public TrayItemMore {
 public:
  explicit IMEDefaultView(SystemTrayItem* owner)
      : TrayItemMore(owner) {
    ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();

    SetImage(bundle.GetImageNamed(
        IDR_AURA_UBER_TRAY_IME).ToSkBitmap());

    IMEInfo info;
    Shell::GetInstance()->tray_delegate()->GetCurrentIME(&info);
    UpdateLabel(info);
  }

  virtual ~IMEDefaultView() {}

  void UpdateLabel(const IMEInfo& info) {
    SetLabel(info.name);
    SetAccessibleName(info.name);
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(IMEDefaultView);
};

class IMEDetailedView : public views::View,
                        public ViewClickListener {
 public:
  IMEDetailedView(SystemTrayItem* owner, user::LoginStatus login)
      : login_(login),
        header_(NULL) {
    SetLayoutManager(new views::BoxLayout(
        views::BoxLayout::kVertical, 0, 0, 0));
    set_background(views::Background::CreateSolidBackground(kBackgroundColor));
    SystemTrayDelegate* delegate = Shell::GetInstance()->tray_delegate();
    IMEInfoList list;
    delegate->GetAvailableIMEList(&list);
    IMEPropertyInfoList property_list;
    delegate->GetCurrentIMEProperties(&property_list);
    Update(list, property_list);
  }

  virtual ~IMEDetailedView() {}

  void Update(const IMEInfoList& list,
              const IMEPropertyInfoList& property_list) {
    RemoveAllChildViews(true);

    header_ = NULL;

    AppendHeaderEntry();
    AppendIMEList(list);
    if (!property_list.empty())
      AppendIMEProperties(property_list);
    if (login_ != user::LOGGED_IN_NONE && login_ != user::LOGGED_IN_LOCKED)
      AppendSettings();

    Layout();
    SchedulePaint();
  }

 private:
  void AppendHeaderEntry() {
    header_ = CreateDetailedHeaderEntry(IDS_ASH_STATUS_TRAY_IME, this);
    AddChildView(header_);
  }

  void AppendIMEList(const IMEInfoList& list) {
    ime_map_.clear();
    views::View* imes = new views::View;
    imes->SetLayoutManager(new views::BoxLayout(
        views::BoxLayout::kVertical, 0, 0, 1));
    for (size_t i = 0; i < list.size(); i++) {
      HoverHighlightView* container = new HoverHighlightView(this);
      container->set_fixed_height(kTrayPopupItemHeight);
      container->AddLabel(list[i].name,
          list[i].selected ? gfx::Font::BOLD : gfx::Font::NORMAL);
      imes->AddChildView(container);
      ime_map_[container] = list[i].id;
    }
    imes->set_border(views::Border::CreateSolidSidedBorder(1, 0, 1, 0,
        kBorderLightColor));
    AddChildView(imes);
  }

  void AppendIMEProperties(const IMEPropertyInfoList& property_list) {
    property_map_.clear();
    views::View* properties = new views::View;
    properties->SetLayoutManager(new views::BoxLayout(
        views::BoxLayout::kVertical, 0, 0, 1));
    for (size_t i = 0; i < property_list.size(); i++) {
      HoverHighlightView* container = new HoverHighlightView(this);
      container->set_fixed_height(kTrayPopupItemHeight);
      container->AddLabel(
          property_list[i].name,
          property_list[i].selected ? gfx::Font::BOLD : gfx::Font::NORMAL);
      properties->AddChildView(container);
      property_map_[container] = property_list[i].key;
    }
    properties->set_border(views::Border::CreateSolidSidedBorder(
        0, 0, 1, 0, kBorderLightColor));
    AddChildView(properties);
  }

  void AppendSettings() {
    HoverHighlightView* container = new HoverHighlightView(this);
    container->set_fixed_height(kTrayPopupItemHeight);
    container->AddLabel(ui::ResourceBundle::GetSharedInstance().
        GetLocalizedString(IDS_ASH_STATUS_TRAY_IME_SETTINGS),
        gfx::Font::NORMAL);
    AddChildView(container);
    settings_ = container;
  }

  // Overridden from ViewClickListener.
  virtual void ClickedOn(views::View* sender) OVERRIDE {
    SystemTrayDelegate* delegate = Shell::GetInstance()->tray_delegate();
    if (sender == header_) {
      Shell::GetInstance()->tray()->ShowDefaultView();
    } else if (sender == settings_) {
      delegate->ShowIMESettings();
    } else {
      std::map<views::View*, std::string>::const_iterator ime_find;
      ime_find = ime_map_.find(sender);
      if (ime_find != ime_map_.end()) {
        std::string ime_id = ime_find->second;
        delegate->SwitchIME(ime_id);
        GetWidget()->Close();
      } else {
        std::map<views::View*, std::string>::const_iterator prop_find;
        prop_find = property_map_.find(sender);
        if (prop_find != property_map_.end()) {
          const std::string key = prop_find->second;
          delegate->ActivateIMEProperty(key);
          GetWidget()->Close();
        }
      }
    }
  }

  user::LoginStatus login_;

  std::map<views::View*, std::string> ime_map_;
  std::map<views::View*, std::string> property_map_;
  views::View* header_;
  views::View* settings_;

  DISALLOW_COPY_AND_ASSIGN(IMEDetailedView);
};

}  // namespace tray

TrayIME::TrayIME() {
}

TrayIME::~TrayIME() {
}

void TrayIME::UpdateTrayLabel(const IMEInfo& current, size_t count) {
  tray_label_->SetText(current.short_name);
  tray_label_->SetVisible(count > 1);
}

views::View* TrayIME::CreateTrayView(user::LoginStatus status) {
  tray_label_.reset(new views::Label);
  SetupLabelForTray(tray_label_.get());
  tray_label_->set_border(
      views::Border::CreateEmptyBorder(0, 2, 0, 2));
  return tray_label_.get();
}

views::View* TrayIME::CreateDefaultView(user::LoginStatus status) {
  default_.reset(new tray::IMEDefaultView(this));
  return default_.get();
}

views::View* TrayIME::CreateDetailedView(user::LoginStatus status) {
  detailed_.reset(new tray::IMEDetailedView(this, status));
  return detailed_.get();
}

void TrayIME::DestroyTrayView() {
  tray_label_.reset();
}

void TrayIME::DestroyDefaultView() {
  default_.reset();
}

void TrayIME::DestroyDetailedView() {
  detailed_.reset();
}

void TrayIME::OnIMERefresh() {
  SystemTrayDelegate* delegate = Shell::GetInstance()->tray_delegate();
  IMEInfoList list;
  IMEInfo current;
  IMEPropertyInfoList property_list;
  delegate->GetCurrentIME(&current);
  delegate->GetAvailableIMEList(&list);
  delegate->GetCurrentIMEProperties(&property_list);

  UpdateTrayLabel(current, list.size());

  if (default_.get())
    default_->UpdateLabel(current);
  if (detailed_.get())
    detailed_->Update(list, property_list);
  else if (!Shell::GetInstance()->shelf()->IsVisible())
    PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false);
}

}  // namespace internal
}  // namespace ash
<|endoftext|>


<commit_before>#include "model_common_functions.hpp"
#include "species.hpp"

// Include GLEW
#ifndef __GL_GLEW_H_INCLUDED
#define __GL_GLEW_H_INCLUDED
#include <GL/glew.h> // GLfloat, GLuint etc.
#endif

// Include GLFW
#ifndef __GLFW3_H_INCLUDED
#define __GLFW3_H_INCLUDED
#include <glfw3.h>
#endif

// Include GLM
#ifndef __GLM_GLM_HPP_INCLUDED
#define __GLM_GLM_HPP_INCLUDED
#include <glm/glm.hpp> // glm
#endif

// Include standard headers
#include <cmath>     // NAN
#include <vector>    // std::vector

namespace model
{
    void set_child_pointer(GLuint childID, void* child_pointer, std::vector<void*> &child_pointer_vector, std::queue<GLuint> &free_childID_queue)
    {
        child_pointer_vector[childID] = child_pointer;

        if (child_pointer == NULL)
        {
            if (childID == child_pointer_vector.size() - 1)
            {
                // OK, this is the biggest childID of all childID's of this 'object'.
                // We can reduce the size of the child pointer vector at least by 1.
                while ((!child_pointer_vector.empty()) && (child_pointer_vector.back() == NULL))
                {
                    // Reduce the size of child pointer vector by 1.
                    child_pointer_vector.pop_back();
                }
            }
        }
    }

    GLuint get_childID(std::vector<void*> &child_pointer_vector, std::queue<GLuint> &free_childID_queue)
    {
        GLuint childID;

        while (!free_childID_queue.empty())
        {
            // return the first (oldest) free childID.
            childID = free_childID_queue.front();
            free_childID_queue.pop();

            // check that the child index does not exceed current child pointer vector.
            if (childID < child_pointer_vector.size())
            {
                // OK, it does not exceed current child pointer vector.
                return childID;
            }
        }
        // OK, the queue is empty.
        // A new child index must be created.
        childID = child_pointer_vector.size();

        // child pointer vector must also be extended with an appropriate NULL pointer.
        child_pointer_vector.push_back(NULL);

        return childID;
    }

}
<commit_msg>Poistettu tyhjä rivi.<commit_after>#include "model_common_functions.hpp"
#include "species.hpp"

// Include GLEW
#ifndef __GL_GLEW_H_INCLUDED
#define __GL_GLEW_H_INCLUDED
#include <GL/glew.h> // GLfloat, GLuint etc.
#endif

// Include GLFW
#ifndef __GLFW3_H_INCLUDED
#define __GLFW3_H_INCLUDED
#include <glfw3.h>
#endif

// Include GLM
#ifndef __GLM_GLM_HPP_INCLUDED
#define __GLM_GLM_HPP_INCLUDED
#include <glm/glm.hpp> // glm
#endif

// Include standard headers
#include <cmath>     // NAN
#include <vector>    // std::vector

namespace model
{
    void set_child_pointer(GLuint childID, void* child_pointer, std::vector<void*> &child_pointer_vector, std::queue<GLuint> &free_childID_queue)
    {
        child_pointer_vector[childID] = child_pointer;

        if (child_pointer == NULL)
        {
            if (childID == child_pointer_vector.size() - 1)
            {
                // OK, this is the biggest childID of all childID's of this 'object'.
                // We can reduce the size of the child pointer vector at least by 1.
                while ((!child_pointer_vector.empty()) && (child_pointer_vector.back() == NULL))
                {
                    // Reduce the size of child pointer vector by 1.
                    child_pointer_vector.pop_back();
                }
            }
        }
    }

    GLuint get_childID(std::vector<void*> &child_pointer_vector, std::queue<GLuint> &free_childID_queue)
    {
        GLuint childID;

        while (!free_childID_queue.empty())
        {
            // return the first (oldest) free childID.
            childID = free_childID_queue.front();
            free_childID_queue.pop();

            // check that the child index does not exceed current child pointer vector.
            if (childID < child_pointer_vector.size())
            {
                // OK, it does not exceed current child pointer vector.
                return childID;
            }
        }
        // OK, the queue is empty.
        // A new child index must be created.
        childID = child_pointer_vector.size();

        // child pointer vector must also be extended with an appropriate NULL pointer.
        child_pointer_vector.push_back(NULL);

        return childID;
    }
}
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: implfont.hxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-08 08:28:49 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef _CPPCANVAS_IMPLFONT_HXX
#define _CPPCANVAS_IMPLFONT_HXX

#ifndef _COM_SUN_STAR_UNO_REFERENCE_HXX_
#include <com/sun/star/uno/Reference.hxx>
#endif

#ifndef BOOST_SHARED_PTR_HPP_INCLUDED
#include <boost/shared_ptr.hpp>
#endif

#ifndef _COM_SUN_STAR_RENDERING_XCANVAS_HPP__
#include <com/sun/star/rendering/XCanvas.hpp>
#endif

#ifndef _CPPCANVAS_FONT_HXX
#include <cppcanvas/font.hxx>
#endif

namespace rtl
{
    class OUString;
}

namespace com { namespace sun { namespace star { namespace rendering
{
    class  XCanvasFont;
} } } }

/* Definition of Font class */

namespace cppcanvas
{

    namespace internal
    {

        class ImplFont : public Font
        {
        public:
            ImplFont( const ::com::sun::star::uno::Reference<
                          ::com::sun::star::rendering::XCanvas >& rCanvas,
                      const ::rtl::OUString& rFontName,
                      const double& rCellSize );

            virtual ~ImplFont();

            virtual ::rtl::OUString getName() const;
            virtual double          getCellSize() const;

            virtual ::com::sun::star::uno::Reference<
                ::com::sun::star::rendering::XCanvasFont > getUNOFont() const;

        private:
            ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCanvas >        mxCanvas;
            ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCanvasFont >    mxFont;
        };
    }
}

#endif /* _CPPCANVAS_IMPLFONT_HXX */
<commit_msg>INTEGRATION: CWS changefileheader (1.5.80); FILE MERGED 2008/04/01 15:10:07 thb 1.5.80.3: #i85898# Stripping all external header guards 2008/04/01 12:27:51 thb 1.5.80.2: #i85898# Stripping all external header guards 2008/03/31 13:07:09 rt 1.5.80.1: #i87441# Change license header to LPGL v3.<commit_after>/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: implfont.hxx,v $
 * $Revision: 1.6 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#ifndef _CPPCANVAS_IMPLFONT_HXX
#define _CPPCANVAS_IMPLFONT_HXX

#include <com/sun/star/uno/Reference.hxx>

#ifndef BOOST_SHARED_PTR_HPP_INCLUDED
#include <boost/shared_ptr.hpp>
#endif

#ifndef _COM_SUN_STAR_RENDERING_XCANVAS_HPP__
#include <com/sun/star/rendering/XCanvas.hpp>
#endif
#include <cppcanvas/font.hxx>

namespace rtl
{
    class OUString;
}

namespace com { namespace sun { namespace star { namespace rendering
{
    class  XCanvasFont;
} } } }

/* Definition of Font class */

namespace cppcanvas
{

    namespace internal
    {

        class ImplFont : public Font
        {
        public:
            ImplFont( const ::com::sun::star::uno::Reference<
                          ::com::sun::star::rendering::XCanvas >& rCanvas,
                      const ::rtl::OUString& rFontName,
                      const double& rCellSize );

            virtual ~ImplFont();

            virtual ::rtl::OUString getName() const;
            virtual double          getCellSize() const;

            virtual ::com::sun::star::uno::Reference<
                ::com::sun::star::rendering::XCanvasFont > getUNOFont() const;

        private:
            ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCanvas >        mxCanvas;
            ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCanvasFont >    mxFont;
        };
    }
}

#endif /* _CPPCANVAS_IMPLFONT_HXX */
<|endoftext|>


<commit_before>/*  This file is part of the Vc library.

    Copyright (C) 2009 Matthias Kretz <kretz@kde.org>

    Vc is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as
    published by the Free Software Foundation, either version 3 of
    the License, or (at your option) any later version.

    Vc is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with Vc.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <Vc/Vc>
#include "benchmark.h"
#include "random.h"
#include "../cpuid.h"
#include <cstdio>
#include <cstdlib>

using namespace Vc;

template<typename T, int S> struct KeepResultsHelper {
    static inline void keep(const T &tmp0) { asm volatile(""::"r"(tmp0)); }
    static inline void keep(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3) {
#ifdef __x86_64__
        asm volatile(""::"r"(tmp0), "r"(tmp1), "r"(tmp2), "r"(tmp3));
#else
        asm volatile(""::"r"(tmp0), "r"(tmp1));
        asm volatile(""::"r"(tmp2), "r"(tmp3));
#endif
    }
};
#if defined(VC_IMPL_AVX)
template<typename T> struct KeepResultsHelper<T, 16> {
    static inline void keep(const T &tmp0) { asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0))); }
    static inline void keep(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3) {
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0)), "x"(reinterpret_cast<const __m128 &>(tmp1)), "x"(reinterpret_cast<const __m128 &>(tmp2)), "x"(reinterpret_cast<const __m128 &>(tmp3)));
    }
};
template<typename T> struct KeepResultsHelper<T, 32> {
    static inline void keep(const T &tmp0) {
        asm volatile(""::"x"(reinterpret_cast<const __m256 &>(tmp0)));
    }
    static inline void keep(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3) {
        asm volatile(""::"x"(reinterpret_cast<const __m256 &>(tmp0)), "x"(reinterpret_cast<const __m256 &>(tmp1)),
                "x"(reinterpret_cast<const __m256 &>(tmp2)), "x"(reinterpret_cast<const __m256 &>(tmp3)));
    }
};
#elif defined(VC_IMPL_SSE)
template<typename T> struct KeepResultsHelper<T, 16> {
    static inline void keep(const T &tmp0) { asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0))); }
    static inline void keep(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3) {
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0)), "x"(reinterpret_cast<const __m128 &>(tmp1)), "x"(reinterpret_cast<const __m128 &>(tmp2)), "x"(reinterpret_cast<const __m128 &>(tmp3)));
    }
};
template<typename T> struct KeepResultsHelper<T, 32> {
    static inline void keep(const T &tmp0) {
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0)), "x"(reinterpret_cast<const __m128 *>(&tmp0)[1]));
    }
    static inline void keep(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3) {
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0)), "x"(reinterpret_cast<const __m128 *>(&tmp0)[1]),
                "x"(reinterpret_cast<const __m128 &>(tmp1)), "x"(reinterpret_cast<const __m128 *>(&tmp1)[1]),
                "x"(reinterpret_cast<const __m128 &>(tmp2)), "x"(reinterpret_cast<const __m128 *>(&tmp2)[1]),
                "x"(reinterpret_cast<const __m128 &>(tmp3)), "x"(reinterpret_cast<const __m128 *>(&tmp3)[1]));
    }
};
#elif defined(VC_IMPL_LRBni)
template<typename T> struct KeepResultsHelper<T, 64> {
    static inline void keep(const T &tmp0) {
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0)), "x"(reinterpret_cast<const __m128 *>(&tmp0)[1]), "x"(reinterpret_cast<const __m128 *>(&tmp0)[2]), "x"(reinterpret_cast<const __m128 *>(&tmp0)[3]));
    }
    static inline void keep(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3) {
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0)), "x"(reinterpret_cast<const __m128 *>(&tmp0)[1]), "x"(reinterpret_cast<const __m128 *>(&tmp0)[2]), "x"(reinterpret_cast<const __m128 *>(&tmp0)[3]),
                "x"(reinterpret_cast<const __m128 &>(tmp1)), "x"(reinterpret_cast<const __m128 *>(&tmp1)[1]), "x"(reinterpret_cast<const __m128 *>(&tmp1)[2]), "x"(reinterpret_cast<const __m128 *>(&tmp1)[3]));
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp2)), "x"(reinterpret_cast<const __m128 *>(&tmp2)[1]), "x"(reinterpret_cast<const __m128 *>(&tmp2)[2]), "x"(reinterpret_cast<const __m128 *>(&tmp2)[3]),
                "x"(reinterpret_cast<const __m128 &>(tmp3)), "x"(reinterpret_cast<const __m128 *>(&tmp3)[1]), "x"(reinterpret_cast<const __m128 *>(&tmp3)[2]), "x"(reinterpret_cast<const __m128 *>(&tmp3)[3]));
    }
};
#endif

template<typename T> static inline void keepResults(const T &tmp0)
{
    KeepResultsHelper<T, sizeof(T)>::keep(tmp0);
}

template<typename T> static inline void keepResults(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3)
{
    KeepResultsHelper<T, sizeof(T)>::keep(tmp0, tmp1, tmp2, tmp3);
}

template<typename Vector> class DoMemIos
{
    public:
        static void run()
        {
            Benchmark::setColumnData("MemorySize", "half L1");
            run(CpuId::L1Data() / (sizeof(Vector) * 2), 128);
            Benchmark::setColumnData("MemorySize", "L1");
            run(CpuId::L1Data() / (sizeof(Vector) * 1), 128);
            Benchmark::setColumnData("MemorySize", "half L2");
            run(CpuId::L2Data() / (sizeof(Vector) * 2), 32);
            Benchmark::setColumnData("MemorySize", "L2");
            run(CpuId::L2Data() / (sizeof(Vector) * 1), 16);
            if (CpuId::L3Data() > 0) {
                Benchmark::setColumnData("MemorySize", "half L3");
                run(CpuId::L3Data() / (sizeof(Vector) * 2), 2);
                Benchmark::setColumnData("MemorySize", "L3");
                run(CpuId::L3Data() / (sizeof(Vector) * 1), 2);
                Benchmark::setColumnData("MemorySize", "4x L3");
                run(CpuId::L3Data() / sizeof(Vector) * 4, 1);
            } else {
                Benchmark::setColumnData("MemorySize", "4x L2");
                run(CpuId::L2Data() / sizeof(Vector) * 4, 1);
            }
        }
    private:
        static void run(const int Factor, const int Factor2)
        {
            Vector *a = new Vector[Factor];
#ifndef VC_BENCHMARK_NO_MLOCK
            mlock(a, Factor * sizeof(Vector));
#endif

            {
                Benchmark bm("write", sizeof(Vector) * Factor * Factor2, "Byte");
                const Vector foo = PseudoRandom<Vector>::next();
                keepResults(foo);
                for (int i = 0; i < Factor; i += 4) {
                    a[i + 0] = foo;
                    a[i + 1] = foo;
                    a[i + 2] = foo;
                    a[i + 3] = foo;
                }
                while (bm.wantsMoreDataPoints()) {
                    bm.Start();
                    for (int j = 0; j < Factor2; ++j) {
                        keepResults(foo);
                        for (int i = 0; i < Factor; i += 4) {
                            a[i + 0] = foo;
                            a[i + 1] = foo;
                            a[i + 2] = foo;
                            a[i + 3] = foo;
                        }
                    }
                    bm.Stop();
                }
                bm.Print();
            }
            {
                Benchmark timer("r/w", sizeof(Vector) * Factor * Factor2, "Byte");
                const Vector foo = PseudoRandom<Vector>::next();
                while (timer.wantsMoreDataPoints()) {
                    timer.Start();
                    for (int j = 0; j < Factor2; ++j) {
                        keepResults(foo);
                        for (int i = 0; i < Factor; i += 4) {
                            const Vector &tmp0 = a[i + 0];
                            const Vector &tmp1 = a[i + 1];
                            const Vector &tmp2 = a[i + 2];
                            const Vector &tmp3 = a[i + 3];
                            keepResults(tmp0, tmp1, tmp2, tmp3);
                            a[i + 0] = foo;
                            a[i + 1] = foo;
                            a[i + 2] = foo;
                            a[i + 3] = foo;
                        }
                    }
                    timer.Stop();
                }
                timer.Print();
            }
            {
                Benchmark timer("read", sizeof(Vector) * Factor * Factor2, "Byte");
                while (timer.wantsMoreDataPoints()) {
                    timer.Start();
                    for (int j = 0; j < Factor2; ++j) {
                        for (int i = 0; i < Factor; i += 4) {
                            const Vector &tmp0 = a[i + 0];
                            const Vector &tmp1 = a[i + 1];
                            const Vector &tmp2 = a[i + 2];
                            const Vector &tmp3 = a[i + 3];
                            keepResults(tmp0, tmp1, tmp2, tmp3);
                        }
                    }
                    timer.Stop();
                }
                timer.Print();
            }
            delete[] a;
        }
};

int bmain()
{
    Benchmark::addColumn("MemorySize");
    Benchmark::addColumn("datatype");

    Benchmark::setColumnData("datatype", "double_v");
    DoMemIos<double_v>::run();
    Benchmark::setColumnData("datatype", "float_v");
    DoMemIos<float_v>::run();
    Benchmark::setColumnData("datatype", "short_v");
    DoMemIos<short_v>::run();

    return 0;
}
<commit_msg>do a first round of touching all values; then start with read so that the cachelines are not marked as dirty there<commit_after>/*  This file is part of the Vc library.

    Copyright (C) 2009 Matthias Kretz <kretz@kde.org>

    Vc is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as
    published by the Free Software Foundation, either version 3 of
    the License, or (at your option) any later version.

    Vc is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with Vc.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <Vc/Vc>
#include "benchmark.h"
#include "random.h"
#include "../cpuid.h"
#include <cstdio>
#include <cstdlib>

using namespace Vc;

template<typename T, int S> struct KeepResultsHelper {
    static inline void keep(const T &tmp0) { asm volatile(""::"r"(tmp0)); }
    static inline void keep(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3) {
#ifdef __x86_64__
        asm volatile(""::"r"(tmp0), "r"(tmp1), "r"(tmp2), "r"(tmp3));
#else
        asm volatile(""::"r"(tmp0), "r"(tmp1));
        asm volatile(""::"r"(tmp2), "r"(tmp3));
#endif
    }
};
#if defined(VC_IMPL_AVX)
template<typename T> struct KeepResultsHelper<T, 16> {
    static inline void keep(const T &tmp0) { asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0))); }
    static inline void keep(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3) {
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0)), "x"(reinterpret_cast<const __m128 &>(tmp1)), "x"(reinterpret_cast<const __m128 &>(tmp2)), "x"(reinterpret_cast<const __m128 &>(tmp3)));
    }
};
template<typename T> struct KeepResultsHelper<T, 32> {
    static inline void keep(const T &tmp0) {
        asm volatile(""::"x"(reinterpret_cast<const __m256 &>(tmp0)));
    }
    static inline void keep(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3) {
        asm volatile(""::"x"(reinterpret_cast<const __m256 &>(tmp0)), "x"(reinterpret_cast<const __m256 &>(tmp1)),
                "x"(reinterpret_cast<const __m256 &>(tmp2)), "x"(reinterpret_cast<const __m256 &>(tmp3)));
    }
};
#elif defined(VC_IMPL_SSE)
template<typename T> struct KeepResultsHelper<T, 16> {
    static inline void keep(const T &tmp0) { asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0))); }
    static inline void keep(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3) {
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0)), "x"(reinterpret_cast<const __m128 &>(tmp1)), "x"(reinterpret_cast<const __m128 &>(tmp2)), "x"(reinterpret_cast<const __m128 &>(tmp3)));
    }
};
template<typename T> struct KeepResultsHelper<T, 32> {
    static inline void keep(const T &tmp0) {
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0)), "x"(reinterpret_cast<const __m128 *>(&tmp0)[1]));
    }
    static inline void keep(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3) {
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0)), "x"(reinterpret_cast<const __m128 *>(&tmp0)[1]),
                "x"(reinterpret_cast<const __m128 &>(tmp1)), "x"(reinterpret_cast<const __m128 *>(&tmp1)[1]),
                "x"(reinterpret_cast<const __m128 &>(tmp2)), "x"(reinterpret_cast<const __m128 *>(&tmp2)[1]),
                "x"(reinterpret_cast<const __m128 &>(tmp3)), "x"(reinterpret_cast<const __m128 *>(&tmp3)[1]));
    }
};
#elif defined(VC_IMPL_LRBni)
template<typename T> struct KeepResultsHelper<T, 64> {
    static inline void keep(const T &tmp0) {
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0)), "x"(reinterpret_cast<const __m128 *>(&tmp0)[1]), "x"(reinterpret_cast<const __m128 *>(&tmp0)[2]), "x"(reinterpret_cast<const __m128 *>(&tmp0)[3]));
    }
    static inline void keep(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3) {
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp0)), "x"(reinterpret_cast<const __m128 *>(&tmp0)[1]), "x"(reinterpret_cast<const __m128 *>(&tmp0)[2]), "x"(reinterpret_cast<const __m128 *>(&tmp0)[3]),
                "x"(reinterpret_cast<const __m128 &>(tmp1)), "x"(reinterpret_cast<const __m128 *>(&tmp1)[1]), "x"(reinterpret_cast<const __m128 *>(&tmp1)[2]), "x"(reinterpret_cast<const __m128 *>(&tmp1)[3]));
        asm volatile(""::"x"(reinterpret_cast<const __m128 &>(tmp2)), "x"(reinterpret_cast<const __m128 *>(&tmp2)[1]), "x"(reinterpret_cast<const __m128 *>(&tmp2)[2]), "x"(reinterpret_cast<const __m128 *>(&tmp2)[3]),
                "x"(reinterpret_cast<const __m128 &>(tmp3)), "x"(reinterpret_cast<const __m128 *>(&tmp3)[1]), "x"(reinterpret_cast<const __m128 *>(&tmp3)[2]), "x"(reinterpret_cast<const __m128 *>(&tmp3)[3]));
    }
};
#endif

template<typename T> static inline void keepResults(const T &tmp0)
{
    KeepResultsHelper<T, sizeof(T)>::keep(tmp0);
}

template<typename T> static inline void keepResults(const T &tmp0, const T &tmp1, const T &tmp2, const T &tmp3)
{
    KeepResultsHelper<T, sizeof(T)>::keep(tmp0, tmp1, tmp2, tmp3);
}

template<typename Vector> class DoMemIos
{
    public:
        static void run()
        {
            Benchmark::setColumnData("MemorySize", "half L1");
            run(CpuId::L1Data() / (sizeof(Vector) * 2), 128);
            Benchmark::setColumnData("MemorySize", "L1");
            run(CpuId::L1Data() / (sizeof(Vector) * 1), 128);
            Benchmark::setColumnData("MemorySize", "half L2");
            run(CpuId::L2Data() / (sizeof(Vector) * 2), 32);
            Benchmark::setColumnData("MemorySize", "L2");
            run(CpuId::L2Data() / (sizeof(Vector) * 1), 16);
            if (CpuId::L3Data() > 0) {
                Benchmark::setColumnData("MemorySize", "half L3");
                run(CpuId::L3Data() / (sizeof(Vector) * 2), 2);
                Benchmark::setColumnData("MemorySize", "L3");
                run(CpuId::L3Data() / (sizeof(Vector) * 1), 2);
                Benchmark::setColumnData("MemorySize", "4x L3");
                run(CpuId::L3Data() / sizeof(Vector) * 4, 1);
            } else {
                Benchmark::setColumnData("MemorySize", "4x L2");
                run(CpuId::L2Data() / sizeof(Vector) * 4, 1);
            }
        }
    private:
        static void run(const int Factor, const int Factor2)
        {
            Vector *a = new Vector[Factor];
#ifndef VC_BENCHMARK_NO_MLOCK
            mlock(a, Factor * sizeof(Vector));
#endif
            // initial loop so that the first iteration in the benchmark loop
            // has the same cache history as subsequent runs
            for (int i = 0; i < Factor; ++i) {
                Vector tmp = a[i];
                keepResults(tmp);
            }

            { // start with reads so that the cache lines are not marked as dirty yet
                Benchmark timer("read", sizeof(Vector) * Factor * Factor2, "Byte");
                benchmark_loop(timer) {
                    for (int j = 0; j < Factor2; ++j) {
                        for (int i = 0; i < Factor; i += 4) {
                            const Vector &tmp0 = a[i + 0];
                            const Vector &tmp1 = a[i + 1];
                            const Vector &tmp2 = a[i + 2];
                            const Vector &tmp3 = a[i + 3];
                            keepResults(tmp0, tmp1, tmp2, tmp3);
                        }
                    }
                }
            }
            {
                Benchmark bm("write", sizeof(Vector) * Factor * Factor2, "Byte");
                const Vector foo = PseudoRandom<Vector>::next();
                keepResults(foo);
                for (int i = 0; i < Factor; i += 4) {
                    a[i + 0] = foo;
                    a[i + 1] = foo;
                    a[i + 2] = foo;
                    a[i + 3] = foo;
                }
                while (bm.wantsMoreDataPoints()) {
                    bm.Start();
                    for (int j = 0; j < Factor2; ++j) {
                        keepResults(foo);
                        for (int i = 0; i < Factor; i += 4) {
                            a[i + 0] = foo;
                            a[i + 1] = foo;
                            a[i + 2] = foo;
                            a[i + 3] = foo;
                        }
                    }
                    bm.Stop();
                }
                bm.Print();
            }
            {
                Benchmark timer("r/w", sizeof(Vector) * Factor * Factor2, "Byte");
                const Vector foo = PseudoRandom<Vector>::next();
                while (timer.wantsMoreDataPoints()) {
                    timer.Start();
                    for (int j = 0; j < Factor2; ++j) {
                        keepResults(foo);
                        for (int i = 0; i < Factor; i += 4) {
                            const Vector &tmp0 = a[i + 0];
                            const Vector &tmp1 = a[i + 1];
                            const Vector &tmp2 = a[i + 2];
                            const Vector &tmp3 = a[i + 3];
                            keepResults(tmp0, tmp1, tmp2, tmp3);
                            a[i + 0] = foo;
                            a[i + 1] = foo;
                            a[i + 2] = foo;
                            a[i + 3] = foo;
                        }
                    }
                    timer.Stop();
                }
                timer.Print();
            }
            delete[] a;
        }
};

int bmain()
{
    Benchmark::addColumn("MemorySize");
    Benchmark::addColumn("datatype");

    Benchmark::setColumnData("datatype", "double_v");
    DoMemIos<double_v>::run();
    Benchmark::setColumnData("datatype", "float_v");
    DoMemIos<float_v>::run();
    Benchmark::setColumnData("datatype", "short_v");
    DoMemIos<short_v>::run();

    return 0;
}
<|endoftext|>


<commit_before>/*
TidyEngine
Copyright (C) 2016 Jakob Sinclair

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
Contact the author at: jakob.sinclair99@gmail.com
*/

#include "camera2d.hpp"
#include <glm/gtc/matrix_transform.hpp>

Camera2D::~Camera2D()
{
	;
}

/*
* Always set the scale of the y axis
* to (-m_Scale) because that makes the positive
* y direction to go down the screen
*/
void Camera2D::Initialise(uint16_t width, uint16_t height)
{
	m_Width = width;
	m_Height = height;
	m_Position.x = 640.0f;
	m_Position.y = 360.0f;
	m_Position.z = 0.0f;

	m_OrthoMatrix = glm::ortho(0.0f, (float)m_Width, 0.0f, (float)m_Height,
			0.0f, 100.0f);
	glm::vec3 translate = glm::vec3(-m_Position.x + m_Width / 2,
			m_Position.y + m_Height / 2, m_Position.z);
	m_View = glm::translate(m_View, translate);

	glm::vec3 scale = glm::vec3(m_Scale, -m_Scale, m_Scale);
	m_Model = glm::scale(glm::mat4(1.0f), scale);
	m_Update = false;
}

void Camera2D::Update()
{
	if (m_Update == true) {
		glm::vec3 translate = glm::vec3(-m_Position.x + m_Width / 2,
			m_Position.y + m_Height / 2, 0.0f);
		m_View = glm::translate(m_View, translate);

		glm::vec3 scale = glm::vec3(m_Scale, -m_Scale, 0.0f);
		m_Model = glm::scale(glm::mat4(1.0f), scale);
		m_Update = false;
	}
}

void Camera2D::SetScale(float scale)
{
	m_Scale = scale;
	m_Update = true;
}

void Camera2D::SetPosition(glm::vec3 position)
{
	m_Position = position;
	m_Update = true;
}

const float &Camera2D::GetScale() const
{
	return m_Scale;
}

const glm::vec3 &Camera2D::GetPos() const
{
	return m_Position;
}

const glm::mat4 &Camera2D::GetProj() const
{
	return m_OrthoMatrix;
}

const glm::mat4 &Camera2D::GetView() const
{
	return m_View;
}

const glm::mat4 &Camera2D::GetModel() const
{
	return m_Model;
}<commit_msg>libTidyEngine: added comment to Camera2D<commit_after>/*
TidyEngine
Copyright (C) 2016 Jakob Sinclair

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
Contact the author at: jakob.sinclair99@gmail.com
*/

#include "camera2d.hpp"
#include <glm/gtc/matrix_transform.hpp>

Camera2D::~Camera2D()
{
	;
}

/*
* Always set the scale of the y axis
* to (-m_Scale) because that makes the positive
* y direction to go down the screen
*/
void Camera2D::Initialise(uint16_t width, uint16_t height)
{
	m_Width = width;
	m_Height = height;
	m_Position.x = 640.0f;
	m_Position.y = 360.0f;
	m_Position.z = 0.0f;

	m_OrthoMatrix = glm::ortho(0.0f, (float)m_Width, 0.0f, (float)m_Height,
			0.0f, 100.0f);
	glm::vec3 translate = glm::vec3(-m_Position.x + m_Width / 2,
			m_Position.y + m_Height / 2, m_Position.z);
	m_View = glm::translate(m_View, translate);

	glm::vec3 scale = glm::vec3(m_Scale, -m_Scale, m_Scale);
	m_Model = glm::scale(glm::mat4(1.0f), scale);
	m_Update = false;
}

void Camera2D::Update()
{
	/* Only update the camera if it has moved, scaled or rotated */
	if (m_Update == true) {
		glm::vec3 translate = glm::vec3(-m_Position.x + m_Width / 2,
			m_Position.y + m_Height / 2, 0.0f);
		m_View = glm::translate(m_View, translate);

		glm::vec3 scale = glm::vec3(m_Scale, -m_Scale, 0.0f);
		m_Model = glm::scale(glm::mat4(1.0f), scale);
		m_Update = false;
	}
}

void Camera2D::SetScale(float scale)
{
	m_Scale = scale;
	m_Update = true;
}

void Camera2D::SetPosition(glm::vec3 position)
{
	m_Position = position;
	m_Update = true;
}

const float &Camera2D::GetScale() const
{
	return m_Scale;
}

const glm::vec3 &Camera2D::GetPos() const
{
	return m_Position;
}

const glm::mat4 &Camera2D::GetProj() const
{
	return m_OrthoMatrix;
}

const glm::mat4 &Camera2D::GetView() const
{
	return m_View;
}

const glm::mat4 &Camera2D::GetModel() const
{
	return m_Model;
}
<|endoftext|>


<commit_before>/***************************************************************************
**
** Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include <MTheme>

#include "mcontainer.h"
#include "mcontainer_p.h"

#include "mwidgetcreator.h"
M_REGISTER_WIDGET(MContainer)

MContainerPrivate::MContainerPrivate()
{
}

MContainerPrivate::~MContainerPrivate()
{
}

void MContainerPrivate::_q_onCentralWidgetDestroyed()
{
    Q_Q(MContainer);
    q->model()->setCentralWidget(0);
}

void MContainerPrivate::init(const QString &newTitle)
{
    Q_Q(MContainer);

    q->model()->setCentralWidget(new MWidget(q));

    q->setTitle(newTitle);

    q->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

MContainer::MContainer(QGraphicsItem *parent)
    : MWidgetController(new MContainerPrivate(), new MContainerModel(), parent)
{
    Q_D(MContainer);

    d->init();
}

MContainer::MContainer(const QString &title, QGraphicsItem *parent)
    : MWidgetController(new MContainerPrivate(), new MContainerModel(), parent)
{
    Q_D(MContainer);

    d->init(title);
}

MContainer::~MContainer()
{
}

QGraphicsWidget *MContainer::centralWidget()
{
    return model()->centralWidget();
}

void MContainer::setCentralWidget(QGraphicsWidget *centralWidget, bool destroy)
{
    QGraphicsWidget *oldCentralWidget = model()->centralWidget();

    // Set the new central widget
    model()->setCentralWidget(centralWidget);

    // Destroy the old central widget if requested
    if (destroy) {
        delete oldCentralWidget;

        if (centralWidget)
            connect(centralWidget, SIGNAL(destroyed()), SLOT(_q_onCentralWidgetDestroyed()));
    } else {
        // pass the ownership back to the caller.
        oldCentralWidget->setParentItem(0);
        oldCentralWidget->setParent(0);
        if (scene())
            scene()->removeItem(oldCentralWidget);

        if (oldCentralWidget)
            disconnect(oldCentralWidget, SIGNAL(destroyed()), this, SLOT(_q_onCentralWidgetDestroyed()));
    }

}

QString MContainer::title() const
{
    return model()->title();
}

void MContainer::setTitle(const QString &newTitle)
{
    model()->setTitle(newTitle);
}

QString MContainer::text() const
{
    return model()->text();
}

void MContainer::setText(const QString &text)
{
    model()->setText(text);
}

QString MContainer::iconID() const
{
    return model()->icon();
}

void MContainer::setIconID(const QString &iconID)
{
    model()->setIcon(iconID);
}

bool MContainer::headerVisible() const
{
    return model()->headerVisible();
}

void MContainer::setHeaderVisible(const bool visibility)
{
    model()->setHeaderVisible(visibility);
}

void MContainer::toggleHeaderVisible()
{
    setHeaderVisible(!model()->headerVisible());
}

bool MContainer::isProgressIndicatorVisible() const
{
    return model()->progressIndicatorVisible();
}

void MContainer::setProgressIndicatorVisible(bool visibility)
{
    model()->setProgressIndicatorVisible(visibility);
}

void MContainer::toggleProgressIndicatorVisible()
{
    setProgressIndicatorVisible(!isProgressIndicatorVisible());
}

#include "moc_mcontainer.cpp"
<commit_msg>Changes: MContainer should set only one size policy.<commit_after>/***************************************************************************
**
** Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include <MTheme>

#include "mcontainer.h"
#include "mcontainer_p.h"

#include "mwidgetcreator.h"
M_REGISTER_WIDGET(MContainer)

MContainerPrivate::MContainerPrivate()
{
}

MContainerPrivate::~MContainerPrivate()
{
}

void MContainerPrivate::_q_onCentralWidgetDestroyed()
{
    Q_Q(MContainer);
    q->model()->setCentralWidget(0);
}

void MContainerPrivate::init(const QString &newTitle)
{
    Q_Q(MContainer);

    q->model()->setCentralWidget(new MWidget(q));

    q->setTitle(newTitle);
}

MContainer::MContainer(QGraphicsItem *parent)
    : MWidgetController(new MContainerPrivate(), new MContainerModel(), parent)
{
    Q_D(MContainer);

    d->init();
}

MContainer::MContainer(const QString &title, QGraphicsItem *parent)
    : MWidgetController(new MContainerPrivate(), new MContainerModel(), parent)
{
    Q_D(MContainer);

    d->init(title);
}

MContainer::~MContainer()
{
}

QGraphicsWidget *MContainer::centralWidget()
{
    return model()->centralWidget();
}

void MContainer::setCentralWidget(QGraphicsWidget *centralWidget, bool destroy)
{
    QGraphicsWidget *oldCentralWidget = model()->centralWidget();

    // Set the new central widget
    model()->setCentralWidget(centralWidget);

    // Destroy the old central widget if requested
    if (destroy) {
        delete oldCentralWidget;

        if (centralWidget)
            connect(centralWidget, SIGNAL(destroyed()), SLOT(_q_onCentralWidgetDestroyed()));
    } else {
        // pass the ownership back to the caller.
        oldCentralWidget->setParentItem(0);
        oldCentralWidget->setParent(0);
        if (scene())
            scene()->removeItem(oldCentralWidget);

        if (oldCentralWidget)
            disconnect(oldCentralWidget, SIGNAL(destroyed()), this, SLOT(_q_onCentralWidgetDestroyed()));
    }

}

QString MContainer::title() const
{
    return model()->title();
}

void MContainer::setTitle(const QString &newTitle)
{
    model()->setTitle(newTitle);
}

QString MContainer::text() const
{
    return model()->text();
}

void MContainer::setText(const QString &text)
{
    model()->setText(text);
}

QString MContainer::iconID() const
{
    return model()->icon();
}

void MContainer::setIconID(const QString &iconID)
{
    model()->setIcon(iconID);
}

bool MContainer::headerVisible() const
{
    return model()->headerVisible();
}

void MContainer::setHeaderVisible(const bool visibility)
{
    model()->setHeaderVisible(visibility);
}

void MContainer::toggleHeaderVisible()
{
    setHeaderVisible(!model()->headerVisible());
}

bool MContainer::isProgressIndicatorVisible() const
{
    return model()->progressIndicatorVisible();
}

void MContainer::setProgressIndicatorVisible(bool visibility)
{
    model()->setProgressIndicatorVisible(visibility);
}

void MContainer::toggleProgressIndicatorVisible()
{
    setProgressIndicatorVisible(!isProgressIndicatorVisible());
}

#include "moc_mcontainer.cpp"
<|endoftext|>


<commit_before>#include "collision-map.h"

#include "platformer/object/object.h"
#include "platformer/game/camera.h"

#include "util/debug.h"
#include "util/token.h"
#include "util/graphics/bitmap.h"
#include "util/exceptions/load_exception.h"

#include <math.h>

namespace Platformer{

static bool within(const Area & area1, const Area & area2){
    return !((area1.x > area2.getX2()) ||
        (area1.y > area2.getY2()) ||
        (area2.x > area1.getX2()) ||
        (area2.y > area1.getY2()));
}

CollisionBody::CollisionBody():
velocityX(0),
velocityY(0){
}

CollisionBody::~CollisionBody(){
}

CollisionMap::CollisionMap(const Token * token){
    if (*token != "collision-map"){
        throw LoadException(__FILE__, __LINE__, "Not a collision map.");
    }
    TokenView view = token->view();
    while (view.hasMore()){
        try{
            const Token * tok;
            view >> tok;
            if (*tok == "area"){
                // get the name
                Area area;
                tok->view() >> area.x >> area.y >> area.width >> area.height;
                regions.push_back(area);
            } else {
                Global::debug( 3 ) << "Unhandled Collision attribute: " << tok->getName() << std::endl;
            }
        } catch ( const TokenException & ex ) {
            throw LoadException(__FILE__, __LINE__, ex, "Collision parse error");
        } catch ( const LoadException & ex ) {
            throw ex;
        }
    }
}

CollisionMap::~CollisionMap(){
}

void CollisionMap::collides(const CollisionBody & body) const{
    Area nextMovement = body.getArea();
    // Scan ahead
    nextMovement.x += body.getVelocityX();
    nextMovement.y += body.getVelocityY();
    bool collides = false;
    for (std::vector<Area>::const_iterator i = regions.begin(); i != regions.end(); i++){
        const Area & area = *i;
        if (within(nextMovement, area)){
            CollisionInfo info;
            info.top = info.bottom = info.left = info.right = false;
            // FIXME need to figure out where the collision occurs
            if (nextMovement.getX2() >= area.x && body.getArea().getX2() <= area.x){
                // Collision is happening at the top
                info.left = true;
            }
            if (nextMovement.getY2() >= area.y && body.getArea().getY2() <= area.y){
                // Collision is happening at the left
                info.top = true;
            }
            if (nextMovement.x <= area.getX2() && body.getArea().x >= area.getX2()){
                // Collision is happening at the bottom
                info.right = true;
            }
            if (nextMovement.getY2() >= area.y && body.getArea().y >= area.getY2()){
                // Collision is happening at the right
                info.bottom = true;
            }
            body.response(info);
            collides = true;
        }
    }
    if (!collides){
        body.noCollision();
    }
}

void CollisionMap::act(){
    // Check if they collide with bodies
    for (std::vector<Area>::iterator i = regions.begin(); i != regions.end(); i++){
        const Area & area = *i;
        // Nothing for now
    }
}

void CollisionMap::render(const Camera & camera){
    Area cameraWindow = { camera.getX(), camera.getY(), camera.getWidth(), camera.getHeight() };
    for (std::vector<Area>::iterator i = regions.begin(); i != regions.end(); i++){
        const Area & area = *i;
        if (within(area, cameraWindow)){
            const int x1 = (area.x <= camera.getX() ? 0 : area.x - camera.getX());
            const int y1 = (area.y <= camera.getY() ? 0 : area.y - camera.getY());
            const int x2 = ((area.x + area.width) >= (camera.getX() + camera.getWidth()) ?
                            0 : (camera.getX() + camera.getWidth()) - (area.x + area.width));
            const int y2 = ((area.y + area.height) >= (camera.getY() + camera.getHeight()) ?
                            0 : (camera.getY() + camera.getHeight()) - (area.y + area.height));
            // Draw to current screen
            camera.getWindow().rectangle(x1, y1, (camera.getWidth() - x2), (camera.getHeight() - y2), Graphics::makeColor(0,255,0));
        }
    }
}

}
<commit_msg>[platformer] Use double for collision rect draw placement.<commit_after>#include "collision-map.h"

#include "platformer/object/object.h"
#include "platformer/game/camera.h"

#include "util/debug.h"
#include "util/token.h"
#include "util/graphics/bitmap.h"
#include "util/exceptions/load_exception.h"

#include <math.h>

namespace Platformer{

static bool within(const Area & area1, const Area & area2){
    return !((area1.x > area2.getX2()) ||
        (area1.y > area2.getY2()) ||
        (area2.x > area1.getX2()) ||
        (area2.y > area1.getY2()));
}

CollisionBody::CollisionBody():
velocityX(0),
velocityY(0){
}

CollisionBody::~CollisionBody(){
}

CollisionMap::CollisionMap(const Token * token){
    if (*token != "collision-map"){
        throw LoadException(__FILE__, __LINE__, "Not a collision map.");
    }
    TokenView view = token->view();
    while (view.hasMore()){
        try{
            const Token * tok;
            view >> tok;
            if (*tok == "area"){
                // get the name
                Area area;
                tok->view() >> area.x >> area.y >> area.width >> area.height;
                regions.push_back(area);
            } else {
                Global::debug( 3 ) << "Unhandled Collision attribute: " << tok->getName() << std::endl;
            }
        } catch ( const TokenException & ex ) {
            throw LoadException(__FILE__, __LINE__, ex, "Collision parse error");
        } catch ( const LoadException & ex ) {
            throw ex;
        }
    }
}

CollisionMap::~CollisionMap(){
}

void CollisionMap::collides(const CollisionBody & body) const{
    Area nextMovement = body.getArea();
    // Scan ahead
    nextMovement.x += body.getVelocityX();
    nextMovement.y += body.getVelocityY();
    bool collides = false;
    for (std::vector<Area>::const_iterator i = regions.begin(); i != regions.end(); i++){
        const Area & area = *i;
        if (within(nextMovement, area)){
            CollisionInfo info;
            info.top = info.bottom = info.left = info.right = false;
            // FIXME need to figure out where the collision occurs
            if (nextMovement.getX2() >= area.x && body.getArea().getX2() <= area.x){
                // Collision is happening at the top
                info.left = true;
            }
            if (nextMovement.getY2() >= area.y && body.getArea().getY2() <= area.y){
                // Collision is happening at the left
                info.top = true;
            }
            if (nextMovement.x <= area.getX2() && body.getArea().x >= area.getX2()){
                // Collision is happening at the bottom
                info.right = true;
            }
            if (nextMovement.getY2() >= area.y && body.getArea().y >= area.getY2()){
                // Collision is happening at the right
                info.bottom = true;
            }
            body.response(info);
            collides = true;
        }
    }
    if (!collides){
        body.noCollision();
    }
}

void CollisionMap::act(){
    // Check if they collide with bodies
    for (std::vector<Area>::iterator i = regions.begin(); i != regions.end(); i++){
        const Area & area = *i;
        // Nothing for now
    }
}

void CollisionMap::render(const Camera & camera){
    Area cameraWindow = { camera.getX(), camera.getY(), camera.getWidth(), camera.getHeight() };
    for (std::vector<Area>::iterator i = regions.begin(); i != regions.end(); i++){
        const Area & area = *i;
        if (within(area, cameraWindow)){
            const double x1 = (area.x <= camera.getX() ? 0 : area.x - camera.getX());
            const double y1 = (area.y <= camera.getY() ? 0 : area.y - camera.getY());
            const double x2 = ((area.x + area.width) >= (camera.getX() + camera.getWidth()) ?
                            0 : (camera.getX() + camera.getWidth()) - (area.x + area.width));
            const double y2 = ((area.y + area.height) >= (camera.getY() + camera.getHeight()) ?
                            0 : (camera.getY() + camera.getHeight()) - (area.y + area.height));
            // Draw to current screen
            camera.getWindow().rectangle(x1, y1, (camera.getWidth() - x2), (camera.getHeight() - y2), Graphics::makeColor(0,255,0));
        }
    }
}

}
<|endoftext|>