chromium/chrome/browser/vr/databinding/binding.h

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_VR_DATABINDING_BINDING_H_
#define CHROME_BROWSER_VR_DATABINDING_BINDING_H_

#include <memory>
#include <optional>

#include "base/functional/bind.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/vr/databinding/binding_base.h"

namespace vr {

// This class represents a one-way binding that propagates a change from a
// source/model property to a sink/view. This is inappropriate for use in the
// case of, say, an editor view like a text field where changes must also be
// propagated back to the model.
//
// IMPORTANT: it is assumed that a Binding instance will outlive the model to
// which it is bound. This class in not appropriate for use with models that
// come and go in the lifetime of the application.
template <typename T>
class Binding : public BindingBase {};

// These macros are sugar for constructing a simple binding. It is meant to make
// setting up bindings a little less painful, but it is not meant to handle all
// cases. If you need to do something more complex (eg, convert type T before
// it is propagated), you should use the constructor directly.
//
// For example:
//
// struct MyModel { int source; };
// struct MyView {
//   int sink;
//   int awesomeness;
//   void SetAwesomeness(int new_awesomeness) {
//     awesomeness = new_awesomeness;
//   }
// };
//
// MyModel m;
// m.source = 20;
//
// MyView v;
// v.sink = 10;
// v.awesomeness = 30;
//
// auto binding = VR_BIND(int, MyModel, &m, source, MyView, &v, sink = value);
//
// Or, equivalently:
//
// auto binding = VR_BIND_FIELD(int, MyModel, &m, source, MyView, &v, sink);
//
// If your view has a setter, you may find VR_BIND_FUNC handy:
//
// auto binding =
//     VR_BIND_FUNC(int, MyModel, &m, source, MyView, &v, SetAwesomeness);
//
#ifndef NDEBUG
#define VR_BIND(T, M, m, Get, V, v, Set)
#else
#define VR_BIND
#endif

#define VR_BIND_FUNC(T, M, m, Get, V, v, f)

#define VR_BIND_FIELD(T, M, m, Get, V, v, f)

#ifndef NDEBUG
#define VR_BIND_LAMBDA(...)
#else
#define VR_BIND_LAMBDA
#endif

}  // namespace vr

#endif  // CHROME_BROWSER_VR_DATABINDING_BINDING_H_