security/sandbox/chromium/base/location.h

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #ifndef BASE_LOCATION_H_
michael@0 6 #define BASE_LOCATION_H_
michael@0 7
michael@0 8 #include <string>
michael@0 9
michael@0 10 #include "base/base_export.h"
michael@0 11 #include "base/basictypes.h"
michael@0 12
michael@0 13 namespace tracked_objects {
michael@0 14
michael@0 15 // Location provides basic info where of an object was constructed, or was
michael@0 16 // significantly brought to life.
michael@0 17 class BASE_EXPORT Location {
michael@0 18 public:
michael@0 19 // Constructor should be called with a long-lived char*, such as __FILE__.
michael@0 20 // It assumes the provided value will persist as a global constant, and it
michael@0 21 // will not make a copy of it.
michael@0 22 Location(const char* function_name,
michael@0 23 const char* file_name,
michael@0 24 int line_number,
michael@0 25 const void* program_counter);
michael@0 26
michael@0 27 // Provide a default constructor for easy of debugging.
michael@0 28 Location();
michael@0 29
michael@0 30 // Comparison operator for insertion into a std::map<> hash tables.
michael@0 31 // All we need is *some* (any) hashing distinction. Strings should already
michael@0 32 // be unique, so we don't bother with strcmp or such.
michael@0 33 // Use line number as the primary key (because it is fast, and usually gets us
michael@0 34 // a difference), and then pointers as secondary keys (just to get some
michael@0 35 // distinctions).
michael@0 36 bool operator < (const Location& other) const {
michael@0 37 if (line_number_ != other.line_number_)
michael@0 38 return line_number_ < other.line_number_;
michael@0 39 if (file_name_ != other.file_name_)
michael@0 40 return file_name_ < other.file_name_;
michael@0 41 return function_name_ < other.function_name_;
michael@0 42 }
michael@0 43
michael@0 44 const char* function_name() const { return function_name_; }
michael@0 45 const char* file_name() const { return file_name_; }
michael@0 46 int line_number() const { return line_number_; }
michael@0 47 const void* program_counter() const { return program_counter_; }
michael@0 48
michael@0 49 std::string ToString() const;
michael@0 50
michael@0 51 // Translate the some of the state in this instance into a human readable
michael@0 52 // string with HTML characters in the function names escaped, and append that
michael@0 53 // string to |output|. Inclusion of the file_name_ and function_name_ are
michael@0 54 // optional, and controlled by the boolean arguments.
michael@0 55 void Write(bool display_filename, bool display_function_name,
michael@0 56 std::string* output) const;
michael@0 57
michael@0 58 // Write function_name_ in HTML with '<' and '>' properly encoded.
michael@0 59 void WriteFunctionName(std::string* output) const;
michael@0 60
michael@0 61 private:
michael@0 62 const char* function_name_;
michael@0 63 const char* file_name_;
michael@0 64 int line_number_;
michael@0 65 const void* program_counter_;
michael@0 66 };
michael@0 67
michael@0 68 // A "snapshotted" representation of the Location class that can safely be
michael@0 69 // passed across process boundaries.
michael@0 70 struct BASE_EXPORT LocationSnapshot {
michael@0 71 // The default constructor is exposed to support the IPC serialization macros.
michael@0 72 LocationSnapshot();
michael@0 73 explicit LocationSnapshot(const tracked_objects::Location& location);
michael@0 74 ~LocationSnapshot();
michael@0 75
michael@0 76 std::string file_name;
michael@0 77 std::string function_name;
michael@0 78 int line_number;
michael@0 79 };
michael@0 80
michael@0 81 BASE_EXPORT const void* GetProgramCounter();
michael@0 82
michael@0 83 // Define a macro to record the current source location.
michael@0 84 #define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__FUNCTION__)
michael@0 85
michael@0 86 #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \
michael@0 87 ::tracked_objects::Location(function_name, \
michael@0 88 __FILE__, \
michael@0 89 __LINE__, \
michael@0 90 ::tracked_objects::GetProgramCounter())
michael@0 91
michael@0 92 } // namespace tracked_objects
michael@0 93
michael@0 94 #endif // BASE_LOCATION_H_

mercurial